Camera Concept Primer
In the case of 2D graphics, you normally define an area where you draw your shapes and lines etc. This is the canvas. Its coordinate center is at (0,0) at the top-left corner and you define a width and a height for it in pixels. You can draw inside the canvas, but you can also draw a shape that is bigger or only partially inside the frame, which is clipped automatically at the borders of the canvas. This would be the "scene" to be built and rendered, but being visible only partially.
You can think of all those properties as being defined by the "camera" in the 3D case. Because of those similarities some 2D engines may provide a camera concept to tweak all those settings as well.
Your strategy when rendering a 2D image will be to paint the background first, then the middleground and the foreground last. This is the painter's algorithm. For a 3D scene you need to determine those layers in a similar fashion. Therefore you have a "camera" which defines via its view-axis which elements to draw first and last.
To understand the concept of a "camera" in (3D) computer graphics, you need to understand coordinate systems (and transformation matrices). There are several of them involved in building and rendering a scene.
I sacrifice some details in the following, for clarity of the example, but the general direction is valid:
- Imagine a 3D model in local coordinate space, with (0,0,0) at its center
- Imagine a scene containing that model at the position (100,100,100)
- Imagine a camera in the scene at (500,500,500) looking towards the model
- Imagine a canvas on which the image the camera "sees" is drawn.
All those stages are part of the OpenGL rendering pipeline and occur in all 3D engines as well (more generally named Graphics Pipeline).
If the image of the scene needs to be rendered, OpenGL will take the vertex information of all models in the scene and transform them into the coordinate system of the camera. This is done by a series of matrix multiplications.
The resulting coordinate space has the camera always at the origin (0,0,0). Its negative Z-axis points forward (in case of OpenGL) and there is a vector which defines what is "up", usually the Y-axis is used for that, so vec3(0,1,0). The camera defines the maximum render distance beyond which all vertices are not rendered anymore. The camera also defines the "viewport" (having the origin at the center) and the "canvas"/"image" in width and height in pixels (and origin at the top left). This will be the final rendered image to be displayed on screen.
Here is a tutorial that explains the steps in terms of OpenGL commands. You will notice that all the vectors to define a camera coordinate system are assembled first and used as input for the glLookAt()
. That is how OpenGL defines the transformation matrix of the camera.
Because the camera has a 3D position, it is almost always visualized as an entity of the scene graph in game engines like Unity for example. This allows easy manipulation of the camera's properties. However, this does not make clear that the camera is in fact a very special entity which defines the whole camera coordinate system needed during the render process. Thus, defines the cut-out of the scene that will be visible as the final image.
Sidenote: A scene can have more than one camera setup in the scene, but only one of them can be the active one, whose image is being rendered. Strictly speaking, there has to be one active camera per viewport, because you can have multiple viewports on a screen for example. This and more is covered under the topics of "Multi target rendering" and "Render to texture".