You are on page 1of 19

World-View Matrix

Camera Class
FPS Controls
Using different points of view can make a
single scene appear to be different worlds.
Every vertex in the world has a 4d coordinate:
<x, y, z, 1>.
This is the vertexs world location
gl_Position = worldCameraMat * worldLoc;
//(worldCameraMat = projectionMat * lookatMat)
//a 4x4 matrix transforms from world to screen
Translates the world

Rotates the world around the POV

Simulates a moving, turning camera or eye


// Move or turn camera
vec3 camLoc = { 0, 0, 2 };
vec3 camRot = { 0, 0, 0 };

mat3 rotMat =
(mat3)yawPitchRoll(camRot.y, camRot.x, camRot.z);

vec3 eye = camLoc;


vec3 center = eye + rotMat * vec3(0, 0, -1);
vec3 up = rotMat * vec3(0, 1, 0);

mat4 lookAtMat = glm::lookAt(eye, center, up);


// Zoom or change aspect ratio
float zoom = 1.f;
int width = 800;
int height = 600;

float fovy = 3.14159f * .4f / zoom;


float aspect = (float)width / (float)height;
float zNear = .01f;
float zFar = 1000.f;

mat4 perspectiveMat =
glm::perspective(fovy, aspect, zNear, zFar);
Member Data
Updating & Uploading
FPS Controls
4x4 Matrix
Well update this and upload it every frame

Use these to calculate projection


Field Of View (what if we zoom in?)
Aspect ratio (what if window resizes?)
Near & far plane distances

Use to calc & update lookat matrix


vec3 rotation, location, velocity
How do we apply velocity to change location?
loc = t vel
Get t every frame: step simulation forward 1 frame
t0 = t; // Prev time
t = (float)glfwGetTime(); // Cur time
dt = t - t0; // Delta time

Calculate a new matrix


camMat = glm::perspective() * glm::lookAt()
(refer to previous section for how to call these).

Upload the matrix


glUniformMatrix4fv( #, 1, GL_FALSE, &camMat[0][0]);
// Turn with mouse
float sens = .005;
int w = 800, h = 600;
double x, y;
glfwGetCursorPos(window, &x, &y);

camRot.y -= sens * (x - w * .5f); // Yaw


camRot.x -= sens * (y - h * .5f); // Pitch
camRot.x =
glm::clamp(camRot.x, -.5f * pi, .5f * pi);

glfwSetCursorPos(window , w * .5f, h * .5f);


// Move with arrows
vec3 camVel;
mat3 R = (mat3)glm::yawPitchRoll(camRot.y, camRot.x, camRot.z);

if (inputIsDown[GLFW_KEY_LEFT])
camVel += R * vec3(-1, 0, 0);
if (inputIsDown[GLFW_KEY_RIGHT])
camVel += R * vec3(1, 0, 0);
if (inputIsDown[GLFW_KEY_UP])
camVel += R * vec3(0, 0, -1);
if (inputIsDown[GLFW_KEY_DOWN])
camVel += R * vec3(0, 0, 1);

float speed = 1.f;


if (camVel != vec3())
camVel = glm::normalize(camVel) * speed;
glfwSetInputMode(window,
GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
int w, h;
glfwGetWindowSize(window, &w, &h);
glfwSetCursorPos(window, w/2, h/2);

You might also like