顯示具有 week16 標籤的文章。 顯示所有文章
顯示具有 week16 標籤的文章。 顯示所有文章

2021年6月25日 星期五

距地表面160 Week16

  •  先用上一周的開始加新的東西


///把 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();
}

2021年6月9日 星期三

嘿嘿嘿(⊙v⊙)  Week16

 第一步( 不要忘了glutTimerFunc(0, timer, 0); )





















————————————————————————————————————————

第二步( fprintf(fout, "%.1f", angle[i]); )




















————————————————————————————————————————

第三步(會播放上一步的動作)













————————————————————————————————————————

第四步
























————————————————————————————————————————

第五步




【】week16 錄製動畫、內插【】

 

將原先的東西剪到motion裡,加上滑鼠,可用滑鼠動左手


將移動的軌跡(用滑鼠)存成txt檔(在freeglut\bin裡),順帶印出來


剛剛移動的軌跡成為自己會動的動畫,改專案執行目錄



按s可以存目前的位置、再重新開啟時按r可以一行一行讀檔


會自動播放原先的檔案(angle.txt裡儲存的位置)

week16

 


複製week15的程式碼
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
int oldX=0;
void timer(int t){
    ///glutTimerFunc(30,timer,t+1);
    
}
void mouse(int button, int state,int x,int y){
    oldX = x;
}
void motion(int x,int y){
    angle[angle ID] += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
void keyboard( unsigned char key, int x, int y){
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
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();
         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);
    glutTimerFunc(0, timer ,0);
    glutDisplayFunc(display);
    glutMainLoop();
}


#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;
int angleID=0; 
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;
void timer( int t ){
    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();
}
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();

    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){ 
    if(key=='0') angleID=0; 
    if(key=='1') angleID=1; 
    if(key=='2') angleID=2; 
    if(key=='3') angleID=3; 
}
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);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(display);
    glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;
int angleID=0;
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;
void timer( int t ){
    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();
}
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();

   // 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){
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    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");
         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'){

    }
}
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);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(display);
    glutMainLoop();
}



阿~一代一代 week16







 

欸幹你看,是電腦圖學欸Week16

 1.把15週的程式拿來用

2.製作要讀入的檔案"angle.txt"

#include <stdio.h>

#include <GL/glut.h>

float angle[20]={},diff=2;

int angleID=0; ///現在要改第幾個角度

int oldx=0;

FILE*fout = NULL;

///FILE*fin = NULL;///檔案指標fin

void timer(int t)

{

    ///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();///播放動畫,重畫畫面

}

void mouse (int button,int state,int x,int y)

{

    oldx=x;///mouse按下去時,記下位置

}

void motion(int x,int y)

{

    angle[angleID]+=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)

{

    if (key == '0') angleID=0;

    if (key == '1') angleID=1;

    if (key == '2') angleID=2;

    if (key == '3') angleID=3;

}

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)旋轉

            glTranslatef(-0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(-0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[1],0,0,1);///(2)旋轉

                    glTranslatef(-0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

        glPushMatrix();///右半邊

            glTranslatef(+0.3,0,0);///(3)掛在正確的地方

            glRotatef(angle[2],0,0,1);///(2)旋轉

            glTranslatef(+0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(+0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[3],0,0,1);///(2)旋轉

                    glTranslatef(+0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

}

int main( int argc, char ** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15 file");


    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutTimerFunc(0,timer,0);

    glutMainLoop();

}


3.讀檔案,讓他自己動起來

#include <stdio.h>

#include <GL/glut.h>

float angle[20]={},diff=2;

int angleID=0; ///現在要改第幾個角度

int oldx=0;

FILE*fout = NULL;

FILE*fin = NULL;///檔案指標fin

void timer(int t)

{

    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();///播放動畫,重畫畫面

}

void mouse (int button,int state,int x,int y)

{

    oldx=x;///mouse按下去時,記下位置

}

void motion(int x,int y)

{

    angle[angleID]+=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)

{

    if (key == '0') angleID=0;

    if (key == '1') angleID=1;

    if (key == '2') angleID=2;

    if (key == '3') angleID=3;

}

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)旋轉

            glTranslatef(-0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(-0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[1],0,0,1);///(2)旋轉

                    glTranslatef(-0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

        glPushMatrix();///右半邊

            glTranslatef(+0.3,0,0);///(3)掛在正確的地方

            glRotatef(angle[2],0,0,1);///(2)旋轉

            glTranslatef(+0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(+0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[3],0,0,1);///(2)旋轉

                    glTranslatef(+0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

}

int main( int argc, char ** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15 file");


    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutTimerFunc(0,timer,0);

    glutMainLoop();

}

4.用按鍵來讀入以及撥放

///把新專案 week16 的程式刪掉

///再把 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;///Step02-2 播放動畫,檔案指標fin

void timer( int t ){

//    glutTimerFunc( 30, timer, t+1 );///Step02-2 播放動畫

//    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 播放動畫 重畫畫面

}

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();

}


5.讓他可以自動撥放之前的存檔

#include <stdio.h>

#include <GL/glut.h>

float angle[20]={},diff=2;

float angleold[20]={},anglenew[20]={};///新舊角度準備內插

int angleID=0; ///現在要改第幾個角度

int oldx=0;

FILE*fout = NULL;

FILE*fin = NULL;///檔案指標fin

void timer(int t)

{

    glutTimerFunc(30, timer,t+1);

    if (t%10==0)

    {

        for (int i=0;i<20;i++) angleold[i]=anglenew[i];

        ///讀新資料之前,先備份

        if (fin==NULL) fin=fopen("angle.txt","r");

        for(int i=0;i<20;i++) fscanf (fin ,"%f",&anglenew[i]);

        ///讀新資料 anglenew[i]^^^^^

    }

    float alpha = (t%10)/10.0;

    for (int i=0;i<20;i++)

    {

        angle[i]=alpha*anglenew[i]+(1-alpha)*angleold[i];

    }

    glutPostRedisplay();///播放動畫,重畫畫面

}

void mouse (int button,int state,int x,int y)

{

    oldx=x;///mouse按下去時,記下位置

}

void motion(int x,int y)

{

    angle[angleID]+=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)

{

    if (key == '0') angleID=0;

    if (key == '1') angleID=1;

    if (key == '2') angleID=2;

    if (key == '3') angleID=3;



    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");

        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')///播放

    {


    }

}

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)旋轉

            glTranslatef(-0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(-0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[1],0,0,1);///(2)旋轉

                    glTranslatef(-0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

        glPushMatrix();///右半邊

            glTranslatef(+0.3,0,0);///(3)掛在正確的地方

            glRotatef(angle[2],0,0,1);///(2)旋轉

            glTranslatef(+0.3,0,0);///(1)把關節放到正中心

            glutSolidTeapot( 0.3 );///左手臂(重疊了)

                glPushMatrix();

                    glTranslatef(+0.3,0,0);///(3)掛在正確的地方

                    glRotatef(angle[3],0,0,1);///(2)旋轉

                    glTranslatef(+0.3,0,0);///(1)把關節放到正中心

                    glutSolidTeapot( 0.3 );///左手臂(重疊了)

                    glutSolidTeapot( 0.3 );///左手肘(重疊了)

                glPopMatrix();

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

}

int main( int argc, char ** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15 file");


    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutTimerFunc(0,timer,0);

    glutMainLoop();

}

week16

 

-利用上禮拜的程式碼
新增void mouse(); void motion();
設x,oldx
動畫按0123並配合滑鼠移動能讓關節移動 
-再motion裡面新增程式碼:


    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");

可以看到關節移動的角度
-播放動畫 FILE * fin = NULL;

再timer裡面新增動畫指令 
-再keyboard()裡面新增save,read,play
利用key==' '; 用鍵盤上的s可以存檔 
儲存位置後再讀檔,並利用鍵盤上的r讀檔
讀檔後就能撥放了


-再timer新增內插 讓動畫更順
設float angleOld[20]={}, angleNew[20]={};
更新angle, 利用alpha 0~1中間進行angle[i]= alpha*angleNew[i]+(1-alpha)*angleOld[i];

程式碼:

#include <stdio.h>

#include <GL/glut.h>
float angle[20]={}, diff=2;
float angleOld[20]={}, angleNew[20]={};
int angleID=0;
int oldx=0;
FILE * fout = NULL;
FILE * fin = NULL;///播放動畫
void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    if(t%30==0){///每10個frame,讀一筆新資料
        for(int i=0;i<20;i++) angleOld[i]=angleNew[i];
        if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0;i<20;i++) fscanf(fin, "%f", &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();
}
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();

//    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 )
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    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");
        for(int i=0;i<20;i++) printf("%.1f", angle[i]);
        printf("\n");

    }
    if(key=='r'){///read
        if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0;i<20;i++) fscanf(fin, "%f",&angle[i]);
        glutPostRedisplay();///重畫畫面
    }
    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);
            glRotated(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotated(angle[0],0,0,1);
                glTranslatef(-0.3,0,0);
                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[2],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("08160661");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}



電腦圖學 week16

 

step01-1把上週week15的程式拿來用,先開新的GLUT專案,把程式貼上week15的main cpp程式

step01-2利用mouse() motion()來讓angle可以動,記得要加入glutMouseFunc()及glutMotionFunc()


step02-1在motion()時,順便寫檔案,也順便印到小黑畫面。記得小墨關掉後,angle txt 檔才會更新到硬碟
///把新專案 week16 的程式刪掉
///再把 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;
void timer( int t ){///Step01-2 不用timer動,改keyboard動
///程式搬家,改用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();
}

step02-2利用timer()來讀檔案,將angle[i]都讀進來,同時glutPostRedisplay()更新畫面,就可以播放動畫了
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;///本來只有一個角度,現在有很多個,初始為0
int angleID=0;///現在要改第幾個角度
int oldX=0;
void timer(int t){

}
void mouse(int button,int state,int x,int y){
    oldX=x;///當mouse按下去時,記下位置
}
void motion(int x,int y){///當我motion動
    angle[angleID]+= x-oldX;///用-oldX
    oldX = x;///再更新位置
    glutPostRedisplay();///現在這個比較好,以前會用display
}
void keyboard(unsigned char key,int x, int y){
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
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)旋轉
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot(0.3);///左手臂
            glPushMatrix();
                 glTranslatef(-0.3,0,0);///(3)掛在正確的地方
                glRotatef(angle[1],0,0,1);///(2)旋轉
                glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
                glutSolidTeapot( 0.3 );///左手臂(重疊了)
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();///右半邊
        glTranslatef(+0.3,0,0);///(3)掛在正確的地方
            glRotatef(-angle[2],0,0,1);///(2)旋轉
            glTranslatef(+0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot(0.3);///右手臂
             glPushMatrix();
                 glTranslatef(+0.3,0,0);///(3)掛在正確的地方
                glRotatef(-angle[3],0,0,1);///(2)旋轉
                glTranslatef(+0.3,0,0);///(1)把關節旋轉中心放到畫面中心
                glutSolidTeapot( 0.3 );///右手臂(重疊了)
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main( int argc, char ** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week16");

    glutKeyboardFunc(keyboard);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

step02-3我們把工作執行目錄working_dir改成小數點,以便在同一目錄工作。

再把 freeglut dll 及 angle txt 也從 freeglut bin 複製到專案目錄裡
///把新專案 week16 的程式刪掉
///再把 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;///Step02-2 播放動畫,檔案指標fin
void timer( int t ){
glutTimerFunc( 30, timer, t+1 );///Step02-2 播放動畫
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 播放動畫 重畫畫面
}
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();
}


step03-1註解掉原本motion()的寫檔、原本timer()的讀檔,改在keyboard()裡利用's'來存檔,'r'來讀檔,末來'p'做播放play,

重開之後按下's',就會出現剛剛的動作
///把新專案 week16 的程式刪掉
///再把 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;///Step02-2 播放動畫,檔案指標fin
void timer( int t ){
// glutTimerFunc( 30, timer, t+1 );///Step02-2 播放動畫
// 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 播放動畫 重畫畫面
}
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();
}


step03-2老師利用timer()來做出動畫的內插,其中
(1)要更新angle[i]再重畫,
(2)利用alpha介於0到1中間,進行alpha乘angleNew[i]+(1-alpha乘angleOld[i],
(3) if(t%10==0)來決定更新新舊的時間
(4)alpha怎麼算
///把新專案 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();
}





距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...