///把新專案 week16 的程式刪掉
///再把 Week15_file 的 main.cpp 拿來用
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///angle[i]畫的角度,內插 angleOld[i] angleNew[i]
float angleOld[20]={}, angleNew[20]={};///Step03-2 新舊角度準備內插
int angleID=0; ///Step05 現在要改第幾個角度!!!
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;///Step02-2 播放動畫,檔案指標fin
void timer( int t ){
glutTimerFunc( 30, timer, t+1 );///Step02-2 播放動畫
if( t%30==0 ){///Step03-2 每10frame,就讀一筆新資料
for(int i=0; i<20; i++) angleOld[i] = angleNew[i];///Step03-2
///讀新資料之前,要先把資料備份^^^^^^^^^^^^^^^^^^
if( fin==NULL ) fin = fopen( "angle.txt", "r" );///Step03-2
for(int i=0; i<20; i++) fscanf( fin, "%f", & angleNew[i] );///Step03-2
}///再讀入新的資料 angleNew[i] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
float alpha = (t%30)/30.0;
for(int i=0; i<20; i++){
angle[i] = alpha*angleNew[i] + (1-alpha)*angleOld[i];
}
glutPostRedisplay();///Step02-2 播放動畫 重畫畫面
}
void mouse(int button, int state, int x, int y){
oldX = x;///當mouse按下去時,記下位置
}
void motion(int x, int y){///當我motion動
angle[angleID] += x - oldX;///用x-oldX
oldX = x;///再更新位置
glutPostRedisplay();///現在這個比較好, 以前會用 display()重畫
// if(fout==NULL) fout=fopen("angle.txt", "w+");
// for(int i=0; i<20; i++) fprintf( fout, "%.1f ", angle[i]);
// fprintf( fout, "\n");///以上2行,寫到檔案
// for(int i=0; i<20; i++) printf( "%.1f ", angle[i]);
// printf( "\n");///以上2行,印到畫面 (讓你知道印了什麼資料)
}
void keyboard( unsigned char key, int x, int y){ ///Step05
if(key=='0') angleID=0; ///Step05
if(key=='1') angleID=1; ///Step05
if(key=='2') angleID=2; ///Step05
if(key=='3') angleID=3; ///Step05
if(key=='s'){///save存檔, 把所有的動作存檔後,要關掉
if(fout==NULL) fout=fopen("angle.txt", "w+");
for(int i=0; i<20; i++) fprintf( fout, "%.1f ", angle[i]);
fprintf( fout, "\n");///以上2行,寫到檔案
for(int i=0; i<20; i++) printf( "%.1f ", angle[i]);
printf( "\n");///以上2行,印到畫面 (讓你知道印了什麼資料)
}
if(key=='r'){///read讀檔, 再開之後,才能讀檔
if( fin==NULL ) fin = fopen( "angle.txt", "r" );///Step02-2
for(int i=0; i<20; i++) fscanf( fin, "%f", & angle[i] );///Step02-2
glutPostRedisplay();///Step02-2 播放動畫 重畫畫面
}
if(key=='p'){///play利用timer整個播放, 再開之後,才能讀檔
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///左半邊
glTranslatef(-0.3,0,0);///(3)掛在正確的地方
glRotatef(angle[0], 0,0,1);///(2)旋轉 ///Step05
glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
glPushMatrix();
glTranslatef(-0.3,0,0);///(3)掛在正確的地方
glRotatef(angle[1], 0,0,1);///(2)旋轉 ///Step05
glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPopMatrix();
glPopMatrix();
glPushMatrix();///右半邊
glTranslatef(+0.3, 0,0 );
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3, 0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();
glTranslatef(+0.3, 0,0 );
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3, 0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15 file");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
glutDisplayFunc(display);
glutMainLoop();
}