2014年6月12日 星期四

運鏡

#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float eyeX=-0.3, eyeY=1.2, eyeZ=0.4, objX=-0.2, objY=-0.5, objZ=-0.2, UpX=0, UpY=1, UpZ=0;
float angleX =0,angleA=0,angleY=0;
float thesize=0.2;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5,angleX+=5,angleY=(1);
if(key=='2') angleA-=5,angleX-=5,angleY=0;
if(key==' '){
objX+=0.02;
}
if(key=='0'){
glutTimerFunc(0, timerRotateShow,0);
}


glutPostRedisplay();
}
void timerRotateShow(int t)
{
if(t%10==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;
thesize+=0.01;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.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, UpX, UpY, UpZ);
glScalef(thesize,thesize,thesize);
glPushMatrix();//Eyes
glRotatef(angleX, 0,0.1,0);
glColor3f(0,0,0);
glTranslatef(0.1,0.3,-0.25);
glutSolidSphere(0.02,10,10);
glPushMatrix();
glTranslatef(-0.2,0,0);
glutSolidSphere(0.02,10,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.68, 0);
glRotatef(angleA, -0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.68, 0);
glRotatef(angleA, 0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myLight();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);

glutMainLoop();
}

沒有留言:

張貼留言