Can anyone help me understand this...
A snippit from the robot program from OpgenGL game programming.
void DrawRobot(float xPos, float yPos, float zPos)
{
static bool leg1 = true; // robot''s leg states
static bool leg2 = false; // true = forward, false = back
static bool arm1 = true;
static bool arm2 = false;
glPushMatrix();
glTranslatef(xPos, yPos, zPos); // draw robot at desired coordinates
// draw components
DrawHead(1.0f, 2.0f, 0.0f);
DrawTorso(1.5f, 0.0f, 0.0f);
glPushMatrix();
// if leg is moving forward, increase angle, else decrease angle
if (arm1)
armAngle[0] = armAngle[0] + 0.1f;
else
armAngle[0] = armAngle[0] - 0.1f;
// once leg has reached its maximum angle in a direction,
// reverse it
if (armAngle[0] >= 15.0f)
arm1 = false;
if (armAngle[0] <= -15.0f)
arm1 = true;
// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[0], 1.0f, 0.0f, 0.0f);
DrawArm(2.5f, 0.0f, -0.5f);
glPopMatrix();
glPushMatrix();
// if leg is moving forward, increase angle, else decrease angle
if (arm2)
armAngle[1] = armAngle[1] + 0.1f;
else
armAngle[1] = armAngle[1] - 0.1f;
// once leg has reached its maximum angle in a direction,
// reverse it
if (armAngle[1] >= 15.0f)
arm2 = false;
if (armAngle[1] <= -15.0f)
arm2 = true;
// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[1], 1.0f, 0.0f, 0.0f);
DrawArm(-1.5f, 0.0f, -0.5f);
glPopMatrix();
//DrawArm(-1.5f, 0.0f, -0.5f);
// we want to rotate the legs relative to the robot''s position in the world
// this is leg 1, the robot''s right leg
glPushMatrix();
// if leg is moving forward, increase angle, else decrease angle
if (leg1)
legAngle[0] = legAngle[0] + 0.1f;
else
legAngle[0] = legAngle[0] - 0.1f;
// once leg has reached its maximum angle in a direction,
// reverse it
if (legAngle[0] >= 15.0f)
leg1 = false;
if (legAngle[0] <= -15.0f)
leg1 = true;
// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[0], 1.0f, 0.0f, 0.0f);
// draw the leg
DrawLeg(-0.5f, -5.0f, -0.5f);
glPopMatrix();
// do the same as above with leg 2, the robot''s left leg
glPushMatrix();
if (leg2)
legAngle[1] = legAngle[1] + 0.1f;
else
legAngle[1] = legAngle[1] - 0.1f;
if (legAngle[1] >= 15.0f)
leg2 = false;
if (legAngle[1] <= -15.0f)
leg2 = true;
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[1], 1.0f, 0.0f, 0.0f);
DrawLeg(1.5f, -5.0f, -0.5f);
glPopMatrix();
glPopMatrix();
}
Now, How are you supposed to keep track of what matrix corresponds to what parts... I mean it is very confusing.... You have to do a pushmatrix to get to a certain part... How could I do a pushmatrix thing to get to the head to rotate it and such....????
--Tantalus
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement