Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.

Similar presentations


Presentation on theme: "CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation."— Presentation transcript:

1 CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation

2 CGGM Lab. Tan-Chi Ho 2001 The Camera Analogy 1/2 Set up tripod and pointing the camera at the scene (viewing transformation). Arrange the scene to be photographed into the desired composition (modeling transformation).

3 CGGM Lab. Tan-Chi Ho 2001 The Camera Analogy 2/2 Choose a camera lens or adjust the zoom (projection transformation). Determine how large you want the final photograph to be (viewport transformation).

4 CGGM Lab. Tan-Chi Ho 2001 Overview Modelview Matrix Modelview Matrix View Transformation View Transformation Modelview Matrix Modelview Matrix Perspective Division eye coordinates clip coordinates normalized device coordinates window coordinates XYZWXYZW Object coordinates Vertex

5 CGGM Lab. Tan-Chi Ho 2001 Transformations 1/12 The Viewing Transformation To positioning and aiming the camera. Use gluLookAt() to indicate where the camera is placed and aimed. If gluLookAt() was not called, the camera has a default position at the origin, points down the negative Z-axis, and an up- vector of (0, 1, 0).

6 CGGM Lab. Tan-Chi Ho 2001 Transformations 2/12 gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdoubpe upz); eyex, eyey, eyez is where the camera is positioned. centerx, centery, centerz is where the camera look at. Upx, upy, upz is the up-vector of the camera.

7 CGGM Lab. Tan-Chi Ho 2001 Transformations 3/12 The Modeling Transformation To position and orient the models. Rotate, translate, scale and some combination of these operations. In OpenGL, modeling and viewing transformation are combined into the modelview matrix before the transformation are applied.

8 CGGM Lab. Tan-Chi Ho 2001 Transformations 4/12 glTranslate{fd}(TYPE x,TYPE y,TYPE z); Multiplies current matrix by a matrix that moves an object by x,y,z glRotate{fd}(TYPR angle,TYPE x,TYPR y,TYPE z); Multiplies current matrix by a matrix that rotates an object in a counterclockwise direction about the ray from origin to (x,y,z) with angle as the degrees.

9 CGGM Lab. Tan-Chi Ho 2001 Transformations 5/12 glScale{fd}(TYPE x,TYPE y,TYPE z); Multiplies current matrix by a matrix that scales an object along axes.

10 CGGM Lab. Tan-Chi Ho 2001 Transformations 6/12 The Projection Transformation Determine what the field of view(or viewing volume) is and how objects are projected onto the screen. Two types of projections are provided: Perspective projection Orthographic projection

11 CGGM Lab. Tan-Chi Ho 2001 Transformations 7/12 Perspective Projection glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

12 CGGM Lab. Tan-Chi Ho 2001 Transformations 8/12 gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far);

13 CGGM Lab. Tan-Chi Ho 2001 Transformations 9/12 Orthographic Projection glOrtho(Gldouble left, Gldouble right, Gldouble bottom, Gldouble top, Gldouble near, Gldouble far); gluOrtho2D(Gldouble left, Gldouble right, Gldouble bottom, Gldouble top); For 2D projection matrix The Z coordinates for objects are assumed to lie between – 1.0 and 1.0.

14 CGGM Lab. Tan-Chi Ho 2001 Transformations 10/12

15 CGGM Lab. Tan-Chi Ho 2001 Transformations 11/12 The Viewport Transformation Transform the final image into some region of the window. The viewport is measured in window coordinates. glViewport(GLint x, GLint y, GLsizei width, GLsizei height); Initial values are (0, 0, winWidth, winHeight).

16 CGGM Lab. Tan-Chi Ho 2001 Transformations 12/12 Switch between matrix modes glMatrixMode(GLenum mode); Switch between modelview, projection, tecture matrix mode.(GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE) Current matrix mode is a state variable. glLoadIdentity(); Set current matrix to the 4x4 identity matrix. Always call this after change matrix mode.

17 CGGM Lab. Tan-Chi Ho 2001 The Matrix Manipulation glLoadIdentity(); glLoadMatrix{f,d}( const TYPE* m ); glMultMatrix{f,d}( const TYPE* m ); glPushMatrix(); glPopMatrix(); Stack operation of matrix is very useful for constructing a hierarchical structures. Ex: a car with four wheels.

18 CGGM Lab. Tan-Chi Ho 2001 Transformation Program 1/5 #include static GLfloat year=0.0f, day=0.0f; void init() { glClearColor(0.0, 0.0, 0.0, 0.0); } void GL_display() { // clear the buffer glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glPushMatrix(); glutWireSphere(1.0, 20, 16);// the Sun glRotatef(year, 0.0, 1.0, 0.0);

19 CGGM Lab. Tan-Chi Ho 2001 Transformation Program 2/5 glTranslatef(3.0, 0.0, 0.0); glRotatef(day, 0.0, 1.0, 0.0); glutWireSphere(0.5, 10, 8);// the Planet glPopMatrix(); // swap the front and back buffers glutSwapBuffers(); } void GL_reshape(GLsizei w, GLsizei h) { // viewport transformation glViewport(0, 0, w, h); // projection transformation glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);

20 CGGM Lab. Tan-Chi Ho 2001 Transformation Program 3/5 // viewing and modeling transformation glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 3.0, 5.0,// eye 0.0, 0.0, 0.0,// center 0.0, 1.0, 0.0);// up } // GLUT idle function void GL_idle() { day += 10.0; if(day > 360.0) day -= 360.0; year += 1.0; if(year > 360.0) year -= 360.0; // recall GL_display() function glutPostRedisplay(); }

21 CGGM Lab. Tan-Chi Ho 2001 Transformation Program 4/5 // GLUT keyboard function void GL_keyboard(unsigned char key, int x, int y) { switch(key) { case 'd':day += 10.0; if(day > 360.0) day -= 360.0; glutPostRedisplay(); break; case 'y':year += 1.0; if(year > 360.0) year -= 360.0; glutPostRedisplay(); break; case 'a':// assign idle function glutIdleFunc(GL_idle); break; case 'A':glutIdleFunc(NULL); break; case 27:exit(0); }

22 CGGM Lab. Tan-Chi Ho 2001 Transformation Program 5/5 int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Planet"); init(); glutDisplayFunc(GL_display); glutReshapeFunc(GL_reshape); glutKeyboardFunc(GL_keyboard); glutMainLoop(); }

23 CGGM Lab. Tan-Chi Ho 2001 Any Question? ?


Download ppt "CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation."

Similar presentations


Ads by Google