2017年5月2日 星期二

Week11_嗯對,這是賴小沫的

 1_3D Exploration 將模型轉成函式

開啟網頁搜尋 3D Exploration (3D的檔案總管)

下載1.81免費試用版(4.5MB)安裝
 
 


開啟安裝完的執行檔3D Exploration
 
jsyeh.org/3dcg10 下載data.zip (有3D模型解壓縮在桌面\data)


3D Exploration去看data中的模型 ex.soccerball, ai

匯出(save as...)OpenGL程式檔案

CPP(OpenGL)程式 soccerball.cpp
    註:一定要記得修改儲存類型
 
 Export Dialog裡SampleAPP(才會有main()函式)
 

Notepad++觀察


新增一個OpenGL專案




偷天換日:Remove移除main.c,Add新增soccerball.cpp
 
main.c移除

把剛才透過3D Exploration新增的檔案soccerball.cpp複製到資料夾中


新增soccerball.cpp到專案中



按下F9(Build&Run)後,會發現有兩行錯誤,只要刪除就可以成功執行
(LPLOGPALETTE) lpPalette = (LPLOGPALETTE)HeapAlloc (hHeap, 0,
         sizeof (LOGPALETTE) + (nColors * sizeof (PALETTEENTRY)));
    //因為程式碼內容太過久遠,故無法讀取,所以會發生錯誤

只要刪除即可


再次按下F9(Build&Run),執行結果如下:
 

 2_3D Exploration來拆模型


使用3D Expioration開啟dolphins.obj,調整成僅顯示一


 

匯出(save as...)obj檔
 
要設定為Visible objects only,否則會將其他未顯示的模型也會一起匯出

 
確認有無勾選,如果沒有錯誤按下OK即可匯出檔案
 
第二隻海豚和第三隻海豚也是用一樣的方法匯出
 
 匯出後會產生各自的obj檔及mtl檔
 
將剛才的檔案放進myGLMsample資料夾中的data資料夾
桌面上記得要有freeglut
 
 
開啟專案,並修改程式碼來讀取dolphins1.obj模型

#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);

        if (!pmodel) {
        pmodel = glmReadOBJ("data/dolphins1.obj");  //第一隻海豚
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
       
    glPopMatrix();
    glutSwapBuffers();
}

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 };

#include <mmsystem.h>  //(1)Multimedia system
void keyboard(unsigned char key,int x,int y)
{   //(5)Add keyboard for Do/Re/Mi/Fa/So
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("So.wav",NULL,SND_ASYNC);
}

void mouse(int button,int state,int x,int y)
{   //(2)mouse function, use PlaySound
    PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\data\\Shot.wav",NULL,SND_ASYNC);
    //(3)PlaySound的路徑要記得用\\或/
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);

    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    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);

    glutMainLoop();
}
 
按下F9執行,即可看到dolphins1的模型
 
繼續新增第二個模型程式碼




...
GLMmodel* pmodel2 = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/dolphins1.obj");  //第一隻海豚
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);

        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/dolphins2.obj");  //第二隻海豚
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

    glPopMatrix();
    glutSwapBuffers();
}

...


按下F9即可看到執行結果

 
再繼續新增第三隻海豚的模型


...
GLMmodel* pmodel3 = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/dolphins1.obj");  //第一隻海豚
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);

        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/dolphins2.obj");  //第二隻海豚
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);\

        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/dolphins3.obj");  //第三隻海豚
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);

    glPopMatrix();
    glutSwapBuffers();
}
...


 
按下F9即可看到執行結果
 

 3_將海豚組成海豚人(?)

將三隻海豚的模型,分別移動到適當的位置

#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);

        if (!pmodel) {
        pmodel = glmReadOBJ("data/dolphins1.obj");  //第一隻海豚
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }

        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/dolphins2.obj");  //第二隻海豚
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }

        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/dolphins3.obj");  //第三隻海豚
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }

        glPushMatrix();  //對第一隻海豚作移動旋轉縮放的動作
            glTranslatef(0,0,0);
            glRotatef(90,0,1,0);
            glScalef(0.8,0.8,0.8);
            glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();  //對第二隻海豚作移動旋轉縮放的動作
            glTranslatef(0.5,0,0);
            glScalef(0.4,0.4,0.4);
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();  //對第三隻海豚作移動旋轉縮放的動作
            glTranslatef(-0.5,0,0);
            glRotatef(180,0,1,0);
            glScalef(0.4,0.4,0.4);
            glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}

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 };

#include <mmsystem.h>  //(1)Multimedia system
void keyboard(unsigned char key,int x,int y)
{   //(5)Add keyboard for Do/Re/Mi/Fa/So
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("So.wav",NULL,SND_ASYNC);
}

void mouse(int button,int state,int x,int y)
{   //(2)mouse function, use PlaySound
    PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\data\\Shot.wav",NULL,SND_ASYNC);
    //(3)PlaySound的路徑要記得用\\或/
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);

    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    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);

    glutMainLoop();
}

按下F9即可看到執行結果

 4_下載3D模型 利用3D Exploration 開啟

Google搜尋3d model download(下載免費模型),點擊第一個即可
 
 找到想要的模型,並下載

選擇Download

創辦帳戶,並登入進行下載的工作

利用3D Exploration 開啟剛剛下載的模型
 

沒有留言:

張貼留言