51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include "Camera.hpp"
|
|
|
|
class FPSCamera : public Camera {
|
|
protected:
|
|
float yaw; // rotation y-axis
|
|
float pitch; // rotating x-axis
|
|
glm::vec3 lookAt;
|
|
const float mouseSensitivity = 0.3f;
|
|
glm::vec3 up;
|
|
|
|
public:
|
|
FPSCamera(float fov, float width, float height) : Camera(fov, width, height) {
|
|
up = glm::vec3(0.0f, 1.0f, 0.0f);
|
|
yaw = -90.0f;
|
|
pitch = 0.0f;
|
|
onMouseMoved(0.0f, 0.0f);
|
|
}
|
|
|
|
void onMouseMoved(float xRel, float yRel) {
|
|
yaw += xRel * mouseSensitivity;
|
|
pitch -= yRel * mouseSensitivity;
|
|
if (pitch > 89.0f) pitch = 89.0f;
|
|
else if (pitch < -89.0f) pitch = -89.0f;
|
|
|
|
glm::vec3 front;
|
|
front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
|
|
front.y = sin(glm::radians(pitch));
|
|
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
|
|
lookAt = glm::normalize(front);
|
|
|
|
update();
|
|
}
|
|
|
|
void update() override {
|
|
view = glm::lookAt(position, position+lookAt, up);
|
|
viewProj = projection * view;
|
|
}
|
|
|
|
void moveFront(float amount) {
|
|
translate(lookAt * amount);
|
|
update();
|
|
}
|
|
|
|
void moveSideways(float amount) {
|
|
translate(glm::normalize(glm::cross(lookAt, up)) * amount);
|
|
update();
|
|
}
|
|
|
|
}; |