1.主題:打光Part1
Light 打光到 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
--> 下載: windows.zip, data.zip, glut32.dll (week03
now -->source.zip (+下載,程式範例
執行 Light Material.exe 檔案
陣列宣告(Light,Material)
設定函式
GLfloat material_Kd[]={};
//模型顏色
-
開啟GLUT程式 (week01
進入transmission zero 網站,下載"freeglut 3.0.0 MinGW Package"在 CodeBlocks 開啟GLUTproject
寫打光程式:
#include <GL/glut.h> 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(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidTeapot(0.3); glutSwapBuffers(); } int main(int argc,char**argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("Week06"); glutDisplayFunc(display); ///OpenGL設定 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(); }
2.實作:GLM vs OBJ
從source.zip檔案中 ,開啟transformation.c ,找出要有的glm程式 ,輸入打光程式中從source.zip檔案中 ,glm.h和glm.c放入專案的資料夾中 ,且glm.c改成glm.cpp
將CodeBlocks 的GLUTproject 中匯入glm.cpp
- 將odj模型放入 freeglut\bin 中(才能讀到模型
-
更改後的打光程式:
#include <GL/glut.h> #include "glm.h" ///now要有3D Model 第1行 GLMmodel* pmodel = NULL; ///now要有3D Model 第2行 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(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (!pmodel) { ///now要有3D Model pmodel = glmReadOBJ("data/flowers.obj"); ///now要有3D Model if (!pmodel) exit(0); ///now要有3D Model glmUnitize(pmodel); ///now要有3D Model glmFacetNormals(pmodel); ///now要有3D Model glmVertexNormals(pmodel, 90.0); ///now要有3D Model } ///now要有3D Model glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model glutSwapBuffers(); } int main(int argc,char**argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("Week06"); glutDisplayFunc(display); ///OpenGL設定 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(); }
3.作業
#include <GL/glut.h>
#include "glm.h" ///now要有3D Model 第1行
GLMmodel* pmodel = NULL; ///now要有3D Model 第2行
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);
glPushMatrix(); //備份矩陣
glRotatef(angle,0,1,0); //旋轉,對y軸
if (!pmodel) { ///now要有3D Model
pmodel = glmReadOBJ("data/IB.obj"); ///now要有3D Model
if (!pmodel) exit(0); ///now要有3D Model
glmUnitize(pmodel); ///now要有3D Model
glmFacetNormals(pmodel); ///now要有3D Model
glmVertexNormals(pmodel, 90.0); ///now要有3D Model
} ///now要有3D Model
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
glPopMatrix(); //還原矩陣
glutSwapBuffers();
angle+=1;
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06");
glutDisplayFunc(display);
glutIdleFunc(display);
///OpenGL設定
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();
}
沒有留言:
張貼留言