jsyeh.org/3dcg10
windows.zip ----> 下載\windows\light Materials.exe
data.zip
glut32.dll
source.zip ---> 等一下要用glm.h
glm.c ----> glm.cpp
(再參考 LightMaterial.c)

將甜桶改成茶壺

(1)打光的範例 Light Material.exe
(2)CodeBlock.File-New-Project,GLUT
(freeglut 也 ok)
(3)把上週3D Model ,glm.h
glm.c ---> glm.cpp
放到 #include "glm.h"/// a.呼叫glm.h外掛
GLMmodel * pmodel = NULL;/// b.準備好指標
c.畫模型


(4)
打光自己來,留下
(A)陣列......[]={...};
......[]={...};
......[]={...};
......[]={...};
......[]={...};
......[]={...};
......[]={...};
......[]={...};
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);
(B)在main()打光設定
打光理論 cos
程式碼:
#include "glm.h"///(1)呼叫glm.h外掛
GLMmodel * pmodel = NULL;///(2)準備好指標
float angle=0;///Now2:角度
void motion(int x,int y)
{
angle=x;///Now2: 改變轉動的角度
glutPostRedisplay();///Now2:請重畫
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotated(angle,0,1,0);///Now2:轉動
///(0)將3D模型準備好 放在C:\Users\user\Desktop\freeglut\bin\data\porsche.obj
///(3)忘了!畫模型!!!
if (!pmodel) {
pmodel = glmReadOBJ("data/porsche.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
///glutSolidTeapot(1);///glutSolidCone(1,1,slices,stacks);
glPopMatrix();
glutSwapBuffers();
}
///Now: 設定陣列,等一下要給 OpenGL用的
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 };///Now3: 把光移到前面
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);///Now2:用mouse motion讓車子轉
glClearColor(1,1,1,1);
///glEnable(GL_CULL_FACE);/// Now3: 把切面關掉
///glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);///Now :打開Light 0打光
glEnable(GL_NORMALIZE);///Now: 打開法向量 Normal Vetor Normalize, 長度變成1, 才好打光
glEnable(GL_COLOR_MATERIAL);///Now: 打開下面的 glLight & glMaterial
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();
}

左手臂,右手臂
#include "glm.h"///(1)呼叫glm.h外掛
GLMmodel * pmodel = NULL;///(2)準備好指標
float angle=0;///Now2:角度
void motion(int x,int y)
{
angle=x;///Now2: 改變轉動的角度
glutPostRedisplay();///Now2:請重畫
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotated(angle,0,1,0);///Now2:轉動
///(0)將3D模型準備好 放在C:\Users\user\Desktop\freeglut\bin\data\porsche.obj
///(3)忘了!畫模型!!!
if (!pmodel) {
pmodel = glmReadOBJ("data/porsche.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
///Now3: 上面是準備毒入模型, 下面才是真的在畫
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 身體
///glutSolidTeapot(1);///glutSolidCone(1,1,slices,stacks);
glPushMatrix();///備份
glTranslatef(0.4,0,0);///Now2: (3)掛在右邊
glRotatef(angle,0,0,1);///Now2: 轉動 ///(2)對中心旋轉
glTranslatef(0.4,0,0);///Now2: (1)把旋轉中心 移到畫面中心
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 右手臂
glPopMatrix();///還原
glPushMatrix();///備份
glTranslatef(-0.4,0,0);///Now2: (3)掛在左邊
glRotatef(angle,0,0,1);///Now2: 轉動 ///(2)對中心旋轉
glTranslatef(-0.4,0,0);///Now2: (1)把旋轉中心 移到畫面中心
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 左手臂
glPopMatrix();///還原
glPopMatrix();
glutSwapBuffers();
}
///Now: 設定陣列,等一下要給 OpenGL用的
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 };///Now3: 把光移到前面
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);///Now2:用mouse motion讓車子轉
glClearColor(1,1,1,1);
///glEnable(GL_CULL_FACE);/// Now3: 把切面關掉
///glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);///Now :打開Light 0打光
glEnable(GL_NORMALIZE);///Now: 打開法向量 Normal Vetor Normalize, 長度變成1, 才好打光
glEnable(GL_COLOR_MATERIAL);///Now: 打開下面的 glLight & glMaterial
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();
}
機器人
#include "glm.h"///(1)呼叫glm.h外掛
GLMmodel * pmodel = NULL;///(2)準備好指標
float angle=0;///Now2:角度
void motion(int x,int y)
{
angle=x;///Now2: 改變轉動的角度
glutPostRedisplay();///Now2:請重畫
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//glRotated(angle,0,1,0);///Now2:轉動
///(0)將3D模型準備好 放在C:\Users\user\Desktop\freeglut\bin\data\porsche.obj
///(3)忘了!畫模型!!!
if (!pmodel) {
pmodel = glmReadOBJ("data/porsche.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
///Now3: 上面是準備毒入模型, 下面才是真的在畫
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 身體
///glutSolidTeapot(1);///glutSolidCone(1,1,slices,stacks);
glPushMatrix();///備份
glTranslatef(0.4,0,0);///Now2: (3)掛在右邊
glRotatef(angle,0,0,1);///Now2: 轉動 ///(2)對中心旋轉
glTranslatef(0.4,0,0);///Now2: (1)把旋轉中心 移到畫面中心
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 右手臂
glPopMatrix();///還原
glPushMatrix();///備份
glTranslatef(-0.4,0,0);///Now2: (3)掛在左邊
glRotatef(-angle,0,0,1);///Now2: 轉動 ///(2)對中心旋轉
glTranslatef(-0.4,0,0);///Now2: (1)把旋轉中心 移到畫面中心
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);///Now2: 左手臂
glPopMatrix();///還原
glPopMatrix();
glutSwapBuffers();
}
///Now: 設定陣列,等一下要給 OpenGL用的
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 };///Now3: 把光移到前面
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);///Now2:用mouse motion讓車子轉
glClearColor(1,1,1,1);
///glEnable(GL_CULL_FACE);/// Now3: 把切面關掉
///glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);///Now :打開Light 0打光
glEnable(GL_NORMALIZE);///Now: 打開法向量 Normal Vetor Normalize, 長度變成1, 才好打光
glEnable(GL_COLOR_MATERIAL);///Now: 打開下面的 glLight & glMaterial
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();
}



沒有留言:
張貼留言