2023年3月30日 星期四

大吃七斤Week07

week07

從老師給的網址( https://jsyeh.org/3dcg10/ )下載windows.zip和data.zip,解壓縮後把data資料夾中的data放入windows的資料夾,後開啟windows裡的Texture.exe

通常是右手座標系統(4個頂點是逆時針繞)
0到1之間重複,有時會改設成左右上下鏡射,有時候直接拿0或1的值

jsyeh.org/gl/opengl_10_func.html

glPushMatrix();//備份矩陣
    glTranslatef(x,y,z);//移動
    glRotatef(angle,x,y,z);//轉動
    glScalef(x,y,z);//縮放
    glBegin(GL_POLYGON);//開始畫
        glColor3f(r,g,b);//色彩
        glTexCoord2f(tx,ty);//貼圖座標
        glNormal3f(nx,ny,nz);//打光的法向量
        glVertex2f(x,y);//頂點
    glEnd();//結束畫
glPopMatrix();//還原矩陣

從2022葉正聖老師上課軟體安裝 OpenCV-2.1.0-win32-vs2008.exe 要注意以下選項

從Settings ==>  Compiler settings 
Search dirctories ==> Compiler 目錄加入 C:\OpenCV2.1\include
                                    Linker 目錄加入 C:\OpenCV2.1\lib
 Linker settings 加入 cv210  cxcore210  highgui210 三個



week07-1_opencv_cvLoadImage

檔名一定要.cpp

#include <opencv/highgui.h>
int main()
{
    IplImage * img = cvLoadImage("image.jpg"); ///圖檔要放桌面
    cvShowImage("Week07",img);
    cvWaitKey(0);
}

week07-2_myTexture

從 https://gist.github.com/jsyeh(myTexture.cpp)複製程式碼,earth.jpg(myEarth.cpp)要放在桌面/freeglut/bin裡面

#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);
    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();
    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();
}
增加
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();

week07-3_myEarth

從 https://gist.github.com/jsyeh(myEarth.cpp)複製程式碼,earth.jpg(myEarth.cpp)要放在桌面/freeglut/bin裡面

#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 background");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    myTexture("earth.jpg");
    sphere = gluNewQuadric();
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
}

沒有留言:

張貼留言