57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#include "FpsCam.h"
|
|
#include <GLFW/glfw3.h>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
FpsCam::FpsCam(GLFWwindow* window)
|
|
{
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
if (glfwRawMouseMotionSupported())
|
|
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
|
}
|
|
|
|
|
|
glm::mat4 FpsCam::getMatrix()
|
|
{
|
|
glm::mat4 ret(1.0f);
|
|
ret = glm::rotate(ret, rotation.x, glm::vec3(1, 0, 0));
|
|
ret = glm::rotate(ret, rotation.y, glm::vec3(0, 1, 0));
|
|
ret = glm::translate(ret, position);
|
|
return ret;
|
|
}
|
|
|
|
void FpsCam::move(float angle, float fac)
|
|
{
|
|
position.x += (float)cos(rotation.y + glm::radians(angle)) * fac;
|
|
position.z += (float)sin(rotation.y + glm::radians(angle)) * fac;
|
|
}
|
|
|
|
|
|
void FpsCam::update(GLFWwindow* window)
|
|
{
|
|
double currentTime = glfwGetTime();
|
|
static double lastFrameTime = 0;
|
|
double deltaTime = currentTime - lastFrameTime;
|
|
lastFrameTime = currentTime;
|
|
|
|
double x, y;
|
|
glfwGetCursorPos(window, &x, &y);
|
|
|
|
static double lastX = x;
|
|
static double lastY = y;
|
|
|
|
rotation.x -= (float)(lastY - y) / 100.0f;
|
|
rotation.y -= (float)(lastX - x) / 100.0f;
|
|
|
|
lastX = x;
|
|
lastY = y;
|
|
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
move(0, 4.0f * deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
move(180, 4.0f * deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
move(90, 4.0f * deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
move(-90, 4.0f * deltaTime);
|
|
} |