顯示具有 Week09 標籤的文章。 顯示所有文章
顯示具有 Week09 標籤的文章。 顯示所有文章

2017年5月8日 星期一

Week 09 HMTse的上課筆記

To Do: 把檔案準備好
點開 projection


打開老師給的檔案


調  gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
會改變視角


會旋轉的茶壺

組裝

2017年5月1日 星期一

20170501_Week09 佳諭der課堂筆記



Week 09 課堂內容筆記摘要



作業一 - 使用史上最強軟體 (3D Exploration檔案總管) 讓3D模型直接轉換成C++程式碼
1.在Google搜尋以下網址(3D exploration > 3D 檔案總管)並下載安裝檔(3D Exploration v1.81)。
*盡量在學校做下載的動作而不要在家裡下載,因為可能會有付費的資訊。
















2.安裝軟體後並開啟執行3D 檔案總管(Exploration)。
















3.從以下網址下載 [data] .zip 並解壓縮到桌面,從3D檔案總管開啟此資料夾並檢視3D模型檔。





























4.從 File > Save As > 選擇存取 Open GL(.cpp*/) > 在視窗選擇介面選取SAMPLE APP並OK另存新檔。
(記得要勾選匯出Export Dialog)





























5.按右鍵選擇Notepad++檢視3D模型程式碼。


6.開啟CodeBlocks > File > New Project > 選擇Open GL專案。
















7.建好專案後開啟原本main.c程式碼,按右鍵刪除並重新加入.cpp檔。































6.完成3D模型檔程式碼的匯入執行顯示。
(原本一開始執行的時候會產生錯誤是因為此程式碼版本太舊無法執行,只要把這些錯誤的地方註解掉即可)
















作業二 - 利用CodeBlocks的GLUT檔案做出3D模型實作
1.從老師給取的檔案中( [my GLM sample].zip > [data].zip + source.zip [glm.c+glm.h+transformation.c] )利用CodeBlocks的Open File開啟檔案並執行.cbp檔。


















2.從3D 檔案總管中利用3D模型的各個構造中建立不同部位的obj模型檔。
*注意:建立的obj模型檔要儲存在data的資料夾中才可做應用。
(紫色的框框地方須注意,照著下圖修改)






3.打出以下程式碼並利用程式碼匯入各部位模型檔的概念做出不一樣位置的3D模型視窗執行顯示。
*注意:freeglut資料夾須放在桌面中才能執行以下程式碼。

截圖檔

















程式碼

#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"

GLMmodel* pmodel1 = 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 (!pmodel1)
    {
        pmodel1 = glmReadOBJ("data/dolphins1.obj");
        if (!pmodel1) exit(0);
        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 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);
    }
    /*此為匯入模型檔的動作,medel變數可做修改*/

    glPushMatrix();
    glTranslatef(-0.5,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(225,0,0,1);
    glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(-90,0,0,1);
    glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0.5,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(-45,0,0,1);
    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 };

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

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


★在自己建立的GLUT專案中須包含以下檔案才可正常編譯執行哦~















Week09陳冠霖的上課筆記

本周課程:
上網搜尋下載3D Exploration 1.81(3D 檔案總管)
到老師的網站http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data,3D模型
用3D Exploration 1.81開啟data裡的3D模型
將3D模型匯出成cpp檔
選擇sample app
在Codeblock建立專案OpenGL project
將已經建立的專案OpenGL project裡Sources資料夾的main.c移除
將專案OpenGL project資料夾裡的main刪除
將Soccer.cpp檔放入專案OpenGL project資料夾裡
到Codeblock裡的專案OpenGL project加入檔案Scooer.cpp
程式執行後會有錯誤,將錯誤部分用/*和*/助解掉,讓程式在執行時能忽略他
程式修改後,再次執行程式,成功了
在Codeblock裡新增老師給的專案,並打開執行
在3D Exploration 1.81裡選擇想要的圖片中的部分,在旁邊的Object List將喜歡的部分打勾,不喜歡的部分不要打勾。
將喜歡的部分存檔,選擇obj檔,調整成只儲存看得見的部分
將色彩和法向量也都要載入,所以全部打勾
打開Codeblock將老師給的程式碼進行修改,
第一部:新增GLMmodel* pmode1 = NULL;GLMmodel* pmode2 = NULL;GLMmodel* pmode3 = NULL;,用來放剛剛做的obj檔。
第二部:將原本的城市馬修改成if (!pmode1) {
        pmode1 = glmReadOBJ("data/dolphin1.obj");
       if (!pmode1) exit(0);
        glmUnitize(pmode1);
        glmFacetNormals(pmode1);
        glmVertexNormals(pmode1, 90.0);
        }把有原本的pmodel改成pmode1,把"data/....... .obj"裡匯入檔案改成dolphin1。
第三部:將改好的程式碼if (!pmode1) {
        pmode1 = glmReadOBJ("data/dolphin1.obj");
       if (!pmode1) exit(0);
        glmUnitize(pmode1);
        glmFacetNormals(pmode1);
        glmVertexNormals(pmode1, 90.0);
        }進行複製,之後將複製出的程式中的pmode1改成相對放obj檔的空間pmode2或pmode3,然後跟著修改"data/....... .obj"裡匯入檔案改成相對匯入檔案dolphin2或dolphin3。
第四部:在程式碼中加入縮放和移動的程式碼來改變pmode1、pmode2、pmode3的位置和大小glPushMatrix();~~~備份矩陣
        glTranslatef(0.5,0,0);~~~移動
        glScalef(0.4,0.4,0.4);~~~縮放
        glmDraw(pmode1, GLM_SMOOTH | GLM_MATERIAL);~~~載入pmode1
        glPopMatrix();  ~~~還原矩陣
glPushMatrix();~~~備份矩陣
        glTranslatef(-0.5,0,0);~~~移動
        glScalef(0.4,0.4,0.4);;~~~縮放
        glmDraw(pmode2, GLM_SMOOTH | GLM_MATERIAL);~~~載入pmode2
        glPopMatrix();~~~備份矩陣
程式碼如下:
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"

GLMmodel* pmodel1 = 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 (!pmodel1)
    {
        pmodel1 = glmReadOBJ("data/dolphins1.obj");
        if (!pmodel1) exit(0);
        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 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.5,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(225,0,0,1);
    glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(-90,0,0,1);
    glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0.5,0,0);
    glScalef(0.4,0.4,0.4);
    glRotatef(-45,0,0,1);
    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 };

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

    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();

}