GLART week 3: Space (Perspective, Ortho, Frustum)
OpenGL provides several functions for defining the dimensions of the viewable space of a scene. One of the functions in the GLU utility class, gluPerspective(), provides a simple way to specify the depth and field of view (FOV). Another GLU function, gluLookat() sets the position of the "eye" in the scene. Together these two functions define how much we can see on the screen and what direction we are facing. One way to think of these functions is that gluLookat defines a camera position and gluPerspective defines the type of lens (wide-angle vs. telephoto). OpenGL can also create spaces with no perspective at all. The glOrtho() command "flattens out" the space, creating an orthographic projection, like a blueprint. In this case, the Z position of a shape has no effect on it's size or position on screen. This is not literally a two dimensional space though. Objects still have Z coordinates and objects can be in front of one another, but they will be rendered without perspective. This mode also can be used to exactly map the geometric space to the pixel dimensions of the screen. In this case a vertex at position 100,200 will appear at pixel position 100,200 on screen. Both the gluPerspective() and the glOrtho() commands affect the frustum, which means the shape of the viewing area. When viewing in perspective the frustum looks like section of a pyramid. The glFrustum() command is a lower level function that can be used to create the perspective and ortho matrices directly. This is useful when you need more exact control over how you will view the space. After defining the camera lens (gluPerspective() or glOrtho()) and setting the camera position and direction (gluLookAt()), you can also use glViewport() to control the dimensions of the area that OpenGL renders into on your window. By default this is the same size as the window itself, so in most cases you don't have to do anything. At times you may want to render into a specific area of a window and glViewport() allows you to do that. Assigment: Space, Perspective and Ortho Reading: Redbook pages 96-104 "Overview: the Camera Analogy" (Chapter 3) Redbook pages 116-130 "Viewing Transformations" (Chapter 3) Redbook pages 177-180 "Real World and OpenGL Lighting" -- a brief intro to light Examples: Ortho Mode Ortho Mode and quad-strip circles Using gluLookat() to position eye Images: The Invention and Re-Invention of space See also: nate robbins projection tutorial: http://www.xmission.com/~nate/tutors.html opengl FAQ -- Viewing and Camera Transforms http://www.opengl.org/resources/faq/technical/viewing.htm Matrix Math primer http://chortle.ccsu.edu/VectorLessons/vmch13/vmch13_1.html Speculations on the origin of Linear Perspective, by Richard Talbot http://www.nexusjournal.com/Talbot-pt01.html Interesting article discusses some early techniques in perspective rendering. "... Masaccio's Trinity, (circa 1427), which is thought to be the earliest painting to show a systematic approach to spatial diminution, and Piero della Francesca's Flagellation. These paintings are considered to be particularly significant because of the artists' respective links with Brunelleschi, credited with discovering perspective, and Alberti, credited with the first written account of the theory of linear perspective in 1435." Lucid explanation of VSync http://forums.rojakpot.com/showthread.php?p=263648#post263648 NOTES: ----------------------------------------------------------------------- GLU.gluPerspective( fieldOfView, aspectRatio, nearZplane, farZplane ) -------------------- Use gluPerspective() to define the viewing space. Think of this as the type of lens on a camera, a wide-angle lens has a wider field of view. The nearZplane and farZplane are not Z coordinates in the world, they are distances from the eye position. Think of the z planes as something like the focal distance of a camera, ie. the camera can only see things between one inch and 10 miles in front of it. Call glMatrixMode(GL_PROJECTION) to switch to the projection matrix before calling gluPerspective(). GLU.gluLookAt(eyeX, eyeY, eyeZ, // eye position tgtX, tgtY, tgtZ, // target to look at upX, upY, upZ) // which way is up ------------------------------ Use gluLookat() to place the camera in the world space. Where gluPerspective() defines the camera lens properties, gluLookAt() is the position and orientation of the camera in the world space. Call glMatrixMode(GL_MODELVIEW) before calling gluLookAt(), since this command acts on the object space, not the projection. And call gluLookAt() before creating shapes (glBegin()... glVertex() ...glEnd()). glOrtho(leftX, rightX, bottomY, topY, nearZ, farZ) ------------------------------ Defines an orthographic viewing space, meaning a space that has no perspective. This is not literally a two dimensional space. The geometry may still have Z coordinates and be fully 3D, but the view of it will appear as a blueprint. glFrustum(leftX, rightX, bottomY, topY, nearZ, farZ) ------------------------------ Defines a "frustum", a perspective viewing space. gluPerspective() is a higher level function that calls glFrustum() to create the appropriate matrix. glFrustum() can be used in situations where you need more direct control of the viewing space. glViewport() ------------------------------ Sets the position and dimensions of the OpenGL "screen". By default the viewport is the same dimensions as the window. The glViewport() command can be called to render into a smaller area of the window.