diff --git a/Camera.cpp b/Camera.cpp index 154efb1..02140a3 100644 --- a/Camera.cpp +++ b/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); } diff --git a/Camera.h b/Camera.h index 2671cee..1d05ba0 100644 --- a/Camera.h +++ b/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; diff --git a/Controller.cpp b/Controller.cpp index 176f1f5..2b16913 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -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); + } } } diff --git a/EventHandler.cpp b/EventHandler.cpp index 7d90fb4..cb31191 100644 --- a/EventHandler.cpp +++ b/EventHandler.cpp @@ -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 #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); } diff --git a/Window.h b/Window.h index c91cf3d..8862c89 100644 --- a/Window.h +++ b/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; diff --git a/defines.h b/defines.h index a1f61ee..c40453c 100644 --- a/defines.h +++ b/defines.h @@ -1,5 +1,7 @@ #pragma once +#define INIT_WINDOW_WIDTH 800 +#define INIT_WINDOW_HEIGHT 600 struct Vertex { // Postition float x;