On the 5th week of my lab session:
We were asked to do a robot and animate it.
However currently I have only been able to rotate it.
Either way here's a picture of it
Here's a sample code of the creation of the robot
void drawBody()
{
glBegin(GL_QUADS);
glVertex3f(15,5,5);
glVertex3f(15,5,-5);
glVertex3f(15,-20,-5);
glVertex3f(15,-20,5);
glVertex3f(-15,-20,5);
glVertex3f(-15,-20,-5);
glVertex3f(-15,5,-5);
glVertex3f(-15,5,5);
glVertex3f(-15,5,5);
glVertex3f(-15,-20,5);
glVertex3f(15,-20,5);
glVertex3f(15,5,5);
glVertex3f(-15,5,-5);
glVertex3f(-15,-20,-5);
glVertex3f(15,-20,-5);
glVertex3f(15,5,-5);
glVertex3f(-15,5,-5);
glVertex3f(-15,5,5);
glVertex3f(15,5,5);
glVertex3f(15,5,-5);
glVertex3f(-15,-20,-5);
glVertex3f(-15,-20,5);
glVertex3f(15,-20,5);
glVertex3f(15,-20,-5);
glEnd();
}
void head()
{
glutSolidSphere(9,34,23);
}
void drawLimb()
{
glBegin(GL_QUADS);
glVertex3f(5,5,5);
glVertex3f(5,5,-5);
glVertex3f(5,-15,-5);
glVertex3f(5,-15,5);
glVertex3f(5,-15,5);
glVertex3f(5,-15,-5);
glVertex3f(-5,-15,-5);
glVertex3f(-5,-15,5);
glVertex3f(-5,-15,5);
glVertex3f(-5,-15,-5);
glVertex3f(-5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(-5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(5,5,5);
glVertex3f(5,5,-5);
glVertex3f(-5,5,5);
glVertex3f(5,5,5);
glVertex3f(5,-15,5);
glVertex3f(-5,-15,5);
glVertex3f(5,5,5);
glVertex3f(-5,5,5);
glVertex3f(-5,-15,5);
glVertex3f(5,-15,5);
glEnd();
}
void drawRobot()
{
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(-20, 0.0, 0.0);
glRotatef(40.0, 1.0, 0.0, 0.0);
drawLimb(); //draw upper right arm
glPopMatrix();
glPushMatrix();
glColor3f(0,1,0);
glTranslatef(-20, -15.0, -2.0);
glRotatef(-30.0, 1.0, 0.0, 0.0);
drawLimb(); //lower right limb
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-10, -45.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); // bot left leg
glPopMatrix();
glPushMatrix();
glColor3f(0,1,1);
glTranslatef(10, -45.0, 0.0);
glRotatef(60.0, 1.0, 0.0, 0.0);
drawLimb(); // bot right leg
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(10, -25.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); //top right leg
glPopMatrix();
glPushMatrix();
glColor3f(0,1,1);
glTranslatef(-10, -25.0, 0.0);
glRotatef(0.0, 0.0, 0.0, 0.0);
drawLimb(); //top left leg
glPopMatrix();
glPushMatrix();
//glTranslatef(0, 0.0, 2.5);
glColor3f(0,0,1);
drawBody();
glPopMatrix();
glPushMatrix();
glColor3f(1,0,1);
glTranslatef(0, 12.0, 0.0);
//glTranslatef(20,30.0,20.0);
head();
glPopMatrix();
glPushMatrix();
glColor3f(1,1,0);
glTranslatef(20, 0.0, 0.0);
glRotatef(-40.0, 1.0, 0.0, 0.0);
drawLimb(); //draw upper left arm
glPopMatrix();
glPushMatrix();
glTranslatef(20, -12.5, 7.5);
glRotatef(-90.0, 1.0, 0.0, 0.0);
drawLimb(); //lower left limb
glPopMatrix();
}
And here is code for rotation
In the Display function:
glPushMatrix();
glRotatef(::rot,xx,yy,zz);
drawRobot();
glPopMatrix();
In the KeyboardFunc:
switch(key)
{
case 'x':
if(pressed==0)
{
xx=1;
pressed=1;
}
else if(pressed==1)
{
xx=0;
pressed=0;
}
break;
case 'y':
if(pressed==0)
{
yy=1;
pressed=1;
}
else if(pressed==1)
{
yy=0;
pressed=0;
}
break;
case 'z':
if(pressed==0)
{
zz=1;
pressed=1;
}
else if(pressed==1)
{
zz=0;
pressed=0;
}
break;
}
In my DoMainLoop:
void DoMainLoop()
{
if(::rot>=-90&&::rot<=90&&::set==0) { ::rot++; if(::rot==90) { ::set=1; } } else if(::rot>=-90&&::rot>=-90&&::set==1)
{
::rot--;
if(::rot==-90)
{
::set=0;
}
}
}
Erm i made a mistake on the rotation, somehow i thought it was to rotate the whole body and this code shows that, i will do the rotating of arms later
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment