2023年5月11日 星期四

shen_week13~~今天好熱

用 git clone將自己2023graphicsb的檔案載下來

剛剛老師講的有點快來不及打步驟

然後將freeglut從老師給我們的資料夾
拉進去我們的專案檔
一樣要複製

然後使用第8周glm的兩個檔案
加入下面的程式碼
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * lefthand = NULL;
GLMmodel * righthand = NULL;


 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
       head=glmReadOBJ("model/head.obj");
       body=glmReadOBJ("model/body.obj");
       lefthand=glmReadOBJ("model/lefthand.obj");
       righthand=glmReadOBJ("model/righthand.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glmDraw(head, GLM_MATERIAL);
        glmDraw(body, GLM_MATERIAL);
        glmDraw(lefthand, GLM_MATERIAL);
        glmDraw(righthand, GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();


更改程式碼
void keyboard(unsigned char key, int x, int y){
    if(key=='0') show[0]= ! show [0];
    if(key=='1') show[1]= ! show [1];
    if(key=='2') show[2]= ! show [2];
    if(key=='3') show[3]= ! show [3];
    glutPostRedisplay();
}
下面改這樣
 if (show[0]) glmDraw(head, GLM_MATERIAL);
 if (show[1])glmDraw(body, GLM_MATERIAL);
 if (show[2])glmDraw(lefthand, GLM_MATERIAL);
 if (show[3])glmDraw(righthand, GLM_MATERIAL);

利用打數字顯示不同部位~~


接著要增加滑鼠移動的功能
glPushMatrix();
            glTranslatef(teapotX,teapotY,0);
            if (show[0]) glmDraw(head, GLM_MATERIAL);
        glPopMatrix();
然後加
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN){
    oldX = x;
    oldY = y;
    }
}
void motion(int x, int y){
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
最後 加int main
glutMotionFunc(motion);
就完成了

沒有留言:

張貼留言