2014年5月30日 星期五

神轉茶壺

#include <GL/glut.h>
float a=0;
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(angle[3], angle[4], angle[5]);
glPushMatrix();
   glColor3f(1,1,0);
   glRotatef(angle[0],0,0,1);
   glutSolidTeapot(0.3);
  glPopMatrix();
  glPushMatrix();
   glTranslatef(0.5,0,0);
   glRotatef(angle[1],0,0,1);
   glutSolidTeapot(0.3);
  glPopMatrix();
 glPopMatrix();
 glutSwapBuffers();
}

void readNext()
{
 a+=0.1;
 for(int i=0;i<20;i++) angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
}

void timer(int t)
{
 glutTimerFunc(50, timer, 0);
 readNext();
 glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
 if(key=='r'){
  readNext();
 }
 else if(key='p'){
  glutTimerFunc(0, timer, 0);
 }
 glutPostRedisplay();
}

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

 glutKeyboardFunc(keyboard);
 glutDisplayFunc(display);
 glutMainLoop();
}

沒有留言:

張貼留言