1 / 25

OpenGL Transformations

CSC 341 Introduction to Computer Graphics. OpenGL Transformations. OpenGL Matrices. In OpenGL matrices are part of the state Once set, it is applied to everything that is drawn until set again Three types Model-View ( GL_MODELVIEW ) Transforming objects (translate, scale, etc.)

louis
Download Presentation

OpenGL Transformations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 341 Introduction to Computer Graphics OpenGL Transformations

  2. OpenGL Matrices • In OpenGL matrices are part of the state • Once set, it is applied to everything that is drawn until set again • Three types • Model-View (GL_MODELVIEW) • Transforming objects (translate, scale, etc.) • Changing from standard frame to camera frame • Projection (GL_PROJECTION) • Defines the clipping rectangle/volume • Texture (GL_TEXTURE) (ignore for now)

  3. OpenGL Matrices • Single set of functions for manipulation • Select which to manipulated by • glMatrixMode(GL_MODELVIEW); //default • glMatrixMode(GL_PROJECTION);

  4. Current Transformation Matrix (CTM) • Conceptually there is a 4 x 4 homogeneous coordinate matrix, the current transformation matrix (CTM) that is 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

  5. CTM operations • The CTM can be altered either by loading a new CTM or by postmutiplication • Let C be the CTM Load an identity matrix: CI Load an arbitrary matrix: CM Postmultiply by an arbitrary matrix: CCM Postmultiply by a translation matrix: CCT Postmultiply by a rotation matrix: CCR Postmultiply by a scaling matrix: CCS

  6. Rotation about a Fixed Point Start with identity matrix: CI Move fixed point to origin: CCT Rotate: CCR Move fixed point back: CCT -1 Result: C = TR T –1which is backwards. This result is a consequence of doing postmultiplications.

  7. Reversing the Order We wantC = T –1 R T so we must reverse the order of the operations CI CCT -1 CCR CCT Each operation corresponds to one function call in the program. Note that the last operation specified is the first executed in the program

  8. CTM in OpenGL • OpenGL has a model-view and a projection matrix in the pipeline which are concatenated together to form the CTM • Can manipulate each by first setting the correct matrix mode

  9. OpenGL Functions • glLoadIdentity() • CI • glLoadMatrixf(M) • Load a matrix M to current matrix • C M • The matrix M is a one dimensional array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns • glMultMatrixf(M) • CCM

  10. OpenGL Functions • glRotatef(theta, vx, vy, vz) • Theta is the angle of rotation in degrees • (vx, vy, vz) define the axis of rotation • Alters current matrix by postmultiplication C CR

  11. OpenGL Functions • glTranslatef(dx, dy, dz) • CCT • glScalef( sx, sy, sz) • CCS • Each function has a float (f) and double (d) format (glScaled)

  12. Example • 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 glMatrixMode(GL_MODELVIEW); glTranslatef(1.0, 2.0, 3.0); glRotatef(30.0, 0.0, 0.0, 1.0); glTranslatef(-1.0, -2.0, -3.0); //draw here

  13. Example • Reflect with respect to line x=2

  14. Matrix Stacks • In many situations we want to save transformation matrices for use later • OpenGL maintains stacks for each type of matrix • The current matrix is the one on the top of the stack • Save the current matrix using glPushMatrix() • Restore later by glPopMatrix()

  15. glPushMatrix() pushes a copy of the current matrix on top of the stack C C C glPushMatrix() B B A A

  16. glPopMatrix() pops the current matrix off the stack C glPopMatrix() B B A A

  17. Load C I gloadIdentity() B B A A

  18. Postmultiplication C CR glRotatef() B B A A

  19. Reading Back Matrices • Can also access matrices (and other parts of the state) by enquiry (query) functions double m[16]; glGetFloatv(GL_MODELVIEW, m);

  20. Example • If you only want to apply rotation a to a particular drawing but not to future drawing glMatrixMode(GL_MODELVIEW); glPushMatrix(); //save C glRotatef(30.0, 0.0, 0.0, 1.0); glRectf(….); //only rotate the rectangle glPopMatrix(); //restore C // draw others, will not be rotated

  21. Example • If you do not want C to apply to a particular drawing glMatrixMode(GL_MODELVIEW); glPushMatrix(); //save C glLoadIdentity(); glRectf(….); //draw rectangle glPopMatrix(); //restore C // draw others

  22. Using Transformations • Example: use idle function to rotate a cube and mouse function to change direction of rotation • Start with a program that draws a cube (colorcube.c) in a standard way • Centered at origin • Sides aligned with axes • Will discuss modeling in next lecture

  23. main.c 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(); }

  24. Idle and Mouse callbacks void spinCube() { theta[axis] += 2.0; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { 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; }

  25. Display callback void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); 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(); } Note that because of fixed form of callbacks, variables such as theta and axis must be defined as globals Camera information is in standard reshape callback

More Related