290 likes | 486 Views
OpenGL (II). How to Draw a 3-D object on Screen?. Drawing a Scene. Apply modeling and viewing transformations Apply projection transformation Clip if it lies outside the viewing volume Apply viewport transformation to display on the screen. Transformations.
E N D
Drawing a Scene • Apply modeling and viewing transformations • Apply projection transformation • Clip if it lies outside the viewing volume • Apply viewport transformation to display on the screen
Transformations • Modeling Transformation • Viewing Transformation • Projection Transformation • Viewport Transformation
Stages of Vertex Transformations • Object Local Coordinates • ModelView • Matrix • Projection • Matrix • Eye Coordinates • Vertex • Clip Coordinates • Normalized Device Coordinates • Viewport • Transformation • Perspective • Division • Windows Coordinates
Transformations in OGL • Recall that once you define the attributes (such as color), all the subsequent objects are drawn using those attributes. • Same rule applied for transformations • Specify a transformation, and then this transformation is automatically applied to every object that is drawn, until the transformation is set again. • Important: set transformations prior to drawing
Transformations in OGL • Transformations have different purposes • We discuss only two of OGL’s matrix types (the third is Texture matrix) • Modelview matrix for moving objects or change of coordinates • Projection matrix for projection • Project objects from the world window to the viewport, and mapping the viewport to the graphics display window.
Transformations in OGL • For each matrix type, OGL maintains a stack of matrices • Stack: last in, first out • To operate on a specific matrix type,call glMatrixMode(mode) • Different modes: GL_MODELVIEW, GL_PROJECTION • Once the matrix mode is set, performvarious operations on the stack.
Matrix Stacks (Con’t) • There are several routines for manipulating matrix stacks • glPushMatrix() • glPopMatrix() • glLoadMatrix() • glMultMatrix() • glLoadIdentity() • To avoid destroying the contents of the Modelview matrix, save the contents of the Modelview matrix and restore its contents after we are done.
Modeling Transformation • Positions and orients the objects • Rotation: glRotate{fd}(angle, x, y, z) • Translation: glTranslate{fd}(x, y, z) • Scaling: glScale{fd}(x, y, z)
Model space World space Model space Modeling Transformation (Con’t)
Transformation Example • In OGL, whenever you draw, the points are automatically transformed using the current Modelview matrix. • Common way of object transformations in OGL • Push the matrix stack. • Apply all desired transformations • Draw your objects (the transformations will be applied automatically) • Pop the matrix stack.
M M MT MTR M M M Post-multiplication glTranslate() glPushMatrix() glRotate()
Order of Transformations • Specify the transformations in the reverse order of the way you conceptualize them Specify M1M2v Conceptualize
Order of Transformations (Con’t) • Example 1: TRv glTranslatef(2.0,0.0,0.0) glRotatef(45.0,0.0,0.0,1.0)
Order of Transformations (Con’t) • Example 2: RTv glRotatef(45.0,0.0,0.0,1.0) glTranslatef(2.0,0.0,0.0)
Transformation Example • First attempt • Rotation command • glRotatef(angle_in_degree, x, y, z); • Rotation is performed about the origin of the coordinate system. • Translation command • glTranslatef(x, y, z); glPushMatrix(); // save the current matrix glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW glRectf(x-2, y-2, x+2, y+2); // draw the rectangle glPopMatrix(); // restore the old matrix equivalent glPushMatrix(); // save the current matrix glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW glTranslatef(x, y, 0); // apply translation glRectf(-2, -2, 2, 2); // draw the rectangle glPopMatrix(); // restore the old matrix
Transformation Example • Correct way glPushMatrix(); // save the current matrix glTranslatef(x, y, 0); // apply translation glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW glRectf(-2, -2, 2, 2); // draw the rectangle glPopMatrix(); // restore the old matrix
OpenGL viewing • Modelview transformation • Modeling transformation: local coordinates world coordinates • Viewing transformation: world coordinates eye coordinates
Modelview Matrix • Pulling the camera back from the object (viewing transformation) moving the object away from the camera (modeling transformation) • Thus, both viewing and modelingtransformations are stored in the modelview matrix stack
y x z Viewing Transformation • Position and aim the camera • gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) • Default location at origin looking down the negative z-axis
Viewing Transformation (Con’t) Up vector Eye position Center position
OpenGL viewing • gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z) • Viewing direction: center – eye • Up is the upward direction • Viewing direction and up vector eye coordinate system • X axis points to the right of viewer • Y axis points upward • Z axis points to the back of viewer • Generate a matrix, which is postmultiplied to the top-of-the-stack matrix on the Modelview stack • Thus, must be called before any modeling transformations
OpenGL viewing • Default OpenGL viewing (if no gluLookAt is specified) • Eye is at origin of world space • Looking down the negative z axis of world space • Up vector is positive y axis • The viewing transformation matrix is identity matrix (i.e. eye coordinate system = world coordinate system)
Viewing Transformation (Con’t) gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0) Default location
OpenGL projection • glOrtho(), gluPerspective() or glFrustum() • Produce a matrix which is stored in the projection matrix stack • All geometry objects are already transformed to the eye coordinate system before projection transformation is applied • The parameters of these functions are with respect to the eye coordinate system • The parameters define 6 clipping planes • To simplify clipping, the viewing space is transformed into a canonical view volume (all coordinates in [-1, +1])
OpenGL orthographic projection glOrtho(left, right, bottom, top, near, far) • left, right, bottom, top are coordinates in eye space • left, right are the x-coordinate limits • bottom, top are the y-coordinate limits • near, far are signed distances from the eye to the near and far clipping planes (e.g., near = 2, far = 15 mean the clipping planes are at z=-2 and z= -15)
OpenGL perspective projection • The center of projection and the portion of the projection plane that map to the final image form an infinite pyramid. The sides of the pyramid are clipping planes. • All of the clipping planes bound the viewing frustum. • In OpenGL, PP = near plane
OpenGL perspective projection glFrustum(left, right, bottom, top, near, far) • View frustum may not be centered along view vector • Less often used than gluPerspective() gluPerspective(fov_y, aspect_ratio, near, far) • fov_y is vertical field-of-view in degrees • aspect ratio = width / height • near, far are distances from eye to two clipping planes • must be positive • Keep them close so as to maximize depth precision