Using a different set of maths to manipulate the camera
This commit is contained in:
parent
19ad21dd7e
commit
8539780043
2 changed files with 14 additions and 43 deletions
|
|
@ -8,50 +8,23 @@ PlayerCamera::PlayerCamera() {
|
||||||
PlayerCamera::~PlayerCamera() {}
|
PlayerCamera::~PlayerCamera() {}
|
||||||
|
|
||||||
void PlayerCamera::tick() {
|
void PlayerCamera::tick() {
|
||||||
pos.vectorPos = glm::vec4(0.0f, 0.0f, 5.0f, 1.0f);
|
// get camera right
|
||||||
// Get the mouse movement
|
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||||
int mouseX, mouseY;
|
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward));
|
||||||
SDL_GetRelativeMouseState(&mouseX, &mouseY);
|
|
||||||
|
|
||||||
// Calculate rotation
|
// get position to look at
|
||||||
pos.yaw -= mouseX * mouseSensitivity;
|
glm::vec3 cameraTarget = cameraPosition + cameraForward;
|
||||||
pos.pitch -= mouseX * mouseSensitivity;
|
|
||||||
|
|
||||||
// Stop camera from looking too far up/down
|
// MVP stuff
|
||||||
const float maxPitch = glm::radians(89.0f);
|
glm::mat4 model = glm::mat4(1.0f);
|
||||||
if (pos.pitch > maxPitch)
|
|
||||||
pos.pitch = maxPitch;
|
|
||||||
if (pos.pitch < -maxPitch)
|
|
||||||
pos.pitch = -maxPitch;
|
|
||||||
|
|
||||||
glm::vec4 playerForward(0.0f, 0.0f, -1.0f, 0.0f);
|
glm::mat4 view;
|
||||||
glm::mat4 playerRotation;
|
view = glm::lookAt(cameraPosition, cameraTarget, up);
|
||||||
// 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;
|
|
||||||
|
|
||||||
// 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::mat4 projection = glm::perspective(
|
||||||
glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
|
glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.0f);
|
||||||
glm::mat4 transform = glm::mat4(1.0f);
|
|
||||||
|
|
||||||
MVP = projection * view * transform;
|
MVP = projection * view * model;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 PlayerCamera::getMVP() { return MVP; }
|
glm::mat4 PlayerCamera::getMVP() { return MVP; }
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,9 @@ private:
|
||||||
const float mouseSensitivity = 0.005f;
|
const float mouseSensitivity = 0.005f;
|
||||||
|
|
||||||
// Player position
|
// Player position
|
||||||
struct position {
|
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f);
|
||||||
glm::vec4 vectorPos;
|
glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
float pitch;
|
glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
float yaw;
|
|
||||||
} pos;
|
|
||||||
|
|
||||||
// Mouse position
|
// Mouse position
|
||||||
int mouseX, mouseY;
|
int mouseX, mouseY;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue