先複製上周的程式碼
再來修改!
先把timmer內的搬到motion
再新增mouse讓系統知道滑鼠的位置
程式碼:#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
int oldx=0;
void timer(int t)
{
}
void mouse(int button,int state,int x,int y)
{
oldx=x;
}
void motion(int x,int y)
{
angle[angleID] += x-oldx;
oldx=x;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') angleID=1;
if(key=='0') angleID=0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot( 0.3 );///身體
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPopMatrix();
glopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week16");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutTimerFunc(0,timer,0);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
float angle[20]={},diff=2;
int angleID=0;
int oldx=0;
void timer(int t)
{
}
void mouse(int button,int state,int x,int y)
{
oldx=x;
}
void motion(int x,int y)
{
angle[angleID] += x-oldx;
oldx=x;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1') angleID=1;
if(key=='0') angleID=0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot( 0.3 );///身體
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手肘(重疊了)
glPopMatrix();
glopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week16");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutTimerFunc(0,timer,0);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
錄影片
新增
FILE*fout=NULL;
且在motion內加上
if(fout=NULL)fout=fopen("angle.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"#.lf",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.lf",angle[i]);
printf("\n");
這時候bin資料中會有angle.txt(內容與小窗一樣)
之後就是要把錄好的撥出來
#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 ){
glutTimerFunc(30,timer,t+1);///step-02播放動畫
if(fin==NULL)fin=fopen("angle.txt","r");
for(int i=0;i<20;i++)fscanf(fin,"%f",&angle[i]);
glutPostRedisplay();///step-02播放動畫 重劃畫面
}
void mouse(int button,int state,int x,int y)
{
oldX=x;///當mouse按下去 記下位置
}
void motion(int x,int y)
{
angle[angleID] += x-oldX; ///用x-oldX
oldX=x;///在更新位置
glutPostRedisplay();///現在這個比較好,
if(fout==NULL) fout=fopen("angle.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%.1f",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.1f",angle[i]);
printf("\n");///以上兩行讓你知道印了什麼
}
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();
}
#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 ){
glutTimerFunc(30,timer,t+1);///step-02播放動畫
if(fin==NULL)fin=fopen("angle.txt","r");
for(int i=0;i<20;i++)fscanf(fin,"%f",&angle[i]);
glutPostRedisplay();///step-02播放動畫 重劃畫面
}
void mouse(int button,int state,int x,int y)
{
oldX=x;///當mouse按下去 記下位置
}
void motion(int x,int y)
{
angle[angleID] += x-oldX; ///用x-oldX
oldX=x;///在更新位置
glutPostRedisplay();///現在這個比較好,
if(fout==NULL) fout=fopen("angle.txt","w+");
for(int i=0;i<20;i++)fprintf(fout,"%.1f",angle[i]);
fprintf(fout,"\n");
for(int i=0;i<20;i++)printf("%.1f",angle[i]);
printf("\n");///以上兩行讓你知道印了什麼
}
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();
}
最後拖到了一點時間
下周繼續
#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();
}




沒有留言:
張貼留言