#week16
1.freeglut
2.安裝openCV2.1 add PATH, C:\OpenCV2.1目錄
3.重開CB,Setting-Compiler,3個設定
-Search directiories的 Compiler 對應
C:\OpenCV2.1\include
-Search directiories的 Linker 對應
C:\OpenCV2.1\lib
-Linker setting 要加1. cv210 2.cxcore 3.highgui210
#week16-0_sample
到 https://jsyeh.org/3dcg10 下載 data.zip windonws.zip
windows.zip => 下載windows\Projection.exe
data.zip= >放進windonws資料夾
#week16-0_sample_gluLookAt
1.new-project ,GLUT專案 week16-1_sample_gluLookAt
2.要改resize()函式 在裡面加上gluLookAt() 函式
就能看著左上角的圓球
加上gluLookAt( 0, 0, 0, -2.4, 1.2, -6, 0, 1, 0); ///加這段程式
要做另一個修改,想要在按按鍵時,看不同地方
if(key=='1') 看左上方
if(key=='2') 看上方
if(key=='3') 看右上方
99-111行
if(key=='1'){ ///看左上方
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 0, -2.4, 1.2, -6, 0, 1, 0);
}else if(key=='2'){ ///看上方
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 0, 0, 1.2, -6, 0, 1, 0);
}else if(key=='3'){ ///看右上方
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 0, +2.4, 1.2, -6, 0, 1, 0);
}
#week16-2_glut_...
看Projection.exe 範例
gluPerpective 透視投影
glOrtho 垂直投影
glFrustum 比較奇怪,我們通常不會使用
左,右,下,上,近,遠 共6組參數
glutPerspective ( 視角, 長寬比, z近, z遠)
fovy (field of view in y視角
aspect ratio 長寬比
11+8行程式 做出投影透視
1.newProject GLUT專案
2.glutReshapeFunc_gluPerspective_gluLookAt
-----------------------------------------------------------------------------------------------------------------------------
#include<GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3); ///正中比放茶壺
glutSwapBuffers();
}
void reshape(int w, int h){ ///加這段
glViewport(0,0,w,h); ///設定視窗會看到的區域
float ar = w / (float) h; ///長寬比 , 寬長比
glMatrixMode(GL_PROJECTION); ///切換投影矩陣
glLoadIdentity();
gluPerspective( 60, ar, 0.01, 100); ///透視投影
glMatrixMode(GL_MODELVIEW); ///切換MODEL VIEW矩陣
glLoadIdentity();
gluLookAt(0, 0, -2, 0, 0, 0, 0, 1, 0);
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(300,600); ///這行更改視窗長寬
glutCreateWindow("week16-2");
glutReshapeFunc(reshape); ///加這個
glutDisplayFunc(display);
glutMainLoop();
}
-----------------------------------------------------------------------------------------------------------------------------
#week16-3_myTexture_id1_id2_glBindTexture
若想要有 "兩張貼圖" (一張背景, 一張模型)
1.newproject GLUT專案
2.使用week05_ritating_earth/main.cpp裡的程式碼
-----------------------------------------------------------------------------------------------------------------------------
#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * quad = NULL; ///todo: 要有一顆指標
int id1, id2; ///準備兩個整數,用來存貼圖的id
int myTexture(char * filename)
{
IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
return id;
}
float angle = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D,id2); ///背景舞台
glBegin(GL_POLYGON);
glTexCoord2f(0,0); glVertex2f(-1,+1);
glTexCoord2f(0,1); glVertex2f(-1,-1);
glTexCoord2f(1,1); glVertex2f(+1,-1);
glTexCoord2f(1,0); glVertex2f(+1,+1);
glEnd();
glBindTexture(GL_TEXTURE_2D,id1); ///地球
glPushMatrix();
glRotatef(90, 1, 0, 0);
glRotatef(angle++, 0, 0, 1);
gluSphere(quad, 0.5, 30, 30);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char*argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week05");
glutIdleFunc(display);
glutDisplayFunc(display);
id1 = myTexture("c:/earth.jpg");
id2 = myTexture("c:/background.jpg");
quad = gluNewQuadric(); ///todo:把這顆指標,指好
gluQuadricTexture(quad, 1);///todo: 做好地球的貼圖
glutMainLoop();
}
-----------------------------------------------------------------------------------------------------------------------------
沒有留言:
張貼留言