OpenGL L02-Transformations

Embed Size (px)

DESCRIPTION

OpenGL L02-Transformations

Text of OpenGL L02-Transformations

  • 1. Mohammad Shakermohammadshaker.com@ZGTRShaker2014OpenGL GraphicsL02 TRANSFORMATIONS

2. Last Class, We Talked AboutOpenGL Program Structure 3. OpenGL Program StructureApplication StructureConfigure and open windowInitialize OpenGL stateRegister input callback functionsrenderresizeinput: keyboard, mouse, etc.Enter event processing loop 4. OpenGL Program StructureApplication StructureConfigure and open windowInitialize OpenGL stateRegister input callback functionsrenderresizeinput: keyboard, mouse, etc.Enter event processing loopvoid main( int argc, char** argv ){int mode = GLUT_RGB|GLUT_DOUBLE;glutInitDisplayMode( mode );glutCreateWindow( argv[0] );init();glutDisplayFunc( display );glutReshapeFunc( resize );glutKeyboardFunc( key );glutIdleFunc( idle );glutMainLoop();} 5. OpenGL InitializationSet up whatever state youre going to usevoid init( void ){glClearColor( 0.0, 0.0, 0.0, 1.0 );glClearDepth( 1.0 );glEnable( GL_LIGHT0 );glEnable( GL_LIGHTING );glEnable( GL_DEPTH_TEST );} 6. GLUT Callback FunctionsRoutine to call when something happenswindow resize or redrawuser inputanimationRegister callbacks with GLUTglutDisplayFunc( display );glutIdleFunc( idle );glutKeyboardFunc( keyboard ); 7. Rendering CallbackDo all of your drawing hereglutDisplayFunc(DrawGLScene);void DrawGLScene( void ){glClear( GL_COLOR_BUFFER_BIT );glBegin( GL_TRIANGLE_STRIP );glVertex3fv( v[0] );glVertex3fv( v[1] );glVertex3fv( v[2] );glVertex3fv( v[3] );glEnd();glutSwapBuffers();} 8. Rendering CallbackProcess user inputglutKeyboardFunc( keyboard );void keyboard( char key, intx, inty ){switch( key ) {case q : case Q :exit( EXIT_SUCCESS );break;case r : case R :rotate = GL_TRUE;break;}} 9. Matrices in OpenGL, 4x43 7 11 152 6 10 141 5 9 130 4 8 12m m m mm m m mm m m mm m m mM 10. RULE 11. RULETO SEE A 3D SCENE YOU SHOULD SET UP: 12. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA 13. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERAPROJECTION 14. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERAPROJECTIONWORLD MATRIX 15. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 16. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 17. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 18. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 19. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 20. Camera (View) Matrix 21. Camera (View) Matrixvoid gluLookAt(GLdoubleeyeX, GLdoubleeyeY, GLdoubleeyeZ,GLdoublecenterX, GLdoublecenterY, GLdoublecenterZ,GLdoubleupX, GLdoubleupY, GLdoubleupZ); 22. Camera (View) Matrixvoid gluLookAt(GLdoubleeyeX, GLdoubleeyeY, GLdoubleeyeZ,GLdoublecenterX, GLdoublecenterY, GLdoublecenterZ,GLdoubleupX, GLdoubleupY, GLdoubleupZ); 23. Camera (View) Matrixvoid gluLookAt(GLdoubleeyeX, GLdoubleeyeY, GLdoubleeyeZ,GLdoublecenterX, GLdoublecenterY, GLdoublecenterZ,GLdoubleupX, GLdoubleupY, GLdoubleupZ); 24. Camera (View) Matrixvoid gluLookAt(GLdoubleeyeX, GLdoubleeyeY, GLdoubleeyeZ,GLdoublecenterX, GLdoublecenterY, GLdoublecenterZ,GLdoubleupX, GLdoubleupY, GLdoubleupZ); 25. Camera Movement 26. Set the View (Camera) MatrixFor example, in ReSizeGLScenemethodGLvoidReSizeGLScene(GLsizeiwidth, GLsizeiheight){if (height==0) {height=1;}glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt( 0.0, 0.0, 5.0,0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );} 27. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 28. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 29. Projection Matrix 30. Projection 31. Projection Matrices 32. Perspective Projection 33. Projection MatricesThe Difference?! 34. Projection MatricesThe Difference?!perspective 35. Projection MatricesThe Difference?!perspectiveorthographic 36. Projection MatricesRealistic?perspectiveorthographic 37. Projection MatricesRealistic?perspectiveorthographic 38. Projection MatricesArchitecture?perspectiveorthographic 39. Projection MatricesArchitecture?perspectiveorthographic 40. Projection MatricesUsability?perspectiveorthographic 41. Projection MatricesUsability?perspectiveorthographic 42. Perspective Projectionvoid gluPerspective(GLdoublefovy, GLdoubleaspect, GLdoublezNear, GLdoublezFar)void glFrustum(GLdoubleleft, GLdoubleright, GLdoublebottom, GLdoubletop,GLdoubleznear, GLdoublezfar); 43. Orthographic Projectionvoid gluOrtho(GLdoubleleft, GLdoubleright, GLdoublebottom, GLdoubletop,GLdoublenear, GLdoublefar) 44. Orthographic Projection 45. Near and Far Clipping PlanesWrong Near and Far clipping planes can cause problems. 46. Set the Projection MatrixFor example, in ReSizeGLScenemethodGLvoidReSizeGLScene(GLsizeiwidth, GLsizeiheight){if (height==0) {height=1;}glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt( 0.0, 0.0, 5.0,0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );} 47. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 48. RULETO SEE A 3D SCENE YOU SHOULD SET UP:CAMERA (Singleton, for all objects)PROJECTION (Singleton, for all objects)WORLD MATRIX (For each object separately) 49. World MatrixEach Object in 3D Has Its Own World Matrix 50. World Matrix 51. World MatrixExampleLets assume that the coordinates of the triangle vertices are as follows: 52. World MatrixExampleTo translate 40 units over the y axiss positive direction,allyou need to do is to add 40 toeachy position, and you have the new coordinates for the vertices: 53. World Matrix 54. Many Transformation? 55. Many Transformation? Always Use I.S.R.O.T 56. IdentityScaleRotateOrbitTranslate 57. World MatricesglScalef(x, y, z);glTranslatef(x, y, z);glRotatef(angle, x, y, z); 58. World Matrices glScalef(x, y, z); glTranslatef(x, y, z); glRotatef(angle, x, y, z);=0 0 0 10 0 10 1 01 0 0( , , )zyxx y ztttT t t t=0 0 0 10 0 00 0 00 0 0( , , )zyxx y zsssS s s s 59. World MatricesglLoadIdentity(); 60. NEWRULERemember, Remember?OpenGL programming is state-machine based 61. NEWRULERemember, Remember?OpenGL programming is state-machine basedChanging State Without you knowing youre changing the state is a very big deal/Problem 62. NEWRULERemember, Remember?OpenGL programming is state-machine basedChanging State Without you knowing youre changing the state is a very big deal/Problem 63. Changing State Without you knowing youre changing the state is a very big deal/ProblemEXAMPLE 64. Changing State Without you knowing youre changing the state is a very big deal/ProblemEXAMPLEPrimitive and Transformation 65. Primitive and Transformation Example 66. Primitive and Transformation ExampleintDrawGLScene(GLvoid)// Here's Where We Do All The Drawing{// Clear The Screen And The Depth BufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity(); // Reset The ViewglTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using TrianglesglVertex3f( 0.0f, 1.0f, 0.0f); // TopglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The TriangleglTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A QuadglVertex3f(-1.0f, 1.0f, 0.0f); // Top LeftglVertex3f( 1.0f, 1.0f, 0.0f); // Top RightglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglEnd(); // Done Drawing The Quadreturn TRUE; // Keep Going} 67. Primitive and Transformation ExampleintDrawGLScene(GLvoid)// Here's Where We Do All The Drawing{// Clear The Screen And The Depth BufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity(); // Reset The ViewglTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using TrianglesglVertex3f( 0.0f, 1.0f, 0.0f); // TopglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The TriangleglTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A QuadglVertex3f(-1.0f, 1.0f, 0.0f); // Top LeftglVertex3f( 1.0f, 1.0f, 0.0f); // Top RightglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglEnd(); // Done Drawing The Quadreturn TRUE; // Keep Going} 68. Primitive and Transformation ExampleintDrawGLScene(GLvoid)// Here's Where We Do All The Drawing{// Clear The Screen And The Depth BufferglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity(); // Reset The ViewglTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0glBegin(GL_TRIANGLES); // Drawing Using TrianglesglVertex3f( 0.0f, 1.0f, 0.0f); // TopglVertex3f(-1.0f,-1.0f, 0.0f); // Bottom LeftglVertex3f( 1.0f,-1.0f, 0.0f); // Bottom RightglEnd(); // Finished Drawing The TriangleglTranslatef(3.0f,0.0f,0.0f); // Move Right 3 UnitsglBegin(GL_QUADS); // Draw A QuadglVertex3f(-1.0f, 1.0f, 0.0f); // Top LeftglVertex3f( 1.0f, 1.0f, 0.0f); //