OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 2.1, 6th Edition
Excerpt from Chapter 3: Viewing
Troubleshooting TransformationsIt's pretty easy to get a camera pointed in the right direction, but in computer graphics, you have to specify position and direction with coordinates and angles. As we can attest, it's all too easy to achieve the well-known black-screen effect. Although any number of things can go wrong, often you get this effect—which results in absolutely nothing being drawn in the window you open on the screen—from incorrectly aiming the "camera" and taking a picture with the model behind you. A similar problem arises if you don't choose a field of view that's wide enough to view your objects but narrow enough so they appear reasonably large. If you find yourself exerting great programming effort only to create a black window, try these diagnostic steps:
Even after you've aimed the camera in the correct direction and you can see your objects, they might appear too small or too large. If you're using gluPerspective(), you might need to alter the angle defining the field of view by changing the value of the first parameter for this command. You can use trigonometry to calculate the desired field of view given the size of the object and its distance from the viewpoint: the tangent of half the desired angle is half the size of the object divided by the distance to the object (see Figure 3-19). Thus, you can use an arctangent routine to compute half the desired angle. Example 3-3 assumes such a routine, atan2(), which calculates the arctangent given the length of the opposite and adjacent sides of a right triangle. This result then needs to be converted from radians to degrees. Figure 3-19 Example 3-3 Calculating Field of View#define PI 3.1415926535
double calculateAngle(double size, double distance)
{
double radtheta, degtheta;
radtheta = 2.0 * atan2 (size/2.0, distance);
degtheta = (180.0 * radtheta) / PI;
return degtheta;
}
Typically, of course, you don't know the exact size of an object, and only the distance between the viewpoint and a single point in your scene can be determined. To obtain a fairly good approximate value, find the bounding box for your scene by determining the maximum and minimum x-, y-, and z-coordinates of all the objects in your scene. Then calculate the radius of a bounding sphere for that box, and use the center of the sphere to determine the distance and the radius to determine the size. For example, suppose all the coordinates in your object satisfy the equations –1 £ x £ 3, 5 £ y £ 7, and –5 £ z £ 5. The center of the bounding box is (1, 6, 0), and the radius of a bounding sphere is the distance from the center of the box to any corner—say (3, 7, 5)—or
If the viewpoint is at (8, 9, 10), the distance between it and the center is
The tangent of the half-angle is 5.477 divided by 12.570, which equals 0.4357, so the half-angle is 23.54 degrees. Remember that the field-of-view angle affects the optimal position for the viewpoint, if you're trying to achieve a realistic image. For example, if your calculations indicate that you need a 179-degree field of view, the viewpoint must be a fraction of an inch from the screen to achieve realism. If your calculated field of view is too large, you might need to move the viewpoint farther away from the object. |
|