2024年5月20日 星期一

SY-BlingBangBang電腦圖學🎬-Week14

   1. 

    step1: 下載 freeglut-MinGW-3.0.0-1.mp.zip 

    step2: 進到資料夾,把檔案 freeglut 拉到桌面

    step3: 點進 freeglut 👉 lib 資料夾

    step4: 複製 libfreeglut.a 並且改名為libglut32.a

    step5: 安裝 OpenCV 要勾 Add PATH (第二個),裝在預設目錄

              👉 Setting-Compiler 裡,要把 OpenCV 的三個設定設好

                  👇 

                  (1) 重開 CodeBlocks

                       👇 原本的檔案在 View 👉 Start Page

                  (2) 在 Search directories 加入2個目錄

                        1-1 Compiler 👉 C:\OpenCV2.1\include

                        1-2 Linker 👉 C:\OpenCV2.1\lib

                  (3) 在 Linker setting 裡,加入 👉 cv210

                                                            👉 cxcore210

                                                            👉 highgui210

    step6: 把上週 week13 的程式碼拿下來

    step7: 複製 week13-3_File_fclose_fprintf_fscanf 到桌面

    step8: 改目錄名 week14-1_angle_motion_save

    step9: 把 .cbp,改檔名 week14-1_angle_motion_save.cbp

    step10: 再用 CodeBlocks 的 File-Open 開啟剛剛的 .cbp 檔

    step11: 在專案名稱上按右鍵,選最下面的 Properties,進去改檔名

    step12: 存檔 File-Save project

    step13: 新增、修改程式碼 👇

                (1) 將 angle[20]={}; 修改成 angleX[10]={};

                                                新加 angleY[10]={};

                (2) 將 Body、Upper、Lower 修改成 glRotatef( angleX[0], 1, 0, 0);

                                                                  glRotatef( angleY[0], 0, 1, 0);

                :angle[] 裡面的數字 X、Y 要一樣

                (3) keyboard 裡的 angle 改成 angleX、angle

                      "r" 👇

                      fscanf(fin, "%f ", &angleX[i]);

                      fscanf(fin, "%f ", &angleY[i]);

                      "s" 👇

                      //rintf("%.1f ", anglex[i]);

                      fprintf(fout, "%.1f ", angleX[i]);

                      fprintf(fout, "%.1f ", angleY[i]);

                (4) motion 👇

                      angleX[angleID] += y - oldY;

                      angleY[angleID] += x - oldX;








2.

    step1: 複製 week14-1_angle_motion_save 到桌面

    step2: 改目錄名 week14-2_alpha_blending

    step3: 把 .cbp,改檔名 week14-2_alpha_blending.cbp

    step4: 開啟 Excel 檔,輸入一些資料,套用公式

              A(時間)、B(alpha 0~1)、C(舊的座標)、D(新的座標)、E(內插結果)

              alpha 那格,先打入 = 等於的英文符號,再打左邊的座標 / 20

              👉 =A2/20

              內插結果

              👉 =D2*B2+C2*(1-B2)




    step5: 開啟複製、修改好的專案,新增、修改程式碼 👇

              void timer(int t)

              {

                  printf("你呼叫了 timer(%d)\n", t);

              }

              int main(int argc, char* argv[])

              {

                  printf("程式開始執行\n");

                  glutInit(&argc, argv);

                  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

                  glutCreateWindow("week14-2_alpha_blending");

                  glutDisplayFunc(display);

                  glutIdleFunc(display); ///加入旋轉

                  glutKeyboardFunc(keyboard);

                  glutMouseFunc(mouse);

                  glutMotionFunc(motion);

                  glutTimerFunc(1000, timer, 1);

                  glutTimerFunc(2000, timer, 2);

                  glutTimerFunc(3000, timer, 3);

                  ...

              }





               void timer(int t)

              {

                  glutTimerFunc(1000, timer, t+1);

                  printf("你呼叫了 timer(%d)\n", t);

              }

              int main(int argc, char* argv[])

              {

                  printf("程式開始執行\n");

                  glutInit(&argc, argv);

                  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

                  glutCreateWindow("week14-2_alpha_blending");

                  glutDisplayFunc(display);

                  glutIdleFunc(display); ///加入旋轉

                  glutKeyboardFunc(keyboard);

                  glutMouseFunc(mouse);

                  glutMotionFunc(motion);

                  glutTimerFunc( 0, timer, 0);

                  ///計時器,2000ms(2秒)後,會呼叫timer(t)的函式

                  ...

              }




    step6: 新增程式碼 👇

              (1) void timer(int t); ///函式的形狀的宣告

                    void keyboard(unsigned char key, int x, int y){

                        if(key=='p'){ ///播放

                            glutTimerFunc(0, timer, 0);

                        }

              (2) 再 main 註解 👇

                    ///glutTimerFunc( 0, timer, 0);

              (3) keyboard 數值修改

                    if(key=='r'){ ///讀取 👇

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

                          for(int i=0; i<10; i++){

                              fscanf(fin, "%f ", &angleX[i]);

                              fscanf(fin, "%f ", &angleY[i]);

                          }

                          glutPostRedisplay();

                      }

                      if(key=='s'){ ///存檔

                              if(fout==NULL) fout = fopen("angle.txt", "w+");

                              for(int i=0; i<10; i++){

                                  printf("%.1f ", angleX[i]);

                                  printf("%.1f ", angleY[i]);

                                  fprintf(fout, "%.1f ", angleX[i]);

                                  fprintf(fout, "%.1f ", angleY[i]);

                              }

                              printf("\n");

                              fprintf(fout, "\n");

                      }





    step7: 修改 timer 讓機器人自己動

                float oldAngleX[10] = {};

                float oldAngleY[10] = {};

                float newAngleX[10] = {};

                float newAngleY[10] = {};

                void timer(int t)

                {

                    glutTimerFunc(50, timer, t+1);

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

                    if(t%20==0){

                        for(int i=0; i<10; i++){

                            oldAngleX[i] = newAngleX[i];

                            oldAngleY[i] = newAngleY[i];

                            fscanf(fin, "%f", &newAngleX[i]);

                            fscanf(fin, "%f", &newAngleY[i]);

                        }

                    }

                    float alpha = (t%20 / 20.0);

                    for(int i=0; i<10; i++){

                        angleX[i] = alpha * newAngleX[i] + (1-alpha) * oldAngleX[i];

                        angleY[i] = alpha * newAngleY[i] + (1-alpha) * oldAngleY[i];

                        printf("%.1f %.1f ", angleX[i], angleY[i]);

                    }

                    glutPostRedisplay();

                    printf("你呼叫了 timer(%d)\n", t);

                }












沒有留言:

張貼留言