2021年6月7日 星期一

Week_16 2021.06.07

1. 複習上週程式碼

2. 用鍵盤控制

1. 開新專案  複製上週程式碼
2. 增加陣列

 float angle[20]={} , diff = 2;   ///angle 都是 0的陣列
int angleID = 0;   ///計算現在是第幾個angle
void timer( int t )
{
    glutTimerFunc(30 , timer , t+1 );
    angle[angleID] += diff;
    if( angle[angleID] > 90 ) diff = -2;
    if( angle[angleID] < 0 ) diff = +2;
    glutPostRedisplay();   ///有空就redisplay
}

 3. 增加 keyboard  

 void keyboard( unsigned char key , int x , int y )
{
    if( key=='0' ) angleID=0;   ///timer 會改變關節angle[0]
    if( key=='1' ) angleID=1;   ///timer 會改變關節angle[1]
    if( key=='2' ) angleID=2;   ///timer 會改變關節angle[2]
    if( key=='3' ) angleID=3;   ///timer 會改變關節angle[3]
}

 4. 記得在main 加上 glutKeyboardFunc( keyboard ) ;
 5. 其他 glPushMatrix(); 裡面的 angle 也要改喔
 6. 成功!

3. 加上滑鼠控制

1. 把 timer 先休息
2.  加上 mouse 

int oldX=0 ,oldY=0;
///mouse按下去會記得位置
void mouse( int button, int state , int x, int y)
{
    oldX = x;
    oldY = y;

3. 加上 motion

 void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX = x;
    glutPostRedisplay();   ///display
}

4. main 裡面也要程式喔

glutMouseFunc(mouse); 

glutMotionFunc(motion); 

5. 成功!

 4. 存檔

1. 在 keyboard 加上指標

FILE * fout = NULL; 

2. 在 keyboard 增加程式碼

if( key=='s' )   ///按s 會存檔
        {
            if( fout == NULL) fout = fopen("angle.txt","w+");   ///如果NULL 就fopen
            ///會存在小黑
            for( int i=0; i<20; i++)   printf("%.2f", angle[i]);
            printf("\n");
            ///會存在txt檔
            for( int i=0; i<20; i++)   fprintf(fout,"%.2f", angle[i]);
            fprintf(fout,"\n");
        }

 3. 成功!

5. 讀檔

1. 在 keyboard 加上指標

FILE * fin = NULL;

2. 在 keyboard 增加程式碼

 
if( key=='r')   ///讀進來
    {
        if( fin == NULL )fin = fopen("angle.txt","r");
        for( int i=0; i<20; i++)   fscanf(fin,"%f", &angle[i]);
            glutPostRedisplay();
    }

3. 可是好像不會讀欸...?

!!!必須關閉fout 或程式關閉 才會更新 angle.txt 

4. 成功!! 

 

6. 改檔案位置

1. 用 Notepad++ 改成 "."
2. 回到 Code Blocks 會要Reload
3. 複製 freeglut.dll 到專案資料夾
4. 成功!

7. 可以邊 motion 邊存檔嗎...!

1. 複製程式碼到 motion

if( fout == NULL) fout = fopen("angle.txt","w+");   ///如果NULL 就fopen
            ///會存在小黑
            for( int i=0; i<20; i++)   printf("%.2f", angle[i]);
            printf("\n");
            ///會存在txt檔
            for( int i=0; i<20; i++)   fprintf(fout,"%.2f", angle[i]);
            fprintf(fout,"\n"); 

2. 成功!! 


 3. 完整程式碼

github   
---------------------------------------------------------------------------------------------------  

 

沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...