Using a different set of maths to manipulate the camera

This commit is contained in:
Warwick 2022-02-03 16:16:32 +00:00
parent 19ad21dd7e
commit 8539780043
No known key found for this signature in database
GPG key ID: A063045576839DB7
2 changed files with 14 additions and 43 deletions

View file

@ -8,50 +8,23 @@ PlayerCamera::PlayerCamera() {
PlayerCamera::~PlayerCamera() {}
void PlayerCamera::tick() {
pos.vectorPos = glm::vec4(0.0f, 0.0f, 5.0f, 1.0f);
// Get the mouse movement
int mouseX, mouseY;
SDL_GetRelativeMouseState(&mouseX, &mouseY);
// get camera right
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward));
// Calculate rotation
pos.yaw -= mouseX * mouseSensitivity;
pos.pitch -= mouseX * mouseSensitivity;
// get position to look at
glm::vec3 cameraTarget = cameraPosition + cameraForward;
// Stop camera from looking too far up/down
const float maxPitch = glm::radians(89.0f);
if (pos.pitch > maxPitch)
pos.pitch = maxPitch;
if (pos.pitch < -maxPitch)
pos.pitch = -maxPitch;
// MVP stuff
glm::mat4 model = glm::mat4(1.0f);
glm::vec4 playerForward(0.0f, 0.0f, -1.0f, 0.0f);
glm::mat4 playerRotation;
// Rotate yaw
playerRotation =
glm::rotate(playerRotation, pos.yaw, glm::vec3(0.0f, 1.0f, 0.0f));
// Rotate pitch
playerRotation =
glm::rotate(playerRotation, pos.pitch, glm::vec3(1.0f, 0.0f, 0.0f));
playerForward = playerRotation * playerForward;
glm::mat4 view;
view = glm::lookAt(cameraPosition, cameraTarget, up);
// until we add keyboard stuff set the position to a static value
// create MVP
glm::mat4 view = glm::lookAt(
glm::vec3(pos.vectorPos),
glm::vec3(pos.vectorPos +
playerForward /* glm::vec4(0.0f, 0.0f, -1.0f, 0.0f) */),
glm::vec3(0.0f, 1.0f, 0.0f));
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units
float width = 800;
float height = 600;
glm::mat4 projection = glm::perspective(
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
glm::mat4 transform = glm::mat4(1.0f);
glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.0f);
MVP = projection * view * transform;
MVP = projection * view * model;
}
glm::mat4 PlayerCamera::getMVP() { return MVP; }

View file

@ -16,11 +16,9 @@ private:
const float mouseSensitivity = 0.005f;
// Player position
struct position {
glm::vec4 vectorPos;
float pitch;
float yaw;
} pos;
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f);
// Mouse position
int mouseX, mouseY;