2014年5月29日 星期四

01160235_謝采玲, HW15, Week15





#include <stdio.h>
#include <GL/glut.h>
int angleID=0;
float a=1;
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]={0, 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(){
printf("here! %f \n", angle[0]);
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){
printf("here load file !\n");
a=0;
for(int i=0;i<20;i++){
angleOld[i]=angleNew[i];
fscanf(fin, "%f ", &angleNew[i]);
printf(" %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=1;
}
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();
}

***檔名要正確!!!
***記得加咒語!!!

沒有留言:

張貼留言