Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.

Similar presentations


Presentation on theme: "OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360."— Presentation transcript:

1 OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360

2 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

3 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”:

4 OpenGL Transformations, 2/2 Again - viewing process has 2 parts: –Use model-view matrix To switch (transform 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

5 Again, 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

6 Transformations and Coordinate Systems Again! 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

7 OpenGL Matrix Programming Arch. 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); Stack Current Stack Current Load Matrix Vertices 3D Model Vertices 3D 2D ModelviewProjection Matrix Mode

8 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” CTMvertices p p’=Cp C

9 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: C  I Load an arbitrary matrix: C  M Load a translation matrix: C  T Load a rotation matrix: C  R Load a scaling matrix: C  S Postmultiply by an arbitrary matrix: C  CM Postmultiply by a translation matrix: C  CT Postmultiply by a rotation matrix: C  C R Postmultiply by a scaling matrix: C  C S

10 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);

11 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);

12 Matrix Stacks 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 Stack Current Stack Current Load Matrix Vertices 3D Model Vertices 3D 2D ModelviewProjection Matrix Mode

13 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

14 Using Transformations Angel Example 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 void main(int argc, 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(); }

15 Idle and Mouse callbacks void mouse(int btn, 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 } 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

16 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 (ok, almost always) 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)

17 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 (ok, almost always) 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

18 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 (ok, almost always) 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

19 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.

20 End.


Download ppt "OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360."

Similar presentations


Ads by Google