In Chapter 2, Your Point of View we compared creating games to making a video recording. Your video camera captures a part of the view in front of you. If objects move into or out of that field of view, they are no longer in the video recording.
3D games use a camera as well. OpenGL allows you to move the game camera on six axes: up, down, left, right, in, and out. As you move the game camera, the objects that are in its view change.
Let's say that you center the camera on the car in the scene and pan to the left or right. The car will move in and out of the field of view. Of course the same occurs if you pan the camera up or down. Move back (or zoom out) and the car appears smaller. Move forward (or zoom in) and the car appears larger. Tilt the camera and the car will appear to be going uphill, downhill, or even appear upside down!
Remember those home movies where the whole scene would jump around as the camera moved? Obviously, the position and movement of the camera has a lot to do with the appearance of the car. The same is true in the game world.
OpenGL uses the concept of a camera to determine exactly what shows up on the screen, and how it shows up. You have the ability to move the camera up or down, and left or right. You can rotate or tilt the camera. You have complete control!
Although you have complete control over moving the camera, some games simply place the camera at a particular spot and then leave it fixed. This is similar to taking your home video camera and attaching it to a tripod.
Many 2D games use a fixed camera, and this is exactly what we did in RoboRacer2D. All of the motion in the game came from changing the position of the objects in the game, not from changing the position of the camera.
In 3D games, it is very common to move both the camera and objects in the game. Imagine that we have a 3D scene with a moving car. If the camera remained fixed, the car would eventually move out of the scene. In order to keep the car in the scene, we need to move the camera so that it follows the car. Both the car and the camera need to move.
In game terminology, the area that can be seen by the camera at any time is called the viewport. The viewport defines the area of the game world that the camera can see:
The preceding illustration shows a viewport with a certain width and height. If the car moves outside of these boundaries, it will no longer be visible. In a 3D world, we must also define the depth of the image that we want to capture.
The preceding image shows how the 3D viewport is defined:
3.138.61.133