2021年5月5日 星期三

小乖的圖學筆記 week11

 1. Moodle下載範例 source.zip windows.zip, data.zip

  windows.zip 解壓縮至 下載\windows\Transformation.exe

  data.zip 解壓縮至 下載\windows\data\模型

2.打開GLUT專案(自己去week01)

3.將glm.h和glm.c放同目錄 (week11-1專案目錄)

glm.c 改檔名成 glm.cpp

transformation.c 內容放到 main.cpp

4.把 glm.cpp 放入專案(左邊Project>Add檔案>glm.cpp加入)

5.把 data.zip 裡的檔案放入執行目錄

C:\Users\Administrator\Desktop\freeglut\bin

6.程式碼執行如下

7.將main.cpp裡的程式碼刪掉至剩下

drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/Al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}

並且從GitHub裡下載並複製

void display()以及int main()

修改成以下

#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel=NULL;
drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/Al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(180,0,1,0);
        drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GL_DEPTH_BUFFER_BIT);
    glutCreateWindow("¤p¨Äªº3D¼Ò«¬");
    glutDisplayFunc(display);
    glutMainLoop();
}

8.程式碼執行如下

9.加入打光程式碼兩段並翻轉模型

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

加在void display()以及int main()之間

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

加在int main()裡的glutDisplayFunc(display);以及glutCreateWindow(" ");之間

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   
glPushMatrix();
        glRotatef(180,0,1,0);

        drawmodel();
    glPopMatrix();
    glutSwapBuffers();
}

加上這段已翻轉模型

10.程式碼執行如下

11.安裝OpenCV並重開(教學在week08)

12.開啟老師給的gundam中的cbp檔並執行


沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...