week07
1-1 https://jsyeh.org/3dcg10/ 下載data、win32資料夾,解壓縮windows.zip資料夾,將data資料夾拉進windows資料夾,打開Textrue.exe可以更改顏色、頂點座標
1-2 講解貼圖座標,改貼圖座標時,介於0到1之間。但超過範圍,如果是GL_REOEAT,會切回0到1之間重複。
2-1 期中模擬 https://jsyeh.org/gl/opengl_10_func.html3-1 上課軟體中下載OpenCV要勾選下面兩個其中之一
裝好後重開Codeblocks
3-2 設置
settings>Compiler>Serch directories>Compiler>Add>C:\OpenCV2.1\include
settings>Compiler>Serch directories>Linker>Add>C:\OpenCV2.1\lib
settings>Compiler>Serch directories>Linker settings>Add>cv210、cxcore210、highgui210
3-3 新增empty file命名為week07-1_opencv_cvLoadImage_cvShowImage
#include <opencv/highgui.h>
int main()
{
IplImage * img = cvLoadImage("檔名.png"); ///記得將圖片放在同一個位置
cvShowImage("week07", img);
cvWaitKey(0);
}
4-1 開新專案 Week07-2_myTexture
貼上從gist.github.com/jsyeh複製的myTextrue程式
把下載的earth.jpg放在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);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week07 texture");
glutDisplayFunc(display);
myTexture("earth.jpg");///這個圖檔, 現在不存在
///圖檔要放在特別的工作執行目錄
glutMainLoop();
}
4-2 改void display()程式
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///glutSolidTeapot( 0.3 );
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();
}
4-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("week10 texture background");
glutIdleFunc(display);
glutDisplayFunc(display);
myTexture("myEarth.jpg");
sphere = gluNewQuadric();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
沒有留言:
張貼留言