2024年5月16日 星期四

電腦圖學 Week13 🥴

week13-1_
glm_gundam_keyboard_angleID_mouse_motion_angle

1. moodle下載 freeglut 拖到桌面

2. 把桌面\freglut\lib\libfreeglut.a 複製成 libglut32.a

3. 安裝 OpenCV 2.1

- 安裝第三步驟, Add OpenCV to system PATH(勾第2或3個, 不要勾第1個)

- 目錄不要改,要用預設的

- 要重開CodeBlocks

4. CodeBlocks 的 settings>compiler 加入 opencv 的3個設定

- Search directories > Compiler 要加上 C:\OpenCV2.1\include

- Search directories > Linker 要加上 C:\OpenCV2.1\lib

- Linker settings 要加上 cv210 / cxcore210 / highgui210

5. 安裝git, 把github上之前寫過的程式複製到桌面

- cd desktop

- git clone https://github.com/xiyu-emma/2024graphicsa

6. 把week12-3 複製成week13-1_glm_gundam_keyboard_angleID_mouse_motion_angle, cbp檔用Notepad++把12-3都改成13-1

7. 開啟week13-1.cbp

8. 把float angle = 0, da=1; 改成 float angle[20]= {}; int angleID = 0;

9. 修改motion函式
void motion(int x, int y){
    angle[angleID]=y;
    glutPostRedisplay();///要重畫畫面
}

10. 增加一個keyboard函式
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;
}

11. 在main函式新增一行
glutKeyboardFunc(keyboard);

12. 修改angle的程式碼
glPushMatrix();
    ///glTranslatef();掛在肩上
    glRotatef(angle[0], 0, 0, 1);
    glTranslatef(0, -0.07, 0);
    drawUpperA();
    glPushMatrix();
        glTranslatef(-0.02, -0.09, 0);
        glRotatef(angle[1], 1, 0, 0);
        glTranslatef(0, -0.21, 0);
        drawLowerA();
    glPopMatrix();
glPopMatrix();

13. 鍵盤的0跟1控制要轉動哪關節, 滑鼠控制旋轉角度

14. 新增一行程式
int oldX=0, oldY=0;

15. 新增mouse函式
void mouse(int button, int state, int x, int y)
{
    oldX = x;
    oldY = y;
}

16. 修改motion函式
void motion(int x, int y){
    angle[angleID] += y - oldY;
    oldX = x;
    oldY = y;
    glutPostRedisplay();///要重畫畫面
}

17. 在main函式新增一行程式
glutMouseFunc(mouse);

18. 最後滑鼠改變角度時才不會從0開始

week13-2_FILE_fopen_fprintf_fscanf

1. 把week13-1複製成week13-2, cbp檔用Notepad++把13-1都改成13-2

2. 開啟week12-3.cbp

3. 加上motion()前面加上兩個指標
FILE * fin = NULL;
FILE * fout = NULL;

4. 在motion()加上一段程式
if(fout==NULL) fout = fopen("angle.txt", "w+");
for(int i=0; i<20; i++){
    print("%.1f ", angle[i]);
    fprint(fout, "%.1f ", angle[i])
}

5. 執行時, 滑鼠一邊改變角度, 一邊產生大量的角度值。小黑關掉時, 在目錄裡可以看到angle.txt裡面有剛剛的數值

6. keyboard()裡加上一段程式, 執行時按r會讀取txt檔裡的內容, 手臂會自己旋轉
if(key=='r'){
    if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0; i<20; i++){
        fscanf(fin, "%f", & angle[i]);
     }
    glutPostRedisplay();
}

7. 修改 pmodel() 變成 drawmodel
void drawBody(void)
{
    if (!body) {
body = glmReadOBJ("data/body.obj");
if (!body) exit(0);
glmUnitize(body);
glmFacetNormals(body);
glmVertexNormals(body, 90.0);
    }
    glmDraw(body, GLM_SMOOTH | GLM_TEXTURE);
}

8. 新增一個指標
GLMmodel * body = NULL;

9. display()增加一行drawBody();

10. 把手移動到肩上

11. 把模型檔放到目錄下

12. 新增其他關節

沒有留言:

張貼留言