2014年5月29日 星期四

Week15 課堂作業_01161160劉景晟

#include<stdio.h>
#include<GL/glut.h>
int angleID=0;
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,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();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void readNext(){
if(fin==NULL)fin=fopen("this_is_my_saved_angles.txt","r");
a+=0.1;
for(int i=0;i<20;i++)angle[i]=angleOld[i]*(1-a) + angleNew[i]*a;
if(a>=1){
a=0;
for(int i=0;i<20;i++){
angleOld[i]=angleNew[i];
fscanf(fin,"%f",&angleNew[i]);
}
}
}
void timer(int t)
{
glutTimerFunc(100,timer,0);
readNext();
glutPostRedisplay();
}

void keyboard(unsigned char key, int x,int y)
{
if(key=='0'){
angleID=0;
}
else if(key=='1'){
angleID=1;
}
else if(key=='2'){
angleID=2;
}
else if(key=='s'){
if(fout==NULL)fout=fopen("this_is_my_saved_angles.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%f ",angle[i]);
fprintf(fout,"\n");
}
else if(key=='r'){
readNext();
glutPostRedisplay();
}
else if(key=='p'){
glutTimerFunc(0,timer,0);
}
}
float oldX=0,oldY=0;
void mouse(int button,int state, int x,int y){
oldX=x;oldY=y;
}
void motion(int x, int y){
angle[angleID]+=(x-oldX); oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
讀取檔案讓他照方式動作

沒有留言:

張貼留言