Last active
April 13, 2020 14:48
-
-
Save pr0g/d1e896563b569e358bf2e7cf375b9ce9 to your computer and use it in GitHub Desktop.
Simple FPS Camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// glm | |
struct Camera | |
{ | |
glm::vec3 position { 0.0f }; | |
float yaw { 0.0f }; | |
float pitch { 0.0f }; | |
// pass this to the shader (the 'v' in the mvp multiplication) | |
glm::mat4 view() const | |
{ | |
glm::mat4 invView = glm::mat4(orientation()); | |
invView[3] = glm::vec4(position.x, position.y, position.z, 1.0f); | |
return glm::inverse(invView); | |
} | |
// basis of the camera (right/up/forward) | |
glm::mat3 orientation() const | |
{ | |
return glm::mat3(glm::rotate(yaw, glm::vec3{ 0.0f, 1.0f, 0.0f })) * | |
glm::mat3(glm::rotate(pitch, glm::vec3{ 1.0f, 0.0f, 0.0f })); | |
} | |
}; | |
// for right/up/forward vectors, just use orientation[0/1/2] when translating | |
#include "as/as-math-ops.hpp" // from https://github.com/pr0g/as | |
struct camera_t | |
{ | |
as::vec3_t position { 0.0f }; | |
float yaw { 0.0f }; | |
float pitch { 0.0f }; | |
as::mat4_t view() const | |
{ | |
return as::mat::inverse(as::mat4_t{ orientation(), position }); | |
} | |
as::mat3_t orientation() const | |
{ | |
return as::mat3::rotation_zxy(pitch, yaw, 0.0f); | |
} | |
}; | |
// for right/up/forward vectors, just use orientation()[0/1/2] or orientation().col[0/1/2]() when translating | |
// note: if using row major matrices then use orientation().row[0/1/2]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment