2024年6月6日 星期四

week16

前置作業:

下載freeglut

安裝opencv2.1,記得要add path

重開codeblocks

設定compiler

search directories-compiler->C:\OpenCV2.1\include

search directories-linker->C:\OpenCV2.1\lib

linker setting->cxcore210 cv210 highgui210


#week16-0_sample


jsyeh.org/3dcg10

下載data.zip windows.zip
window解壓縮
把data資料夾拉進window裡
打開projection

eye從哪裡看中心點

center畫面中心點座標

up轉動攝影機

#week16-1_sample_gluLookAT

在resize函式最下面加程式碼

gluLookAt(0,0,0,-2.4,1.2,-6,0,1,0);


現在想做一個修改,想再按按鍵的時候看不同地方

if(key=='1')

if(key=='2')

if(key=='3')


#week16-2_glutReshapeFunc_gluPerspective_gluLookAt
gluPerspective透視投影
glOrtho垂直投影
glFrustum不常用
左、右、上、下、近、遠 共6個參數

寫個程式11+8行做出透視投影
先貼上11行簡單程式碼
在int main裡加入glutReshapeFunc(reshape)
寫自己的void reshape(int w,int h)



#week16-3_myTexture_id1_id2_glBindTexture

從github下載week05_rotating_earth
直接執行會出錯因為沒有圖檔



加背景

新增的程式


完整程式碼:

///week05_rotating_earth 專案
/// 把 week05_opencv_opengl_myTexture 程式拿來改
#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * quad = NULL; ///todo: 要有一顆指標
int id1,id2;///準備兩個整數用來存貼圖
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;
}
///再貼上10行GLUT程式 (不要貼剛剛的程式)
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); ///glutSolidTeapot( 0.3 );
    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);
    id2 = myTexture("c:/1.jpg");
    id1 = myTexture("c:/earth.jpg");

    quad = gluNewQuadric(); ///todo:把這顆指標,指好
    gluQuadricTexture(quad, 1);///todo: 做好地球的貼圖
    glutMainLoop();
}





沒有留言:

張貼留言