168 471 Computer Graphics, KKU. Lecture 13
1
Transformation and Viewing in OpenGL
168 471 Computer Graphics, KKU. Lecture 13
2
Scenario:We want to make sure that• The final image of the scene
contains a good view.• The portion of the floor is visible.• All objects in the scene are
visible.• All objects in the scene are
presented in an interesting arrangement.Questions
• How to position the models?• How to orient the models?• How to establish the location of
the viewpoint?
Note: all tasks have to happen in three space.
168 471 Computer Graphics, KKU. Lecture 13
3
Series of Operations
3There are computer operations that convert an object’s 3D coordinates to pixel position on the screen.
• Matrix Multiplications: Transformations (modeling, viewi ng and projection) to roate, scale, reflect, orthographical
ly project and perspectively project. Generally, we use a combination of several of these.
• Clipping operation: throw out objects that lie outside the area or volume.
• Mapping operation: established between the transforme d coordinates and pixels. This is known as a Viewport tra
nsformations
168 471 Computer Graphics, KKU. Lecture 13
4
The Camera AnalogyTT TTTT T TTTTTTTTTT T TTT T TTT TTT
TTTTT T TTTT TT,• Set up your tripod and pointing th
e camera at the scene (viewing trans.)
• Arrange the scene to be photogra phed into the desired compositio
n (modoling trans.)• Choose a camera lens or adjust th
e zoom (projection trans.)• Determine how large you want th
- e final photograph to be for exa mple, you might want it enlarged
(viewport trans.)• After these steps are performed,
the picture can be snapped or the scene can be drawn.
168 471 Computer Graphics, KKU. Lecture 13
5
Stages of Vertex Transformation
• Modelview matrix: orients the model and the camera relativ e to each other.
• Projection matrix: specifies the shape and orietation of the v iewing volume.
• Viewport transformation: controls the conversion of 3D mod el coordinates to screen coordinates.
168 471 Computer Graphics, KKU. Lecture 13
6
Example: Drawing Cube
#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>
void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}
void display(void){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation */ glutWireCube (1.0); glFlush ();}
168 471 Computer Graphics, KKU. Lecture 13
7
Example: Drawing Cube (cont.)
void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW);}
int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;}
168 471 Computer Graphics, KKU. Lecture 13
8
Example: Drawing Cube (cont.)T TTT TTT TTTTTTTTT TTTTT• Analogus to positioning and aiming the camera.• Usage: gluLookAt()• Arguments indicate where the camera (eye position) is pla
ced, where it is aimed, and which way is up.• In the example, we place the camera at (0, 0, 5), aim the c
- amera lens toward (0, 0, 0) and specify the up vector as (0 , 1, 0)
• 000By default the camera is at the origin ( , , ), points dow - - n the negative z axis and has an up vector of (0, 0, 1)T TTTTTTT TTTTTTTTT TTTTT
• Analogous to positioning and orienting the model.• Uasge: glScalef()• Arguments specify how scaling should occur along the 3 a
xes.• In the example, the cube is drawn twice as large in the y di
rrrrrrr Whataboutusing glTranslatef(0 , 0 , 5 ) instead of gluLookAt()
?
168 471 Computer Graphics, KKU. Lecture 13
9
Example: Drawing Cube (cont.)T TTTTTTTTT TTTTTTTTT TTTTT• Similar to choosing a lens for a camera as determining wh
at the field of view (FOV) or viewing volume is.• In addition, it determines how objects are projected onto s
creen.• Usage: glFrustum()• Arguments describe values of left, right, bottom, top, near
and far for a viewing volume.• Before calling glFrustum(), glMatrixMode() with the argum
ent GL_PROJECTION must be called.• After calling glFrustum(), the matrix stack must be set bac
k to GL_MODELVIEW• Take car e t he cur r ent mat r i x wi t h gl LoadI dent i t y( )
Note: Default matrix stack is GL_MODELVIEW
168 471 Computer Graphics, KKU. Lecture 13
10
Example: Drawing Cube (cont.) Viewport Transformation
• I ndi cat es t he r egi on of avai l abl e scr een ar ea i nt o wh ich the scene is mapped.
• Usage: glViewport()• The arguments describe the origin, the width and height o
f the region within the window.
What does OpenGL do when all transformations have been specified?• Transforms each vertex of every object in the scene by th
e modeling and viewing transformations.• Transforms the vertices and clips the objects by the projec
rrrr rrrrrrrrr rrrrrr • rrrrrrr rrr rrrrrrrrr rrrrrrrrrrr rrrrrrrr rrrr r rrr rrrr r
hem onto the viewport.
168 471 Computer Graphics, KKU. Lecture 13
11
Let’s Think About Transformations
TTTTTTTT TTT TTTTT TT TTTTTTTTT TTTTT TT TTTTTTTT,• If oyu do transformation A and then transformation B, you
almost always get something different than you do them t he the opposite order.
168 471 Computer Graphics, KKU. Lecture 13
12
Order of Transformations in OpenGL
Consider the following code sequence, which draws a single point using three transformations:
glMatrixMode(GL_MODELVIEW);glLoadIdentity();glMultMatrixf(N); /* apply transformation N */glMultMatrixf(M); /* apply transformation M */glMultMatrixf(L); /* apply transformation L */glBegin(GL_POINTS);glVertex3f(v); /* draw transformed vertex v */glEnd();
• The modelview matrix successively contains I, N, NM and fi nally NML.
• The transformed vertex is NMLv, equivalent to N(M(LvThe t ransformations to vertex v effectively occur in the opposite
order than they were specified.• Actually, the N, M and L metrices are alrady multiplied into
a single matrix before applied to v.
168 471 Computer Graphics, KKU. Lecture 13
13
Modeling Transformations
1. Translate
voi d glTranslate {fd}(TYPEx, TYPE y, TYPEz);
Multiplies the current matrix by a matri x that moves (translates) an object by t
he given x, y, and z values (or moves t he local coordinate system by the sam
e amounts).
168 471 Computer Graphics, KKU. Lecture 13
14
Modeling Transformations (cont.)
2. Rotate voi d glRotate {fd}(TYPE angle, TYPE x, T
YPE y, TYPE z); Multiplies the current matrix by a matri
x that rotates an object (or the local co ordinate system) in a counterclockwise direction about the ray from the origin
through the point (x, y, z). The angle pa rameter specifies the angle of rotation i
n degrees.glRotatef( 450 00 00 10. , . , . , . ) ,
168 471 Computer Graphics, KKU. Lecture 13
15
Modeling Transformations (cont.)
3. Scale voi d glScale {fd}(TYPEx, TYPE y, TYPEz);
Multiplies the current matrix by a matrix t hat stretches, shrinks, or reflects an obje
ct along the axes. Each x, y, and z coordi nate of every point in the object is multipl
ied by the corresponding argument x, y, or z. With the local coordinate system ap
proach, the local coordinate axes are stre tched, shrunk, or reflected by the x, y, an
d z factors, and the associated object is tr ansformed with them.
glScalef( - 20 05 10. , . , . )
Note: A scale value of zero collapses allobjects coordinates along that axis to zero
168 471 Computer Graphics, KKU. Lecture 13
16
Viewing Transformations
A viewing transformation changes the position and orientation of the viewpoint.
voi d gluLookAt (GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GL
double centery, GLdouble centerz, GLdoubl e upx, GLdouble upy, GLdouble upz);
Defines a viewing matrix and multiplies it to the right of the current matrix. The des
ired viewpoint is specified by eyex, eyey, and eyez. The centerx, centery, and cent
erz arguments specify any point along th e desired line of sight, but typically they'r e some point in the center of the scene b
eing looked at. The upx, upy, and upz arg uments indicate which direction is up (th
at is, the direction from the bottom to the top of the viewing volume).
168 471 Computer Graphics, KKU. Lecture 13
17
Viewing Transformations (cont.)
Default camera position - -40 20 10 20 40 30 20 20 10gluLookAt( . , . , . , . , . , . , . , . , . );
168 471 Computer Graphics, KKU. Lecture 13
18
Viewing Transformations (cont.)
T TT TT TTT TTTT TTT TTT T TTTTTTTTTT T TTTT TTTTTTTTT TT TTT TT TTTTTTT T TTTT.• Use one or more modeling transformation commands (that is, gl
*Translate () and glRotate*() ). You can think of the effect of these transf ormations as moving the camera position or as moving all the o
bjects in the world, relative to a stationary camera.• Use the Utility Library routine gluLookAt() to define a line of sight. This rou
tine encapsulates a series of rotation and translation commands.
• Create your own utility routine that encapsulates rotations and translations.
168 471 Computer Graphics, KKU. Lecture 13
19
Projection Transformations
The purpose of the projection transformation is to define a viewing volume. There are 2 types of projections:• Perspective projection
• Orthographic projection
Remember that before you issue any of the projection transformation commands, you must call
glMatrixMode(GL_PROJECTION);glLoadIdentity();
168 471 Computer Graphics, KKU. Lecture 13
20
Perspective Projection
voi d glFrustum (GLdouble left, GLdouble rig ht, GLdouble bottom,GLdouble top, GLdoubl
e near, GLdouble far); - Creates a matrix for a perspective view frus tum and multiplies the current matrix by it . The frustum's viewing volume is defined - by the parameters: (left, bottom, near) an - d (right, top, near) specify the (x, y, z) coo - - rdinates of the lower left and upper right c orners of the near clipping plane; near and far give the distances from the viewpoint t o the near and far clipping planes. They sh ould always be positive.
168 471 Computer Graphics, KKU. Lecture 13
21
Perspective Projection (cont.)
voi d gluPerspective (GLdouble fovy, GLdoub le aspect, GLdouble near, GLdouble far);
Creates a matrix for a symmetric perspective- - viewfrustumandmultipliesthecurrentmatrixbyit. fovyistheangleofthefieldofviewint he x z pl ane; i t s val ue must b
e in the range [0.0,180.0]. aspect is the asp ect ratio of the frustum, its width divided by
its height. near and far values the distances between the viewpoint and the clipping plan
- es, along the negative z axis. They should al ways be positive.
168 471 Computer Graphics, KKU. Lecture 13
22
Orthographic Projection
voi d glOrtho (GLdouble left, GLdouble right, G Ldouble bottom, GLdouble top, GLdouble near,
GLdouble far); Creates a matrix for an orthographic parallel viewing volume and multiplies the current - matrix by it. (left, bottom, near) and (right, - top, near) are points on the near clipping pl - ane that are mapped to the lower left and u- pper right corners of the viewport window, r - espectively. (left, bottom, far) and (right, to - p, far) are points on the far clipping plane t hat are mapped to the same respective corn ers of the viewport. Both near and far can b e positive or negative.
168 471 Computer Graphics, KKU. Lecture 13
23
Orthographic Projection (cont.)
voi d gluOrtho2D (GLdouble left, GLdouble rig ht, GLdouble bottom,
GLdouble top); -Creates a matrix for projecting two dimensio
nal coordinates onto the screen and multipli es the current projection matrix by it. The cli
-pping region is a rectangle with the lower le -ft corner at (left, bottom) and the upper righ
t corner at (right, top).
168 471 Computer Graphics, KKU. Lecture 13
24
Viewport Transformation
TTT TTTTTTTT TTTTTTTTTTTTTT TTTTTTTTT TTT TTTTTTTTTTT TTT TT TTT TTTTTT TTTTT TTT TTTTT TT TTTTTT.
voi d glViewport (GLint x, GLint y, GLsizei w idth, GLsizei height);
Defines a pixel rectangle in the window int o which the final image is mapped. The (x
- , y) parameter specifies the lower left cor ner of the viewport, and width and height are the size of the viewport rectangle. By
default, the initial viewport values are (0, 0, winWidth, winHeight), where winWidth
and winHeight are the size of the window.
168 471 Computer Graphics, KKU. Lecture 13
25
Viewport Transformation (cont.)
TTT TTTTTT TTTTT TT T TTTT TTTT TTTTTT TTTTTTTTT TTTTT TT TTT T TTTTT TT TTT TTTT TTT TTTTT TT.
gluPerspective(fovy, 1.0, near, far); glViewport(0, 0, 400, 400);
gluPerspective(fovy, 1.0, near, far);glViewport (0, 0, 400, 200);
168 471 Computer Graphics, KKU. Lecture 13
26
• OpenGL supports two stacks of matrices – Modelview matrix stack (32 4x4 matrices)– Projection matrix stack (2 4x4 matrices)
• These stacks are useful for constructing hierarchical models. For example a car made of its body and the four wheels:
Rotate wheelsRotate wheels
+Rotate body
Matrix Stacks
168 471 Computer Graphics, KKU. Lecture 13
27
• glPushMatrix(void) - Pushes all matrices in the current stack down one level.
• glPopMatrix(void) - Pops the top matrix off the current stack, losing the topmost matrix!
• (The current stack is determined by glMatrixMode).
Matrix Stacks
M5
M4
M3
M2
M1
M5
M4
M3
M2
M1
Pop
Push
M4
M3
M2
M1
M4
M4
M3
M2
M1
Current matrix
level
Current matrix
level
168 471 Computer Graphics, KKU. Lecture 13
28
• Example code:void drawCar() {
glMatrixMode(GL_MODELVIEW) ;glTranslatef(x,y,z) ; /*/glRotatef(car_ang, 0, 1, 0) ; /*/draw_car_body() ;glPushMatrix() ; glTranslate(-1,0,1) ; glRotatef(wheels_ang, 0, 1, 0) ; draw_car_wheel() ; glPopMatrix() ; glPushMatrix() ; glTranslate(1,0,1) ; glRotatef(wheels_ang, 0, 1, 0) ; draw_car_wheel() ; glPopMatrix() ;
}
• First we move and rotate the car (body + wheels) - as it is the top level in the hierarchy.
• Next we push the stack - and therefore store a copy.
• Then we draw the right and left wheels in their appropriate position and orientation. Note that on each wheel the transformation /*/ will operate.
• The last pop will retrieve the matrix containing only the /*/ transformations.
Matrix Stacks
168 471 Computer Graphics, KKU. Lecture 13
29
Additional Clipping Planesvoid glClipPlane(GLenum plane, const GLdouble *equation);
Defines a clipping plane. The equation argument points to the four coefficients of the plane equation, Ax+By+Cz+D = 0. All points with eye coordinates (xe, ye, ze, we) that satisfy (A B C D)M-1 (xe ye ze we)T >= 0 lie in the half-space defined by the plane, where M is the current modelview matrix at the time glClipPlane() is called. All points not in this half-space are clipped away. The plane argument is GL_CLIP_PLANEi, where i is an integer specifying which of the available clipping planes to define. i is a number between 0 and one less than the maximum number of additional clipping planes.
Each plane is specified by the coefficients of its equation: Ax+By+Cz +D = 0.
168 471 Computer Graphics, KKU. Lecture 13
30
Global and Local Frames
glPushMatrix();glutWireSphere(1.0, 20, 16); /* draw sun */glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);glTranslatef (2.0, 0.0, 0.0);glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);glutWireSphere(0.2, 10, 8); /* draw smaller planet */glPopMatrix();
In summary, these are the OpenGL commands to draw the sun and planet
168 471 Computer Graphics, KKU. Lecture 13
31
168 471 Computer Graphics, KKU. Lecture 13
32
Global and Local Frames#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>
static int year = 0, day = 0;
void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}
void display(void){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0);
glPushMatrix(); glutWireSphere(1.0, 20, 16); /* draw sun */ glRotatef ((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0); glRotatef ((GLfloat) day, 0.0, 1.0, 0.0); glutWireSphere(0.2, 10, 8); /* draw smaller planet */ glPopMatrix(); glutSwapBuffers();}
void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);}
void keyboard (unsigned char key, int x, int y){ switch (key) { case `d': day = (day + 10) % 360; glutPostRedisplay(); break; case `D': day = (day - 10) % 360; glutPostRedisplay(); break; case `y': year = (year + 5) % 360; glutPostRedisplay(); break; case `Y': year = (year - 5) % 360; glutPostRedisplay(); break; default: break; }}
int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}
Top Related