Hierarchical Transformations

Hierarchical Transformations

1

Hierarchical Modelling in OpenGL OpenGL has two matrix stacks, the modelview matrix stack and the projection matrix stack. The modelview matrix stack is at least 32 levels deep. The projection matrix stack is at least 2 levels deep. The modelview matrix stack is conjunction with hierarchical models.

used

in

The matrix stacks may be pushed and popped using glPushMatrix() and glPopMatrix().

a e i m

b f j n

c g k o

d h l p

a e i m

b f j n

c g k o

d h l p

modelview matrix stack (32 4x4 matrices) a e i m

b f j n

c g k o

d h l p

a e i m

b f j n

c g k o

d h l p

Pushing and popping the matrix stack

A a hierarchical model is typically traversed in a depth first manner. COSC1224/1226: Real-Time Rendering and 3D Games Programming

Semester 1, 2007

Hierarchical Transformations

2

Each time the traversal moves deeper into the hierarchy the transformation for the node currently being visited — and everything in the subgraph below that node — is added, by matrix postmultiplication, to the CTM, that is, the top of the matrix stack By pushing the matrix stack before adding another transformation it is easy to restore the previous CTM by just popping the stack.

COSC1224/1226: Real-Time Rendering and 3D Games Programming

Semester 1, 2007

Hierarchical Transformations

3

A Car Wheel Example

A simple example of hierarchical modelling is drawing a car. To draw a car’s four wheels, including a wheel’s five bolts, we only have one wheel drawing function but we call it four times with different transformations (translations in this case) to position the wheel in the correct place. Simlarly we only have one function to draw a bolt and call it five times for each wheel — after setting up the appropriate transformation. The following program shows how to manipulate the matrix stack to draw a car in this manner.

COSC1224/1226: Real-Time Rendering and 3D Games Programming

Semester 1, 2007

Hierarchical Transformations

4

draw_wheel_and_bolts() { long i; draw_wheel(); for(i=0;itype) { case box: draw_box( .....); break; /* Add other objects here */ default: printf(‘‘Error - unknown object type\n’’); break; COSC1224/1226: Real-Time Rendering and 3D Games Programming

Semester 1, 2007

Hierarchical Transformations

14

} /* Render next object lower in the hierarchy. */ render_object(curr_object->next_level); /* Restore transformation matrix. */ glPopMatrix(); /* Render next object at same level in the hierarchy. render_object(curr_object->next_object); } }

COSC1224/1226: Real-Time Rendering and 3D Games Programming

Semester 1, 2007