Game Development Reference
In-Depth Information
Building the View Matrix
By default, the camera view for OpenGL is located at the origin, with the lens pointed down the
negative z axis. The camera can be moved and rotated anywhere in the 3D world. What this actually
means is that the camera is stationary, but the object vertices are translated and rotated to simulate
the movement of the camera. The matrix that does this is called the view matrix. The resulting
coordinates are called the eye coordinates.
There is an easy way to do this in Android, and that is by using the Matrix.setLookAtM() function,
which generates a view matrix based on the camera or eye position; center of the camera, where the
view is focused; and the up direction of the camera. The Camera class represents our camera, and
Figure 4-10 shows the up, center, and right vectors that specify the camera's orientation.
Up Vector
Right Vector
Center Vector
Figure 4-10. Camera or eye
Listing 4-1 shows in code how the view matrix is created in our Camera class in the SetCameraView()
function.
Listing 4-1. Setting the View Matrix in the Camera Class
void SetCameraView(Vector3 Eye,
Vector3 Center,
Vector3 Up)
{
// Create Matrix
Matrix.setLookAtM(m_ViewMatrix,0,
Eye.x, Eye.y, Eye.z,
Center.x, Center.y, Center.z,
Up.x, Up.y, Up.z);
}
Building the Projection Matrix
The projection matrix transforms the 3D object vertices onto a 2D viewing surface. For 3D game
programming, we need to use the frustrum to project a 3D image onto a 2D surface. As noted
previously, a frustrum is a viewing area similar in shape to a pyramid with the top capstone cut off.
It is defined by six clip planes, which are named top, bottom, right, left, near, and far, as indicated
in Figure 4-11 . By using a frustrum, objects that are closer to the viewer are larger than objects that
are farther away. A frustrum also limits the viewing area to the area within the frustrum and excludes
vertices outside of the frustrum. The vertices are transformed from world coordinates into clip
coordinates.
 
Search WWH ::




Custom Search