/ week06 /
複習 : 打光
主題 : 3D模型OBJ
(1)先到 這裡 下載 source、data、win32、glut32.dll 並解壓縮
(2)並到 這裡 下載 freeglut 並解壓縮
(3)打開codeblocks先建一個glut project
將glm.c、glm.h放進剛剛開codeblocks的資料夾內
並將 glm.c 改成 glm.cpp
(4)把data丟入freeglut裡的bin
(5)先將上禮拜的程式碼放進來並新增資料
(6)選擇剛剛丟進資料夾的 glm.cpp
(7)選取所需的程式碼貼上原本的程式碼
(8)以下為程式碼
#include <GL/glut.h>
#include "glm.h" ///我們要毒入3D模型,使用GLM,"glm.h"要放在同一個目錄裡
GLMmodel* pmodel=NULL;///GLMmodel變數,等一下讀入3D模型
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);
glScalef(0.8,0.8,0.8);
glRotatef(90, 0,1,0);
if(!pmodel){ ///讀入3D模型
pmodel = glmReadOBJ("data/AL.obj");///讀入3D模型
if(!pmodel)exit(0);///讀入3D模型
glmUnitize(pmodel);///讀入3D模型
glmFacetNormals(pmodel);///讀入3D模型
glmVertexNormals(pmodel,90.0);///讀入3D模型
}
glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);///讀入3D模型
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("translate");
glutDisplayFunc(display);
glutIdleFunc(display);///[新加]如果沒加這行就不會自動轉動
glClearColor(0,0,0,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();
}
#include "glm.h" ///我們要毒入3D模型,使用GLM,"glm.h"要放在同一個目錄裡
GLMmodel* pmodel=NULL;///GLMmodel變數,等一下讀入3D模型
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);
glScalef(0.8,0.8,0.8);
glRotatef(90, 0,1,0);
if(!pmodel){ ///讀入3D模型
pmodel = glmReadOBJ("data/AL.obj");///讀入3D模型
if(!pmodel)exit(0);///讀入3D模型
glmUnitize(pmodel);///讀入3D模型
glmFacetNormals(pmodel);///讀入3D模型
glmVertexNormals(pmodel,90.0);///讀入3D模型
}
glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);///讀入3D模型
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("translate");
glutDisplayFunc(display);
glutIdleFunc(display);///[新加]如果沒加這行就不會自動轉動
glClearColor(0,0,0,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();
}
(9)最後加入MAYA的圖並匯入codeblocks
( 以下為程式碼 )
#include <GL/glut.h>
#include "glm.h" ///我們要毒入3D模型,使用GLM,"glm.h"要放在同一個目錄裡
GLMmodel* pmodel=NULL;///GLMmodel變數,等一下讀入3D模型
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 0.5f, 0.5f, 0.5f, 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 light_position1[] = { -2.0f, 0.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);
glScalef(0.8,0.8,0.8);
glRotatef(90, 0,1,0);
if(!pmodel){ ///讀入3D模型
pmodel = glmReadOBJ("pencil1.obj");///讀入3D模型
if(!pmodel)exit(0);///讀入3D模型
glmUnitize(pmodel);///讀入3D模型
glmFacetNormals(pmodel);///讀入3D模型
glmVertexNormals(pmodel,90.0);///讀入3D模型
}
glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);///讀入3D模型
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("translate");
glutDisplayFunc(display);
glutIdleFunc(display);///[新加]如果沒加這行就不會自動轉動
glClearColor(0,0,0,1);///可以改背景顏色
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
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);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
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 <GL/glut.h>
#include "glm.h" ///我們要毒入3D模型,使用GLM,"glm.h"要放在同一個目錄裡
GLMmodel* pmodel=NULL;///GLMmodel變數,等一下讀入3D模型
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 0.5f, 0.5f, 0.5f, 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 light_position1[] = { -2.0f, 0.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);
glScalef(0.8,0.8,0.8);
glRotatef(90, 0,1,0);
if(!pmodel){ ///讀入3D模型
pmodel = glmReadOBJ("pencil1.obj");///讀入3D模型
if(!pmodel)exit(0);///讀入3D模型
glmUnitize(pmodel);///讀入3D模型
glmFacetNormals(pmodel);///讀入3D模型
glmVertexNormals(pmodel,90.0);///讀入3D模型
}
glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);///讀入3D模型
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("translate");
glutDisplayFunc(display);
glutIdleFunc(display);///[新加]如果沒加這行就不會自動轉動
glClearColor(0,0,0,1);///可以改背景顏色
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
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);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
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();
}
沒有留言:
張貼留言