mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 09:34:16 +00:00
feat: rework few fonctions and change glew to glad
- game files deleted
This commit is contained in:
102
src/camera.cpp
102
src/camera.cpp
@@ -1,5 +1,8 @@
|
||||
#include "camera.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||
: screenWidth(width),
|
||||
screenHeight(height),
|
||||
@@ -14,86 +17,87 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||
worldUp(0.0f, 1.0f, 0.0f),
|
||||
firstMouse(true)
|
||||
{
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void Camera::update(float deltaTime)
|
||||
{
|
||||
processInput(deltaTime);
|
||||
processMouseMovement();
|
||||
updateCameraVectors();
|
||||
processInput(deltaTime);
|
||||
processMouseMovement();
|
||||
updateCameraVectors();
|
||||
}
|
||||
|
||||
void Camera::processInput(float deltaTime)
|
||||
{
|
||||
float velocity = speed * deltaTime;
|
||||
float velocity = speed * deltaTime;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
cameraPosition += cameraFront * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
cameraPosition += cameraFront * velocity;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
cameraPosition -= cameraFront * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
cameraPosition -= cameraFront * velocity;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
cameraPosition += cameraRight * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
cameraPosition += cameraRight * velocity;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
cameraPosition -= cameraRight * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
cameraPosition -= cameraRight * velocity;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
cameraPosition += cameraUp * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
cameraPosition += cameraUp * velocity;
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
||||
cameraPosition -= cameraUp * velocity;
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
||||
cameraPosition -= cameraUp * velocity;
|
||||
|
||||
// Zoom
|
||||
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
||||
fov = 35.0f;
|
||||
else
|
||||
fov = 45.0f;
|
||||
// Zoom
|
||||
fov = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
||||
? 35.f
|
||||
: 45.f;
|
||||
}
|
||||
|
||||
void Camera::processMouseMovement()
|
||||
{
|
||||
double mouseX, mouseY;
|
||||
glfwGetCursorPos(window, &mouseX, &mouseY);
|
||||
|
||||
if (firstMouse) {
|
||||
mousePosX = mouseX;
|
||||
mousePosY = mouseY;
|
||||
firstMouse = false;
|
||||
return;
|
||||
}
|
||||
|
||||
float deltaX = static_cast<float>(mouseX - mousePosX);
|
||||
float deltaY = static_cast<float>(mousePosY - mouseY);
|
||||
double mouseX, mouseY;
|
||||
glfwGetCursorPos(window, &mouseX, &mouseY);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
mousePosX = mouseX;
|
||||
mousePosY = mouseY;
|
||||
firstMouse = false;
|
||||
return;
|
||||
}
|
||||
|
||||
deltaX *= cameraSensitivity;
|
||||
deltaY *= cameraSensitivity;
|
||||
float deltaX = static_cast<float>(mouseX - mousePosX);
|
||||
float deltaY = static_cast<float>(mousePosY - mouseY);
|
||||
|
||||
cameraYaw += deltaX;
|
||||
cameraPitch += deltaY;
|
||||
mousePosX = mouseX;
|
||||
mousePosY = mouseY;
|
||||
|
||||
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
|
||||
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
|
||||
deltaX *= cameraSensitivity;
|
||||
deltaY *= cameraSensitivity;
|
||||
|
||||
cameraYaw += deltaX;
|
||||
cameraPitch += deltaY;
|
||||
|
||||
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
|
||||
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
|
||||
}
|
||||
|
||||
void Camera::updateCameraVectors()
|
||||
{
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||
front.y = sin(glm::radians(cameraPitch));
|
||||
front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||
cameraFront = glm::normalize(front);
|
||||
glm::vec3 front;
|
||||
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||
front.y = sin(glm::radians(cameraPitch));
|
||||
front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||
cameraFront = glm::normalize(front);
|
||||
|
||||
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
|
||||
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
|
||||
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
|
||||
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getViewMatrix()
|
||||
{
|
||||
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
|
||||
}
|
||||
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user