Fix mouse bug
This commit is contained in:
28
.vscode/settings.json
vendored
28
.vscode/settings.json
vendored
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
|
||||
"files.associations": {
|
||||
"*.h": "c",
|
||||
"ostream": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
@@ -46,6 +47,31 @@
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
"typeinfo": "cpp",
|
||||
"csignal": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"hash_map": "cpp",
|
||||
"strstream": "cpp",
|
||||
"bitset": "cpp",
|
||||
"chrono": "cpp",
|
||||
"complex": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"ratio": "cpp",
|
||||
"fstream": "cpp",
|
||||
"future": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"mutex": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"thread": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp"
|
||||
}
|
||||
}
|
||||
11
Camera.cpp
11
Camera.cpp
@@ -1,6 +1,8 @@
|
||||
#include "Camera.h"
|
||||
#include "eventActions.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Camera::Camera(float fov, int width, int height) {
|
||||
this->fov = fov;
|
||||
viewMatrix = glm::mat4(1.0f);
|
||||
@@ -41,13 +43,13 @@ void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister, float d
|
||||
glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
if(actionCameraRegister[cameraForward])
|
||||
deltaPos += speed * deltaTime * frontVecWithoutY;
|
||||
deltaPos += speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
if(actionCameraRegister[cameraBackward])
|
||||
deltaPos -= speed * deltaTime * frontVecWithoutY;
|
||||
deltaPos -= speed * deltaTime * glm::normalize(frontVecWithoutY);
|
||||
if(actionCameraRegister[cameraLeft])
|
||||
deltaPos -= speed * deltaTime * glm::cross(frontVec, upVec);
|
||||
deltaPos -= speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
if(actionCameraRegister[cameraRight])
|
||||
deltaPos += speed * deltaTime * glm::cross(frontVec, upVec);
|
||||
deltaPos += speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
|
||||
if(actionCameraRegister[cameraUp])
|
||||
deltaPos += speed * deltaTime * upVec;
|
||||
if(actionCameraRegister[cameraDown])
|
||||
@@ -57,6 +59,7 @@ void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister, float d
|
||||
}
|
||||
|
||||
void Camera::updateDirectionFromMouseInput(float deltaCursorX, float deltaCursorY) {
|
||||
if(deltaCursorX==0 && deltaCursorY==0) return;
|
||||
yaw += deltaCursorX;
|
||||
pitch += deltaCursorY;
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ void Controller::run() {
|
||||
// ...
|
||||
model = glm::rotate(model, (float)(this->deltaTime*0.0005), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
camera->lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
//camera->lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
camera->lookForward();
|
||||
camera->updateVPM();
|
||||
glm::mat4 modelViewProj = camera->getViewProj() * model;
|
||||
@@ -115,15 +115,16 @@ void Controller::run() {
|
||||
vertexBuffer.unbind();
|
||||
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
|
||||
// Update window size
|
||||
if(gameWindow->checkWindowWasResized())
|
||||
updateWindowSize();
|
||||
|
||||
// Check events, handle input
|
||||
gameEventHandler->handleEvents();
|
||||
|
||||
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister(), deltaTime);
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
|
||||
|
||||
// Update window size
|
||||
if(gameWindow->getWindowWasResized())
|
||||
updateWindowSize();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -156,10 +157,6 @@ void Controller::error_callback(int error, const char* description) {
|
||||
}
|
||||
|
||||
void Controller::updateWindowSize() {
|
||||
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);
|
||||
camera->updateAspectRatio(new_window_width, new_window_height);
|
||||
camera->updateAspectRatio(gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
|
||||
gameEventHandler->setFirstMouseInput(1);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "EventHandler.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
bool EventHandler::wireFrameMode = 0;
|
||||
|
||||
EventHandler::EventHandler(GLFWwindow *window)
|
||||
@@ -58,37 +60,18 @@ 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) {
|
||||
// Check if this is the first VALID mouse event after window being resized
|
||||
if(firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
|
||||
firstMouseInput = 0;
|
||||
lastCursorPosX = xpos;
|
||||
lastCursorPosY = ypos;
|
||||
deltaCursorPosX = 0.0f;
|
||||
deltaCursorPosY = 0.0f;
|
||||
}
|
||||
|
||||
deltaCursorPosX = xpos - lastCursorPosX;
|
||||
deltaCursorPosY = lastCursorPosY - ypos;
|
||||
lastCursorPosX = xpos;
|
||||
lastCursorPosY = ypos;
|
||||
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public:
|
||||
float getCursorDeltaY() { return deltaCursorPosY; }
|
||||
|
||||
void setFirstMouseInput(bool val) { firstMouseInput = val; }
|
||||
void setLastCursorPos(float posX, float posY) { lastCursorPosX=posX; lastCursorPosY=posY; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -32,10 +33,10 @@ private:
|
||||
|
||||
static bool wireFrameMode;
|
||||
|
||||
float lastCursorPosX;
|
||||
float lastCursorPosY;
|
||||
float deltaCursorPosX;
|
||||
float deltaCursorPosY;
|
||||
float lastCursorPosX = 0.0f;
|
||||
float lastCursorPosY = 0.0f;
|
||||
float deltaCursorPosX = 0.0f;
|
||||
float deltaCursorPosY = 0.0f;
|
||||
|
||||
float mouseSensitivity = 0.08f;
|
||||
|
||||
|
||||
32
Window.cpp
32
Window.cpp
@@ -4,7 +4,6 @@
|
||||
#include "Window.h"
|
||||
#include "defines.h"
|
||||
|
||||
bool Window::windowWasResized = 0;
|
||||
|
||||
Window::Window() {
|
||||
width = INIT_WINDOW_WIDTH; height = INIT_WINDOW_HEIGHT;
|
||||
@@ -14,6 +13,8 @@ Window::Window() {
|
||||
std::cout << "Failed to create window" << std::endl;
|
||||
}
|
||||
|
||||
glfwGetWindowPos(window, &posX, &posY);
|
||||
|
||||
// Create OpenGL context
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
@@ -27,7 +28,7 @@ Window::Window() {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Disable mouse cursor
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
setGrabbedCursor(1);
|
||||
|
||||
#ifdef _DEBUG
|
||||
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
|
||||
@@ -39,8 +40,7 @@ Window::Window() {
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
// Tell GLFW which function to call when window is resized
|
||||
// Currently not used...
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
//glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
}
|
||||
|
||||
@@ -48,10 +48,30 @@ Window::~Window() {
|
||||
glfwDestroyWindow(window);
|
||||
}
|
||||
|
||||
// This function is called when the window gets resized
|
||||
bool Window::checkWindowWasResized() {
|
||||
int new_width, new_height, new_posx, new_posy;
|
||||
glfwGetFramebufferSize(window, &new_width, &new_height);
|
||||
glfwGetWindowPos(window, &new_posx, &new_posy);
|
||||
if(new_width == width && new_height == height && new_posx == posX && new_posy == posY)
|
||||
return 0;
|
||||
|
||||
width = new_width; height = new_height; posX = new_posx; posY = new_posy;
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Window::setGrabbedCursor(bool value) {
|
||||
if(value) {
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
} else {
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
}
|
||||
}
|
||||
|
||||
// This function is called when the window gets resized (currently not used)
|
||||
void Window::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
(void)window;
|
||||
windowWasResized = 1;
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
|
||||
11
Window.h
11
Window.h
@@ -11,26 +11,21 @@ public:
|
||||
|
||||
GLFWwindow * getGLFWwindow() { return window; }
|
||||
|
||||
void setWindowDimensions(int width, int height) {this->width = width; this->height = height;}
|
||||
|
||||
int getWindowWidth() { return width; }
|
||||
int getWindowHeight() { return height; }
|
||||
|
||||
// Beware: side effect!
|
||||
bool getWindowWasResized() {
|
||||
bool temp = windowWasResized;
|
||||
windowWasResized = 0;
|
||||
return temp;
|
||||
}
|
||||
bool checkWindowWasResized();
|
||||
|
||||
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;
|
||||
void setGrabbedCursor(bool value);
|
||||
|
||||
GLFWwindow *window;
|
||||
|
||||
int posX, posY;
|
||||
int width, height;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user