Skip to content
Fjodor Schelichow edited this page Nov 13, 2017 · 6 revisions

Types of camera

sgltk provides three types of camera. The main difference is the type of projection matrix used.

  • O_Camera is an orthographic projection camera
  • P_Camera is a perspective projection camera
  • IP_Camera is an infinite perspective projection camera

Creating a camera

Creating a new camera in sgltk is as easy as creating an object of either the O_Camera, the P_Camera or the IP_Camera class.

sgltk::Window window("Title", 1024, 768, 100, 100);
sgltk::P_Camera camera(glm::vec3(5, 10, 5),   //camera position
                       -glm::vec3(5, 10, 5),  //camera view direction
                       glm::vec3(0, 1, 0),    //the up vector
                       glm::radians(90),      //the vertical field of view in radians
                       window.width,          //the width of the screen, here 1024
                       window.height,         //the height of the screen, here 768
                       0.1f,                  //the distance to the near plane
                       800.0f);               //the distance to the far plane

Every camera provides a view and a projection matrix.

Changing camera parameters

If you change either the position, direction or up vector of the camera, you will have to update the view matrix by calling the update_view_matrix function.

camera.position = glm::vec3(10, 10, 10);
camera.update_view_matrix();

If you change the field of view, display width or height or the distance to the near or the far planes, you will have to update the projection matrix by calling the update_projection_matrix function.

camera.near_plane = 10.0f;
camera.update_projection_matrix();

Frustum calculations

All camera subclasses provide functions to calculate the corner points of their frustum.

std::vector<glm::vec3> fpoints = camera.calculate_frustum_points();

Since the an infinite perspective camera does not have a far plane, only the four points of the near plane are calculated.

You can also calculate the distance of a point to the frustum planes

glm::vec3 point(4, 2, 3);
std::vector<float> dist = calculate_frustum_distance(point);

A positive value indicates that the point and the frustum are on the opposite sides of the plane.

Additionally the O_Camera class provides a function that allows to setup it's frustum so that it encloses the frustum of another O_Camera or P_Camera object.

float offset = 10.0f;                 //The frustum of the camera will be tightly
                                      //enclosed by the newly calculated frustum.
                                      //Since this is not always desirable, you can
                                      //set a value by which to offset the planes
                                      //of the new frustum

glm::vec3 light_direction(5, -3, 2);  //The new view direction
sgltk::O_Camera light_camera;
light_camera.calculate_bounding_frustum(camera, light_direction, offset);

Clone this wiki locally