Original Post
I''m typing up the robot from OGLGP book, but something is wrong that I am just not seeing. The arms and legs are rotating 360 degrees instead of moving back and forth. I thought it was my angle checks, but I''ve compared it line by line and found nothing. Does anyone see what''s wrong?
//Draw Arm
void DrawArm(float xPos, float yPos, float zPos)
{
glPushMatrix();
glColor3f(0.4f, 0.4f, 0.4f);
glTranslatef(xPos, yPos, zPos);
glScalef(1.0f, 5.0f, 1.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
//Draw Head
void DrawHead(float xPos, float yPos, float zPos)
{
glPushMatrix();
glColor3f(0.5f, 0.5f, 0.5f);
glTranslatef(xPos, yPos, zPos);
glScalef(2.0f, 2.0f, 2.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
//Draw Torso
void DrawTorso(float xPos, float yPos, float zPos)
{
glPushMatrix();
glColor3f(0.4f, 0.4f, 0.4f);
glTranslatef(xPos, yPos, zPos);
glScalef(3.0f, 5.0f, 2.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
//Draw Leg
void DrawLeg(float xPos, float yPos, float zPos)
{
glPushMatrix();
glColor3f(0.4f, 0.4f, 0.4f);
glTranslatef(xPos, yPos, zPos);
glScalef(1.5f, 6.0f, 2.0f);
DrawCube(0.0f, 0.0f, 0.0f);
glPopMatrix();
}
//Draw Robot
void DrawRobot(float xPos, float yPos, float zPos)
{
static bool leg1 = true;
static bool leg2 = false;
static bool arm1 = false;
static bool arm2 = true;
glPushMatrix();
glTranslatef(xPos, yPos, zPos);
DrawHead(1.0f, 2.0f, 0.0f);
DrawTorso(1.5f, 0.0f, 0.0f);
glPushMatrix();
//Define Arm #1 and Movement
if (arm1)
armAngle[0] = armAngle[0] + 0.5f;
else
armAngle[0] = armAngle[0] - 0.5f;
if (armAngle[0] >= 15.0f)
arm1 = false;
if (armAngle[0] <= -15.0f)
arm1 = true;
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();
//Define Arm #2 and Movement
if (arm2)
armAngle[1] = armAngle[1] + 0.5f;
else
armAngle[1] = armAngle[1] - 0.5f;
if (armAngle[1] >= 15.0f)
arm1 = false;
if (armAngle[1] <= -15.0f)
arm1 = true;
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[1], 1.0f, 0.0f, 0.0f);
DrawArm(-1.5f, 0.0f, -0.5f);
glPopMatrix();
glPushMatrix();
//Define Leg #1 and Movement
if(leg1)
legAngle[0] = legAngle[0] + 0.5f;
else
legAngle[0] = legAngle[0] - 0.5f;
if (legAngle[0] >= 15.0f)
leg1 = false;
if (legAngle[0] <= -15.0f)
leg1 = true;
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[0], 1.0f, 0.0f, 0.0f);
DrawLeg(-0.5f, -5.0f, -0.5f);
glPopMatrix();
glPushMatrix();
//Define Leg #2 and Movement
if(leg2)
legAngle[1] = legAngle[1] + 0.5f;
else
legAngle[1] = legAngle[1] - 0.5f;
if (legAngle[1] >= 15.0f)
leg1 = false;
if (legAngle[1] <= -15.0f)
leg1 = 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();
}
void Render()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
angle = angle + 1.0f;
if (angle >= 360.0f)
angle = 0;
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -30.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
DrawRobot(0.0f, 0.0f, 0.0f);
glPopMatrix();
glFlush();
SwapBuffers(g_HDC);
}