diff --git a/Camera.cpp b/Camera.cpp index 4bf867e..154efb1 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -1,13 +1,13 @@ #include "Camera.h" +#include "eventActions.h" #include #include Camera::Camera(float fov, int width, int height) { - //projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); this->fov = fov; - projectionMatrix = glm::perspective(this->fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f); viewMatrix = glm::mat4(1.0f); + updateAspectRatio(width, height); updateVPM(); } @@ -20,10 +20,35 @@ void Camera::updateVPM() { } void Camera::updateAspectRatio(int width, int height) { + //projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); projectionMatrix = glm::perspective(fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f); updateVPM(); } -void Camera::translate(glm::vec3 vector) { - viewMatrix = glm::translate(viewMatrix, vector * -1.0f); +void Camera::translate(glm::vec3 translateVector) { + position += translateVector; + viewMatrix = glm::translate(viewMatrix, translateVector * -1.0f); +} + +void Camera::lookAtTarget(glm::vec3 target) { + viewMatrix = glm::lookAt(position, target, upVec); +} + +void Camera::lookForward() { + viewMatrix = glm::lookAt(position, position + frontVec, upVec); +} + +void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister) { + if(actionCameraRegister[cameraForward]) + position += speed * frontVec; + if(actionCameraRegister[cameraBackward]) + position -= speed * frontVec; + if(actionCameraRegister[cameraLeft]) + position -= speed * glm::cross(frontVec, upVec); + if(actionCameraRegister[cameraRight]) + position += speed * glm::cross(frontVec, upVec); + if(actionCameraRegister[cameraUp]) + position += speed * upVec; + if(actionCameraRegister[cameraDown]) + position -= speed * upVec; } diff --git a/Camera.h b/Camera.h index 363d932..2671cee 100644 --- a/Camera.h +++ b/Camera.h @@ -12,8 +12,13 @@ public: glm::mat4 getViewProj(); void updateVPM(); void updateAspectRatio(int width, int height); + void updatePositionFromKeyboardInput(bool *actionCameraRegister); - void translate(glm::vec3 vector); + void translate(glm::vec3 translateVector); + + void setPosition(glm::vec3 position) { this->position = position; } + void lookAtTarget(glm::vec3 target); + void lookForward(); private: @@ -23,6 +28,13 @@ private: glm::mat4 viewProjectionMatrix; + glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); + glm::vec3 direction = glm::vec3(0.0f, 0.0f, 1.0f); + 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 fov; }; diff --git a/Controller.cpp b/Controller.cpp index 4c7f0bc..176f1f5 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -73,7 +73,8 @@ void Controller::run() { glm::mat4 model = glm::mat4(1.0f); Camera cam1(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight()); - cam1.translate(glm::vec3(0.0f, 0.0f, 1.5f)); + + cam1.translate(glm::vec3(0.0f, 0.0f, 2.5f)); // This is the game loop while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { @@ -90,17 +91,27 @@ void Controller::run() { // Update game // ... - model = glm::rotate(model, (float)this->deltaTime, glm::vec3(0.0f, 1.0f, 0.0f)); + model = glm::rotate(model, (float)(this->deltaTime*0.05), glm::vec3(0.0f, 1.0f, 0.0f)); + + cam1.lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f)); + cam1.lookForward(); cam1.updateVPM(); glm::mat4 modelViewProj = cam1.getViewProj() * model; shaderProgram.setUniform("u_modelViewProj", modelViewProj); // Render and buffer swap - glClear(GL_COLOR_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); vertexBuffer.bind(); tex1.bind(0); - glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); + for(int i=0;i<20;i++) { + glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); + model = glm::translate(model, glm::vec3(0.0f, 0.0f, 1.0f)); + cam1.updateVPM(); + glm::mat4 modelViewProj = cam1.getViewProj() * model; + shaderProgram.setUniform("u_modelViewProj", modelViewProj); + } + model = glm::translate(model, glm::vec3(0.0f, 0.0f, -20.0f)); tex1.unbind(); vertexBuffer.unbind(); @@ -108,6 +119,7 @@ void Controller::run() { // Check events, handle input gameEventHandler->handleEvents(); + cam1.updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister()); // Update window size int new_window_width, new_window_height; diff --git a/EventHandler.cpp b/EventHandler.cpp index 38de26d..7d90fb4 100644 --- a/EventHandler.cpp +++ b/EventHandler.cpp @@ -11,10 +11,26 @@ EventHandler::EventHandler(GLFWwindow *p_window) void EventHandler::handleEvents() { glfwPollEvents(); + for(int i=0; i +#include "eventActions.h" + class EventHandler { public: @@ -11,10 +13,14 @@ public: void handleEvents(); + bool * getCameraActionRegister() { return actionCameraRegister; } + private: static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); + bool actionCameraRegister[CAMERA_ACTION_NUM_ITEMS] = {0}; + GLFWwindow *window; static bool wireFrameMode; diff --git a/Window.cpp b/Window.cpp index 6fd6d24..846920a 100644 --- a/Window.cpp +++ b/Window.cpp @@ -20,6 +20,9 @@ Window::Window() { exit(-1); } + // Enable z buffer + glEnable(GL_DEPTH_TEST); + #ifdef _DEBUG std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; glEnable(GL_DEBUG_OUTPUT); diff --git a/eventActions.h b/eventActions.h new file mode 100644 index 0000000..4ed8acb --- /dev/null +++ b/eventActions.h @@ -0,0 +1,9 @@ +enum cameraActions{ + cameraUp, + cameraDown, + cameraForward, + cameraBackward, + cameraLeft, + cameraRight, + CAMERA_ACTION_NUM_ITEMS +};