object move?

when i first complie and run the program, my object is out of place, but when i rotate the object, after sometime it went to the correct position.
y is it so?

#include “model.h”

void drawAnt(int frame)
{
glPushMatrix();
glTranslatef(0.0, base_move, 0.0);
drawBase(frame);

glPushMatrix();
glTranslatef(BASE * 1.7, 0.0, 0.0);
drawHead(frame);
glPopMatrix();

glPushMatrix();
glTranslatef(BASE / 1.3, 0.0, 0.0);
drawBody(frame);
glPopMatrix();

glPushMatrix();
glTranslatef(BODY * 2.3, 0.0, 0.0);
drawTail(frame);
glPopMatrix();

glPushMatrix();
drawLeftLegs(LEFT,frame);
glPopMatrix();
glPushMatrix();
drawRightLegs(RIGHT,frame);
glPopMatrix();

glPopMatrix();
}

void drawHead(int frame)
{
glPushMatrix();
glPushMatrix();
glScalef(HEAD, HEAD, HEAD);
glColor3f(1.0, 0.0, 0.0);
if(frame == WIRE)
{
glutWireSphere(0.57, 30.0, 10.0);
}
else
{
glutSolidSphere(0.57, 30.0, 10.0);
}
glPopMatrix();
glPopMatrix();
}

void drawBody(int frame)
{
glPushMatrix();
glPushMatrix();
glScalef(BASE, BASE, BODY);
glColor3f(1.0, 0.0, 0.0);
if(frame == WIRE)
{
glutWireSphere(0.5, 30.0, 10.0);
}
else
{
glutSolidSphere(0.5, 30.0, 10.0);
}
glPopMatrix();
glPopMatrix();
}

void drawBase(int frame)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glPushMatrix();
glScalef(BASE, BASE, BODY);
glColor3f(1.0, 0.0, 0.0);
if(frame == WIRE)
{
glutWireSphere(0.5, 30.0, 10.0);
}
else
{
glutSolidSphere(0.5, 30.0, 10.0);
}
glPopMatrix();
}

void drawTail(int frame)
{
glPushMatrix();
glPushMatrix();
glTranslatef(- (HEAD+BODY+BASE), 0.0, 0.0);
glPushMatrix();
glScalef(BASE, BASE, TAIL);
glColor3f(1.0, 0.0, 0.0);
if(frame == WIRE)
{
glutWireSphere(0.75, 30.0, 10.0);
}
else
{
glutSolidSphere(0.75, 30.0, 10.0);
}
glPopMatrix();
glPopMatrix();
}

void drawRightLowerLeg(int side, int frame)
{
glPushMatrix();
glScalef(LO_LEG_JOINT_SIZE,LO_LEG_JOINT_SIZE,LO_LEG_JOINT_SIZE);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireSphere(1.0,8,8);
}
else
{
glutSolidSphere(1.0,8,8);
}
glPopMatrix();
glTranslatef(0.0,- LO_LEG_HEIGHT * 0.75,0.0);
glPushMatrix();
glScalef(LO_LEG_WIDTH,LO_LEG_HEIGHT,LO_LEG_WIDTH);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix();
}

void drawLeftLowerLeg(int side, int frame)
{
glPushMatrix();
glScalef(LO_LEG_JOINT_SIZE,LO_LEG_JOINT_SIZE,LO_LEG_JOINT_SIZE);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireSphere(1.0,8,8);
}
else
{
glutSolidSphere(1.0,8,8);
}
glPopMatrix();
glTranslatef(0.0,- LO_LEG_HEIGHT * 0.75,0.0);
glPushMatrix();
glScalef(LO_LEG_WIDTH,LO_LEG_HEIGHT,LO_LEG_WIDTH);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix();
}

void drawRightFoot(int side, int frame)
{
glPushMatrix() ;
glScalef(FOOT_JOINT_SIZE,FOOT_JOINT_SIZE,FOOT_JOINT_SIZE);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix() ;
glTranslatef(0.0,- FOOT_JOINT_SIZE * 2.0, FOOT_WIDTH * -0.5);
glPushMatrix() ;
glScalef(FOOT_WIDTH, FOOT_HEIGHT, FOOT);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix();
}

void drawLeftFoot(int side, int frame)
{
glPushMatrix();
glScalef(FOOT_JOINT_SIZE,FOOT_JOINT_SIZE,FOOT_JOINT_SIZE);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix();
glTranslatef(0.0,- FOOT_JOINT_SIZE * 2.0, FOOT_WIDTH * 0.5);
glPushMatrix();
glScalef(FOOT_WIDTH, FOOT_HEIGHT, FOOT);
glColor3f(1.0, 0.0, 0.0);
if (frame == WIRE)
{
glutWireCube(1.0);
}
else
{
glutSolidCube(1.0);
}
glPopMatrix();
}

void drawLeftLegs(int side, int frame)
{
glPushMatrix();
glTranslatef(0.0,- LO_LEG_WIDTH * 0.75,0.35);
glRotatef(walking_angles[side][0],1.0,0.0,0.0);
drawLeftLowerLeg(side,frame);
glTranslatef(0.0,- LO_LEG_HEIGHT * 0.625, 0.0);
glRotatef(walking_angles[side][0],1.0,0.0,0.0);
drawLeftFoot(side,frame);
glPopMatrix();
}

void drawRightLegs(int side, int frame)
{
glPushMatrix();
glTranslatef(0.0, -LO_LEG_WIDTH * 0.75,-0.35);
glRotatef(walking_angles[side][0],1.0,0.0,0.0);
drawRightLowerLeg(side,frame);
glTranslatef(0.0, -LO_LEG_HEIGHT * 0.625, 0.0);
glRotatef(walking_angles[side][0],1.0,0.0,0.0);
drawRightFoot(side,frame);
glPopMatrix();
}

Looking at your code, there’s an awful lot of glPushMatrix()/glPopMatrix() going on. Are you sure you need all these and have matched them up right?

For example, I can’t see why in drawHead() (one of a few), you do two pushes and two pops? Surely only one push/pop is necessary.

I didn’t go through all of it but my first quick look seemed to show a few mismatches (i.e. more pushes than pops or vice versa) and this could well be the source of the instability.

There is not problem with using push/pop matrix, just have to keep up and make sure you par them correctly.

There are maybe a few extra push/pop’s that are not needed.

Like your object ant,

void draw_ant(int x, int y, int z)
{
glPushMatrix() // First push for object
glTranslate(x ,y ,z) location of Ant.

// ether put glPush here or inside the Draw_Head but not both.

Draw_Head();
other code emitted…

}

void Draw_Head( void )
{
glPushMatrix();
glTranslate // Location of head relative to body
// head code

glPopMatrix() // End code for head
}

I use lot’s of them in my code, www.angelfire.com/linux/nexusone/

You need to go thought your whole code and look to make sure you do not have any extra push or pop’s and also are called in the correct order.

[This message has been edited by nexusone (edited 05-26-2003).]