Updated camera class
This commit is contained in:
parent
419ab8261d
commit
5e086c884f
41
src/main.cpp
41
src/main.cpp
|
@ -23,6 +23,9 @@ int height = 800;
|
||||||
|
|
||||||
bool trapCursor = true;
|
bool trapCursor = true;
|
||||||
|
|
||||||
|
float movementSpeed = 6.0f;
|
||||||
|
float sensitivity = 0.1f;
|
||||||
|
|
||||||
std::function<void(GLFWwindow*, float, float)> handleMouseCallback;
|
std::function<void(GLFWwindow*, float, float)> handleMouseCallback;
|
||||||
|
|
||||||
void LogGlfwError(const char* message)
|
void LogGlfwError(const char* message)
|
||||||
|
@ -64,7 +67,7 @@ void FramebufferSizeCallback(GLFWwindow* window, int w, int h)
|
||||||
void CursorPosCallback(GLFWwindow* window, double x, double y)
|
void CursorPosCallback(GLFWwindow* window, double x, double y)
|
||||||
{
|
{
|
||||||
if (trapCursor)
|
if (trapCursor)
|
||||||
handleMouseCallback(window, x, y);
|
handleMouseCallback(window, (float)x, (float)y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyCallback(GLFWwindow* window, int key, int mode, int action, int mods)
|
void KeyCallback(GLFWwindow* window, int key, int mode, int action, int mods)
|
||||||
|
@ -79,13 +82,11 @@ void KeyCallback(GLFWwindow* window, int key, int mode, int action, int mods)
|
||||||
|
|
||||||
void HandleKeyboard(GLFWwindow* window, Camera& cam, float frametime)
|
void HandleKeyboard(GLFWwindow* window, Camera& cam, float frametime)
|
||||||
{
|
{
|
||||||
static float movementSpeed = 4.5f;
|
|
||||||
|
|
||||||
// Movement
|
// Movement
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
cam.Move(cam.front * movementSpeed * frametime);
|
cam.Move(glm::normalize(glm::vec3(cam.front.x, 0.0f, cam.front.z)) * movementSpeed * frametime);
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
cam.Move(-cam.front * movementSpeed * frametime);
|
cam.Move(-glm::normalize(glm::vec3(cam.front.x, 0.0f, cam.front.z)) * movementSpeed * frametime);
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
cam.Move(-cam.right * movementSpeed * frametime);
|
cam.Move(-cam.right * movementSpeed * frametime);
|
||||||
|
@ -100,20 +101,7 @@ void HandleKeyboard(GLFWwindow* window, Camera& cam, float frametime)
|
||||||
|
|
||||||
void HandleMouse(GLFWwindow* window, Camera* cam, float x, float y)
|
void HandleMouse(GLFWwindow* window, Camera* cam, float x, float y)
|
||||||
{
|
{
|
||||||
static const float sensitivity = 0.1f;
|
cam->Rotate(sensitivity * glm::vec2((height / 2 - y), (x - width / 2)));
|
||||||
|
|
||||||
static float pitch = 0.0f;
|
|
||||||
static float yaw = -90.0f;
|
|
||||||
|
|
||||||
pitch += (height / 2 - y) * sensitivity;
|
|
||||||
yaw += (x - width / 2) * sensitivity;
|
|
||||||
|
|
||||||
if (pitch > 89.0f)
|
|
||||||
pitch = 89.0f;
|
|
||||||
else if (pitch < -89.0f)
|
|
||||||
pitch = -89.0f;
|
|
||||||
|
|
||||||
cam->SetRotation(glm::vec2(pitch, yaw));
|
|
||||||
|
|
||||||
glfwSetCursorPos(window, width / 2.0f, height / 2.0f);
|
glfwSetCursorPos(window, width / 2.0f, height / 2.0f);
|
||||||
}
|
}
|
||||||
|
@ -208,26 +196,21 @@ int main(int argc, char** argv)
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
|
||||||
float lastTime = glfwGetTime();
|
float lastTime = (float)glfwGetTime();
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
float currentTime = glfwGetTime();
|
float currentTime = (float)glfwGetTime();
|
||||||
HandleKeyboard(window, cam, currentTime - lastTime);
|
HandleKeyboard(window, cam, currentTime - lastTime);
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
|
|
||||||
float t = glfwGetTime();
|
|
||||||
|
|
||||||
static float PI = glm::pi<float>();
|
static float PI = glm::pi<float>();
|
||||||
|
|
||||||
static float fov = glm::radians(45.0f);
|
static float fov = glm::radians(45.0f);
|
||||||
static float zNear = 0.1f;
|
static float zNear = 0.1f;
|
||||||
static float zFar = 100.0f;
|
static float zFar = 100.0f;
|
||||||
|
|
||||||
static float orbitSpeed = 1.0f;
|
|
||||||
static float orbitRadius = 10.0f;
|
|
||||||
|
|
||||||
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
@ -246,6 +229,12 @@ int main(int argc, char** argv)
|
||||||
ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f);
|
ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ImGui::CollapsingHeader("Movement"))
|
||||||
|
{
|
||||||
|
ImGui::SliderFloat("Speed", &movementSpeed, 0.01f, 10.0f);
|
||||||
|
ImGui::SliderFloat("Sensitivity", &sensitivity, 0.01, 10.0f);
|
||||||
|
}
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar);
|
perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar);
|
||||||
|
|
|
@ -2,20 +2,8 @@
|
||||||
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
Camera::Camera(glm::vec3 position, glm::vec3 direction) :
|
|
||||||
position(position), direction(direction)
|
|
||||||
{
|
|
||||||
CalculateCamera();
|
|
||||||
}
|
|
||||||
|
|
||||||
Camera::Camera(glm::vec3 position, glm::vec2 pitchYaw) :
|
Camera::Camera(glm::vec3 position, glm::vec2 pitchYaw) :
|
||||||
position(position), direction(
|
position(position), rotation(pitchYaw)
|
||||||
glm::vec3(
|
|
||||||
cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)),
|
|
||||||
sin(glm::radians(pitchYaw.x)),
|
|
||||||
sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
CalculateCamera();
|
CalculateCamera();
|
||||||
}
|
}
|
||||||
|
@ -34,24 +22,25 @@ void Camera::Move(glm::vec3 movement)
|
||||||
|
|
||||||
void Camera::SetRotation(glm::vec2 pitchYaw)
|
void Camera::SetRotation(glm::vec2 pitchYaw)
|
||||||
{
|
{
|
||||||
direction = glm::vec3(
|
rotation = pitchYaw;
|
||||||
cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)),
|
|
||||||
sin(glm::radians(pitchYaw.x)),
|
|
||||||
sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x))
|
|
||||||
);
|
|
||||||
CalculateCamera();
|
CalculateCamera();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::Rotate(glm::vec2 pitchYaw)
|
void Camera::Rotate(glm::vec2 pitchYaw)
|
||||||
{
|
{
|
||||||
direction += glm::vec3(
|
rotation += pitchYaw;
|
||||||
cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)),
|
if (rotation.x > 89.0f)
|
||||||
sin(glm::radians(pitchYaw.x)),
|
rotation.x = 89.0f;
|
||||||
sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x))
|
else if (rotation.x < -89.0f)
|
||||||
);
|
rotation.x = -89.0f;
|
||||||
CalculateCamera();
|
CalculateCamera();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Camera::Constrain(glm::vec4 constraints)
|
||||||
|
{
|
||||||
|
rotationConstraints = constraints;
|
||||||
|
}
|
||||||
|
|
||||||
void Camera::Use(const Shader& program)
|
void Camera::Use(const Shader& program)
|
||||||
{
|
{
|
||||||
program.SetUniformMat4("view", &viewMatrix[0][0]);
|
program.SetUniformMat4("view", &viewMatrix[0][0]);
|
||||||
|
@ -59,9 +48,12 @@ void Camera::Use(const Shader& program)
|
||||||
|
|
||||||
void Camera::CalculateCamera()
|
void Camera::CalculateCamera()
|
||||||
{
|
{
|
||||||
front = glm::normalize(direction);
|
front = glm::normalize(glm::vec3(
|
||||||
|
cos(glm::radians(rotation.y)) * cos(glm::radians(rotation.x)),
|
||||||
|
sin(glm::radians(rotation.x)),
|
||||||
|
sin(glm::radians(rotation.y)) * cos(glm::radians(rotation.x))
|
||||||
|
));
|
||||||
right = glm::normalize(glm::cross(front, glm::vec3(0.0f, 1.0f, 0.0f)));
|
right = glm::normalize(glm::cross(front, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||||
// right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), front));
|
|
||||||
up = glm::cross(right, front);
|
up = glm::cross(right, front);
|
||||||
|
|
||||||
viewMatrix = glm::lookAt(position, position + front, up);
|
viewMatrix = glm::lookAt(position, position + front, up);
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
|
|
||||||
|
// TODO: reimplement, make it better
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Camera() = default;
|
Camera() = default;
|
||||||
Camera(glm::vec3 position, glm::vec3 direction);
|
|
||||||
Camera(glm::vec3 position, glm::vec2 pitchYaw);
|
Camera(glm::vec3 position, glm::vec2 pitchYaw);
|
||||||
|
|
||||||
void SetPosition(glm::vec3 newPos);
|
void SetPosition(glm::vec3 newPos);
|
||||||
|
@ -17,6 +17,8 @@ public:
|
||||||
void SetRotation(glm::vec2 pitchYaw);
|
void SetRotation(glm::vec2 pitchYaw);
|
||||||
void Rotate(glm::vec2 pitchYaw);
|
void Rotate(glm::vec2 pitchYaw);
|
||||||
|
|
||||||
|
void Constrain(glm::vec4 constraints);
|
||||||
|
|
||||||
void Use(const Shader& program);
|
void Use(const Shader& program);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -25,7 +27,9 @@ public:
|
||||||
private:
|
private:
|
||||||
void CalculateCamera();
|
void CalculateCamera();
|
||||||
|
|
||||||
glm::vec3 position, direction;
|
glm::vec3 position;
|
||||||
|
glm::vec2 rotation;
|
||||||
|
glm::vec4 rotationConstraints;
|
||||||
|
|
||||||
glm::mat4 viewMatrix;
|
glm::mat4 viewMatrix;
|
||||||
};
|
};
|
Loading…
Reference in a new issue