2017年11月2日 星期四

田蜜Week08

Texture

1.至http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data , win32 , source 及 glut32.gll 檔案
   並將data 和 glut32.gll 放入windows 資料夾中

2.執行 Texture.exe 檔案
















OpenCV專案

1.下載OpenCV-2.1.0-win32-vs2008.exe

2.安裝時記得選擇第三項

















3.安裝完成後開啟codeblocks專案
   file->new->project->console application

4.接著開啟settings->compiler


















5.將main.cpp內容更改為

#include <opencv/highgui.h>

int main()
{
    IplImage * img=cvLoadImage("earth.jpg");
    cvNamedWindow("hello opencv");
    cvShowImage("hello opencv",img);
    cvWaitKey(0);
}

















6.搜尋圖片earth map
   另存圖片至專案資料夾,名稱為earth.jpg


















7.專案上按右鍵->build option

















8.選擇 Search directories
   新增C:\OpenCV2.1\include 至 Compiler
















9.新增C:\OpenCV2.1\lib 至 Linker
















10. 選擇 Linker settings
      新增 lib 資料夾中的 cv210  ,cxcore210 , highgui210  三個檔案

































11. 編譯執行
















用GLUT專案開OpenCV

1.開啟新的GLUT project

2. 加上 #include <opencv/highgui.h>
    並在 main 中加入程式碼

    IplImage * img=cvLoadImage("earth.jpg");
    cvNamedWindow("hello");
    cvShowImage("hello", img);
    cvWaitKey(0);

















3. 將 earth.jpg 放入 freeglut->bin 中

















4. 對新專案再做一次build option的設定
    編譯執行 (按空白鍵叫出左上角的3D圖)
















3D旋轉地球

#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();
}










沒有留言:

張貼留言