標籤:opengl 紋理
紋理映射
一m*n的像素數組,我們並不將其看做有離散元素構成的數組,而是將其視作一個連續數組。該數組中的任意一點通過變數s和t來定義。則每個座標(s,t)都對應一個像素值。現在考慮一個三維空間中的一個幾何對象。其表面上的每一點都對應於三維全局座標系中的一個座標(x,y,z),如果能通過一對函數映射將對象座標系的每一點(x,y,z)與紋理座標中的一點(s,t)建立關聯,則可用紋理映像中的顏色或灰階值來確定對象表面上可見點的顏色。
OpenGL對此問題的解決方案是強制應用程式為每個頂點都定義紋理座標。然後插值。
紋理圖的建立
void glTexImage2D(GLenum target,GLint level,GLint iformat,
GLsizei width,GLsizei height,GLint border,
GLenum format,GLenum type,GLvoid* texels)
//該函數依據類型為type,格式為format的數組texels建立一個二維的大小為height*width的紋理。該紋理的邊界被指定為b個紋理元寬。映像的資料格式為iformat。level用於多級漸進紋理。
使用紋理前先開啟該功能:
glEnable(GL_TEXTURE_2D);
註:紋理映像的維數必須為2的整數次冪。
同理建立一維紋理和三維紋理也是用一樣的方式glTexImage1D()和glTexImage3D().
紋理座標
紋理座標也是OpenGL狀態的一部分。與頂點一樣,其內部形式也是四維的:
void glTexCoord<1234>(sifd)(type scoord,.....)
void glTexCoord<1234>(sifd)v(type* v)
二維情況下,我們假定glTexCoord()指定的紋理元素組在一個連續矩形中,該矩形中的任意一點的為奴隸座標都位於區間(0,1)內。所以左下角座標為(0,0),右上方座標為(1,1)
紋理參數
void glTexParameter<if>(GLenum target,GLenum name,type value)
void glTexParamer<if>v(GLenum target,GLenum name,type value)
//target為GL_TEXTURE_1D、GL_TEXTURE_2D或GL_TEXTURE_3D,將參數name設為true
這些必要的參數決定了當s,t,r或q的值超過(0,1)時做何處理以及如何運用採樣和濾波
對於超出(0,1)的有兩種處理方式:
GL_REPEATE:若該值為正,則直接使用其小數部分,若為負,則取其與其相加為正的最小整數。
GL_CLAMP:若為負,強製取0,若超過1,強製取1。
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEATE)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEATE)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
繪製一個旋轉的紋理立方體:
#include <GL/glut.h>GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};GLfloat colors[][4] = {{0.0,0.0,0.0,0.5},{1.0,0.0,0.0,0.5},{1.0,1.0,0.0,0.5}, {0.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5}, {1.0,0.0,1.0,0.5}, {1.0,1.0,1.0,0.5}, {0.0,1.0,1.0,0.5}};void polygon(int a, int b, int c , int d){/* draw a polygon via list of vertices */ glBegin(GL_POLYGON);glColor4fv(colors[a]);glTexCoord2f(0.0,0.0); glVertex3fv(vertices[a]);glColor4fv(colors[b]); glTexCoord2f(0.0,1.0); glVertex3fv(vertices[b]);glColor4fv(colors[c]); glTexCoord2f(1.0,1.0); glVertex3fv(vertices[c]);glColor4fv(colors[d]); glTexCoord2f(1.0,0.0); glVertex3fv(vertices[d]);glEnd();}void colorcube(void){/* map vertices to faces */polygon(0,3,2,1);polygon(2,3,7,6);polygon(3,0,4,7);polygon(1,2,6,5);polygon(4,5,6,7);polygon(5,4,0,1);}static GLfloat theta[] = {0.0,0.0,0.0};static GLint axis = 2;void display(void){/* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glRotatef(theta[0], 1.0, 0.0, 0.0);glRotatef(theta[1], 0.0, 1.0, 0.0);glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube();glutSwapBuffers();}void spinCube(){/* Idle callback, spin cube 2 degrees about selected axis */theta[axis] += 0.1;if( theta[axis] > 360.0 ) theta[axis] -= 360.0;/* display(); */glutPostRedisplay();}void mouse(int btn, int state, int x, int y){/* mouse callback, selects an axis about which to rotate */if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;}void myReshape(int w, int h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW);}void key(unsigned char k, int x, int y){if(k == ‘1‘) glutIdleFunc(spinCube);if(k == ‘2‘) glutIdleFunc(NULL);}voidmain(int argc, char **argv){ GLubyte image[64][64][3]; int i, j, c; for(i=0;i<64;i++) { for(j=0;j<64;j++) { c = ((((i&0x8)==0)^((j&0x8))==0))*255; image[i][j][0]= (GLubyte) c; image[i][j][1]= (GLubyte) c; image[i][j][2]= (GLubyte) c; } } glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube");/* need both double buffering and z buffer */glutReshapeFunc(myReshape);glutDisplayFunc(display);glutIdleFunc(spinCube); glutMouseFunc(mouse);glutKeyboardFunc(key);glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D); glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0,GL_RGB,GL_UNSIGNED_BYTE, image); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glutMainLoop();}
運行得到:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/48/73/wKioL1QIQG_gntLQAAlLstnD1i4926.gif" title="若水GIF_2014年9月4日18點34分31秒.gif" alt="wKioL1QIQG_gntLQAAlLstnD1i4926.gif" />
OpenGL學習(七)紋理映射