+ | Basic camera but glitched for now

This commit is contained in:
TheRedShip
2024-12-21 21:17:35 +01:00
parent d92b2ca913
commit df44327436
10 changed files with 93 additions and 25 deletions

14
.vscode/settings.json vendored
View File

@ -58,6 +58,18 @@
"format": "cpp", "format": "cpp",
"functional": "cpp", "functional": "cpp",
"sstream": "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"
} }
} }

View File

@ -23,9 +23,12 @@ class Camera
~Camera(void); ~Camera(void);
void update_camera_vectors(); void update_camera_vectors();
glm::mat4 get_view_matrix();
void process_movement(float xoffset, float yoffset, bool constrainPitch); glm::mat4 get_view_matrix();
glm::vec3 get_position();
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: private:

View File

@ -18,6 +18,8 @@
# include "glm/glm.hpp" # include "glm/glm.hpp"
# include "glm/gtc/matrix_transform.hpp" # include "glm/gtc/matrix_transform.hpp"
# include "glm/gtc/type_ptr.hpp"
# include "glad/gl.h" # include "glad/gl.h"
# include "GLFW/glfw3.h" # include "GLFW/glfw3.h"

View File

@ -34,10 +34,10 @@ class Shader
// void setBool(const std::string &name, bool value) const; // void setBool(const std::string &name, bool value) const;
// void setInt(const std::string &name, int value) const; // void setInt(const std::string &name, int value) const;
// void setFloat(const std::string &name, float value) const; // void setFloat(const std::string &name, float value) const;
void setVec2f(const std::string &name, const glm::vec2 &value) const; void set_vec2(const std::string &name, const glm::vec2 &value) const;
// void setVec3(const std::string &name, const RT::Vec3f &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 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; GLuint getProgram(void) const;

View File

@ -24,6 +24,7 @@ class Window
~Window(void); ~Window(void);
GLFWwindow *getWindow(void) const; GLFWwindow *getWindow(void) const;
Camera *get_camera(void) const;
float getFps(void) const; float getFps(void) const;
void display(); void display();
@ -36,7 +37,7 @@ class Window
private: private:
GLFWwindow *_window; GLFWwindow *_window;
Camera *camera; Camera *_camera;
float _fps; float _fps;

View File

@ -2,6 +2,8 @@
out vec4 FragColor; out vec4 FragColor;
uniform vec2 u_resolution; uniform vec2 u_resolution;
uniform vec3 u_cameraPosition;
uniform mat4 u_viewMatrix;
vec3 sphereCenter = vec3(0.0, 0.0, -5.0); vec3 sphereCenter = vec3(0.0, 0.0, -5.0);
float sphereRadius = 1.0; float sphereRadius = 1.0;
@ -55,10 +57,10 @@ void main()
uv = uv * 2.0 - 1.0; uv = uv * 2.0 - 1.0;
uv.x *= u_resolution.x / u_resolution.y; uv.x *= u_resolution.x / u_resolution.y;
vec3 rayOrigin = vec3(0.0, 0.0, 0.0);
vec3 rayDirection = normalize(vec3(uv, -1.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; float t;
if (intersectSphere(ray, sphereCenter, sphereRadius, t)) if (intersectSphere(ray, sphereCenter, sphereRadius, t))

View File

@ -33,6 +33,8 @@ void Camera::update_camera_vectors()
_right = glm::normalize(glm::cross(_forward, _up)); _right = glm::normalize(glm::cross(_forward, _up));
_up = glm::normalize(glm::cross(_right, _forward)); _up = glm::normalize(glm::cross(_right, _forward));
std::cout << _right.x << " " << _right.y << " " << _right.z << std::endl;
} }
glm::mat4 Camera::get_view_matrix() glm::mat4 Camera::get_view_matrix()
@ -40,10 +42,15 @@ glm::mat4 Camera::get_view_matrix()
return (glm::lookAt(_position, _position + _forward, _up)); 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; return (_position);
_pitch += yoffset; }
void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true)
{
_yaw += xoffset * 0.2f;
_pitch += yoffset * 0.2f;
if (constraint_pitch) if (constraint_pitch)
{ {
@ -55,3 +62,17 @@ void Camera::process_movement(float xoffset, float yoffset, bool constraint_pit
std::cout << _yaw << " " << _pitch << std::endl; 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;
}

View File

@ -34,7 +34,14 @@ int main(void)
{ {
glClear(GL_COLOR_BUFFER_BIT); 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()); glUseProgram(shader.getProgram());
shader.drawTriangles(size); shader.drawTriangles(size);

View File

@ -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 GLuint Shader::getProgram(void) const

View File

@ -14,7 +14,7 @@
Window::Window(int width, int height, const char *title, int sleep) 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()) if (!glfwInit())
{ {
@ -47,7 +47,7 @@ Window::Window(int width, int height, const char *title, int sleep)
Window::~Window(void) Window::~Window(void)
{ {
delete camera; delete _camera;
glfwTerminate(); glfwTerminate();
} }
@ -57,10 +57,16 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action,
{ {
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window)); Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) key; (void) scancode; (void) mods; (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) void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{ {
@ -76,17 +82,16 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
lastY = ypos; lastY = ypos;
} }
double xoffset = xpos - lastX; double xoffset = lastX - xpos;
double yoffset = lastY - ypos; double yoffset = ypos - lastY;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) 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; // scene.frameCount = 0;
} }
lastX = xpos; lastX = xpos;
lastY = ypos; lastY = ypos;
} }
@ -121,6 +126,11 @@ bool Window::shouldClose()
return glfwWindowShouldClose(_window); return glfwWindowShouldClose(_window);
} }
Camera *Window::get_camera(void) const
{
return (_camera);
}
GLFWwindow *Window::getWindow(void) const GLFWwindow *Window::getWindow(void) const
{ {
return (_window); return (_window);