2023年3月30日 星期四

補交week07

 week07

打開https://jsyeh.org/3dcg10/下載檔案data和win32,點選Texture.exe

安裝OpenCV(不能下載最新的)

codeblocks的setting後compiler設定
    Search directories的compiler中加入C:\OpenCV2.1\include
    Linker中加入C:\OpenCV2.1\lib
    Linker Setting中加入的cv210 cxcore210 highgui210



公布期中成績

以下是程式碼

1.  glPushMatrix(); //備份矩陣

2.    glTranslatef(x, y, z); //移動

3.    glRotatef(angle, x, y, z); //轉動

4.    glScalef(x, y, z); //縮放

5.    glBegin(GL_POLYGON); //開始畫

6.      glColor3f(r, g, b);//色彩

7.      glNormal3f(nx, ny, nz); //打光的法向量

8.      glTexCoord2f( tx, ty );//貼圖座標

9.      glVertex3f(x, y, z);//頂點

10.   glEnd(); //結束畫

11. glPopMatrix(); //還原矩陣

讓照片放到小黑裡

#include <opencv/highgui.h>


int main()

{

IplImage * img = cvLoadImage("檔名");

}

開啟新的專案week07-2

程式碼全刪,去 gist.github.com/jsyeh 裡的 myTexture

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;

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidTeapot( 0.3 );

    glutSwapBuffers();

}

int main(int argc, char**argv)

{

    glutInit( &argc, argv );

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week09 texture");


    glutDisplayFunc(display);

    myTexture("earth.jpg");


    glutMainLoop();

}

step03-2在void play()裡做修改

多加 

glBegin(GL_POLYGON);

        glTexCoord2f(0, 0); glVertex2f(-1, +1);

        glTexCoord2f(1, 0); glVertex2f(+1, +1);

        glTexCoord2f(1, 1); glVertex2f(+1, -1);

        glTexCoord2f(0, 1); glVertex2f(-1, -1);

glEnd();

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);後面

   glutSolidTeapot( 0.3 );前面


step03-3開啟新的專案week07-3

程式來自 gist.github.com/jsyeh 的 myEarth,修改就可以看到旋轉地球

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

#include <opencv/cv.h>

#include <GL/glut.h>

GLUquadric * sphere = NULL;///一個指到二次曲面的指標

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;

}

float angle=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glRotatef(angle, 0,-1,0);

        glRotatef(90, 1,0,0);

        gluQuadricTexture(sphere, 1);

        gluSphere(sphere, 1, 30, 30);///glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week07 texture earth");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    myTexture("earth.jpg");

    sphere = gluNewQuadric();

    glEnable(GL_DEPTH_TEST);


    glutMainLoop();

}

step03-4上傳github






沒有留言:

張貼留言