2019年3月29日 星期五

alanhc/張牧之 電腦圖學-week06


0329 如何使GLUT可讀入MAYA模型檔?


1. 準備好環境
2. 修改程式
3. Debug
4. 上課筆記
5. 功課




1. 準備好環境

先準備下載好 [source]
(1) windows 裡的 [data資料夾丟到 freeglut/bin 所在位置
(2) [source] 裡的 glm.h、glm.c、transformation.c丟到 專案資料夾
(3) 改 transformation.c 為 transformation.cpp


2.修改程式

(1) 加入#include "glm.h"  ///匯入
(2) link glm.cpp file ///在codeblocks左邊workspace 地方點選專案名字按右鍵add file
(3) 宣告 GLMmodel* pmodel = NULL;  ///宣告指標
(4) 加程式到display裡面
*********************************************************
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);///畫模型 }
*********************************************************
成功~~~

3. Debug

Q1 如果人物很暗
A1 代表光線位置有問題,改light_position[] 裡的z即可

Q2 如果嘴巴曲線炸掉
A2 應該是不小心複製到glEnable(GL_CULL_FACE)
理由在這: cull face 解釋

4. 上課筆記

TD thechnical director 技術
TA Technical Artist


notepad++ 存成cpp >  有顏色
notepad++ 外掛 > NppExport > Export to html


lightMaterial.c 搜尋glm 找第一個、第二個、第三個

lightMaterial.c
讀入3D模型 include glm.h
GLMmodel* pmodel= NULL
"glm.h" 要放在專案目錄
glm.c放入
codeblocks 新增檔案>
drawmodel() 裡面城市複製到display

freeglut
    bin
    include
    lib
丟之前的data到 bin裡

Debug
||=== Build: Debug in class0329 (compiler: GNU GCC Compiler) ===|
C:\Users\alant\Desktop\class0329\main.cpp||In function 'void display()':|
C:\Users\alant\Desktop\class0329\main.cpp|20|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
obj\Debug\main.o||In function `Z7displayv':|
C:\Users\alant\Desktop\class0329\main.cpp|20|undefined reference to `glmReadOBJ(char*)'|
C:\Users\alant\Desktop\class0329\main.cpp|22|undefined reference to `glmUnitize(_GLMmodel*)'|
C:\Users\alant\Desktop\class0329\main.cpp|23|undefined reference to `glmFacetNormals(_GLMmodel*)'|
C:\Users\alant\Desktop\class0329\main.cpp|24|undefined reference to `glmVertexNormals(_GLMmodel*, float)'|
C:\Users\alant\Desktop\class0329\main.cpp|27|undefined reference to `glmDraw(_GLMmodel*, unsigned int)'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


三步驟
(1) include .h檔案
(2) link lib
(3) 執行 bin .dll



.obj黨裡面都是點

名詞解釋:
Normal :法向量
v
vn vertex normal
f: facet t:很小的小

f 1802//1690 
1690:法向量

OBJ > Maya出生的格式


Maya小歷史

縮放大小:glScale

作業

File > export 

5. 功課

(1) 畫好MAYA模型
(2) File > Export ALL
(3) 另存成OBJ
(4) 參考上課步驟,改成讀入自己模型










今天的程式:

#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
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 };
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
angle++;
glPushMatrix();
glRotatef(180,0,1,0);
glScalef(0.5,0.5,0.5);
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);///畫模型
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("alanhc");
/*剪貼--------------------------------------------------------*/
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);
/*剪貼--------------------------------------------------------*/
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
view raw class0329.cpp hosted with ❤ by GitHub

沒有留言:

張貼留言