-
Notifications
You must be signed in to change notification settings - Fork 25
Camera class
The camera class can be used to set up a simple first-person camera. To integrate it with your code you need to (i) set up callback mechanisms for keys, mouse buttons, and cursor movement, (ii) call a function for updating the view matrix inside the rendering loop, and finally (iii) retrieve the view matrix set up by the camera and make them available to your shaders. The following pieces of code illustrate how to set this up in program.cpp.
The first thing we have to do is include the camera class and instantiate it. The constructor has three optional paramaters: default camera position (glm::vec3), movement speed (GLfloat), and mouse sensitivity (GLfloat).
#include "gloom/camera.hpp"
Gloom::Camera camera;The next step is to set up callback mechanisms using GLFW. Make sure to add the necessary functions to your program.hpp header file.
void runProgram(GLFWwindow* window)
{
// Set GLFW callback mechanisms
glfwSetKeyCallback(window, keyboardCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, cursorCallback);
// ...
}
void keyboardCallback(GLFWwindow* window, int key, int scancode,
int action, int mods)
{
// ...
camera.handleKeyboardInputs(key, action);
}
void mouseButtonCallback(GLFWwindow* window, int button, int action,
int mods)
{
camera.handleMouseButtonInputs(button, action);
}
void cursorCallback(GLFWwindow* window, double xpos, double ypos)
{
camera.handleCursorPosInput(xpos, ypos);
}With the callback mechanisms correctly linked with the camera class, the view matrix can be updated, per frame, inside the rendering loop. The function for updating the view matrix relies on the frame time in order to balance the computer performance with movement velocity.
// Variables for keeping track of frame time
GLfloat deltaTime, lastFrame = 0.0f;
// Rendering Loop
while (!glfwWindowShouldClose(window))
{
// Compute the time the last frame took
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Update camera
camera.updateCamera(deltaTime);
// ...
}Now that the view matrix is updated every frame, we can retrieve it from the camera class using the getViewMatrix() function inside the rendering loop.
In addition to a view matrix, you will most likely also need a model matrix and a projection matrix. Below is a simple example for how to set this up. The model matrix is set to the identity matrix which means it will not do anything. A perspective projection is created using GLM with the following parameters: vertical field of view expressed in radians, aspect ratio using windowWidth and windowHeight from gloom/gloom.hpp, and the distance from the viewer to the near and far clipping planes.
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// ...
GLfloat fovy = glm::radians(45.0f);
GLfloat aspect = windowWidth / windowHeight;
GLfloat nearClip = 0.1f;
GLfloat farClip = 1000.0f;
// Set up default projection matrix
glm::mat4 matProj = glm::perspective(fovy, aspect, nearClip, farClip);
// Let the model matrix be the identity matrix
glm::mat4 matModel = glm::mat4(1.0f);Once you have your model, view, and projection matrices available, they can be assigned to your shader programs using uniform variables. Remember to activate the relevant shader before setting a uniform variable.
Below is a listing of the default camera controls:
| Key | Movement |
|---|---|
| W | Forward |
| A | Backward |
| S | Strafe left |
| D | Strafe right |
| Q | Vertical down |
| E | Vertical up |