///把 Week15_file 的 main.cpp 拿來用
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05 本來只有1個角度,現在有很多個,初始為0
int angleID=0; ///Step05 現在要改第幾個角度!!!
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;
void timer( int t ){///Step01-2 不用timer動,改keyboard動
glutTimerFunc( 30,timer,t+1);
if (fin==NULL) fin = fopen("angle.txt","r");
for(int i=0;i<20;i++) fscanf( fin,"%f", & angle[i]);
glutPostRedisplay();///程式搬家,改用mouse motion來做動作
}
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
}
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();
}
- 使用nopepad++更改目錄,改成小數點

----------------------------------------------------------------
最後一步做到一半
///把新專案 week16 的程式刪掉
///再把 Week15_file 的 main.cpp 拿來用
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05 本來只有1個角度,現在有很多個,初始為0
float angleOld[20]={},angleNew[20]={};
int angleID=0; ///Step05 現在要改第幾個角度!!!
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;
void timer( int t ){///Step01-2 不用timer動,改keyboard動
glutTimerFunc( 10,timer,t+1);
if( t%10==0){
if (fin==NULL) fin = fopen("angle.txt","r");
for(int i=0;i<20;i++) fscanf( fin,"%f", & angle[i]);
glutPostRedisplay();
}
// if (fin==NULL) fin = fopen("angle.txt","r");
// for(int i=0;i<20;i++) fscanf( fin,"%f", & angle[i]);
// glutPostRedisplay();///程式搬家,改用mouse motion來做動作
}
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'){///存檔 關掉再開
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");
}
if(key=='r'){///讀黨 再開才能讀
if (fin==NULL) fin = fopen("angle.txt","r");
for(int i=0;i<20;i++) fscanf( fin,"%f", & angle[i]);
glutPostRedisplay();///播放動畫 重畫畫面
}
if(key=='p'){///播放利用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();
}
