#include<windows.h>
#include<gl/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int all_rotate=0;
int head_rotate=0;
int shoulder_y=0;
int shoulder_x=0;
int larm_r=0;
int rarm_r=0;
int llw=0;
int rlw=0;
int robot_move=0;
int turn_back=0;
float l=0;
float k=0;
float a=0.2,b=0.9,c=0.3,d=0.3; //a--torso b--body c--shoulder d--arm and leg
void torso()
{
glPushMatrix();
glScalef(float(1.0/a),1.0,1.0);
glutSolidSphere(a, 30, 17);
glPopMatrix();
}
void body()
{
glPushMatrix();
glTranslatef(0,1.5,0);
glScalef(1.0,float(1.5/b),1.0);
glutSolidSphere(b, 30, 17);
glPopMatrix();
}
void shoulder()
{
glPushMatrix();
glScalef(float(1.5/c),1.0,1.0);
glutSolidSphere(c, 30, 17);
glPopMatrix();
}
void head()
{
glPushMatrix();
glTranslatef(0,0.5,0);
glutSolidSphere(0.5f, 30, 17);
glTranslatef(0,0,0.5);
glutSolidSphere(0.1f, 30, 17);
glPopMatrix();
}
void upperarm() //手脚的画法相同,所以直接使用同一个函数
{
glPushMatrix();
glTranslatef(0,-1,0);
glScalef(1.0,float(1.0/d),1.0);
glutSolidSphere(d, 30, 17);
glPopMatrix();
}
void lowerarm()
{
glPushMatrix();
glTranslatef(0,-1,0);
glScalef(1.0,float(1.0/d),1.0);
glutSolidSphere(d, 30, 17);
glPopMatrix();
}
void init(void)
{
GLfloat mat_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0};
GLfloat light_diffuse[]={0.1,0.9,0.1,1.0};
GLfloat light_position[]={10.0,10.0,10.0,1.0};
GLfloat light_specular[]={0.5,0.5,0.5,1.0};
GLfloat lmodel_ambient[]={0.0,1.0,0.0,1.0};
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
GLfloat mat_ambient[]={1.0,1.0,1.0,1.0};
GLfloat light_ambient[]={1.0,1.0,1.0,1.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.1,0.9,0.1);
glPushMatrix();
glRotatef((float)all_rotate,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(0,0,robot_move);
glPushMatrix();
body(); //身体
glPopMatrix();
glPushMatrix(); //(0,0,0)
glTranslatef(0,3,0); //(0,3,0)
glPushMatrix(); //先画头,这样头和脖子就不会一起旋转
glRotatef(head_rotate,0.0,1.0,0.0);
head();
glPopMatrix(); //(0,3,0)
glRotatef((GLfloat)shoulder_y,0.0,1.0,0.0); //shoulder、arms rotate based on Y axis
glRotatef((GLfloat)shoulder_x,1.0,0.0,0.0); //shoulder、arms rotate based on X axis
shoulder(); //脖子==肩膀
glPushMatrix(); //left hand
glTranslatef(1.5,0,0); //(1.5,3,0)
upperarm();
glPushMatrix(); //(1.5,3,0)
glTranslatef(0,-2,0); //下移2单位,到达手肘
//lelbowx rotate
glRotatef((GLfloat)larm_r,1.0,0.0,0.0);
lowerarm();
glPopMatrix(); //(1.5,3,0)
glPopMatrix(); //(0,3,0)
glPushMatrix(); //right hand
glTranslatef(-1.5,0,0); //(-1.5,3,0)
upperarm();
glPushMatrix(); //(-1.5,3,0)
glTranslatef(0,-2,0); //下移2单位,到达手肘
//relbowx rotate
glRotatef((GLfloat)rarm_r,1.0,0.0,0.0);
lowerarm();
glPopMatrix(); //(-1.5,3,0)
glPopMatrix(); //(0,3,0)
glPopMatrix(); //(0,0,0)
torso(); //胯
glPushMatrix(); //left leg
glTranslatef(1,0,0); //(1,0,0)
//walk
glRotatef(-40*llw,1.0,0.0,0.0);
upperarm(); //upperleg
glPushMatrix(); //(1,0,0)
glTranslatef(0,-2,0);
//lelbowx rotate
lowerarm(); //lowerleg
glPopMatrix(); //(1,0,0)
glPopMatrix(); //(0,0,0)
glPushMatrix(); //right leg
glTranslatef(-1,0,0); //(-1,0,0)
//walk
glRotatef(-40*rlw,1.0,0.0,0.0);
upperarm();
glPushMatrix(); //(-1,0,0)
glTranslatef(0,-2,0);
//relbowx rotate
lowerarm();
glPopMatrix(); //(-1,0,0)
glPopMatrix(); //(0,0,0)
glPopMatrix();
glPopMatrix();
glFlush();
}
/*void idle()
{
inverse+=10;
if(inverse>=360)
inverse=0.0f;
void display(void);
display();
}*/
void keyboard(unsigned char key,int x,int y)
{
switch (key)
{
case 'D' :all_rotate=(all_rotate+10)%360; //D全身转 A倒着转
glutPostRedisplay();
break;
case 'A' :all_rotate=(all_rotate-10)%360;
glutPostRedisplay();
break;
case 'd' :shoulder_y=(shoulder_y+10)%360; //d肩膀左右转 a相反
glutPostRedisplay();
break;
case 'a' :shoulder_y=(shoulder_y-10)%360;
glutPostRedisplay();
break;
case 'w' :shoulder_x=(shoulder_x+10)%360; //w肩膀上下转 s相反
glutPostRedisplay();
break;
case 's' :shoulder_x=(shoulder_x-10)%360;
glutPostRedisplay();
break;
case 'o' :head_rotate=(head_rotate+10)%360; //o转头
glutPostRedisplay();
break;
case 'p' : //p机器人行走
if(llw==0&&rlw==0)
{
llw=1;
}
else
{
int temp=llw;
llw=rlw;
rlw=temp;
robot_move++;
}
glutPostRedisplay();
break;
case 'e' : //e左手手臂上下摆动 r相反
if(larm_r<120)
{
larm_r+=10;
}
glutPostRedisplay();
break;
case 'r' :
if(larm_r>-120)
{
larm_r-=10;
}
glutPostRedisplay();
break;
case 't' : //t右手手臂上下摆动 y相反
if(rarm_r<120)
{
rarm_r+=10;
}
glutPostRedisplay();
break;
case 'y' :
if(rarm_r>-120)
{
rarm_r-=10;
}
glutPostRedisplay();
break;
default:
break;
}
}
void Reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLsizei)w/(GLsizei)h,1.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-25.0);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,800);
glutCreateWindow("robot");
glutReshapeFunc(&Reshape);
init();
glutDisplayFunc(&display);
glutKeyboardFunc(keyboard);
//glutIdleFunc(&idle);
glutMainLoop();
return 0;
}