1 / 21

Content

Content. Hierarchical modelling Creating more complex object/scene OpenGL transformation matrix stacks Store transformation matrices Push/pop transformation matrix stack Save some transformation matrix for later use Some examples Robot Christmas tree Pyramide. Hierarchical Modelling.

eljah
Télécharger la présentation

Content

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. Content • Hierarchical modelling • Creating more complex object/scene • OpenGL transformation matrix stacks • Store transformation matrices • Push/pop transformation matrix stack • Save some transformation matrix for later use • Some examples • Robot • Christmas tree • Pyramide

  2. Hierarchical Modelling • A hierarchical model unites several primitives/instances into one object, e.g., a desk is made up of many blocks • Generally represented as a tree, and each node of the tree has its own local coordinate system • Particularly useful for animation • Human is a hierarchy of body, head, upper arm, lower arm, etc… • Animate an hierarchical object by changing the transformations at the nodes e.g. • Rotate about shoulder (origin of upper arm); • Draw upper arm; • Translate (in upper arm coordinate); • Rotate about elbow (origin of lower arm); • Draw lower arm.

  3. Hierarchical Modelling Root node An arc • Each arc contains • A fixed transformation of node/link to a position relative its parent • A variable transformation for articulate the node/link

  4. L0 Hierarchical Modelling L0 L1 L0 L2 T0 L2 root L1 L0 L1 T0 link1 T1 L1 Each arc contains 1. A fixed transformation of node/link to a position relative its parent 2. A variable transformation for articulate the node/link T1.1 link2 L1 L2 L2

  5. Hierarchical ModellingSkeletal Animation • In skeletal animation, a character is represented by 1) a surface mesh, or skin, as appearance of character; 2) a hierarchical set of interconnected skeletons, or bones, for animating the mesh • Associated with each bone is a 3D transformation, which includes its position, scale and orientation • The full transform of a child node is the product of its parent transform and its own transform - moving an upper arm will move the lower arm too • Each bone is associated with some portion of the mesh (or vertices) - skinningis the process of creating this association

  6. Hierarchical ModellingConstructive Solid Geometry • The internal nodes are set operations: union, intersection or difference (sometimes complement) • The edges of the tree have transformations associated with them • The leaf nodes contain only geometry

  7. OpenGL Matrix Stacks • OpenGL keeps three transformationmatrix stacks: • Model-View - glMatrixMode(GL_MODELVIEW); • Projection - glMatrixMode(GL_PROJECTION); • Textures - glMatrixMode(GL_TEXTURE); • When Model-View is called in a program, an identity matrix is created and becomes ‘current matrix’, let’s use C to represent it • C is at the top of the matrix stack, and represents the current ‘state’ of the scene • All objects are transformed by C.

  8. OpenGL Matrix Stacks • When the program then calls a transformation, say, glRotate(), OpenGl creates a transformation matrix M • OpenGL then right multiplies M with the current matrix C, thus updating current matrix to CM • The last matrix M will be applied first to the object

  9. OpenGL Matrix Stacks • Say you call glMatrixMode(GL_MODELVIEW) (or call glLoadIdentity() ), and subsequently call glTranslatef(0,0,10), the current matrix is • If you then call glVertex3f(0,0,0), the vertex will appear at (0,0,10). C =

  10. OpenGL Matrix StacksAn Example • Snowman 1is at the coordinate origin, looking along the z-axis. Now draw Snowman 2, positioned 8 units along the negative x-axis, 6 units along the positive z-axis, half the size of Snowman 1, and looks down the x-axis. y • glMatrixMode(GL_MODELVIEW) • glTranslatef(-8.0, 0.0, 6.0); • glRotatef(90.0, 0.0, 1.0, 0.0); • glScalef(0.5, 0.5, 0.5); • drawSnowman(); x z

  11. Push/Pop Matrix Stacks • glPushMatrix() makes a copy of the current matrix and puts it on top of the matrix stack, so the copy becomes the current matrix, and later transformations are accumulated to this copy, and the original current matrix remain untouched • glPopMatrix() pops the stack and thus making the saved original current matrix on top of stack, that is the current matrix

  12. Push/Pop Matrix Stacks • If you need the current matrix for later use, you need to save a copy of it on top of the stack using glPushMatrix() • You can then call some other transformations, which will be right-multiplied to the matrix on top of the stack • When you need to go back to the transformation state before glPushMatrix() was used, you use glPopMatrix(), so that the matrix before glPushMatrix() become the current matrix, that is, on top of the stack

  13. Push/Pop Matrix Stacks I * T I * T I * T • The modelling stack in action: • glLoadIdentity(); • glTranslatef(0,10,0); • glPushMatrix(); • glTranslatef(10,0,0); • glRotatef(45,0,1,0); • glPushMatrix(); • glRotatef(45,0,1,0); • glPopMatrix(); • glPopMatrix(); • glPopMatrix(); I * T * T I * T I * T * T * R I * T I * T * T * R I * T * T * R I * T I * T * T * R * R I * T * T * R I * T

  14. Robot Example

  15. OpenGL glutSolidCone creates a cone oriented along the Z axis. The base of the cone is placed at Z = 0, and the top at Z = height We need to rotate the cone so that it stands up in the positive Z direction We need to translate the cones to make the tree We need to draw the star using GL_TRIANGLES We need translations to draw the pot using OpenGL glutSolidTorus We need translations to draw the presents using OpenGL glutSolidCube Christmas Tree Example

  16. Some Glut Functions • glutSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks); • base - radius of the base of the cone. • height - height of the cone. • slices - number of subdivisions around the Z axis. • stacks - number of subdivisions along the Z axis. (The cone is subdivided around the Z axis into slices, and along the Z axis into stacks) • glutSolidTorus(GLdoubleinnerRadius, GLdoubleouterRadius, GLintnsides, GLint rings); • innerRadius - Inner radius of the torus. • outerRadius - Outer radius of the torus. • nsides - Number of sides for each radial section. • rings - Number of radial divisions for the torus.

  17. void drawScene() { glMatrixMode(GL_MODELVIEW); //Rotate about the x-axis so the cones are upright glPushMatrix(); glRotatef(-90, 1.0f, 0.0f, 0.0f); glColor3f(0.0f, 0.8f, 0.0f); //Draw bottom cone glutSolidCone(1.0, 3.0, 10, 10); //Draw middle cone glPushMatrix(); glTranslatef(0.0f, 0.0f, 1.0f); glutSolidCone(0.75, 2.0, 10, 10); //Draw top cone glPushMatrix(); glTranslatef(0.0f, 0.0f, 1.0f); glutSolidCone(0.5, 1.0, 10, 10); glPopMatrix(); glPopMatrix(); glPopMatrix(); //cancel rotation required for the tree to be upright Christmas Tree Example (draw 3 cones)

  18. Christmas Tree Example(draw pot and star) • //Draw a pot • glPushMatrix(); • glTranslatef(0.0f, 0.0f, -0.2f); • glColor3f(0.5f, 0.5f, 0.5f); • glutSolidTorus(0.2, 0.3, 10, 10) • glPopMatrix(); • //Draw a present • glPushMatrix(); • glTranslatef(1.0f, 0.0f, 0.0f); • glColor3f(0.5f, 0.0f, 0.8f); • glutSolidCube(0.5f); • glPopMatrix(); • //Draw a flat star on top • glPushMatrix(); • glTranslatef(0.0f, 3.0f, 0.0f); • glColor3f(1.0f, 1.0f, 0.0f); • glBegin(GL_TRIANGLES); • glVertex3f(-0.2f, -0.1f, 0.0f); • glVertex3f(0.2f, -0.1f, 0.0f); • glVertex3f(0.0f, 0.3f, 0.0f); • glVertex3f(-0.2f, 0.1f, 0.0f); • glVertex3f(0.2f, 0.1f, 0.0f); • glVertex3f(0.0f, -0.3f, 0.0f); • glEnd(); • glPopMatrix(); • }

  19. To make it simple, create a showcube function: void showcube (double x, double y, double z, double angle) { //Shows a cube at x,y,z rotated by angle around y axis glPushMatrix(); glTranslatef(x,y,z); glRotatef(angle,0,1,0); colorcube(); glPopMatrix(); } So the code: Before drawing a cube, a copy of the current matrix is saved onto the matrix stack After the cube is drawn the current matrix is popped and the saved matrix becomes the current matrix. Pyramid Example

  20. Pyramid Example The first cube is drawn uses the call: showcube(-9,0,0,45), which draws a cube translated by 9 in the –x direction and rotated 45 degrees around the y axis, and this results in the following new current matrix: The call glPopMatrix() within showcube() then revert the current matrix to identify matrix, ready for drawing other cubes

  21. void showpyramid(void) { glPushMatrix(); //Move to centre of bottom row glTranslatef(0,-9,0); //Bottom row showcube(-9,0,0,45); showcube(-3,0,0,45); showcube(3,0,0,45); showcube(9,0,0,45); showcube(-6,0,3,45); showcube(0,0,3,45); showcube(6,0,3,45); showcube(-3,0,6,45); showcube(3,0,6,45); showcube(0,0,9,45); //3rd row showcube(-3,6,0,45); showcube(3,6,0,45); showcube(0,6,3,45); showcube(0,6,-3,45); //Top row showcube(0,9,0,0); glPopMatrix(); } Pyramid Example • showcube(-6,0,-3,45); • showcube(0,0,-3,45); • showcube(6,0,-3,45); • showcube(-3,0,-6,45); • showcube(3,0,-6,45); • showcube(0,0,-9,45); • //2nd row • showcube(0,3,0,0); • showcube(6,3,0,0); • showcube(-6,3,0,0); • showcube(-3,3,3,0); • showcube(3,3,3,0); • showcube(0,3,6,0); • showcube(-3,3,-3,0); • showcube(3,3,-3,0); • showcube(0,3,-6,0);

More Related