Filip Smola

Categories

  • Open Sea

Tags

  • C++
  • OpenGL
  • camera
  • orthographic
  • perspective

Cameras are a central part of rendering. That is why they are the first thing I have implemented for rendering in Open Sea. They are essentially boxes of data that spit out projection and view matrices on demand. The two currently implemented camera types are orthographic and perspective. Both are contained in the open_sea::gl namespace, as is their common base class. The relevant pull request is available here.

Cameras in General

All cameras have a position and orientation in the world space. This describes where the camera is looking and goes to produce the view matrix. This matrix can then be used to transform vectors from world space to the view space, where they are then ready for projection.

The way the projection matrix is calculated is what distinguishes different camera types. The parameters of this matrix are, at the very least, the size of the viewport (window) and the distance to the near and far clipping planes. These help define the size of the visible space. The perspective camera also needs the field of view angle. The matrix can be used to project a vector in view space onto the viewport.

With both of these matrices, one can go directly from a vector in world space to the viewport. This is what the product of projection matrix times view matrix does.

Optimisations

Both cameras keep a cache for all three matrices (view, projection, projection-view). This allows them to only recalculate the values when a relevant attribute of the camera changes. This increases performance, because these attributes don’t change often. For example the size of the viewport rarely changes in the whole session, which allows for example the projection matrix of an orthographic camera to be calculated once and then reused for the rest of the session. The need for recalculation is tracked automatically as long as you use the setters provided. The recalculation itself is done whenever the projection-view matrix is requested.

Debug

Each of the cameras has ImGui debug controls that can be show using the showDebugControls() method. These give access to all of the camera’s attributes as well as the projection matrix itself. The access is direct, therefore changing the attributes here doesn’t trigger recalculation. For that purpose, there is a recalculate button which triggers recalculation of both projection and view matrices.

Perspective camera debug window