float angleX=0,angle1=0,X,Y,angle2=0,angle3=0;
float eyeX=0.1, eyeY=0.4, eyeZ=-0.5, objX=0, objY=0, objZ=0, upX=0, upY=1,upZ=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX,eyeY, eyeZ, objX, objY, objZ, upX, upY,upZ);
glColor3f(0.8,0.05,0);
glTranslatef(X,Y+0.25,0);
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.3);
glPushMatrix(); //頭
glColor3f(1,0.8,1);
glTranslatef(0,0,0);
glRotatef(angle1, 0,0,1);
glTranslatef(0,0.38,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix(); //右球
glColor3f(1,1,0);
glTranslatef(0.4,0,0);
glutSolidSphere(0.17,30,30);
glPushMatrix(); //右手
glColor3f(0,0.8,1);
glRotatef(angle1, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(2,0.7,0.7);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix(); //左球
glColor3f(1,1,0);
glTranslatef(-0.4,0,0);
glutSolidSphere(0.17,30,30);
glPushMatrix(); //左手
glColor3f(0,0.8,1);
glRotatef(-angle1, 0,0,1);
glTranslatef(-0.3,0,0);
glScalef(2,0.7,0.7);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix(); //身體
glColor3f(0.9,0,0.5);
glTranslatef(0,-0.32,0);
glScalef(1,1,0.5);
glutSolidCube(0.35);
glPopMatrix();
glPushMatrix(); //左下球
glColor3f(1,1,0);
glTranslatef(-0.15,-0.45,0);
glutSolidSphere(0.15,30,30);
glPushMatrix(); //左腳
glColor3f(0,0.8,1);
glRotatef(-angle2, 1,0,0);
glTranslatef(-0.01,-0.25,0);
glScalef(0.8,1.5,0.8);
glutSolidCube(0.2);
glPushMatrix(); //左下腳
glColor3f(0,0.8,0.1);
glTranslatef(0,0,0);
glRotatef(-angle2, 1,0,0);
glTranslatef(-0.01,-0.15,0);
glScalef(0.125,0.2,0.125);
glutSolidCube(1);
glPushMatrix(); //左腳掌
glColor3f(1,0.5,0);
glTranslatef(0,-0.5,0.01);
glScalef(1.15,0.3,1.15);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix(); //右下球
glColor3f(1,1,0);
glTranslatef(0.15,-0.45,0);
glutSolidSphere(0.15,30,30);
glPushMatrix(); //右腳
glColor3f(0,0.8,1);
glRotatef(angle2, 1,0,0);
glTranslatef(0.005,-0.25,0);
glScalef(0.8,1.5,0.8);
glutSolidCube(0.2);
glPushMatrix(); //右下腳
glColor3f(0,0.8,0.1);
glTranslatef(0,0,0);
glRotatef(angle2, 1,0,0);
glTranslatef(0.01,-0.15,0);
glScalef(0.115,0.2,0.115);
glutSolidCube(1);
glPushMatrix(); //右腳掌
glColor3f(1,0.5,0);
glTranslatef(0,-0.5,0.01);
glScalef(1.25,0.3,1.5);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='d') angleX-=3;
if(key=='q') angle1+=3;
if(key=='e') angle1-=3;
if(key=='z') angle2+=12;
if(key=='c') angle2-=12;
if(key=='1') angle3+=3;
if(key=='3') angle3-=3;
if(key=='8') Y+=0.1;
if(key=='5') Y-=0.1;
if(key=='4') X-=0.1;
if(key=='6') X+=0.1;
if(key==' ') eyeX-=0.02;
if(key=='g')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -2, 2);
glMatrixMode(GL_MODELVIEW);
}
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
沒有留言:
張貼留言