Recent Resources
-
GLSL 4.0: Discarding Fragments to Create a Perf...
-
GLSL 4.0: Using Subroutines to Select Shader Fu...
-
Building a Complete Board-based Puzzle Game wit...
-
JIRA: Programming Workflows
-
.NET Generics 4.0: Container Patterns and Best...
-
Raw Meat: Game Design Tips from Team Meat's...
-
Sedge: An Automated Error Reporting Tool
OpenGL Object Hierarchy
By Arturo Montieri | Published Dec 28 2000 09:51 AM in OpenGL
Download attached article resource
This is a simple example of object hierarchy. We can think of object hierarchy as a mode of representing a complex object composed of more simple ones related to each other. In this tutorial we'll learn two new GL commands: glPushMatrix() and glPopMatrix() to accomplish our goal, which is modeling a simple robot.
The robot structure
Note the relationships between objects: Leg is pelvis child and pelvis is bust's child, forearm is arm's child and arm is shoulder's child, shoulder is bust's child too, and so on. So, if we move bust for example, legs and arms move too. This is an object hierarchy.
Every piece of the robot is modeled into a function and stored in a display list so we'll have:
The most important function is DrawRobot() that draws all the parts of the robot and which uses the OpenGL commands glPushMatrix() and glPopMatrix() to estabilish the relationships between objects.
Pseudocode
The Code
In the main function I added some keys to control the robot. They are:
And finally the bin & source are attached to this article.
Happy coding...
Arturo "IRIX" Montieri : montieri@hotmail.com
Download attached article resource
This is a simple example of object hierarchy. We can think of object hierarchy as a mode of representing a complex object composed of more simple ones related to each other. In this tutorial we'll learn two new GL commands: glPushMatrix() and glPopMatrix() to accomplish our goal, which is modeling a simple robot.
The robot structure
| Fig. 1a - Object Hierarchy | 1b - GL model | 1c - GL light model |
Every piece of the robot is modeled into a function and stored in a display list so we'll have:
void struct_part_name(void)So, for example here is the code for left arm:
{
glNewList(part_name,GL_COMPILE);
.../* GL Commands to model */
glEndList();
}
void struct_left_arm(void)Note that in this example I'll use some GLUT commands to draw Spheres and Cubes so remember to link gl/glut.h in the headers section as indicated below
{
glNewList(left_arm,GL_COMPILE);
// Shoulder
glTranslatef(1.1,0.25,0.0);
glScalef(0.5,0.5,0.5);
glColor3ub(128,128,128);
glutSolidSphere(0.5,20,20);
// Arm
glTranslatef(0.0,-1.10,0.0);
glScalef(0.5,1.5,0.5);
glColor3ub(255,255,255);
glutSolidCube(1.0);
glEndList();
}
...
#include <gl/glut.h>
...
The most important function is DrawRobot() that draws all the parts of the robot and which uses the OpenGL commands glPushMatrix() and glPopMatrix() to estabilish the relationships between objects.
Pseudocode
The Code
void DrawRobot(void)and so on...
{
/*-------------BUST AND HEAD--------------*/
glPushMatrix();
glRotatef(bust_angle_y,0,1,0);
glRotatef(bust_angle_x,1,0,0);
glPushMatrix();
glCallList(bust);
glCallList(head);
glPopMatrix();
/*--------RIGHT ARM AND FOREARM---------*/
glPushMatrix();
glTranslatef(0,0.25,0);
glRotatef(right_arm_angle,1,0,0);
glTranslatef(0,-0.25,0);
glPushMatrix();
glCallList(right_arm);
glPopMatrix();
glPushMatrix();
glTranslatef(1.25,-0.7,0);
glRotatef(right_forearm_angle,1,0,0);
glTranslatef(-1.25,0.7,0);
glCallList(right_forearm);
glPopMatrix();
glPopMatrix();
/*---------LEFT ARM AND FOREARM---------*/
glPushMatrix();
glTranslatef(0,0.25,0);
glRotatef(left_arm_angle,1,0,0);
glTranslatef(0,-0.25,0);
glPushMatrix();
glCallList(left_arm);
glPopMatrix();
glPushMatrix();
glTranslatef(1.25,-0.7,0);
glRotatef(left_forearm_angle,1,0,0);
glTranslatef(-1.25,0.7,0);
glCallList(left_forearm);
glPopMatrix();
glPopMatrix();
In the main function I added some keys to control the robot. They are:
| '1' & 'Q' '2' & 'W' '3' & 'E' '4' & 'R' '5' & 'T' '6' & 'Y' '7' & 'U' '8' & 'I' KEY ARROWS PGUP & PGDOWN 'V' 'B' 'N' 'M' | right arm control right forearm control left arm control left forearm control left thigh control left leg control right thigh control right thigh control rotate model light control camera control |
Happy coding...
Arturo "IRIX" Montieri : montieri@hotmail.com
Download attached article resource


















