Part1.opencv texture
步驟1.下載OpenCV 2.1.0
步驟2.安裝至C槽
步驟3.codeblocks開啟console app專案
步驟4.cbp按右鍵build option->research directories->compiler:add opencv/include
build option->research directories->compiler:add opencv/lib
build option->linker setting:add opencv/lib/cv210.lib
opencv/lib/cxcore210.lib
opencv/lib/highgui210.lib



步驟5.main加入以下程式
#include <opencv/highgui.h>
int main()
{
IplImage * img=cvLoadImage("
earth.jpg");
cvNamedWindow("hello opencv");
cvShowImage("hello opencv", img);
cvWaitKey(0);
return 0;
}
#注意:earth.jpg是自訂的檔案,請放一張圖片在cbp同一層目錄
Part2.opencv+freeglut
步驟1.建立GLUT專案
步驟2.參考Part1的方式把build option的設定都準備好
步驟3.將earth.jpg複製到freeglut資料夾的bin資料夾
Part3.轉動地球
步驟1.將Part2的專案程式碼改寫(外掛,moodle上的week08earth.zip->main)
步驟2.改成以下程式碼
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
}
void timer(int t)
{
glutTimerFunc(20, timer, 0);/// 1000 msec 50fps:20msec
angle+=1;///自動轉很帥
glutPostRedisplay();
}
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;
}
void myInit()
{
quad = gluNewQuadric();
id = myTexture("earth.jpg");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}

轉起來啦