~ | Camera system working

This commit is contained in:
TheRedShip
2024-12-22 01:10:31 +01:00
parent df44327436
commit 4d1d47a46a
5 changed files with 26 additions and 32 deletions

View File

@ -32,8 +32,8 @@ class Camera
private:
glm::vec3 _position;
glm::vec3 _forward;
glm::vec3 _position;
glm::vec3 _up;
glm::vec3 _right;

View File

@ -4,6 +4,9 @@ out vec4 FragColor;
uniform vec2 u_resolution;
uniform vec3 u_cameraPosition;
uniform mat4 u_viewMatrix;
uniform mat4 u_projectionMatrix;
uniform vec3 u_cameraDir;
vec3 sphereCenter = vec3(0.0, 0.0, -5.0);
float sphereRadius = 1.0;
@ -57,9 +60,13 @@ void main()
uv = uv * 2.0 - 1.0;
uv.x *= u_resolution.x / u_resolution.y;
vec3 rayDirection = normalize(vec3(uv, -1.0));
rayDirection = (u_viewMatrix * vec4(rayDirection, 0.0)).xyz;
float fov = 90.0;
float focal_length = 1.0 / tan(radians(fov) / 2.0);
vec3 viewSpaceRay = normalize(vec3(uv.x, uv.y, -focal_length));
vec3 rayDirection = (inverse(u_viewMatrix) * vec4(viewSpaceRay, 0.0)).xyz;
rayDirection = normalize(rayDirection);
Ray ray = Ray(u_cameraPosition, rayDirection);
float t;

View File

@ -31,10 +31,8 @@ void Camera::update_camera_vectors()
frontTemp.z = sin(glm::radians(_yaw)) * cos(glm::radians(_pitch));
_forward = glm::normalize(frontTemp);
_right = glm::normalize(glm::cross(_forward, _up));
_right = glm::normalize(glm::cross(_forward, glm::vec3(0.0f, 1.0f, 0.0f)));
_up = glm::normalize(glm::cross(_right, _forward));
std::cout << _right.x << " " << _right.y << " " << _right.z << std::endl;
}
glm::mat4 Camera::get_view_matrix()
@ -59,20 +57,18 @@ void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch
}
update_camera_vectors();
std::cout << _yaw << " " << _pitch << std::endl;
}
void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down)
{
float speed;
speed = 1.0f;
speed = .1f;
if (forward) _position += _forward * speed;
if (backward) _position -= _forward * speed;
if (left) _position -= _right * speed;
if (right) _position += _right * speed;
if (up) _position += up * speed;
if (down) _position -= up * speed;
if (up) _position += _up * speed;
if (down) _position -= _up * speed;
}

View File

@ -19,10 +19,6 @@ int main(void)
shader.attach();
// glm::vec2 vertices[6] = {
// { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f },
// { 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f }
// };
glm::vec2 vertices[3] = {
{-1.0f, -1.0f}, {3.0f, -1.0f}, {-1.0f, 3.0f}
};
@ -34,13 +30,9 @@ int main(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 view = window.get_camera()->get_view_matrix();
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)WIDTH / HEIGHT, 0.1f, 100.0f);
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
shader.set_vec3("u_cameraPosition", window.get_camera()->get_position());
shader.set_mat4("u_viewMatrix", view);
shader.set_mat4("u_projectionMatrix", projection);
shader.set_mat4("u_viewMatrix", window.get_camera()->get_view_matrix());
glUseProgram(shader.getProgram());
shader.drawTriangles(size);

View File

@ -57,6 +57,7 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action,
{
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) key; (void) scancode; (void) mods;
bool forward = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS;
bool backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS;
bool left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS;
@ -64,9 +65,7 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action,
bool up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS;
bool down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
// Update the camera position based on keyboard input
win->_camera->process_keyboard(forward, backward, left, right, up, down);
}
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{
@ -82,8 +81,8 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
lastY = ypos;
}
double xoffset = lastX - xpos;
double yoffset = ypos - lastY;
double xoffset = xpos - lastX;
double yoffset = lastY - ypos;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{