Add basic camera controls
This commit is contained in:
33
Camera.cpp
33
Camera.cpp
@@ -1,13 +1,13 @@
|
||||
#include "Camera.h"
|
||||
#include "eventActions.h"
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
14
Camera.h
14
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;
|
||||
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -11,10 +11,26 @@ EventHandler::EventHandler(GLFWwindow *p_window)
|
||||
|
||||
void EventHandler::handleEvents() {
|
||||
glfwPollEvents();
|
||||
for(int i=0; i<CAMERA_ACTION_NUM_ITEMS; i++) {
|
||||
actionCameraRegister[i] = 0;
|
||||
}
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraForward] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraBackward] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraUp] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraDown] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraLeft] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
actionCameraRegister[cameraRight] = 1;
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::key_callback(GLFWwindow *p_window, int key, int scancode, int action, int mods) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
9
eventActions.h
Normal file
9
eventActions.h
Normal file
@@ -0,0 +1,9 @@
|
||||
enum cameraActions{
|
||||
cameraUp,
|
||||
cameraDown,
|
||||
cameraForward,
|
||||
cameraBackward,
|
||||
cameraLeft,
|
||||
cameraRight,
|
||||
CAMERA_ACTION_NUM_ITEMS
|
||||
};
|
||||
Reference in New Issue
Block a user