270 likes | 477 Vues
3D Graphics Programming Geometric Transformations. Ming-Te Chi Department of Computer Science, National Chengchi University. Outline. Geometric Transformations(CH4). Transformation Terminology. Viewing Modeling Modelview Projection Viewport. Eye coordinates. +y. +y. +x. +x. +z.
E N D
3D Graphics Programming Geometric Transformations • Ming-Te Chi • Department of Computer Science, • National Chengchi University
Outline • Geometric Transformations(CH4)
Transformation Terminology • Viewing • Modeling • Modelview • Projection • Viewport
Eye coordinates +y +y +x +x +z
Transformations • Translation • Rotation • Scaling
Rotation/Translation +y +y +y +y +y +y +x +x +x +x +x +x
The Modelview Duality +y +y +x +x +z +z
Transformation functions • glTranslatef(x, y, z); • glRotatef(angle, x, y, z); • glScalef(x, y, z); • glLoadIdentity();
Per Vertex Poly. Frag FB Raster CPU DL Texture Pixel v e r t e x Modelview Matrix Projection Matrix Perspective Division Viewport Transform Modelview Projection Modelview l l l TransformationPipeline • other calculations here • material è color • shade model (flat) • polygon rendering mode • polygon culling • clipping normalized device eye object clip window
The Matrix Stacks glPushMatrix glPopMatrix Matrix Stack glGet(GL_MAX_MODELVIEW_STACK_DEPTH)
Atom example // First Electron Orbit // Save viewing transformation glPushMatrix(); // Rotate by angle of revolution glRotatef(fElect1, 0.0f, 1.0f, 0.0f); // Translate out from origin to orbit distance glTranslatef(90.0f, 0.0f, 0.0f); // Draw the electron glutSolidSphere(6.0f, 15, 15); // Restore the viewing transformation glPopMatrix();
// Second Electron Orbit glPushMatrix(); glRotatef(45.0f, 0.0f, 0.0f, 1.0f); glRotatef(fElect1, 0.0f, 1.0f, 0.0f); glTranslatef(-70.0f, 0.0f, 0.0f); glutSolidSphere(6.0f, 15, 15); glPopMatrix();
Projections • Orthograhpic Projections • Perspective Projections
Orthograhpic Projections // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange*2.0f, nRange*2.0f); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange*2.0f, nRange*2.0f);
Perspective Projections // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Produce the perspective projection gluPerspective(60.0f, fAspect, 1.0, 400.0);
Advance Matrix Manipulation • Glfloat matrix[16];
Load a Matrix • glLoadMatrixf(GLfloat *m); • glLoadTransposeMatrixf(Glfloat *m);
Adding Transformations Together • glMultMatrixf(Glfloat *m);
An Actor Frame Class GLFrame { protected: M3Dvector3f vLocation; M3Dvector3f vUp; M3Dvector3f vForward; };
Euler Angles Class GLFrame { protected: M3Dvector3f vPosition; Glfloat fRoll; Glfloat fPitch; Glfloat fYaw; };
Camera Management gluLookat( GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ);
Rendering Loop Save Identity Matrix Apply camera transformation Draw stuff the doesn’t move Draw moving stuff (Actors) Save camera transform Apply actor transform Draw actor geometry Restore camera transform Restore identity Matrix