百度文库的一个用opengl画的机器人的例程

#include
#include
#include
#include
#include




#define M_PI 3.141592653


#define COS(X)   cos( (X) * 3.14159/180.0 )
#define SIN(X)   sin( (X) * 3.14159/180.0 )


#define HEAD 1
#define SHOUDER 2
#define BODY 3
#define WAIST 4


#define UPLEG 5
#define LOWLEG 6
#define FOOT 7


#define UPARM 8
#define LOWARM 9
#define HAND 10


GLUquadricObj *qObj;
static float lightpos[] ={0.f, 200.f, 10.f, 1.f};
double turnbody=0;
double elevation=0;
int turnbodytemp;


int iskick=0;
int iswalk=0;
int canexcludeball=0;
int cancatchball=1;
float G_vLitPosition[4] = { -5.0f, 0.0f, 5.0f, 1.0f };
int fk=1;


static double v0,v1,v2;
static double a0,a1,a2;


struct {     
float pos[2];
float dir[2];
}myrobot;






int mode=0;


int ileg=0;
int iarm=0;
double turnfront =0;
double turnleft=0;
double turnupward=0;


//头,肩,腰。
double turnHead=0;
double turnShouder=0;
double turnWaist=0;


//手臂。
double turnUpArmR=0;
double turnMiddleArmR=0;
double turnHandR=0;


double turnUpArmL=0;
double turnMiddleArmL=0;
double turnHandL=0;




//脚。
double turnUpLegR=0;
double turnMiddleLegR=0;
double turnFootR=0;


double turnUpLegL=0;
double turnMiddleLegL=0;
double turnFootL=0;


//utils
float distance(float x1,float z1,float x2,float z2)
{
return sqrt((x1 - x2) * (x1 - x2) + (z1 - z2) * (z1 - z2));
}
float getMirrorAngle(float posx,float posz,float centerx,float centerz,float vx,float vz)
{
double theta;
double fai;
double alfa;

double temptheta = atan2(  (double)fabs(vz)  , (double) fabs(vx) );
if(vx >= 0 && vz <= 0)
theta = temptheta;
if(vx <= 0 && vz <= 0)
theta = M_PI - temptheta;
if(vx <= 0 && vz >= 0)
theta = M_PI + temptheta;
if(vx >= 0 && vz >= 0)
theta = 2 * M_PI - temptheta;

double tempfai = atan2 ((double)fabs(posz - centerz) ,(double)fabs(posx - centerx));
if(posz <= centerz && posx >= centerx)
fai = tempfai;
if(posz <= centerz && posx <= centerx)
fai = M_PI - tempfai;
if(posz >= centerz && posx <= centerx)
fai = M_PI + tempfai;
if(posz >= centerz && posx >= centerx)
fai = 2 * M_PI - tempfai;

alfa = theta - fai - M_PI;
return (float)(fai - alfa);

}


//Robot functions
void clear()
{
elevation=0;
turnHead=0;
turnShouder=0;
turnWaist=0;
//手臂。
turnUpArmR=0;
turnMiddleArmR=0;
turnHandR=0;
turnUpArmL=0;
turnMiddleArmL=0;
turnHandL=0;
//脚。
turnUpLegR=0;
turnMiddleLegR=0;
turnFootR=0;
turnUpLegL=0;
turnMiddleLegL=0;
turnFootL=0;
}
void Box(GLfloat xdim,GLfloat ydim,GLfloat zdim)
{
glBegin(GL_QUADS);
    //正面。
glNormal3f(0.0f,0.0f,1.0f);
  glVertex3f(xdim/2.0f,ydim/2.0f,zdim/2.0f);//1(以xdim=ydim=zdim=2.0f为准)
  glVertex3f(xdim/2.0f,-ydim/2.0f,zdim/2.0f);//2
  glVertex3f(-xdim/2.0f,-ydim/2.0f,zdim/2.0f);//3
  glVertex3f(-xdim/2.0f,ydim/2.0f,zdim/2.0f);//4
  //后面。
  glNormal3f(0.0f,0.0f,-1.0f);
  glVertex3f(xdim/2.0f,ydim/2.0f,-zdim/2.0f);//5
  glVertex3f(xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//6
  glVertex3f(-xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//7
  glVertex3f(-xdim/2.0f,ydim/2.0f,-zdim/2.0f);//8
  //右面。
       glNormal3f(1.0f,0.0f,0.0f);
  glVertex3f(xdim/2.0f,ydim/2.0f,zdim/2.0f);//1
  glVertex3f(xdim/2.0f,ydim/2.0f,-zdim/2.0f);//5
  glVertex3f(xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//6
  glVertex3f(xdim/2.0f,-ydim/2.0f,zdim/2.0f);//2
  //左面。
       glNormal3f(-1.0f,0.0f,0.0f);
  glVertex3f(-xdim/2.0f,ydim/2.0f,zdim/2.0f);//4
  glVertex3f(-xdim/2.0f,ydim/2.0f,-zdim/2.0f);//8
  glVertex3f(-xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//7
  glVertex3f(-xdim/2.0f,-ydim/2.0f,zdim/2.0f);//3
  //上面。
       glNormal3f(0.0f,1.0f,0.0f);
  glVertex3f(xdim/2.0f,ydim/2.0f,zdim/2.0f);//1
  glVertex3f(xdim/2.0f,ydim/2.0f,-zdim/2.0f);//5
  glVertex3f(-xdim/2.0f,ydim/2.0f,-zdim/2.0f);//8
  glVertex3f(-xdim/2.0f,ydim/2.0f,zdim/2.0f);//4
  //下面。
       glNormal3f(0.0f,-1.0f,0.0f);
  glVertex3f(xdim/2.0f,-ydim/2.0f,zdim/2.0f);//2
  glVertex3f(xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//6
  glVertex3f(-xdim/2.0f,-ydim/2.0f,-zdim/2.0f);//7
  glVertex3f(-xdim/2.0f,-ydim/2.0f,zdim/2.0f);//3
  glEnd();
  
}
void drawTorus(void)
{
int numMajor=32;
int numMinor=24;
float majorRadius=6.f;
float minorRadius=2.f;
    double majorStep=2.0f*M_PI/numMajor;
double minorStep=2.0f*M_PI/numMinor;

int i,j;

for(i=0;i {
double a0=i*majorStep;
double a1=a0+majorStep;
        GLfloat x0=(GLfloat)cos(a0);
GLfloat y0=(GLfloat)sin(a0);
GLfloat x1=(GLfloat)cos(a1);
GLfloat y1=(GLfloat)sin(a1);
        glBegin(GL_TRIANGLE_STRIP);
        for(j=0;j {
double b=j*minorStep;

GLfloat c=(GLfloat)cos(b);
            GLfloat r=minorRadius*c+majorRadius;
GLfloat z=minorRadius*(GLfloat)sin(b);

glNormal3f(x0*c,y0*c,z/minorRadius);
glVertex3f(x0*r,y0*r,z);
            glNormal3f(x1*c,y1*c,z/minorRadius);
glVertex3f(x1*r,y1*r,z);

}

glEnd();

if(i==14)
break;
}
}
void head()
{   
    glNewList(HEAD,GL_COMPILE);
glPushMatrix();
gluSphere(qObj, 10, 10, 10);
glRotatef(-turnHead,0,1,0);
glTranslatef(0,-10,0);
glRotatef(90,1,0,0);
gluCylinder(qObj,5,5,5,30,30);
gluDisk(qObj,0,5,30,30);
glPopMatrix();
    glEndList();
}
void shouder()
{
glNewList(SHOUDER,GL_COMPILE);
glColor3f(1,0,1);
glPushMatrix();
glRotatef(90,1,0,0);
gluCylinder(qObj,8,25,8,30,30);
gluDisk(qObj,0,8,30,30);
glRotatef(-90,1,0,0);
glTranslatef(0,-8,0);
glRotatef(90,1,0,0);
gluDisk(qObj,0,25,30,30);
glPopMatrix();
glColor3f(1,1,1);
    glEndList();
}
void body()
{
glNewList(BODY,GL_COMPILE);
glColor3f(1,1,0);
gluCylinder(qObj,6,15,25,30,30);
glColor3f(1,1,1);
glEndList();
}
void waist()
{
glNewList(WAIST,GL_COMPILE);
glColor3f(1,0,1);
glPushMatrix();
glRotatef(90,1,0,0);
gluCylinder(qObj,6,6,2,30,30);
glRotatef(-90,1,0,0);
glTranslatef(0,-2,0);
glRotatef(90,1,0,0);
gluCylinder(qObj,6,10,10,10,10);
glRotatef(-90,1,0,0);
glTranslatef(0,-10,0);
glRotatef(90,1,0,0);
gluDisk(qObj,0,10,30,30);
glPopMatrix();
glColor3f(1,1,1);
glEndList();
}
void foot()
{
glPushMatrix();
glColor3f(1,0,0);
Box(5,3,15);
glColor3f(1,1,1);
glPopMatrix();
}
void lowleg()
{
glRotatef(90,1,0,0);
gluCylinder(qObj,3,3,50,30,30);
glRotatef(-90,1,0,0);
glTranslatef(-3,-50,0);
glRotatef(90,0,1,0);
gluCylinder(qObj,3,3,6,30,30);
glRotatef(-90,0,1,0);
glTranslatef(3,-2,6);

glPushMatrix();
if(!ileg%2)
{
glRotatef(turnFootR,1,0,0);
foot();
}
else
{
glRotatef(turnFootL,1,0,0);
foot();
}
glPopMatrix();

}
void upleg()
{
glPushMatrix();
for(ileg=0;ileg<2;ileg++)
{
if(ileg%2)
glTranslatef(-12,0,0);
        glColor3f(0,0,1);
gluSphere(qObj,4,30,30);
glPushMatrix();
if(!ileg%2)
            glRotatef(turnUpLegR,1,0,0);
else
glRotatef(turnUpLegL,1,0,0);
if(!ileg%2)
glRotatef(5,0,0,1);
else
glRotatef(-5,0,0,1);
glRotatef(90,1,0,0);
gluCylinder(qObj,4,4,20,30,30);
        glRotatef(-90,1,0,0);
glTranslatef(-4,-23,0);
        glRotatef(90,0,1,0);
gluCylinder(qObj,3,3,8,30,30);
        glRotatef(-90,0,1,0);
        glTranslatef(4,0,0);
        glColor3f(1,1,1);               
glPushMatrix();
if(!ileg%2)
{
glRotatef(turnMiddleLegR,1,0,0);
lowleg();
}
else
{   
glRotatef(turnMiddleLegL,1,0,0);
lowleg();
}
        glPopMatrix();
glPopMatrix();
}
    glPopMatrix();
}
void hand()
{
glPushMatrix();
glTranslatef(-1,-6,-6);
glRotatef(90,0,1,0);
    drawTorus();
glPopMatrix();
}
void lowarm()
{
glRotatef(90,1,0,0);
    gluCylinder(qObj,3,3,20,30,30);
glRotatef(-90,1,0,0);
    glTranslatef(-2,-20,0);
glRotatef(90,0,1,0);
    gluCylinder(qObj,3,3,4,30,30);
    glRotatef(-90,0,1,0);
    glTranslatef(3,-2,6); 
glPushMatrix();
    if(!iarm%2)
{
glRotatef(turnHandR,1,0,0);
hand();
}
else
{
        glRotatef(turnHandL,1,0,0);
        hand();
        if(!cancatchball)
{
glTranslatef(-1,-6,-6);
}
}
glPopMatrix();
}
void uparm()
{  
glPushMatrix();
for(iarm=0;iarm<2;iarm++)
{
if(iarm%2)
glTranslatef(-50,0,0);
glColor3f(1,0,1);
gluSphere(qObj,5,30,30);
glPushMatrix();
if(!iarm%2)
glRotatef(turnUpArmR,1,0,0);
else
glRotatef(turnUpArmL,1,0,0);
if(!iarm%2)
glRotatef(10,0,0,1);
else
glRotatef(-10,0,0,1);
glRotatef(90,1,0,0);
gluCylinder(qObj,4,4,20,30,30);
glRotatef(-90,1,0,0);
glTranslatef(-4,-23,0);
glRotatef(90,0,1,0);
gluCylinder(qObj,3,3,8,30,30);
glColor3f(1,1,1); 
glRotatef(-90,0,1,0);
glTranslatef(4,0,0);
glPushMatrix();
if(!iarm%2)
{
glRotatef(turnMiddleArmR,1,0,0);
lowarm();
}
else
{    
glRotatef(turnMiddleArmL,1,0,0);
lowarm();
}
glPopMatrix();
glPopMatrix();
}
    glPopMatrix();
}
//Robot functions end


void myinit()
{
qObj = gluNewQuadric();
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);

glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    
//init robot pos
myrobot.dir[0]=0;
myrobot.dir[1]=0;
myrobot.pos[0]=0;
myrobot.pos[1]=0;
    
//Robot
    head();
shouder();
body();
waist();
}


//Dynamic Object
void drawRobot()
{


glPushMatrix();
    glPushMatrix();
glRotatef(-90,1,0,0);
glCallList(BODY);
    glPopMatrix();

glPushMatrix();
glTranslatef(0,33,0);
glRotatef(turnShouder,0,1,0);
glCallList(SHOUDER);
glTranslatef(25,-7,0);
uparm();
    glPopMatrix();
glPushMatrix();
glTranslatef(0,45,0);
glRotatef(turnHead,0,1,0);
glCallList(HEAD);
    glPopMatrix();

    glPushMatrix();
glRotatef(turnWaist,0,1,0);
glCallList(WAIST);
glTranslatef(6,-15,0);
upleg();
    glPopMatrix();
glPopMatrix();

}




//Animation
void  robotrun()
{
static int  step=1;
static int stept=7;
double degree=6;
if(step==1)
{  
//left arm
turnUpArmL-=(degree/3);
//right arm
turnUpArmR+=(degree/3);
//left leg
turnUpLegL+=(degree/3);
//right leg
if(turnUpLegR>=-40)
{
turnUpLegR-=degree;
turnMiddleLegR+=degree*60/40;
}
else
{
elevation=2;
step=2;
}
}
else if(step==2)

//left arm
turnUpArmL-=(degree/3);
//right arm
turnUpArmR+=(degree/3);
//left leg
turnUpLegL+=degree/3;
//right leg
if(turnUpLegR<=-20)
{
turnUpLegR+=degree;
turnMiddleLegR-=degree*2.5;
}
else
{
elevation=0;
step=3;
}
}
else if(step==3)
{
//left arm
turnUpArmL+=(degree);
//right arm
turnUpArmR-=(degree);
//left leg
if(turnUpLegL>=0&&stept!=8)
{
turnUpLegL-=degree*3;
turnMiddleLegL+=degree;
}
else if(turnMiddleLegL>=0&&stept!=8)
turnMiddleLegL-=degree;
else 
{
stept=8;
if(turnUpLegL>=-35&&stept!=9)
{
turnUpLegL-=degree*3;
turnMiddleLegL+=degree*60/40;
}
else
{
stept=9;
turnUpLegL+=degree*3;
turnMiddleLegL-=degree*2.5;
}
}
//right leg
if(turnUpLegR<=20)
{
turnUpLegR+=degree;
}
else
step=4;
}
else if(step==4)
{
//left arm
turnUpArmL-=(degree/2.5);
//right arm
turnUpArmR+=(degree/2.5);
//left leg
turnUpLegL+=degree/1.8;
stept=7;
//right leg
if(turnUpLegR>=0)
{   
turnUpLegR-=degree;
turnMiddleLegR+=degree;
}
else if(turnMiddleLegR>=0)
{
turnMiddleLegR-=degree;
}
else 
{
elevation=2;
step=1;
}
}
    glutPostRedisplay();
}
//Key Listener
void  key(unsigned char key, int x, int y)
{
    
if(iskick==2)
{
clear();
iskick=1;
}


if (key == '\033')
exit(0);

if(key=='m')
mode++;

//direction
if(key=='d')
{
turnbody+=10;
}
else if(key=='a')
{
turnbody-=10;
}


myrobot.dir[0]=sin(M_PI/180*turnbody);
myrobot.dir[1]=cos(M_PI/180*turnbody);


if(key=='w')
{
iswalk=1;

float lastpos0 = myrobot.pos[0];
float lastpos1 = myrobot.pos[1];

myrobot.pos[0]+=5*myrobot.dir[0];
myrobot.pos[1]+=5*myrobot.dir[1];

//border deal
if(myrobot.pos[0]>=450||
myrobot.pos[0]<=-450||
myrobot.pos[1]<=-450||
myrobot.pos[1]>=450//wall
)
{
myrobot.pos[0]=lastpos0;
myrobot.pos[1]=lastpos1;
}

robotrun();
}
if(key=='z'){
lightpos[0] += 1.0;
}
if(key=='s')
{
iswalk=1;

float lastpos0 = myrobot.pos[0];
float lastpos1 = myrobot.pos[1];

myrobot.pos[0]-=5*myrobot.dir[0];
myrobot.pos[1]-=5*myrobot.dir[1];

//border deal
if(myrobot.pos[0]>=450||
myrobot.pos[0]<=-450||
myrobot.pos[1]<=-450||
myrobot.pos[1]>=450//wall
)
{
myrobot.pos[0]=lastpos0;
myrobot.pos[1]=lastpos1;
}

robotrun();
}

/*
if(key=='h')
{
turnHead+=10;
}*/
else if(key=='j')
{
turnShouder+=10;
}
else if(key=='k')
{
turnWaist+=10;
}
/* else if(key=='H')
{
turnHead-=10;
}*/
else if(key=='J')
{
turnShouder-=10;
}
else if(key=='K')
{
turnWaist-=10;
}
if(key=='u')
{
turnUpArmR+=10;
}
else if(key=='i')
{
turnMiddleArmR+=10;
}
else if(key=='o')
{
turnHandR+=10;
}
else if(key=='U')
{
turnUpArmR-=10;
}
else if(key=='I')
{
turnMiddleArmR-=10;
}
else if(key=='O')
{
turnHandR-=10;
}
if(key=='v')
{
turnUpArmL+=10;
}
else if(key=='b')
{
turnMiddleArmL+=10;
}
else if(key=='n')
{
turnHandL+=10;
}
else if(key=='V')
{
turnUpArmL-=10;
}
else if(key=='B')
{
turnMiddleArmL-=10;
}
else if(key=='N')
{
turnHandL-=10;
}
if(key=='r')
{
turnUpLegR+=10;
}
else if(key=='t')
{
turnMiddleLegR+=10;
}
else if(key=='y')
{
turnFootR+=10;
}
else if(key=='R')
{
turnUpLegR-=10;
}
else if(key=='T')
{
turnMiddleLegR-=10;
}
else if(key=='Y')
{
turnFootR-=10;
}
if(key=='d')
{
//turnUpLegL+=10;
}
else if(key=='f')
{
turnMiddleLegL+=10;
}
else if(key=='g')
{
turnFootL+=10;
}
else if(key=='D')
{
turnUpLegL-=10;
}
else if(key=='F')
{
turnMiddleLegL-=10;
}
else if(key=='G')
{
turnFootL-=10;
}

glutPostRedisplay();

}




void  redraw(void)
{ //设置清除屏幕的颜色,并清除屏幕和深度缓冲
glClearColor(0.0, 0.6, 0.8, 1.0);
glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//

//变换并绘制物体
    glMatrixMode(GL_PROJECTION);
glLoadIdentity();


   // gluPerspective(80.0, 1, 1,3000.0);
gluPerspective(80.0, 1, 1,3000.0);

glPushMatrix();
glLoadIdentity();
glOrtho(0,100,0,100,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glNormal3f(0,0,1);

glTranslatef(37,37,0);  
// glTranslatef(37,37,600);
glFlush();
glMatrixMode(GL_PROJECTION);
    glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
    glTranslatef(0,-8.6,-300);
if(mode%2)
{
glDisable(GL_LIGHTING);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_NORMALIZE);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else
{
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}

    

    glLoadIdentity();
//gluLookAt(490,450,0,0,-85,myrobot.pos[1],0,1,0);
gluLookAt(490,450,0,0,-85,myrobot.pos[1],0,1,0);
glPushMatrix();
    //glTranslatef(0,-8.6,0);
glTranslatef(0,300,0);
glRotatef(turnleft,0,1,0);
glRotatef(turnupward,1,0,0);
    glRotatef(turnfront,0,0,1);

    glPushMatrix();
//glTranslatef(0,-200,0);
glTranslatef(0,elevation,0);
//Robot
  glPushMatrix();
  if(iswalk)
  glTranslatef(myrobot.pos[0],0,myrobot.pos[1]);
  glRotatef(turnbody,0,1,0);
  drawRobot();       
       glPopMatrix(); 
  
  glPopMatrix();
  
  glFlush();        
  glutSwapBuffers();


  
}


void  reshape(int w,int h)
{
glViewport(0,0,w,h);
//设定透视方式
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0);


}


int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(800, 700);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM | GLUT_DOUBLE);
(void) glutCreateWindow("Made by a student");

myinit();
glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);

glMatrixMode(GL_MODELVIEW);

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

glutMainLoop();

return 0;  

}


转自http://wenku.baidu.com/link?url=KpS6PU2hD7kyoYR2wqUfCdsZp8XcKgxyMksdyC0iIxQW684k6QUu_0hTLF3txMpLjPZLTnHKvhWSUCk8CdZuFHY2X4AhQ3belQc9NsybrC_

你可能感兴趣的:(opengl)