Download presentation
Presentation is loading. Please wait.
Published byClaire Fleming Modified over 9 years ago
1
1 91.427 Computer Graphics I, Fall 2008 Hierarchical Modeling II
2
2 91.427 Computer Graphics I, Fall 2008 Objectives Build tree-structured model of humanoid figure Examine various traversal strategies Build generalized tree-model structure independent of particular model
3
3 91.427 Computer Graphics I, Fall 2008 Humanoid Figure
4
4 91.427 Computer Graphics I, Fall 2008 Building the Model Can build simple implementation using quadrics: ellipsoids and cylinders Access parts through functions torso() left_upper_arm() Matrices describe position of node with respect to its parent M lla positions left lower leg with respect to left upper arm
5
5 91.427 Computer Graphics I, Fall 2008 Tree with Matrices
6
6 91.427 Computer Graphics I, Fall 2008 Display and Traversal Position of figure determined by 11 joint angles 2 for head + 1 for each other part Display of tree requires graph traversal Visit each node once Display function at each node that describes part associated with node, applying correct transformation matrix for position and orientation
7
7 91.427 Computer Graphics I, Fall 2008 Transformation Matrices There are 10 relevant matrices M positions and orients entire figure through torso which is root node M h positions head with respect to torso M lua, M rua, M lul, M rul position arms and legs with respect to torso M lla, M rla, M lll, M rll position lower parts of limbs with respect to corresponding upper limbs
8
8 91.427 Computer Graphics I, Fall 2008 Stack-based Traversal Set model-view matrix to M and draw torso Set model-view matrix to MM h and draw head For left-upper arm need MM lua and so on Rather than recomputing MM lua from scratch or using inverse matrix, can use matrix stack to store M and other matrices as traverse tree
9
9 91.427 Computer Graphics I, Fall 2008 Traversal Code figure() { glPushMatrix() torso(); glRotate3f(…); head(); glPopMatrix(); glPushMatrix(); glTranslate3f(…); glRotate3f(…); left_upper_arm(); glPopMatrix(); glPushMatrix(); save present model-view matrix update model-view matrix for head recover original model-view matrix save it again update model-view matrix for left upper arm recover and save original model-view matrix again rest of code
10
10 91.427 Computer Graphics I, Fall 2008 Analysis Code describes particular tree and particular traversal strategy Can develop more general approach? Note that sample code does not include state changes such as changes to colors May also want to use glPushAttrib and glPopAttrib to protect against unexpected state changes affecting later parts of code
11
11 91.427 Computer Graphics I, Fall 2008 General Tree Data Structure Need data structure to represent tree + algorithm to traverse tree Will use left-child right sibling structure Uses linked lists Each node in data structure = two pointers Left: next node Right: linked list of children
12
12 91.427 Computer Graphics I, Fall 2008 Left-Child Right-Sibling Tree
13
13 91.427 Computer Graphics I, Fall 2008 Tree node Structure At each node need to store Pointer to sibling Pointer to child Pointer to function that draws object represented by node Homogeneous coordinate matrix to multiply on right of current model-view matrix Represents changes going from parent to node In OpenGL this matrix = 1D array storing matrix by columns
14
14 91.427 Computer Graphics I, Fall 2008 C Definition of treenode typedef struct treenode { GLfloat m[16]; void (*f)(); struct treenode *sibling; struct treenode *child; } treenode;
15
15 91.427 Computer Graphics I, Fall 2008 Defining the torso node treenode torso_node, head_node, lua_node, … ; /* use OpenGL functions to form matrix */ glLoadIdentity(); glRotatef(theta[0], 0.0, 1.0, 0.0); /* move model-view matrix to m */ glGetFloatv(GL_MODELVIEW_MATRIX, torso_node.m) torso_node.f = torso; /* torso() draws torso */ Torso_node.sibling = NULL; Torso_node.child = &head_node;
16
16 91.427 Computer Graphics I, Fall 2008 Notes Position of figure determined by 11 joint angles stored in theta[11] Animate by changing angles and redisplaying Form required matrices using glRotate and glTranslate More efficient than software Because matrix formed in model-view matrix, may want to first push original model-view matrix on matrix stack
17
17 91.427 Computer Graphics I, Fall 2008 Preorder Traversal void traverse(treenode *root) { if(root == NULL) return; glPushMatrix(); glMultMatrix(root->m); root->f(); if(root->child != NULL) traverse(root->child); glPopMatrix(); if(root->sibling != NULL) traverse(root->sibling); }
18
18 91.427 Computer Graphics I, Fall 2008 Notes Must save model-view matrix before multiplying it by node matrix Updated matrix applies to children of node but not to siblings which contain their own matrices Traversal program applies to any left-child right-sibling tree Particular tree encoded in definition of individual nodes Order of traversal matters because of possible state changes in functions
19
19 91.427 Computer Graphics I, Fall 2008 Dynamic Trees If use pointers, structure can be dynamic typedef treenode *tree_ptr; tree_ptr torso_ptr; torso_ptr = malloc(sizeof(treenode)); Definition of nodes and traversal essentially same as before but can add and delete nodes during execution
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.