2024年6月3日 星期一

熱血學圖學round16

先用上課教材:https://jsyeh.org/3dcg10/

💨下載 win32&data 

打開 projection

調整glLookAt(eyeX,eyeY,eyeZ,centerX,centerY,centerZ,upX,upY,upZ )裡的參數

፠相機的運鏡














up的參數就像在Z軸旋轉相機的角度














➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

檔名:week16-1_sample_glutLookAt 

我們的眼睛:0 ,0 ,0

要看的主角:-2.4 ,1 ,-6

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

我們要注入的函式是:glutReshapeFunc(resize);


















修改程式碼:利用按按鍵改變相機中心

static void key(unsigned char key, int x, int y)

{

    ///原視角

    if(key=='0'){

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity() ;

    }

    ///左上圓形視角

    if(key=='1'){

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity() ;

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

    }

    ///中上錐形視角

    if(key=='2'){

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity() ;

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

    }

    ///右上環形視角

    if(key=='3'){

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity() ;

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

    }

    switch (key)

    {

        case 27 :

        case 'q':

            exit(0);

            break;

        case '+':

            slices++;

            stacks++;

            break;

        case '-':

            if (slices>3 && stacks>3)

            {

                slices--;

                stacks--;

            }

            break;

    }

    glutPostRedisplay();

}



























➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

檔名:week16-2_teapot_gluLookAt_glutReshapeFunc_reshape

፠垂直投影

gluOrtho(左,右,下,上,遠,近);

፠透視投影法

gluPerspective(張角,長寬比,近,遠);

gluFrustum(左,右,下,上,遠,近);


寫程式,在int main()裡

glutReshapeFunc(reshape);

在前面準備void reshpae(int w,int h)

'''cpp

void reshape(int x,int h){

    float ar=w /(float) h;///aspect radio長寬比

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

    glMatrixMode(GL_PROJECTION);///現在要設定成Projection矩陣

    glLoadIdentity();///最原始的單位矩陣I

    gluPerspective(60, ar, 0.1, 100);///透視投影的參數

    glMatrixMode(GL_MODELVIEW);///現在要切換回model view矩陣

    glLoadIdentity() ;

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

}

'''

#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 radio長寬比

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

    glMatrixMode(GL_PROJECTION);///現在要設定成Projection矩陣

    glLoadIdentity();///最原始的單位矩陣I

    gluPerspective(60, ar, 0.1, 100);///透視投影的參數

    glMatrixMode(GL_MODELVIEW);///現在要切換回model view矩陣

    glLoadIdentity() ;

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

}

int main(int argc, char *argv[])

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

        glutInitWindowSize(1040,640);///更改視窗大小

glutCreateWindow("week03 Mouse");

glutReshapeFunc(reshape);

glutDisplayFunc(display);

glutMainLoop();

}













➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

檔案三:week16-3_myTexture_id1_id2_glBindTexture

讓程式,可以有2張貼圖

可以參考week05-2 和 week05-3

貼上11行的GLUT程式,再貼上week05-2的myTexture 12行程式


#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可

#include <opencv/cv.h>

#include <GL/glut.h>

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;

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    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);

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

int main(int argc, char *argv[])

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("week03 Mouse");

glutDisplayFunc(display);

id1=myTexture("earth.jpg");

        id2=myTexture("background.jpg");

glutMainLoop();

}


















➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖

複習:

調整模型大小:

















沒有留言:

張貼留言