300 likes | 316 Views
Learn about rotation, scaling, and translation in 3D modeling using OpenGL utilities. Experiment with multiple rotations, order of transformations, and projection and modelview matrices. Practice composing transformations and placing multiple objects in a 3D scene.
E N D
Chapter 4/5 glMatrixMode Modeling Transformations glTranslate quick look at glPushMatrix and glPopMatrix glScale reflection glRotate composing transformations placing multiple objects
MJBBasicTransformations.cpp • Notice display list for createshape • note use of glPushMatrix() and glPopMatrix for isolating transformations for stroke text and transformed shape.
MJBBasicTransformations.cpp • rotation • scaling • translation
Multiple rotation experiment • work in pairs: • Both, place book face up so you can read it. • Person 1, rotate 90 degrees around z-axis, then rotate 180 degrees around y axis. • Person 2, rotate 180 degrees around y axis, then rotate 90 degrees around z-axis. • Compare.
Multiple rotation experiment Conclusions: Order Matters!
PROJECTION and MODELVIEW Matrices • GL_MODELVIEW is for organizing the pieces of the picture - building the model. • GL_PROJECTION is for setting viewing box and type of projection for viewing.
PROJECTION and MODELVIEW Matrices • GL_MODELVIEW is for organizing the pieces of the picture - building the model. • drawing: glBegin, glutSphere,.... • GL_PROJECTION is for setting viewing box and type of projection for viewing. • glOrtho, glFrustum
PROJECTION and MODELVIEW Matrices • Both matrices are alway active and being used. • We can only change or set one at a time. • To modify one we have to put it in the "matrix modifying machine", eg • glMatrixMode(GL_PROJECTION); • glMatrixMode(GL_MODELVIEW);
PROJECTION and MODELVIEW Matrices • To make sure we start with a clean slate: glLoadIdentity(); • Any new changes are made to the matrix that is currently "in Mode". • The current state of both is used for whatever is being drawn.
PROJECTION Matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho... or glFrustum... This is usually in resize routine, but it can be modified in display routine. Remember to then return to the MODELVIEW matrix, glMatrixMode(GL_MODELVIEW);
Modeling Transformations • Run box.cpp • In resize: glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0); glMatrixMode(GL_MODELVIEW);
box.cpp • in drawScene glLoadIdentity(); // Modeling transformations. glTranslatef(0.0, 0.0, -15.0); glutWireCube(5.0); // Box. //A 5x5 cube, centered at the origin
glTranslatef( p, q, r) • translate (x,y,z) to (x+p,y+q,z+r); • Reminder - away from your eye is NEGATIVE z direction.
boxWPushPop.cpp //Apply glTranslatef only to glutWireCube glPushMatrix(); glTranslatef(0.0, 0.0, -15.0); glutWireCube(5.0); // Box. glPopMatrix();
boxV2.cpp //Modeling Transformations glTranslatef(0.0, 0.0, -15.0); ... glScalef(.7,2.0,3.0); Run boxV2.cpp
boxV2.cpp Black box is • First scaled (while at the origin) • Then translated.
glScalef(u,v,w); maps (x, y, z) of an object to (u*x, v*y, w*z)
Reflection • glScale(1.0, -1.0, 1.0); • Run boxV3.cpp. press m to see before and after reflection. Notice toggle.
boxV4.cpp //Modeling Transformations glTranslatef(0.0, 0.0, -15.0); ... glRotatef(60.0, 0.0,0.0,1.0); glutWireTeapot(3.0); Run and look at boxV4.cpp
boxV4.cpp glRotatef(60.0, 0.0,0.0,1.0); Counterclockwise rotation looking from • origin to (0,0,1) or • (0,0,1) to origin?
boxV4.cpp glRotatef(60.0, 0.0,0.0,1.0); Counterclockwise rotation looking from • origin to (0,0,1) or • (0,0,1) to origin?
glRotatef(A, p, q, r) • rotate around the vector (p, q, r) • A is the angle • in degrees, counterclockwise • looking from the point (p, q, r) toward the origin
Transformations • Run the experiments in the book. Think through the answer before you run it.
Composing Transformations • run boxV5.cpp, draw on board • Key input t: glTranslatef(10.0,0.0,0.0); glRotatef(45.0, 0.0,0.0,1.0); glutWireCube(5.0); • Key input r: glRotatef(45.0, 0.0,0.0,1.0); glTranslatef(10.0,0.0,0.0); glutWireCube(5.0);
Placing multiple objects • Do experiment with Powerpoint:
Trotate.cpp //Modeling Transformations glTranslatef(0.0, 0.0, -15.0); ... glRotatef(60.0, 0.0,0.0,1.0); glBegin(GL_LINES); glVertex3f(0, 0, 0); glVertex3f(3, 0, 0); glVertex3f(3, -1, 0); glVertex3f(3, 1, 0); glEnd(); Run and look at Trotate.cpp
boxV6.cpp • original - a box and a sphere • draw on board • m: translate together • o: say translate then rotate, but really rotate then translate • Check out the code! • try ortho and frustrum
pushPopDemo.cpp Any transformations (rotate,translate, scale) put inside a glPushMatrix() and a glPopMatrix() pair are ignored outside that pair. Other statements (color, draw something,...) are NOT ignored
pushPopDemo.cpp Any transformations (rotate,translate, scale) put inside a glPushMatrix() and a glPopMatrix() pair are ignored outside that pair. Other statements (color, draw something,...) are NOT ignored