PDA

View Full Version : need help with hierarchy model



kayouti
05-23-2003, 07:12 AM
I'm trying to draw an ant model using a herarchy of joints and segments but i guess i did rather badly on this. can someone tell me how to draw it out?
i did a code of it before, but think there is alot of errors and the joints and segments are not rightly place. maybe someone can complete it for me and point out the mistakes?
i'm working on a unix machine
below is my code. model.h and model.c

----------model.h----------------

#ifndef MODEL
#define MODEL
#define CYLINDER_HEIGHT 0.5
#define JOINT_SIZE CYLINDER_HEIGHT / 4

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

void drawAnt(void);

#endif
/*end of code*/

-----------model.c---------------

#include "model.h"

void drawTail(int frame)
{
/* Draw tail */
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0 ,0.0, 0.0);
if(frame == SOLID)
glutSolidSphere(0.75f,20,16);
else
glutWireSphere(0.75f,20,20);
glPopMatrix();
}

void drawBody(int frame)
{
/* Draw body */
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 1.0);
if(frame == SOLID)
glutSolidSphere(0.50f,20,20);
else
glutWireSphere(0.30f,20,20);
glPopMatrix();
}

void drawHead(int frame)
{
/* Draw Head */
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 2.0);
if(frame == SOLID)
glutSolidSphere(0.30f,20,20);
else
glutWireSphere(0.30f,20,20);
glPopMatrix();
}

void drawEyes(int frame)
{
/* Draw Eyes */
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
if(frame == SOLID)
glutSolidSphere(0.05f,10,10);
else
glutWireSphere(0.05f,10,10);

glTranslatef(-0.1f, 0.0f, 0.0f);
if(frame == SOLID)
glutSolidSphere(0.05f,10,10);
else
glutWireSphere(0.05f,10,10);
glPopMatrix();
}

void drawAntennas(int frame)
{
/* draw Antennas */
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.05f, 0.10f, 0.18f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
if(frame == SOLID)
glutSolidCone(0.08f,0.5f,10,2);
else
glutWireCone(0.08f,0.5f,10,2);

glTranslatef(-0.1f, 0.0f, 0.0f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
if(frame == SOLID)
glutSolidCone(0.08f,0.5f,10,2);
else
glutWireCone(0.08f,0.5f,10,2);
glPopMatrix();
}

void drawWholeHead(int frame)
{
glPushMartix();
drawHead(frame);
drawEyes(frame);
drawAntennas(frame);
glPopMatrix();
}

void drawWholeBody(int frame)
{
glPushMatrix()
drawTail(frame);
drawBody(frame);
drawWholeHead(SOLID);
glPopMatrix();
}

void drawUpperLegs()
{
glColor3f(1.0, 0.0, 0.0);
glScalef(JOINT_SIZE,JOINT_SIZE,JOINT_SIZE);
glPushMatrix();
/* drawLegJoint */
if(frame == SOLID)
glutSolidSphere(1.0, 8, 8);
else
glutWireSphere(1.0, 8, 8);
glPopMatrix();

glTranslate(0.0,-CYLINDER_HEIGHT,0.0);

glPushMatrix();
qobj = gluNewQuadric();
gluQuadricCallBack(qobj, GLU_ERROR, errorCallback);

/* drawUpperLeg */
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj, 0.5, 0.5, CYLINDER_HEIGHT, 15, 5);
glPopMatrix();
}

void drawLowerLegs(int frame)
{
glColor3f(1.0, 0.0, 0.0);
glScalef(JOINT_SIZE,JOINT_SIZE,JOINT_SIZE);
glPushMatrix();
/* drawLegJoint */
if(frame == SOLID)
glutSolidSphere(1.0, 8, 8);
else
glutWireSphere(1.0, 8, 8);
glPopMatrix();

glTranslate(0.0,-CYLINDER_HEIGHT, 0.0);

glPushMatrix();
qobj = gluNewQuadric();
gluQuadricCallBack(qobj, GLU_ERROR, errorCallback);

/* drawLowerLeg */
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj, 0.5, 0.5, CYLINDER_HEIGHT, 15, 5);
glPopMatrix();
}

void drawRightLegs(int frame)
{
glPushMatrix();
drawUpperLegs(frame);
drawLowerLegs(frame);
glPopMatrix();
}

void drawLeftLegs(int frame)
{
glPushMatrix();
drawUpperLegs(frame);
drawLowerLegs(frame);
glPopMatrix();
}

void drawAnt(void)
{
glPushMatrix();
drawWholeBody(SOLID);
drawRightLegs(RIGHT,SOLID);
drawLeftLegs(LEFT,SOLID);
drawRightLegs(RIGHT,SOLID);
drawLeftLegs(LEFT,SOLID);
drawRightLegs(RIGHT,SOLID);
drawLeftLegs(LEFT,SOLID);
glPopMatrix();
}
/*end of code*/