2022年4月18日 星期一

Kuo's Graphic_Note_Week09

Step 1 : Using Example (Texture.exe) to know What We Want to Make.

1-1.Download Example (URL: https://jsyeh.org/3dcg10/)



 1-2. Put file "data" to file "windows"


     1-3. Open example (Path:windows/Texture.exe)



 

    1-4. Open Texture.exe


    1-5. Change data



Step 2 : Install OpenCV

    2-1. Download "OpenCV-2.1.0-win32-vs2008.exe" on "Teams"


    2-2. Click "OpenCV-2.1.0-win32-vs2008.exe" to download OpenCV



    2-3. Choose "Add OpenCV to the system PATH for all users"


    2-4. Using default path (C:\OpenCV2.1)


Step 3 :Setting CodeBlocks and OpenCV 

     *Using default path (C:\OpenCV2.1)

    3-1. Open a new empty file


    3-2. Choose: "Setting" -> "Compiler..." 

    3-3. Choose: "Search directories" -> "Linker" & Click: "Add"



    3-4. Choose the path (C:\OpenCV2.1\include) 


    3-5. Choose: "Linker settings" & Click: "Add"


    3-6.   Choose "cv210.lib", "cxcore210.lib", and "highgui210.lib"



Step 4 :Using CodeBlocks and OpenCV to Coding


Final Code:
#include <opencv/highgui.h>
int main(){
    IpImage *img=cvLoadImage("鯊魚.png");
    cvShowImage("week09",img);
    cvWaitKey(0);
}

Step 5 :Using CodeBlocks to set Texture 

    5-1. Download a world map on Google


    5-2. Save the JPG in work file (.\freeglut-MinGW-3.0.0-1.mp\freeglut\bin)


    5-3. Check the path of the file (In work file)



    5-4. Make a white teapot

Code:
#include <GL/glut.h>
void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int ac, chat**av){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week09 texture");
    glutDisplayFunc(display);
    glutMainLoop();
}


    5-5. Coding



    5-6. Build & Run



    5-7. Texture on the teapot


Final Code:

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.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 ac,char**av){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week09 texture");
    glutDisplayFunc(display);
    myTexture("earth.jpg");
    glutMainLoop();
}






沒有留言:

張貼留言