Jump to content

  • Log In with Google      Sign In   
  • Create Account


Like
0Likes
Dislike

OpenGL Object Hierarchy

By Arturo Montieri | Published Dec 28 2000 03:51 AM in OpenGL

glpushmatrix glpopmatrix #39 draw arm forearm amp child leg
If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource

Attached Image: shot1.jpg

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.<br clear="all">

The robot structure

<table width="75%" border="1" cellpadding="0" cellspacing="0"><tbody><tr><td align="center">Attached Image: struct_.gif </td><td align="center">Attached Image: struct0.jpg </td><td align="center">Attached Image: struct.jpg </td></tr><tr><td class="maintext-1" align="center">Fig. 1a - Object Hierarchy</td><td class="maintext-1" align="center">1b - GL model</td><td class="maintext-1" align="center">1c - GL light model</td></tr></tbody></table>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:

void struct_part_name(void)
{
  glNewList(part_name,GL_COMPILE);
  .../* GL Commands to model */
  glEndList();
}

So, for example here is the code for left arm:

void struct_left_arm(void)
{
  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();
}

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

...
#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

<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td>
void DrawRobot()
{
  glPushMatrix();   /* all objects are bust's son
                  	that is if we move bust all other parts move too */  
  	
	/*-------------------- DRAW HEAD AND BUST -----------------------*/ 
	glPushMatrix();
   	/* Draw head */
  	/* Draw bust */ 

	glPopMatrix();
</td><td align="center">Attached Image: head&bust.jpg </td></tr><tr><td>
/*---------------- DRAW RIGHT ARM AND FOREARM --------------*/ 
	glPushMatrix();
  	glPushMatrix();
     	/* Draw Right arm */ 

  	glPopMatrix();

  	glPushMatrix();   /* forearm is arm's child */
    	/* Draw Forearm */ 
  	glPopMatrix();
	glPopMatrix();
</td><td align="center">Attached Image: arm&forearm.jpg </td></tr><tr><td>
/*-------------- DRAW LEFT ARM AND FOREARM -----------------*/ 

	glPushMatrix();
  	glPushMatrix();
     	/* Draw Left arm */ 
  	glPopMatrix();

  	glPushMatrix();   /* forearm is arm's child */
    	/* Draw Forearm */ 
  	glPopMatrix();
	glPopMatrix();
</td><td align="center">Attached Image: arm&forearml.jpg </td></tr><tr><td>
/*------------------ DRAW PELVIS AND LEGS ----------------*/ 
	glPushMatrix();   /* another glPushMatrix because
                    	if we move pelvis the legs move too...
                    	(legs are pelvis' son) */ 
  	glPushMatrix();
     	/* Draw pelvis */ 
  	glPopMatrix();
    	
  	glPushMatrix();
    	glPushMatrix();
       	/* Draw Right thigh */ 
    	glPopMatrix();
  	
    	glPushMatrix();   /* leg is thigh's child */
      	/* Draw Leg */ 

    	glPopMatrix();
  	glPopMatrix();
	
  	glPushMatrix();
    	glPushMatrix();
       	/* Draw Left thigh */ 
    	glPopMatrix();
  	
    	glPushMatrix();   /* leg is thigh's child */
      	/* Draw Leg */ 
    	glPopMatrix();
  	glPopMatrix();

	glPopMatrix();

  glPopMatrix();
}
</td><td align="center">Attached Image: pelvis&legs.jpg </td></tr></tbody></table>

The Code

void DrawRobot(void)
{
  /*-------------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();

and so on...

In the main function I added some keys to control the robot. They are:

<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td>'<b>1</b>' &amp; '<b>Q</b>'<br>'<b>2</b>' &amp; '<b>W</b>'<br>'<b>3</b>' &amp; '<b>E</b>'<br>'<b>4</b>' &amp; '<b>R</b>'<br>'<b>5</b>' &amp; '<b>T</b>'<br>'<b>6</b>' &amp; '<b>Y</b>'<br>'<b>7</b>' &amp; '<b>U</b>'<br>'<b>8</b>' &amp; '<b>I</b>'<br><b>KEY ARROWS</b><br><b>PGUP</b> &amp; <b>PGDOWN</b>&nbsp;&nbsp;&nbsp;&nbsp;<br>'<b>V</b>' '<b>B</b>' '<b>N</b>' '<b>M</b>'</td><td>right arm control<br>right forearm control<br>left arm control<br>left forearm control<br>left thigh control<br>left leg control<br>right thigh control<br>right thigh control<br>rotate model<br>light control<br>camera control<br></td></tr></tbody></table>And finally the bin & source are attached to this article.

Happy coding...

Arturo "IRIX" Montieri : montieri@hotmail.com







Comments

Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS