到http://www.cmlab.csie.ntu.edu.tw/~0/jsyeh/3dcg1網站下載source、data、win32、glut32.dll這四個檔案
開啟Projection.exe
按右鍵查看不同投影法差別
透視投影
gluPerspecive(視野寬廣)
glLookAt(x,y,z) eye 攝影機
(x,y,z) center 中心點
_
(x,y,z) up 手持鏡頭
freeglut3.0.0 MinGW
解壓縮後將libfreeglut.a檔複製並改名為libglut32.a
到codeblock裡開啟glut 並儲存 若要在codeblock上自己開啟以上project檔
將source資料夾的三個檔案丟到新專案檔 glm.c檔須改成glm.cpp
匯入glm.cpp檔
將projection檔內的程式碼匯入main函式
即可開啟
(2)主題:投影.矩陣
試著開始做投影矩陣
接著嘗試自己做(更改程式碼
#include <GL/glut.h>///GLUT 外掛
#include "glm.h"///glm.cpp for 3D model glmReadOBJ(), glmDraw(), glmUnitized()
GLMmodel * pmodel=NULL;///指標
void drawmodel(void)
{
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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutInitWindowPosition(50, 50);
glutCreateWindow("week15");
// glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
因為少了打光 人體是空白的
#include <GL/glut.h>///GLUT 外掛
#include "glm.h"///glm.cpp for 3D model glmReadOBJ(), glmDraw(), glmUnitized()
GLMmodel * pmodel=NULL;///指標
void drawmodel(void)
{
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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutInitWindowPosition(50, 50);
glutCreateWindow("week15");
// glutReshapeFunc(reshape);
glutDisplayFunc(display);
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glutMainLoop();
return 0;
}
加上打光即可印出
接著試著讓人物旋轉及使用投影矩陣
#include <GL/glut.h>///GLUT 外掛
#include "glm.h"///glm.cpp for 3D model glmReadOBJ(), glmDraw(), glmUnitized()
GLMmodel * pmodel=NULL;///指標
void drawmodel(void)
{
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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
#include <math.h>///for sin() cos() ///NOW3
float eyeX=0.5, eyeY=0, eyeZ=0; ///NOW3
void timer(int t)///NOW3
{
float angle = (t/180.0)*3.1415926; ///NOW3
eyeX=0.5*cos(angle); ///NOW3
eyeZ=0.5*sin(angle); ///NOW3
glutTimerFunc(33, timer, t+1);///NOW3
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt( eyeX, eyeY, eyeZ,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
glutPostRedisplay();///NOW3
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutInitWindowPosition(50, 50);
glutCreateWindow("week15");
// glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glutMainLoop();
return 0;
}
由此畫面可得知 ,人物沒完全顯現
需要再改良程式碼
(加上紅字改良垂直投影)
#include <GL/glut.h>///GLUT 外掛
#include "glm.h"///glm.cpp for 3D model glmReadOBJ(), glmDraw(), glmUnitized()
GLMmodel * pmodel=NULL;///指標
void drawmodel(void)
{
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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
#include <math.h>///for sin() cos() ///NOW3
float eyeX=0.5, eyeY=0, eyeZ=0; ///NOW3
void timer(int t)///NOW3
{
glMatrixMode(GL_PROJECTION); ///NOW4
glLoadIdentity(); ///NOW4
glOrtho(-1, +1, -1,+1, -10,+10); ///NOW4 你可以看到 -10...+10範圍(垂直投影
float angle = (t/180.0)*3.1415926; ///NOW3
eyeX=0.5*cos(angle); ///NOW3
eyeZ=0.5*sin(angle); ///NOW3
glutTimerFunc(33, timer, t+1);///NOW3
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt( eyeX, eyeY, eyeZ,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
glutPostRedisplay();///NOW3
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutInitWindowPosition(50, 50);
glutCreateWindow("week15");
// glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glutMainLoop();
return 0;
}
即可印出
接下來改為透視投影法
#include <GL/glut.h>///GLUT 外掛
#include "glm.h"///glm.cpp for 3D model glmReadOBJ(), glmDraw(), glmUnitized()
GLMmodel * pmodel=NULL;///指標
void drawmodel(void)
{
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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawmodel();
glutSwapBuffers();
}
#include <math.h>///for sin() cos() ///NOW3
float eyeX=2, eyeY=0, eyeZ=0; ///NOW3
void timer(int t)///NOW3
{
glMatrixMode(GL_PROJECTION); ///NOW4
glLoadIdentity(); ///NOW4
///glOrtho(-1, +1, -1,+1, -10.0,10.0); ///NOW4 你可以看到 -10...+10範圍
gluPerspective(60, 1.0, 0.001, 1000);///NOW5 透視投影法
float angle = (t/180.0)*3.1415926; ///NOW3
eyeX=2*cos(angle); ///NOW3
eyeZ=2*sin(angle); ///NOW3
glutTimerFunc(33, timer, t+1);///NOW3
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt( eyeX, eyeY, eyeZ,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
glutPostRedisplay();///NOW3
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(512,512);
glutInitWindowPosition(50, 50);
glutCreateWindow("week15");
// glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glutMainLoop();
return 0;
}
(3)複習:位置移動vs轉動
(4)複習:背景貼圖
沒有留言:
張貼留言