+ | 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

View File

@ -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;
}

View File

@ -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);

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

View File

@ -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);