190 likes | 342 Views
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360/4360. Overview. Saw transformation “in theory” last time, now OpenGL focus OpenGL transformations, matrices and coordinate systems Transformations in OpenGL Rotation Translation Scaling
E N D
OpenGL Matrices and TransformationsAngel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360/4360
Overview • Saw transformation “in theory” last time, now OpenGL focus • OpenGL transformations, matrices and coordinate systems • Transformations in OpenGL • Rotation • Translation • Scaling • … and others • Look at OpenGL matrix modes • Model-view • Projection
OpenGL Transformations, 1/2 • Series of “viewing transformations” • transforms a point (its coordinates) from world space to eye space … to window coord • Set these transformation matrices as part of OpenGL programming • Each transformation operates on different spaces and coordinates • Model – View – Projection – Perspective Divide - Viewport • From “red book”:
OpenGL Transformations, 2/2 • Recall - viewing process has 2 parts: • Use model-view matrix • to switch vertex reps. from object frame in which objects defined • to their representation in eye/camera frame – eye at origin • Allows use of canonical viewing procedures • Type of projection (parallel or perspective) and part of world to image (clipping or view volume) • Specifications allow formation of a projection matrix concatenated with model-view matrix
Recall, Coordinate Systems in Viewing • Coordinate Systems in the Graphics Pipeline • OCS – object coordinate system • WCS – world coordinate system • VCS – viewing coordinate system • CCS – clipping coordinate system • NDCS - normalized device coordinate system • DCS – device coordinate system • And images are formed on the image plane
Transformations and Coordinate Systems • Coordinate Systems in the Graphics Pipeline • OCS – object coordinate system • WCS – world coordinate system • VCS – viewing coordinate system • CCS – clipping coordinate system • NDCS - normalized device coordinate system • DCS – device coordinate system • Series of “viewing transformations” • transforms a point (its coordinates) from world space to eye space • Set these transformation matrices as part of OpenGL programming • Each transformation operates on different spaces and coordinates • Model – View – Projection – Perspective Divide - Viewport
OpenGL Matrix Programming Arch. Load Matrix Current Current Stack Stack • In OpenGL matrices are part of the state • Multiple types of matrices • Model-View (GL_MODELVIEW) • Projection (GL_PROJECTION) • Texture (GL_TEXTURE) (ignore for now) • Color(GL_COLOR) (ignore for now) • Select which to manipulated by • glMatrixMode(GL_MODELVIEW); • glMatrixMode(GL_PROJECTION); Matrix Mode 3D Model Vertices 2D 3D Vertices Modelview Projection
Current Transformation Matrix (CTM) • So, far have thought only in OpenGL terminology • … which is fine • More general “computer graphics” term for the concatenation of the modelview and projection matrix is “current transformation matrix” • Angel describes the current transformation matrix(CTM) as part of the state and is applied to all vertices that pass down the pipeline • The CTM is defined in the user program and loaded into a “transformation unit” C p’=Cp p vertices CTM vertices
CTM and OpenGL Matrix Operations Summary • Again, same as for OpenGL matrices • CTM and OpenGL matrices can be altered either by loading a new matrix or by postmutiplication: Load an identity matrix: CI Load an arbitrary matrix: CM Load a translation matrix: CT Load a rotation matrix: CR Load a scaling matrix: CS Postmultiply by an arbitrary matrix: CCM Postmultiply by a translation matrix: CCT Postmultiply by a rotation matrix: CCR Postmultiply by a scaling matrix: CCS
Example – Object Rotation, 1/1(recall) • Rotation of object (at -4.0, -5.0, -6.0) by 45 degrees • Recall, rotation “rotates” everything in coordinate system • Here, want to just rotate object • So, move to origin, rotate, move back • Get things set up • (save, if need be) • glMatrixMode(GL_MODELVIEW) • glLoadIdentity(); • Move the fixed point to the origin: • glTranslatef(4.0, 5.0, 6.0); • Rotate about the origin: • glRotatef(45.0, 1.0, 2.0, 3.0); • Move the fixed point back again: • glTranslatef(-4.0, -5.0, -6.0);
Example - Postmultiplication, 2/2 • However, recall that OpenGl operates by postmultiplication • The expression (A(B(C))) is evaluated in the order • B * C, then A times that product • So, order in program is reversed (gotcha) • The “conceptual” sequence: • A - Move the fixed point to the origin: • glTranslatef(4.0, 5.0, 6.0); • B - Rotate about the origin: • glRotatef(45.0, 1.0, 2.0, 3.0); • C - Move the fixed point back again: • glTranslatef(-4.0, -5.0, -6.0); • Requires the code: • C - Move the fixed point back again: • glTranslatef(-4.0, -5.0, -6.0); • B - Rotate about the origin: • glRotatef(45.0, 1.0, 2.0, 3.0); • A - Move the fixed point to the origin: • glTranslatef(4.0, 5.0, 6.0);
Matrix Stacks Load Matrix Current Current Stack Stack • In many situations want to save transformation matrices for use later • Traversing hierarchical data structures (Chapter 10) • Avoiding state changes when executing display lists • OpenGL maintains stacks for each type of matrix • Access present type (as set by glMatrixMode) by • glPushMatrix() • glPopMatrix() • Nothing fancy here … • Just convenient place to save • All matrices have associated stack Matrix Mode 3D Model Vertices 2D 3D Vertices Modelview Projection
Matrix Queries and Arbitrary Matrices • Recall, OpenGL query functions for determining state • glGetIntegerv • glGetFloatv • glGetBooleanv • glGetDoublev • glIsEnabled • Etc. • For matrix query (returning entire matrix), use • double m[16]; • glGetFloatv(GL_MODELVIEW, m); • Arbitrary Matrices: • Can load and multiply by matrices defined in the application program • glLoadMatrixf(m) • glMultMatrixf(m) • 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
Using TransformationsAngel Example void main(intargc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glutMainLoop(); } • Angel example: • use idle function to rotate a cube • mouse function to change axis around which rotation occurs rotation • Start with a program that draws a cube (colorcube.c) in a standard way • Centered at origin • Sides aligned with axes
Idle and Mouse callbacks void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // rotate for each of x, y, z, // but only theta[axis] changes … obscure glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glutSwapBuffers(); } void colorcube() // draw the cube void mouse(intbtn, int state, int x, int y) // change direction of rotation, axis is global { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } void spinCube() /* idle function, so continually called */ /* for rotation, change only array theta[axis] */ { theta[axis] += 2.0; // globals if( theta[axis] > 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); // cause redraw }
Model-view and Projection Matrices • In OpenGL the model-view matrix is used to • Position the camera • Can be done by rotations and translations but is often easier to use gluLookAt • Build models of objects • The projection matrix is used to define the view volume and to “select a camera lens” (set frustum or gluperspective) • Although both are manipulated by the same functions, have to be careful because incremental changes are always made by postmultiplication • For example, rotating model-view and projection matrices by the same matrix are not equivalent operations • Postmultiplication of the model-view matrix is equivalent to premultiplication of the projection matrix
Some OpenGL Matrix Operations • Again, matrices are 1-dim arrays type GLfloat in column major order • glLoadIdentity(); • Loads an identity matrix onto the top of the current stack • glLoadMatrixf(pointer_to_matrix); • Loads arbitrary matrix onto top of the current stack • glMultMatrixf(pointer_to_matrix); • Postmultiplies current matrix by arbitrary matrix • glTranslatef(dx, dy, dz); • Multiplies current matrix with translation matrix. dx, dy, and dz are translations along x,y, and z axes. • glRotatef(angle, x, y, z); • Multiplies current matrix with rotation about the line from the origin through the point (x, y, z) by angle. • glScalef(sx, sy, sz); • Multiplies current matrix with scaling matrix. sx, sy, sz are the scale factors along the x, y, and z axes.
End • .