## GiFrustum and the Viewing Volume

You might have noticed that none of the points describing the camera position using gluLookAt deal with size. They deal only with positioning, direction, and orientation. How can you tell the camera where to focus? How far away is the subject you are trying to capture? How wide and how tall is the subject area? You use the OpenGL method glFrustum to specify the area of the scene that you are interested in.

Think of the scene area as bounded by a box, also called the frustum or viewing volume (see the area marked by the bold border in the middle of Figure 10-1). Anything inside the box is captured, and anything outside the box is clipped and ignored. So how do you specify this viewing box? You first decide on the near point, or the distance between the camera and the beginning of the box. Then you can choose a far point, which is the distance between the camera and the end of the box. The distance between the near and far points along the z axis is the depth of the box. If you specify a near point of 50 and a far point of 200, then you will capture everything between those points and your box depth will be 150. You will also need to specify the left side of the box, the right side of the box, the top of the box, and the bottom of the box along the imaginary ray that joins the camera to the look-at point.

In OpenGL, you can imagine this box in one of two ways. One is called a perspective projection, which involves the frustum we've been talking about. This view, which simulates natural camera-like function, involves a pyramidal structure in which the far plane serves as the base and the camera serves as the apex. The near plane cuts off the "top" of the pyramid, forming the frustum between the near plane and the far plane.

The other way to imagine the box involves thinking of it as a cube. This second scenario is called orthographic projection, which is suited for geometrical drawings that need to preserve sizes despite the distance from the camera.

Let's see how to specify the frustum for our example:

//calculate aspect ratio first float ratio = (float) w / h;

//indicate that we want a perspective projection glMatrixMode(GL10.GL_PROJECTION);

//Specify the frustum: the viewing volume gl.glFrustumf(

-ratio, // Left side of the viewing box ratio, // right side of the viewing box 1, // top of the viewing box -1, // bottom of the viewing box

3, // how far is the front of the box from the camera 7); // how far is the back of the box from the camera

Because we set the top to 1 and bottom to -1 in the preceding code, we have set the front height of the box to 2 units. You specify the sizes for the left and right sides of the frustum by using proportional numbers, taking into account the window's aspect ratio. This is why this code uses the window height and width to figure out the proportion. The code also assumes the area of action to be between 3 and 7 units along the z axis. Anything drawn outside these coordinates, relative to the camera, won't be visible.

Because we set the camera at (0,0,5) and pointing toward (0,0,0), 3 units from the camera toward the origin will be (0,0,2) and 7 units from the camera will be (0,0,-2). This leaves the origin plane right in the middle of your 3D box.

So now you've identified how big your viewing volume is. You need to understand one more API to map these sizes to the screen: glViewport.

## Post a comment