mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Basic camera but glitched for now
This commit is contained in:
14
.vscode/settings.json
vendored
14
.vscode/settings.json
vendored
@ -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"
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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))
|
||||
|
@ -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;
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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<Window*>(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);
|
||||
|
Reference in New Issue
Block a user