CHAPTER 10 - Implementation 2

Objectives Objectives<next>


Humanoid Figure


Building the Model


Tree with Matrices

 


Display and Traversal


Transformation Matrices


Stack-based Traversal


Traversal Code


Analysis


General Tree Data Structure


Left-Child Right-Sibling Tree


Tree node Structure


C Definition of treenode

typedef struct treenode {

Glfloat m[16];
void (*f)();
struct treenode *sibling;
struct treenode *child; } treenode;

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;

Notes


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);

Notes


Dynamic Trees


<next>