該做期末作業了
___________________________________________
今日課程主題:
攝影機、運鏡、投影、矩陣
___________________________________________
第一節
________________________________
就能嘗試攝影機的用法
____________________________________
第二節
________________________________________
>開GLUT專案
>將 glm.c 和 glm.h 丟進去
>將 glm.c 改成 glm.cpp
> Add files
>程式碼貼上去
________________________(程式碼)_________________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutMainLoop();
}
________________________(執行結果)_________________________
___________________________________________________________
模型的雛形出來了,那接下來就是為它打光了——
_____________________(第二部份的程式碼)______________________
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
________________________(執行結果)_________________________
___________________________________________________________
好像哪裡怪怪的......?
這是因為我們把光打在它的正面,而我們鏡頭在它的背面的緣故。
只要將紅底的部分改成-1.0就可以了。
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
________________________(執行結果)_________________________
___________________________________________________________
接下來要讓模型自己轉動
一樣,貼上程式碼試看看
_____________________(第三部份的程式碼)______________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.8, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
________________________(執行結果)_________________________
___________________________________________________________
嗯?怎麼破圖了?(((゚Д゚;)))
看來是靠太近了,我們拉遠些,再把攝影距離拉大
_____________________(第三部份的程式碼)______________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_PROJECTION);///NOW4
glLoadIdentity();///NOW4
glOrtho(-1,+1, -1,+1, -10,+10);///NOW4 可以看到範圍比較大的投影法
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.6, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
________________________(執行結果)_________________________
___________________________________________________________
附上我還沒搞清楚的程式碼
___________________________________________________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_PROJECTION);///NOW4
glLoadIdentity();///NOW4
gluPerspective(60, 1, 0.001, 1000);///NOW5
///glOrtho(-1,+1, -1,+1, -10,+10);///NOW4 可以看到範圍比較大的投影法
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.6, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
好像哪裡怪怪的......?
這是因為我們把光打在它的正面,而我們鏡頭在它的背面的緣故。
只要將紅底的部分改成-1.0就可以了。
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };
________________________(執行結果)_________________________
___________________________________________________________
____________________________________
第三節
________________________________________
接下來要讓模型自己轉動
一樣,貼上程式碼試看看
_____________________(第三部份的程式碼)______________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.8, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
___________________________________________________________
嗯?怎麼破圖了?(((゚Д゚;)))
看來是靠太近了,我們拉遠些,再把攝影距離拉大
_____________________(第三部份的程式碼)______________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_PROJECTION);///NOW4
glLoadIdentity();///NOW4
glOrtho(-1,+1, -1,+1, -10,+10);///NOW4 可以看到範圍比較大的投影法
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.6, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
___________________________________________________________
附上我還沒搞清楚的程式碼
___________________________________________________________
#include <GL/glut.h>
#include "glm.h" ///for glmReadOBJ(), glmDraw(), glmUnitized()....
GLMmodel * pmodel=NULL;///NOW
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>
float eyeX=1, eyeY=0, eyeZ=0;
void timer(int t)///NOW3
{
glutTimerFunc(33, timer, t+1);///NOW3
float angle=t/180.0*3.1415926;///NOW3 算出一個角度
eyeX=cos(angle); eyeZ=sin(angle);///NOW3
glMatrixMode(GL_PROJECTION);///NOW4
glLoadIdentity();///NOW4
gluPerspective(60, 1, 0.001, 1000);///NOW5
///glOrtho(-1,+1, -1,+1, -10,+10);///NOW4 可以看到範圍比較大的投影法
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
gluLookAt(eyeX, eyeY, eyeZ, ///NOW3 LookAt的 eye
0.0, 0.6, 0.0, ///NOW3 LookAt的 center
0, 1, 0);///NOW3 LookAt的 up
glutPostRedisplay();///NOW3 Re-display
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutTimerFunc(33, timer, 0);///NOW3
GLfloat light_pos[] = { 0.0, 0.0, 1.0, 0.0 };///NOW2 打光的位置
glEnable(GL_DEPTH_TEST);///NOW2 有立體的功能
glEnable(GL_LIGHT0); ///NOW2 打光後變立體的彩色的
glEnable(GL_LIGHTING); ///NOW2 打光後變立體的彩色的
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);///NOW2 打光的位置
glutMainLoop();
}
沒有留言:
張貼留言