diff --git a/includes/Camera.hpp b/includes/Camera.hpp index ab5be00..17192da 100644 --- a/includes/Camera.hpp +++ b/includes/Camera.hpp @@ -32,8 +32,8 @@ class Camera private: - glm::vec3 _position; glm::vec3 _forward; + glm::vec3 _position; glm::vec3 _up; glm::vec3 _right; diff --git a/shaders/frag.frag b/shaders/frag.frag index 770a4e9..71afa8f 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -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; diff --git a/srcs/Camera.cpp b/srcs/Camera.cpp index 42d70f0..7a7f309 100644 --- a/srcs/Camera.cpp +++ b/srcs/Camera.cpp @@ -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; } \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 9394f6b..372440a 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -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); diff --git a/srcs/Window.cpp b/srcs/Window.cpp index c019ad3..821f204 100644 --- a/srcs/Window.cpp +++ b/srcs/Window.cpp @@ -55,18 +55,17 @@ Window::~Window(void) void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { - Window* win = static_cast(glfwGetWindowUserPointer(window)); - (void) win; (void) key; (void) scancode; (void) mods; + Window* win = static_cast(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; - bool right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS; - 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); + bool backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS; + bool left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS; + bool right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS; + bool up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS; + bool down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; + 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) {