纹理映射
一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); } void main(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(); }
运行得到: