Add basic camera controls

This commit is contained in:
4VRDriver
2020-09-03 18:18:32 +02:00
parent 919f65fac7
commit ee6e783c3f
7 changed files with 92 additions and 9 deletions

View File

@@ -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;
}