Download presentation
Presentation is loading. Please wait.
1
OpenGL (II)
2
How to Draw a 3-D object on Screen?
3
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
4
Transformations Modeling Transformation Viewing Transformation Projection Transformation Viewport Transformation
5
Clip Coordinates ModelView Matrix Projection Matrix Perspective Division Viewport Transformation Eye Coordinates Normalized Device Coordinates Windows Coordinates Vertex Object Local Coordinates Stages of Vertex Transformations
6
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
7
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.
8
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, perform various operations on the stack.
9
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.
10
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)
11
Modeling Transformation (Con ’ t) World space Model space
12
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.
13
Post-multiplication MM M M MT M MTR glPushMatrix() glTranslate() glRotate()
14
Order of Transformations Specify the transformations in the reverse order of the way you conceptualize them Specify Conceptualize M1M2vM1M2v
15
Order of Transformations (Con ’ t) Example 1: TRv glTranslatef(2.0,0.0,0.0) glRotatef(45.0,0.0,0.0,1.0)
16
Order of Transformations (Con ’ t) Example 2: RTv glRotatef(45.0,0.0,0.0,1.0) glTranslatef(2.0,0.0,0.0)
17
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 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 equivalent
18
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
19
OpenGL viewing Modelview transformation Modeling transformation: local coordinates world coordinates Viewing transformation: world coordinates eye coordinates
20
Modelview Matrix Pulling the camera back from the object (viewing transformation) moving the object away from the camera (modeling transformation) Thus, both viewing and modeling transformations are stored in the modelview matrix stack
21
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 x y z
22
Viewing Transformation (Con ’ t) Center position Eye position Up vector
23
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
24
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)
25
Viewing Transformation (Con ’ t) Default location gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, - 1.0)
26
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])
27
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)
28
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
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.