Perspective Projection - A call to glFrustum() ================================================ Previously we looked at the gluLookAt() routine. If gluLookAt() was similar to placing a camera to shoot a picture i.e. the location of the camera, and the direction it points in, then glFrustum is much like a screen onto which the final image is mapped. The purpose of a projection transformation is to defiine a viewing volume. The viewing volume determines how an object is projected onto the screen and determines which objects or portions of objects are clipped out of the final image. As you could have guessed entering this section, there are two types of projection transformations. These are Perspective projection, and Orthographic projection. Here we shall examine perspective projection and specifically the gluFrustrum routine of OpenGL In this regard glFrustum and gluPerspective both fall into the category of perspective projection. FORESHORTENING: =============== Remember this word!!. Foreshortening is an important if not the most important characteristic of Perspective projection. By foreshortening what we mean is that the farther an object is from the camera the smaller it appears in the final image. This occurs because the viewing volume for perspective projection is a frustum of a pyramid (a truncated pyramid whose top has been cut off by a plane parallel to its base). Objects within this viewing volume are projected toward the apex of the pyramid, where the camera (our gluLookAt()) is perched. Objects closer to the viewpoint appear larger because they occupy a proportionally larger amount of viewing volume than those that are farther away. OBSERVATIONS ============ A call to he glFrustum function is shown below : glFrustrum(left, right, bottom, top, near, far); The frustum's viewing volume is defined by the parameters: (left, bottom, -near) and (right, top, -near) specify the (x,y,z) coordinates of the lower-left and upper-right corners of the near clipping plane; near and far give the distances from the viewpoint (i.e where our camera is located - remember gluLookAt()) to the near and far clipping planes. They should be always positive. EXAMPLE: ======== As before you see rendering screens to your left and the actual code in a box with active controls above. o We are again dealing with the same cube and to maintain simplicity we are drawing it with the same colors and again about the orign. o Also as before the coordinate axis is color coded for better visual understanding o For this example the camera (i.e. viewpoint) is positioned using the gluLookAt() utility at (2.0, 4.0, 13.0) coordinates. It still looks at the orign and the upvector for the camera is still the positive y axis. o In the initial state you see the cube rendered about the orign. Also shown below in the second frame is the actual frustum. The frustum has its lower left corner at (-5, -5, (13 - 9)) i.e. (-5, -5, 4). Here 13 is the distance along the z-axis where the viewpoint is located and 9 is the distance of the near clipping plane from this viewpoint. Therefore it follows that the near clipping plane is at a distance of (13 - 9) = 4 from the orign along the z axis. In a similar fashion the other coordinates of the frustum have been plotted. o For starters we have placed the near plane at 4 units along the +ve z axis and the far plane at a distance of -7 units (again 13 - 20) along the -ve z axis. Shown on your screen is a window entitled "gluLookAt() - The Viewing Transformation". This window shows the actual code for the cube rendered to your left. The cube has its six faces painted a distinct color,for purposes of easy recognition. These colors are as follows : The FRONT FACE of the cube is : YELLOW The BACK FACE of the cube is : RED The LEFT FACE of the cube is : GREEN The RIGHT FACE of the cube is : BLUE The BOTTOM FACE of the cube is : MAGENTA The TOP FACE of the cube is : CYAN ORIENTATION AXIS IN OGL ======================= Also the two blue lines you see represent the axis. The x-axis is horizontal going from negative on the left (blue line) to positive x (green line) on the right. The y-axis is the vertical line, going from negative y (red line) below the cube to zero and to positive (yellow) y above the cube. The z axis - not shown here can be imagined as coming out of and going into the screen. The axis is positive (cyan line) coming out of the screen and negative (black line) in the direction into the screen from the orign. OBSERVATIONS : ============== The gluLookAt() function which determines the position of the camera, the direction at which the camera is aimed at and the direction of the upvector i.e. sort of an orientation for the camera itself. Often we shall construct scenes in OGL around the orign and then want to look at it from an arbitrary point. The gluLookAt() function lets us do exactly this. The gluLookAt() function takes 9 arguements. The first three are x,y, and z coordinates for the positon of the camera. In our example these are referred to as Camera X, Camera Y and Camera Z. The next three coordinates refer to the reference point toward which the camera is aimed. These are referred to as Aim X, Aim Y, Aim Z in our example. Finally the routine takes three more arguements to fix the orientation. These are referred to as Vector X, Vector Y, Vector Z. In the default position the camera is at the origin, is looking down the negative z-axis (i.e. into the screen) and has the positive y-axis as straight up. EXAMPLE: ======== o The camera in our example is positioned at 10 units distance along the positive z axis. o The camera is aimed at the orign where we have rendered a cube. o The upvector in our example is defined as the positive y axis. With the camera at this position, we are literally standing in front of the cube and seeing its front face. Hence we see only a yellow square. Now let us try some different positions to get an understanding of this utility. At any point in this exercise you can return to the original values by clicking on the reset button. TRY THE FOLLOWING ----------------- o Move the Camera Z slider to 20 and click on execute. What happened ? We moved further along the positive Z axis i.e. further away from the object hence it now is rendered smaller. o Move the Camera Z slider to 3 and click on execute. Now we have moved closer to the object we are rendering and the yellow face of the cube pretty much occupies the entire screen. o Lets keep travelling into the cube. Move the Camera Z slider to -3 and click on execute. The color of the square changes to RED. Remember this is the back face of the cube. Can you explain why this happened. The reason is that we (the camera) went through the cube and out the back side and now we are on the back of the cube. Remember the camera is still pointing in the (0,0,0) direction (from the Aim X... ) coordinates. Hence we see the back face of the cube. o Lets move back into the cube. At a Camera Z value of -1 we are positioned inside the cube. You can see the different colored faces of the cube now i.e. blue to the left, green to the right, etc. Again we see YELLOW because we are at (0,0, -1) i.e. one unit along the negative Z axis and we are looking from there at the orign. o Looking at the cube from a different position: ---------------------------------------------- Click on the Reset button. Click on Execute again to go back to the original cube we drew. Try the following : Set Camera X -7 Set Camera Y 5 Set Camera Z 10 Now wee see three faces of the cube we have rendered. This is because with the new camera positions we are situated in 3D space at a point 7 units along the -ve X, 5 units upwards along the positive Y, and 10 units out of screen along the +ve Z direction. With the same example above try different Aim points. For example try the following values : Aim X at 0, Aim Y at 10, Aim Z at 0 We lose the cube when we execute the program. This is because although we are situated to in a good viewing position with regards to the cube, our camera is aimed at the wrong area. Much like trying to take a picture of a person but aiming above their head. Yes it is that simple!!!! o Here is an exercise. Try to position the camera so that you see the red back face, the cyan top and the blue face of the cube. If you were not successful try the following coordinates : Camera X at 7 Camera Y at 5 Camera Z at -6 o The upvector just orients the camera with regards to the axis. Say we define the upvector coordinates as : Vector X = 1 Vector Y = 1 Vector Z = 0 Our image seems to be flipped by a 45 degree angle. This is because the Vector coordinates specify which direction is up. In our case this is the (1,1,0) direction. In essence you can imagine that now we have a camera 10 units along the positive Z direction, aimed at the orign, but the camera itself is tilted 45 degrees along the XY plane. On to the next Exercise!!