2023年5月11日 星期四

LAD Week13_綜合練習

 Week13-1 檔案下載/設定

git下載github資料夾,將compler跟link絕對路徑修改成相對路徑,後續就不須下載freeglut



將freeglut拉入資料夾即可啟動程式,設定完成!



備份上傳
- git add .
- git config --global user.email mme99133@
- git config --global user.name ChungYuZ
- git commit -m week12
- git push



組模型

裁切模型各部位
複製各週已做過的模型檔案,修改程式碼

加入按鍵操控

#include <stdio.h> ///要檔案的Input/Output
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head=NULL;
GLMmodel * body=NULL;
GLMmodel * Rshoulder=NULL;
GLMmodel * Rarm=NULL;
int show[4]={1,0,0,0};
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;///step02-1
FILE * fin = NULL;///step02-2
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();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head=glmReadOBJ("model/head.obj");
        body=glmReadOBJ("model/body.obj");
        Rshoulder=glmReadOBJ("model/Rshoulder.obj");
        Rarm=glmReadOBJ("model/Rarm.obj");
    }
    glPushMatrix();
        glScalef(0.1,0.1,0.1);
        if(show[0])glmDraw(head,GLM_MATERIAL);
        if(show[1])glmDraw(body,GLM_MATERIAL);
        if(show[2])glmDraw(Rshoulder,GLM_MATERIAL);
        if(show[3])glmDraw(Rarm,GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if(state==GLUT_DOWN){ ///如果mouse按下去
        if(fout==NULL) fout = fopen("file4.txt", "w");

        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
//void keyboard(unsigned char key, int x, int y)///step02-2
//{
//    if(fin==NULL){
//       fclose(fout);///step02-2
//        fin = fopen("file4.txt", "r");///step02-2
//    }
//    fscanf(fin, "%f%f", &teapotX, &teapotY);///step02-2
//    display();///step02-2
//}///step02-2
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard); ///step02-2
    glutMouseFunc(mouse); ///step02-1

    glutMainLoop();
}
使其可用數字鍵操控,顯示隱藏部位

雲端備份

滑鼠控制

#include <stdio.h> ///要檔案的Input/Output
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head=NULL;
GLMmodel * body=NULL;
GLMmodel * Rshoulder=NULL;
GLMmodel * Rarm=NULL;
int show[4]={1,0,0,0};
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;///step02-1
FILE * fin = NULL;///step02-2
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();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head=glmReadOBJ("model/head.obj");
        body=glmReadOBJ("model/body.obj");
        Rshoulder=glmReadOBJ("model/Rshoulder.obj");
        Rarm=glmReadOBJ("model/Rarm.obj");
    }
        glPushMatrix();
            glTranslatef(teapotX,teapotY,0);
            if(show[0])glmDraw(head,GLM_MATERIAL);
        glPopMatrix();
        if(show[1])glmDraw(body,GLM_MATERIAL);
        if(show[2])glmDraw(Rshoulder,GLM_MATERIAL);
        if(show[3])glmDraw(Rarm,GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
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(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13");

    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard); ///step02-2
    glutMouseFunc(mouse); ///step02-1

    glutMainLoop();
}
















沒有留言:

張貼留言