2024年6月20日 星期四

哈囉你好嗎隨便 week16

 

安裝 OpenCV 和設定 CodeBlocks

1. 安裝 OpenCV

  1. 安裝過程中勾選 Add PATH (第二個選項)。
  2. 將 OpenCV 安裝在預設目錄。

2. 設定 CodeBlocks

  1. 打開 CodeBlocks,進入 Settings -> Compiler
  2. 重啟 CodeBlocks。
  3. View -> Start Page 找到原本的檔案。
  4. Settings -> Compiler -> Search directories 添加以下目錄:
    • Compiler: C:\OpenCV2.1\include
    • Linker: C:\OpenCV2.1\lib
  5. Linker settings 中添加以下項目:
    • cv210
    • cxcore210
    • highgui210



freeglut複製 32搞定

https://github.com/jsyeh/2024graphicsb

https://github.com/jsyeh/2024graphicsb

https://github.com/jsyeh/2024graphicsb

https://github.com/jsyeh/2024graphicsb

https://github.com/jsyeh/2024graphicsb

隨時可以拿老師的




先設好今天的上課環境

0. 安裝 freeglut

1. 安裝 OpenCV 2.1, Add PATH

2.重開 CodeBlocks,設定 opencv的3個設定

3. File-New-Project, GLUT專案 week16-0 sample

## week16-1 sample gluLookAt

1. 把剛剛 week16-0 整個目錄,複製成 week16-1

2.複製後,改目錄名、改.cbp專案檔名,用Notepad++改內容

要修改的程式碼,是想要 Look At 看著某個物體

我們的眼睛:0,0,0

要看的主角 center: -2.4, 1.2, -6

我們的up向量:0,1, 0






cpp>>>>

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(0,0,0,     -2.4,1.2,-6, 0,1,0);
}


28行左右那邊


## week16-2 teapot gluLookAt glutReshapeFunc reshape
1. File-New-Project, GLUT專案
week16-2 teapot gluLookAt glutReshapeFunc reshape
2. 貼上11行 GLUT 的程式
3.看課本的教學,了解 glutReshapeFunc(reshape);怎麼用

有各式各樣的東西可以在裡面玩 自己可過去玩玩看^^


課本教學:
gluOrtho (左,右,下,上,近,遠;
gluPerspective(張角,長寬比,近,遠);
gluFrustum(左,右,下,上,近,遠);


寫程式,在int main()裡 glutReshapeFunc (reshape);

在前面,準備 void reshape (int w, int 1)

cpp

void reshape(int w, int h) {

}

float ar w/ (float) h;

glViewport (0, 0, w, h);

glMatrixMode (GL PROJECTION);

glLoadIdentity();

gluPerspective (60, ar, 0.1, 100);

glMatrixMode (GL_MODELVIEW); glLoadIdentity();




cpp>>>>>>>>>>



#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) {

    float ar=w/ (float) h; ///aspect ratio 長實比

    glViewport (0, 0, w, h); ///設定可以看到的範圍,全看到

    glMatrixMode (GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(60,ar,0.1,100);


    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    gluLookAt(0,0,-3,0,0,0,0,1,0);


}



int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week16-2");


    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutReshapeFunc(reshape);

    glutDisplayFunc(display);


    glutMainLoop();

}



2張貼圖 

cpp>>>>

#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <GL/glut.h>
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;
}
int id1,id2;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindTexture(GL_TEXTURE_2D,id1);
    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,id2);
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week16-3 多個貼圖");

    glutDisplayFunc(display);
    myTexture("c:/earth.jpg");///去下載earth map 地圖
    ///再準備第2張貼 C:/background.jpg
    id2 = myTexture("c:/earth.jpg");///去下載earth map 地圖
    id1 = myTexture("c:/background.jpg");///再準備第2張貼C
    glutMainLoop();
}










沒有留言:

張貼留言