Download presentation
Presentation is loading. Please wait.
Published byAlexina Washington Modified over 9 years ago
1
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon
2
In OpenGL matrices are part of the state Multiple types Model-View (GL_MODELVIEW) Projection (GL_PROJECTION) Texture (GL_TEXTURE) (ignore for now) Color(GL_COLOR) (ignore for now) Single set of functions for manipulation Select which to be manipulated by glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_PROJECTION); OpenGL Matrices
3
Can load and multiply by matrices defined in the application program The matrix m is a one dimension array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns In glMultMatrixf, m multiplies the existing matrix on the right Arbitrary Matrices glLoadMatrixf(m) glMultMatrixf(m)
4
glRotatef(theta, vx, vy, vz) Rotation, Translation, Scaling glTranslatef(dx, dy, dz) glScalef(sx, sy, sz) glLoadIdentity() Load an identity matrix: Multiply on right: theta in degrees, ( vx, vy, vz ) define axis of rotation Each has a float (f) and double (d) format (glScaled)
5
See Lab6 Lab7 Example
6
Simple square void draw_square() { glBegin( GL_POLYGON ); glColor3f( 1, 1, 0 ); glVertex2f( -0.5, -0.5 ); glColor3f( 0, 1, 0 ); glVertex2f( -0.5, 0.5 ); glColor3f( 0, 0, 1.0 ); glVertex2f( 0.5, 0.5 ); glColor3f( 1, 1, 1 ); glVertex2f( 0.5, -0.5 ); glEnd(); }
7
Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0) Remember that last matrix specified in the program is the first applied Example glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.0, 2.0, 3.0); glRotatef(30.0, 0.0, 0.0, 1.0); glTranslatef(-1.0, -2.0, -3.0);
8
Example These two fragment codes produce the same output? glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(100.0,50.0, 0.0); square(); glLoadIdentity(); glTranslatef(50.0,100.0, 0.0); square(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(100.0,50.0, 0.0); square(); glTranslatef(50.0,100.0, 0.0); square();
9
OpenGL Matrix Stacks (2) glPushMatrix() copies the top of stack this makes the top two items in the stack identical The top item can now be altered further with addition call transformation functions glPopMatrix() The top item is popped off the stack and discarded
10
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 M3 M2 M1 Current matrix level Current matrix level
11
Example glMatrixMode(GL_MODELVIEW) glColor3f(0.0, 0.0, 1.0); glRecti(50, 100, 200, 150)//Display blue rectangle glColor3f(1.0, 0.0, 0.0); glTranslatef(-200.0, -50.0, 0.0)//Set translation parameters glRecti(50, 100, 200, 150)//Display red, translated rectangle glLoadIdentity();//Reset current matrix to identity glRotatef(90.0, 0.0, 0.0, 1.0);//Set 90-deg. Rotation about z axis glRecti(50, 100, 200, 150)//Display red, rotated rectangle glLoadIdentity();//Reset current matrix to identity glScalef(-0.5, 1.0, 1.0);//Set scale-reflection parameters glRecti(50, 100, 200, 150)//Display red, transformed rectangle
12
Example and Draw the stack glMatrixMode(GL_MODELVIEW) glColor3f(0.0, 0.0, 1.0); glRecti(50, 100, 200, 150)//Display blue rectangle glPushMatrix();//Make copy of identity (top) matrix glColor3f(1.0, 0.0, 0.0); glTranslatef(-200.0, -50.0, 0.0)//Set translation parameters glRecti(50, 100, 200, 150)//Display red, translated rectangle glPopMatrix();//Throw away the translation matrix glPushMatrix();//Make copy of identity (top) matrix glRotatef(90.0, 0.0, 0.0, 1.0);//Set 90-deg. Rotation about z axis glRecti(50, 100, 200, 150)//Display red, rotated rectangle glPopMatrix();//Throw away the rotation matrix glScalef(-0.5, 1.0, 1.0);//Set scale-reflection parameters glRecti(50, 100, 200, 150)//Display red, transformed rectangle
13
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 wheels + Rotate body Matrix Stacks
14
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.