DOC PREVIEW
OSU CS 419 - OpenGL 3D Viewing

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

mjb -- November 16, 2011 3D Viewing 1 OpenGL 3D Viewing Orthographic 3D Viewing When viewing 3D scene data, OpenGL automatically places the eye at the origin, looking in the -Z direction, as shown below: XYZxleftxrightybottomytopznearzfar If this is not how you want to view your scene, you can “change” this situation by moving the scene with the gluLookAt( ) OpenGL routine: gluLookAt( ex,ey,ez, cx,cy,cz, ux,uy,uz ) (ex,ey,ez) is where you want to view your scene from (i.e., your eye point). (cx,cy,cz) is a point you want the eye to be looking at (i.e., the center point). (ux,uy,uz) is a vector indicating which direction you want to be considered “up” (i.e., how is your face twisted). It is convenient to think of gluLookAt( ) as a way to transform your eye to where you want it to be, but in fact what really happens is that gluLookAt( ) transforms the scene backwards so that it is in the right place so an eye at the origin sees the correct view. Once the eye is in this position, the extent of the scene that the program wants to be visible is defined by a call to glOrtho( ): glOrtho( xleft, xright, ybottom, ytop, znear, zfar );mjb -- November 16, 2011 3D Viewing 2 xleft, xright, ybottom, ytop, znear, and zfar are all floating point numbers in the units in which the scene is defined. Even though the z-clipping takes place in the -Z region, both znear and zfar are defined as distances in front of the eye and are thus positive numbers. glOrtho() defines a rectangular-solid-shaped area as shown above. (It is the rectangular shape of the viewing volume that makes this an orthographic view.) Any elements of the scene that exist in this box will be mapped to the viewport and, thus, be seen. Any elements that exist outside this box will be clipped. It sometimes helps to understand what is happening internally. A 3D point in your scene is mapped into X and Y window coordinates by projecting it onto the znear plane. The Z value is left undisturbed. This being an orthographic projection, the 3D point is projected to the znear plane by a line parallel to the Z axis. Thus, the projection of the point (X,Y,Z) onto the znear plane produces window coordinates of: Xw = X Yw = Y It is tempting to make the znear and zfar planes enormously far apart so that nothing of the scene is missed. This will work OK, unless z-buffering is being used. The distance (zfar-znear) is mapped to the integer range of the z-buffer. Even though the range of values that can be stored in the z-buffer is typically either 216 or 224, super large value of (zfar-znear) will result in a situation where many Z values in the scene map into the same integer Z value in the z-buffer. This is an ugly phenomenon called z-fighting. Perspective 3D Viewing Orthographic 3D viewing works very well. It is straightforward and easy to ask for. But, it has one major deficiency: objects in the scene do not project realistically. In an orthographic projection, an object moving steadily away from us does not get smaller, as we see it happen in real life. We need to define another way of getting a 3D-to-2D projection that gives us the farther-gets-smaller effect that we have come to expect. In contrast to the orthographic world, the perspective viewing volume is a truncated pyramid, or frustum:mjb -- November 16, 2011 3D Viewing 3 XYZxleftxrightybottomytopznearzfar As we did with the orthographic world, we want to project every (X,Y,Z) point in the scene onto the near z-clipping plane to determine X and Y window coordinates. In the perspective case, all projection lines go through the eye position, or origin. If we look at the situation from the +X axis, we see: Y-Zznear( Y, Z )( Yw, Znear ) From similar triangles, we can compute the window coordinates of a point (X,Y,Z):mjb -- November 16, 2011 3D Viewing 4 YwY=Znear−Z XwX=Znear−Z or: Xw=X⋅Znear−Z    Yw=Y⋅Znear−Z    Zw=Z These equations show that: • As objects get farther away (Z becomes larger negatively), the corresponding Xw and Yw values get smaller. • Objects behind the observer (Z>0) will be inverted. Do not let this happen. • Objects at the observer (Z=0) will project to infinity. Do not let this happen. The OpenGL glFrustum( ) procedure is the perspective equivalent to glOrtho(): glFrustum( xleft, xright, ybottom, ytop, znear, zfar ); As before, the values of xleft, xright, ybottom, and ytop are the boundaries of the scene as projected onto the znear plane. But, specifying boundaries is a little tricky. Recognize that specifying znear and the boundaries in glFrustum( ) is really just the same as specifying the angle of vision that comes off the eye. This is called the field-of-view (FOV) angle. The OpenGL gluPerspective( ) routine lets us specify the FOV and skip the X and Y boundaries: float fov; float aspect; float znear, zfar; . . . gluPerspective( fov, aspect, znear, zfar ); where fov is the field-of-view angle in the Y direction (in degrees), aspect is the ratio of width in the X direction to height in the Y direction, and znear and zfar are as specified before.mjb -- November 16, 2011 3D Viewing 5 There is nothing magical about gluPerspective( ): void gluPerspective( float fovy, float aspect, float near, float far ) // fovy is the full field of view angle in degrees // aspect is the x/y aspect ratio // near, far are z clipping distances in front of the eye { float rad; // half field of view angle in radians float left, right; // x clipping boundaries float bottom, top; // y clipping boundaries rad = (M_PI/180.) * (fovy/2.); top = near * tan( rad ); bottom = -top; left = aspect * bottom; right = aspect * top; glFrustum( left, right, bottom, top, near, far ); } Rule of Thumb: a field-of-view value of about 50-100˚ seems to work well. As with the orthographic projection, it is tempting to make the znear and zfar planes enormously far apart so that nothing of the scene is missed. This will work OK, unless z-buffering is being used. The distance (zfar-znear) is mapped to the integer range of the z-buffer. A super large value of (zfar-znear) will result in a situation where many Z values in the scene map into the same integer Z value in the z-buffer. This is an ugly phenomenon called z-fighting. Summary of the Use of


View Full Document

OSU CS 419 - OpenGL 3D Viewing

Download OpenGL 3D Viewing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view OpenGL 3D Viewing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view OpenGL 3D Viewing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?