คอมพิวเตอร์กราฟิกส์ใช้ OpenGL (Computer Graphics using OpenGL)
OpenGL (II). How to Draw a 3-D object on Screen?
-
date post
22-Dec-2015 -
Category
Documents
-
view
225 -
download
0
Transcript of OpenGL (II). How to Draw a 3-D object on Screen?
OpenGL (II) 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
Modeling Transformation Viewing Transformation Projection Transformation Viewport Transformation
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
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)
Modeling Transformation (Con’t)
World space
Model space
Model space
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.
Post-multiplication
M M
M
M
MT
M
MTR
glPushMatrix()
glTranslate()
glRotate()
Order of Transformations
Specify the transformations in the reverse order of the way you conceptualize them
Specify
Conceptualize
M1M2v
Order of Transformations (Con’t) Example 1: TRv
11000
0100
0045cos45sin
0045sin45cos
1000
0100
0010
0.2001
z
y
x
glTranslatef(2.0,0.0,0.0)glRotatef(45.0,0.0,0.0,1.0)
Order of Transformations (Con’t) Example 2: RTv
11000
0100
0010
0.2001
1000
0100
0045cos45sin
0045sin45cos
z
y
x
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
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
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 modeling transformations are stored in the modelview matrix stack
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
Viewing Transformation (Con’t)
Center position
Eye position
Up vector
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)
Default location gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0)
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 projectionglFrustum(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