From df44327436ceb024d827fd185915c1f0c61cf1f1 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Sat, 21 Dec 2024 21:17:35 +0100 Subject: [PATCH] + | Basic camera but glitched for now --- .vscode/settings.json | 14 +++++++++++++- includes/Camera.hpp | 7 +++++-- includes/RT.hpp | 2 ++ includes/Shader.hpp | 6 +++--- includes/Window.hpp | 3 ++- shaders/frag.frag | 6 ++++-- srcs/Camera.cpp | 27 ++++++++++++++++++++++++--- srcs/RT.cpp | 9 ++++++++- srcs/Shader.cpp | 14 ++++++++++++-- srcs/Window.cpp | 30 ++++++++++++++++++++---------- 10 files changed, 93 insertions(+), 25 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 382c553..805bf82 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -58,6 +58,18 @@ "format": "cpp", "functional": "cpp", "sstream": "cpp", - "variant": "cpp" + "variant": "cpp", + "charconv": "cpp", + "cstdarg": "cpp", + "cwctype": "cpp", + "map": "cpp", + "set": "cpp", + "algorithm": "cpp", + "numeric": "cpp", + "random": "cpp", + "ratio": "cpp", + "iomanip": "cpp", + "numbers": "cpp", + "cinttypes": "cpp" } } \ No newline at end of file diff --git a/includes/Camera.hpp b/includes/Camera.hpp index 252d618..ab5be00 100644 --- a/includes/Camera.hpp +++ b/includes/Camera.hpp @@ -23,10 +23,13 @@ class Camera ~Camera(void); void update_camera_vectors(); + glm::mat4 get_view_matrix(); + glm::vec3 get_position(); - void process_movement(float xoffset, float yoffset, bool constrainPitch); - + void process_mouse(float xoffset, float yoffset, bool constrainPitch); + void process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); + private: glm::vec3 _position; diff --git a/includes/RT.hpp b/includes/RT.hpp index 29242ee..198eb28 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -18,6 +18,8 @@ # include "glm/glm.hpp" # include "glm/gtc/matrix_transform.hpp" +# include "glm/gtc/type_ptr.hpp" + # include "glad/gl.h" # include "GLFW/glfw3.h" diff --git a/includes/Shader.hpp b/includes/Shader.hpp index 389ec62..723306f 100644 --- a/includes/Shader.hpp +++ b/includes/Shader.hpp @@ -34,10 +34,10 @@ class Shader // void setBool(const std::string &name, bool value) const; // void setInt(const std::string &name, int value) const; // void setFloat(const std::string &name, float value) const; - void setVec2f(const std::string &name, const glm::vec2 &value) const; - // void setVec3(const std::string &name, const RT::Vec3f &value) const; + void set_vec2(const std::string &name, const glm::vec2 &value) const; + void set_vec3(const std::string &name, const glm::vec3 &value) const; // void setVec4(const std::string &name, const RT::Vec4f &value) const; - // void setMat4(const std::string &name, const RT::Mat4f &value) const; + void set_mat4(const std::string &name, const glm::mat4 &value) const; GLuint getProgram(void) const; diff --git a/includes/Window.hpp b/includes/Window.hpp index 43fff61..f01eaf3 100644 --- a/includes/Window.hpp +++ b/includes/Window.hpp @@ -24,6 +24,7 @@ class Window ~Window(void); GLFWwindow *getWindow(void) const; + Camera *get_camera(void) const; float getFps(void) const; void display(); @@ -36,7 +37,7 @@ class Window private: GLFWwindow *_window; - Camera *camera; + Camera *_camera; float _fps; diff --git a/shaders/frag.frag b/shaders/frag.frag index 3c840a6..770a4e9 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -2,6 +2,8 @@ out vec4 FragColor; uniform vec2 u_resolution; +uniform vec3 u_cameraPosition; +uniform mat4 u_viewMatrix; vec3 sphereCenter = vec3(0.0, 0.0, -5.0); float sphereRadius = 1.0; @@ -55,10 +57,10 @@ void main() uv = uv * 2.0 - 1.0; uv.x *= u_resolution.x / u_resolution.y; - vec3 rayOrigin = vec3(0.0, 0.0, 0.0); vec3 rayDirection = normalize(vec3(uv, -1.0)); + rayDirection = (u_viewMatrix * vec4(rayDirection, 0.0)).xyz; - Ray ray = Ray(rayOrigin, rayDirection); + Ray ray = Ray(u_cameraPosition, rayDirection); float t; if (intersectSphere(ray, sphereCenter, sphereRadius, t)) diff --git a/srcs/Camera.cpp b/srcs/Camera.cpp index fc13cb2..42d70f0 100644 --- a/srcs/Camera.cpp +++ b/srcs/Camera.cpp @@ -33,6 +33,8 @@ void Camera::update_camera_vectors() _right = glm::normalize(glm::cross(_forward, _up)); _up = glm::normalize(glm::cross(_right, _forward)); + + std::cout << _right.x << " " << _right.y << " " << _right.z << std::endl; } glm::mat4 Camera::get_view_matrix() @@ -40,10 +42,15 @@ glm::mat4 Camera::get_view_matrix() return (glm::lookAt(_position, _position + _forward, _up)); } -void Camera::process_movement(float xoffset, float yoffset, bool constraint_pitch = true) +glm::vec3 Camera::get_position() { - _yaw += xoffset; - _pitch += yoffset; + return (_position); +} + +void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true) +{ + _yaw += xoffset * 0.2f; + _pitch += yoffset * 0.2f; if (constraint_pitch) { @@ -54,4 +61,18 @@ void Camera::process_movement(float xoffset, float yoffset, bool constraint_pit 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; + + 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; } \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 7314497..9394f6b 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -34,7 +34,14 @@ int main(void) { glClear(GL_COLOR_BUFFER_BIT); - shader.setVec2f("u_resolution", glm::vec2(WIDTH, HEIGHT)); + 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); + glUseProgram(shader.getProgram()); shader.drawTriangles(size); diff --git a/srcs/Shader.cpp b/srcs/Shader.cpp index b75accb..328613c 100644 --- a/srcs/Shader.cpp +++ b/srcs/Shader.cpp @@ -126,9 +126,19 @@ void Shader::drawTriangles(size_t size) } -void Shader::setVec2f(const std::string &name, const glm::vec2 &value) const +void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const { - glUniform2f(glGetUniformLocation(_program, name.c_str()), value[0], value[1]); + glUniform2fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value)); +} + +void Shader::set_vec3(const std::string &name, const glm::vec3 &value) const +{ + glUniform3fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value)); +} + +void Shader::set_mat4(const std::string &name, const glm::mat4 &value) const +{ + glUniformMatrix4fv(glGetUniformLocation(_program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value)); } GLuint Shader::getProgram(void) const diff --git a/srcs/Window.cpp b/srcs/Window.cpp index 0d57ceb..c019ad3 100644 --- a/srcs/Window.cpp +++ b/srcs/Window.cpp @@ -14,7 +14,7 @@ Window::Window(int width, int height, const char *title, int sleep) { - camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); + _camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f); if (!glfwInit()) { @@ -47,7 +47,7 @@ Window::Window(int width, int height, const char *title, int sleep) Window::~Window(void) { - delete camera; + delete _camera; glfwTerminate(); } @@ -57,10 +57,16 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, { Window* win = static_cast(glfwGetWindowUserPointer(window)); (void) win; (void) key; (void) scancode; (void) mods; - if (action == GLFW_PRESS) - { - - } + 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); + } void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) { @@ -76,17 +82,16 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) lastY = ypos; } - double xoffset = xpos - lastX; - double yoffset = lastY - ypos; + double xoffset = lastX - xpos; + double yoffset = ypos - lastY; if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) { - win->camera->process_movement(xoffset, yoffset, true); + win->_camera->process_mouse(xoffset, yoffset, true); // scene.frameCount = 0; } - lastX = xpos; lastY = ypos; } @@ -121,6 +126,11 @@ bool Window::shouldClose() return glfwWindowShouldClose(_window); } +Camera *Window::get_camera(void) const +{ + return (_camera); +} + GLFWwindow *Window::getWindow(void) const { return (_window);