Basic fps camera
This commit is contained in:
35
Camera.cpp
35
Camera.cpp
@@ -38,17 +38,38 @@ void Camera::lookForward() {
|
||||
viewMatrix = glm::lookAt(position, position + frontVec, upVec);
|
||||
}
|
||||
|
||||
void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister) {
|
||||
void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime) {
|
||||
glm::vec3 frontVecWithoutY = glm::vec3(frontVec.x, 0.0f, frontVec.z);
|
||||
|
||||
glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if(actionCameraRegister[cameraForward])
|
||||
position += speed * frontVec;
|
||||
deltaPos += speed * deltaTime * frontVecWithoutY;
|
||||
if(actionCameraRegister[cameraBackward])
|
||||
position -= speed * frontVec;
|
||||
deltaPos -= speed * deltaTime * frontVecWithoutY;
|
||||
if(actionCameraRegister[cameraLeft])
|
||||
position -= speed * glm::cross(frontVec, upVec);
|
||||
deltaPos -= speed * deltaTime * glm::cross(frontVec, upVec);
|
||||
if(actionCameraRegister[cameraRight])
|
||||
position += speed * glm::cross(frontVec, upVec);
|
||||
deltaPos += speed * deltaTime * glm::cross(frontVec, upVec);
|
||||
if(actionCameraRegister[cameraUp])
|
||||
position += speed * upVec;
|
||||
deltaPos += speed * deltaTime * upVec;
|
||||
if(actionCameraRegister[cameraDown])
|
||||
position -= speed * upVec;
|
||||
deltaPos -= speed * deltaTime * upVec;
|
||||
|
||||
position += deltaPos;
|
||||
}
|
||||
|
||||
void Camera::updateDirectionFromMouseInput(float deltaCursorX, float deltaCursorY) {
|
||||
yaw += deltaCursorX;
|
||||
pitch += deltaCursorY;
|
||||
|
||||
if(pitch > 89.0f)
|
||||
pitch = 89.0f;
|
||||
if(pitch < -89.0f)
|
||||
pitch = -89.0f;
|
||||
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
frontVec = glm::normalize(direction);
|
||||
}
|
||||
|
||||
8
Camera.h
8
Camera.h
@@ -12,7 +12,8 @@ public:
|
||||
glm::mat4 getViewProj();
|
||||
void updateVPM();
|
||||
void updateAspectRatio(int width, int height);
|
||||
void updatePositionFromKeyboardInput(bool *actionCameraRegister);
|
||||
void updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime);
|
||||
void updateDirectionFromMouseInput(float deltaCursorX, float deltaCursorY);
|
||||
|
||||
void translate(glm::vec3 translateVector);
|
||||
|
||||
@@ -33,7 +34,10 @@ private:
|
||||
glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
float speed = 0.05f;
|
||||
float pitch;
|
||||
float yaw = -90.0f;
|
||||
|
||||
float speed = 2.0f;
|
||||
|
||||
float fov;
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ void Controller::run() {
|
||||
|
||||
// Update game
|
||||
// ...
|
||||
model = glm::rotate(model, (float)(this->deltaTime*0.05), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
model = glm::rotate(model, (float)(this->deltaTime*0.0005), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
cam1.lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
cam1.lookForward();
|
||||
@@ -119,14 +119,18 @@ void Controller::run() {
|
||||
|
||||
// Check events, handle input
|
||||
gameEventHandler->handleEvents();
|
||||
cam1.updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister());
|
||||
cam1.updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister(), deltaTime);
|
||||
cam1.updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
|
||||
|
||||
// Update window size
|
||||
int new_window_width, new_window_height;
|
||||
glfwGetFramebufferSize(gameWindow->getGLFWwindow(), &new_window_width, &new_window_height);
|
||||
gameWindow->setWindowDimensions(new_window_width, new_window_height);
|
||||
glViewport(0, 0, new_window_width, new_window_height);
|
||||
cam1.updateAspectRatio(new_window_width, new_window_height);
|
||||
if(gameWindow->getWindowWasResized()) {
|
||||
int new_window_width, new_window_height;
|
||||
glfwGetFramebufferSize(gameWindow->getGLFWwindow(), &new_window_width, &new_window_height);
|
||||
gameWindow->setWindowDimensions(new_window_width, new_window_height);
|
||||
glViewport(0, 0, new_window_width, new_window_height);
|
||||
cam1.updateAspectRatio(new_window_width, new_window_height);
|
||||
gameEventHandler->setFirstMouseInput(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,19 +2,28 @@
|
||||
|
||||
bool EventHandler::wireFrameMode = 0;
|
||||
|
||||
EventHandler::EventHandler(GLFWwindow *p_window)
|
||||
: window(p_window) {
|
||||
EventHandler::EventHandler(GLFWwindow *window)
|
||||
: window(window) {
|
||||
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// Currently disabled because callbacks are shitty
|
||||
//glfwSetCursorPosCallback(window, mouse_callback);
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::handleEvents() {
|
||||
|
||||
// Restore deltaCursorPos BEFORE polling events
|
||||
//deltaCursorPosX = 0.0f; deltaCursorPosY = 0.0f;
|
||||
|
||||
glfwPollEvents();
|
||||
for(int i=0; i<CAMERA_ACTION_NUM_ITEMS; i++) {
|
||||
actionCameraRegister[i] = 0;
|
||||
}
|
||||
|
||||
updateMouseDelta();
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
@@ -33,9 +42,9 @@ void EventHandler::handleEvents() {
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::key_callback(GLFWwindow *p_window, int key, int scancode, int action, int mods) {
|
||||
void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
// Silence warnings of unused variables.
|
||||
(void)p_window; (void)scancode; (void)mods;
|
||||
(void)window; (void)scancode; (void)mods;
|
||||
|
||||
if (key == GLFW_KEY_O && action == GLFW_PRESS) {
|
||||
wireFrameMode = !wireFrameMode;
|
||||
@@ -46,3 +55,42 @@ void EventHandler::key_callback(GLFWwindow *p_window, int key, int scancode, int
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EventHandler::updateMouseDelta() {
|
||||
double xPos, yPos;
|
||||
glfwGetCursorPos(window, &xPos, &yPos);
|
||||
|
||||
if(firstMouseInput) {
|
||||
firstMouseInput = 0;
|
||||
lastCursorPosX = xPos;
|
||||
lastCursorPosY = yPos;
|
||||
}
|
||||
|
||||
deltaCursorPosX = xPos - lastCursorPosX;
|
||||
deltaCursorPosY = -(yPos - lastCursorPosY);
|
||||
lastCursorPosX = xPos;
|
||||
lastCursorPosY = yPos;
|
||||
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
|
||||
}
|
||||
|
||||
/*void EventHandler::mouse_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
// Silence warnings of unused variables.
|
||||
(void)window;
|
||||
|
||||
if(firstMouseInput) {
|
||||
firstMouseInput = 0;
|
||||
lastCursorPosX = xpos;
|
||||
lastCursorPosY = ypos;
|
||||
}
|
||||
|
||||
deltaCursorPosX = xpos - lastCursorPosX;
|
||||
deltaCursorPosY = lastCursorPosY - ypos;
|
||||
lastCursorPosX = xpos;
|
||||
lastCursorPosY = ypos;
|
||||
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
}*/
|
||||
|
||||
@@ -14,10 +14,17 @@ public:
|
||||
void handleEvents();
|
||||
|
||||
bool * getCameraActionRegister() { return actionCameraRegister; }
|
||||
float getCursorDeltaX() { return deltaCursorPosX; }
|
||||
float getCursorDeltaY() { return deltaCursorPosY; }
|
||||
|
||||
void setFirstMouseInput(bool val) { firstMouseInput = val; }
|
||||
|
||||
private:
|
||||
|
||||
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
static void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
|
||||
void updateMouseDelta();
|
||||
|
||||
bool actionCameraRegister[CAMERA_ACTION_NUM_ITEMS] = {0};
|
||||
|
||||
@@ -25,4 +32,13 @@ private:
|
||||
|
||||
static bool wireFrameMode;
|
||||
|
||||
float lastCursorPosX;
|
||||
float lastCursorPosY;
|
||||
float deltaCursorPosX;
|
||||
float deltaCursorPosY;
|
||||
|
||||
float mouseSensitivity = 0.08f;
|
||||
|
||||
bool firstMouseInput = 1;
|
||||
|
||||
};
|
||||
11
Window.cpp
11
Window.cpp
@@ -2,9 +2,12 @@
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include "Window.h"
|
||||
#include "defines.h"
|
||||
|
||||
bool Window::windowWasResized = 0;
|
||||
|
||||
Window::Window() {
|
||||
width = 800; height = 600;
|
||||
width = INIT_WINDOW_WIDTH; height = INIT_WINDOW_HEIGHT;
|
||||
|
||||
window = glfwCreateWindow(width, height, "Fall-Fever", NULL, NULL);
|
||||
if(!window) {
|
||||
@@ -23,6 +26,9 @@ Window::Window() {
|
||||
// Enable z buffer
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Disable mouse cursor
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
#ifdef _DEBUG
|
||||
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
@@ -34,7 +40,7 @@ Window::Window() {
|
||||
|
||||
// Tell GLFW which function to call when window is resized
|
||||
// Currently not used...
|
||||
// glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
}
|
||||
|
||||
@@ -45,6 +51,7 @@ Window::~Window() {
|
||||
// This function is called when the window gets resized
|
||||
void Window::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
(void)window;
|
||||
windowWasResized = 1;
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
|
||||
9
Window.h
9
Window.h
@@ -16,10 +16,19 @@ public:
|
||||
int getWindowWidth() { return width; }
|
||||
int getWindowHeight() { return height; }
|
||||
|
||||
// Beware: side effect!
|
||||
bool getWindowWasResized() {
|
||||
bool temp = windowWasResized;
|
||||
windowWasResized = 0;
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
static void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
static void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
|
||||
|
||||
static bool windowWasResized;
|
||||
|
||||
GLFWwindow *window;
|
||||
|
||||
int width, height;
|
||||
|
||||
Reference in New Issue
Block a user