0% found this document useful (0 votes)
9 views18 pages

Hierarchy Hand

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

3D OpenGL Basic Models

Hierarchical Modeling

GLUT 3D Models
• Two main categories
– Wireframe Models
– Solid Models
• Basic Shapes
– Cube: glutWireCube( ), glutSolidCube( )
– Cone: glutWireCone( ), glutSolidCone( )
– Sphere, Torus, Tetrahedron
• More advanced shapes
– Octahedron, Dodecahedron, Icosahedron
– Teapot (symbolic)
Basic 3D GLUT Objects

glutWireCube(1.0);
glutWireSphere(0.5,10,10);

glutWireCone(1, 1, 10, 10);

glutWireTorus(0.5,1.5,10,10)

GLUT Platonic Objects

glutWireTetrahedron(); glutWireDodecahedron();

glutWireOctahedron(); glutWireIcosahedron();
GLUT Object (Symbolic)

glutWireTeapot(1.0);

GLU Models
• GLUT does not provide a Cylinder object
• GLU provides so called Quadrics objects:

partial disk
disk sphere cylinder
Quadrics
• Algebraic surface
– f(x,y,z) is the sum of polynomials in x, y, z
• Quadric surface
– Algebraic surfaces with a degree up to 2
– Examples: x, y, xy, z², but not xy²
• Quadrics:
– Spheres, disks, cones
– At most 2 intersections with lines
– Approximate quadrics surface by polygons

gluCylinder
Three steps to create a cylinder: sphere, cylinder,
1. Create a GLU quadric object disk, partial disk
GLUquadricObj *p = gluNewQuadric();
2. Set to wire frame mode
gluQuadricDrawStyle(GLU__LINE);
3. Derive a cylinder object from p
gluCylinder(p, base, top, height, slice, stacks);

radius radius number of number of


at z=0 at z=height lines per ring rings

Default position is with base on plane z = 0.


glutWireCone
• Use glutWireCone and gluCylinder to make a lamp

z glutWireCone(base, height, slices, stacks)


y
base: the width of its base
height: the height of the cone
slices: number of vertical lines
stacks: number of horizontal lines
x
A polygonal approximation of a cone.
Base Default position: base at z=0.

Review: Model to World Frame


Model Frame to World Frame

M=TRS
In OpenGL
• Appropriate transformation set up from the model frame
(frame of symbols) to the world frame
• Apply it to the MODELVIEW matrix BEFORE EXECUTING
the code

Model Æ World

glMatrixMode(GL_MODELVIEW); /* M = T R S */
glLoadIdentity ( );
glTranslatef (....);
glRotatef (...);
glScalef(..);
gluCylinder (.....) /* or other symbol */
Hierarchical Models

Articulated Models
• Rigid parts connected by joints

• [Demo 3D Robot]

• By modeling joint angles, you can


do animations
• Question is, given all the join angles,
how do you draw the model?
Making an Articulated Arm
b

a=c
a b c
A B A

• A minimal 2D jointed object:


– Two pieces, A (“forearm”) and B (“upper arm”)
– Attach point c on B to point a on A (“elbow”)
• Desired parameters:
– shoulder position S (point at which b winds up)
– shoulder angle β (A and B rotate together about b)
– elbow angle α (A rotates about a, which stays attached to c)

Making an Arm, step 1

a
A

• Start with A and B in their untransformed configurations


(B is hiding behind A)
• First apply a series of transformations to A, leaving B
where it is… b
a=c
A
Making an Arm, step 2

a
A B
A
a

• Translate by ‐a, bringing a to the origin


• You can now see B peeking out from behind A
b
a=c
A

Making an Arm, step 3

A
B B
A
a a c

• Next, we rotate A by the “elbow” angle α

b
a=c
A
Making an Arm, step 4

A
AB B
a c a c

• Translate A by c, bringing a and c together to


form the elbow joint

• We can regard c as the origin of the lower arm b


coordinate system, and regard A as being in this
a=c
coordinate system.
A

Making an Arm, step 5

A
B A
B
a c
a c

• From now on, each transformation


applies to both A and B (This is
important!)
b
• Translate by ‐a, bringing a to the origin
a=c
– A and B both move together, so the elbow
doesn’t separate! A
Making an Arm, step 6

A b
B
a c
a=c
A

• Next, rotate by the “shoulder” angle ‐β


– again, A and B rotate together
b
a=c
A

Making an Arm, last step


b

a=c
B

b A

a=c
B

• Finally, translate by the shoulder


position S, bringing the arm where b
we want it a=c
• b is at origin of upper arm A
coordinate system
Note that …
b

a=c
A

• S, α, and β are parameters of the model


• But a, b, and c are structural constants.
• Changing S, α, or β wiggles the arm
• Changing a, b, or c dismembers it
– (useful only in video games!)

Hierarchical Transforms
b
TSR‐βT‐b a=c

Upper arm B A

TcRαT‐a Apply all the way down

Lower Arm A

Apply all the way down


Hierarchical Transforms in OpenGL
glLoadIdentity();
TSR‐βT‐b glPushMatrix();
__________________
Upper arm B __________________
__________________
TcRαT‐a __________________

glPushMatrix();
Lower Arm A
__________________
__________________
• Down edges: __________________
• Push, transform, draw __________________
• Up edges: glPopMatrix();
• Pop. glPopMatrix();

Articulated Model Exercise


• Articulated model
40
– rigid parts connected by joints
5

20

60

• Given x, y, α, β, θ1 and θ2, draw the scooter.


Step 1
• Write functions that draw parts in default location:

DrawBody() DrawHandle() DrawWheel() DrawBrake()

40
5

20

60

Step 2 5
40

• Construct the scene graph (model as a tree)


(position in the scene) 20
Tx,y
(transform handle Body 60
w.r.t. default body)

body
(transform brake Handle Back Front
w.r.t. default
handle) Wheel Wheel handle

Lower Upper
wheel
Brake Brake

• Label each edge with transformation necessary to position


child with respect to parent, in parent’s default location brake
Step 3
• Code it up: glMatrixMode(GL_MODELVIEW);
glPushMatrix();
Tx,y
glTranslatef(x, y); Body
T30,20R‐α T30Rβ
DrawBody();
T‐30Rβ
Handle Back Front
Wheel Wheel
T‐35Rθ1 T‐35R-θ2
Lower Upper
Brake Brake

• Down edges: Push, transform, draw. Up edges: Pop.

Back to the Robot …


Robot Guy
lla
l-- left
r-- right lua
-u- upper rua
-l- lower
rla
--a arm
rul lul
--l leg

lll
rll

Step 1
• Write functions that draw parts in default location

head(); right_upper_arm(); right_lower_arm(); left_upper_arm();


h=3, w=2 h=1, w=3 h=1, w=2 h=1, w=3

torso(); left_lower_arm(); left_upper_leg(); left_lower_leg();


h=5, w=2 h=1, w=2 right_upper_leg(); right_lower_leg();
h=3, w=1 h=2, w=1
Step 2
• Scene Graph (Tree) (position in the scene)
Tx,y
Torso

Step 3
• Code it up: glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x, y);
torso();
Hands‐on Session
• Download robotSkeleton.cpp from class website
• Complete Steps 1, 2, 3 to draw the robot
• Add Keyboard events to make robot move and
increase/decrease joint angles
• Add a menu that allows selection of individual
joints and manipulation of joint angles (+, ‐)
• Add Idle or Timer events to make robot dance

Summary
• GLUT 3D Models
– Cube, sphere, cone, torus, teapot
– Tetr/oct/dodec/icos ‐‐ ahedron
• GLU Models
– Cylinder, sphere, disk, partial disk
• Hierarchical Modeling
– Transform each object relative to its parent
– Transformation applies to parent and ALL children
– Convenient for static models
– Vital for animations

You might also like