電腦圖學 2023-03-29 Week07
0. 公布 T-R-T 小考成績1. 主題: 貼圖 Texture
2. 主題: OpenCV
3. 貼圖 15行程式碼
4. 實作 glTexCoord2f(tx, ty) 貼圖座標
介紹Texture
去 https://jsyeh.org/3dcg10/
下載date.zip--->解壓縮至此
下載windows.zip--->解壓縮至此啟動Texture.exe
data.zip > windows\data(解壓後的data拉去windows資料夾下)
3D的世界都用3角形,但人類比較好理解4邊形
glColor4f 調整顏色
glTexCoord2f 下面四邊形的點的上下
glVertex3f 上面四邊形的點的xyz
step01
*期中考題目*
glPushMatrix();//備份矩陣
glTranslatef(x,y,z);//移動
glRotatef(角度,x,y,z);//移動
glRotatef(角度,x,y,z);//移動
glScalef(x,y,z);//縮放
glBegin(GL_POLYGON);//開始畫(多邊形)
glBegin(GL_POLYGON);//開始畫(多邊形)
glColor3f(r,g,b);//色彩
glNormal3f(nx,ny,nz);//打光的法向量
glTexCoord2f(tx,ty);//貼圖座標
glVertex3f(x,y,z);//頂點
glEnd();//結束畫
glPopMatrix();//還原矩陣
step02
OpenCV專案
安裝完後重開codeblock
安裝葉正聖老師上課軟體--OpenCV程式
安裝第二步時 下面兩個有add的都可以選
step1: 開啟settings--compiler
在Link libraries打上
cv210
cxcore210
highgui210
step4:File-New-Empty file
另存成week07-1_opencv_cvLoadimage_cvShowimage.cpp
放在week07資料夾
圖片image.jpg也丟在同資料夾
#include <opencv/highgui.h>
int main()
{
IplImage *img =cvLoadImage("image.jpg");
cvShowImage("week07",img);
cvWaitKey(0);
}
int main()
{
IplImage *img =cvLoadImage("image.jpg");
cvShowImage("week07",img);
cvWaitKey(0);
}
前往老師頁面
https://gist.github.com/jsyeh
複製mytexture_sample.cpp程式碼至CodeBlock
找地球圖片 > 存到freeglut > bin > earth.jpg
freeglut > bin > earth.jpg
GLUT專案
week07-2_myTexture
去https://gist.github.com/jsyeh
複製myEarth.cpp
另存myEarth.jpg到freeglut-bin資料夾
#include <opencv/highgui.h>
#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);
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();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week7 texture background");
glutIdleFunc(display);
glutDisplayFunc(display);
myTexture("myEarth.jpg");
sphere = gluNewQuadric();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
世界地圖
week07-3_myEarth
同上 去https://gist.github.com/jsyeh
複製myEarth.cpp
另存myEarth.jpg到freeglut-bin資料夾
#include <opencv/highgui.h>
#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("week7 texture background");
glutIdleFunc(display);
glutDisplayFunc(display);
myTexture("myEarth.jpg");
sphere = gluNewQuadric();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
沒有留言:
張貼留言