***( upX,upY,upZ : 食指指的方向 )***
float eyeX=0.3,eyeY=0.2,eyeZ=0.4,objX=0,objY=0,objZ=0,upX=0,upY=1,upZ=0;gluLookAt(eyeX,eyeY,eyeZ,objX,objY,objZ,upX,upY,upZ);
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float angleX =0, angle1=0, angle2=0, angle3=0, angle4=0;
float eyeX=0.3,eyeY=0.2,eyeZ=0.4,objX=0,objY=0,objZ=0,upX=0,upY=1,upZ=0;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='8') angle1+=5;
if(key=='5') angle1-=5;
if(key=='4') angleX+=5;
if(key=='6') angleX-=5;
if(key=='7') angle2+=10;
if(key=='9') angle2-=10;
if(key=='1') angle3+=5;
if(key=='3') angle3-=5;
if(key==' '){
eyeX-=0.02;
eyeY-=0.02;
}
else if(key=='t'){
glutTimerFunc(0, timerRotateShow, 0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
float color=0;
void timerRotateShow(int t)
{
if(t%30==0) glClearColor((rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0);
glutTimerFunc(30, timerRotateShow, t+1);
eyeX=cos(t/100.0*3.1415);
eyeZ=sin(t/100.0*3.1415);
eyeY-=0.005;
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);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX,eyeY,eyeZ,objX,objY,objZ-0.5,upX,upY,upZ);
glTranslatef(0, 0.3 ,0);
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.2);
glPushMatrix();//頭
glTranslatef(0, 0.1 ,0);
glRotatef(angle1, 1, 0, 0);
glTranslatef(0, 0.2 ,0);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPushMatrix();//右肩
glTranslatef(0.35, 0 ,0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();//右臂
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.2, 0, 0);
glScalef(3.5, 0.8, 1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.35, 0 ,0);
glutSolidSphere(0.2, 30, 30);
glPushMatrix();//左臂
glRotatef(-angle2, 0, 0, 1);
glTranslatef(-0.2, 0 ,0);
glScalef(3.5 , 0.8, 1);
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();//身體
glTranslatef(0, -0.3 ,0);
glScalef(1 , 1.5, 1);
glutSolidCube(0.2);
glPopMatrix();
glPushMatrix();//屁股
glTranslatef(0, -0.4 ,0);
glPushMatrix();
glScalef(1, 0.8, 0.8);
glutSolidSphere(0.2, 30, 30);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右腿
glTranslatef(0.1, -0.4 ,0);
glRotatef(angle3, 1, 0, 0);
glTranslatef(0, -0.2 ,0);
glPushMatrix();
glScalef(0.15 , 0.4, 0.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//右腳
glTranslatef(0, -0.3 ,0);
glRotatef(-angle3/1.5, 1, 0, 0);
glTranslatef(0, -0.1 ,0);
glScalef(0.13 , 0.5, 0.15);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左腿
glTranslatef(-0.1, -0.4 ,0);
glRotatef(-angle3 , 1, 0, 0);
glTranslatef(0 , -0.2 ,0);
glPushMatrix();
glScalef(0.15 , 0.4, 0.2);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//左腳
glTranslatef(0, -0.3 ,0);
glRotatef(angle3/1.5, 1, 0, 0);
glTranslatef(0, -0.1 ,0);
glScalef(0.13 , 0.5, 0.15);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hw7");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2, -2,2, -10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
沒有留言:
張貼留言