以2D表面为例展示纹理贴图,用opengl设置一个2D纹理,颜色存储在32*32*3的数组中,对应的纹理坐标为0<=s, t<=1.0. 画出几个正方形表面,分别以GL_CLAMP(纹理坐标大于1或小于0都被规范到1和0),GL_REPEAT(类似平铺,超过0或1的部分忽略整数位进行贴图),GL_MODULATE(纹理颜色与对象颜色相乘), GL_REPLACE(纹理颜色替换对象颜色)几种形式进行贴图。
#include <GLUT/GLUT.h> GLsizei winWidht = , winHeight = ; void init (void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-, , -, );
} void xyCoords (void)
{
glBegin(GL_LINES);
glColor3f(0.0, 0.0, 0.0);
glVertex2i(-, );
glVertex2i(, );
glVertex2i(, -);
glVertex2i(, );
glEnd();
} void lineTextureMapping (void)
{
GLint k;
GLubyte texLine []; for (k = ; k <= ; k +=) {
texLine [*k] = ;
texLine [*k+] = ;
texLine [*k+] = ;
texLine [*k+] = ;
} for (k = ; k <= ; k +=) {
texLine [*k] = ;
texLine [*k+] = ;
texLine [*k+] = ;
texLine [*k+] = ;
} glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage1D(GL_TEXTURE_1D, , GL_RGBA, , , GL_RGBA, GL_UNSIGNED_BYTE, texLine);
glEnable(GL_TEXTURE_1D); glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glTexCoord1f(0.25);
glVertex2f(-4.5, 4.0);
glTexCoord1f(1.0);
glVertex2f(-4.5, -4.0);
glEnd(); glDisable(GL_TEXTURE_1D);
} void surfaceTextureMapping (void)
{
GLint k, j;
GLubyte texArray [][][]; for (k = ; k < ; k++) {
for (j = ; j < ; j++) {
texArray [k][j][] = - k*j/;
texArray [k][j][] = + k*j/;
texArray [k][j][] = ;
texArray [k][j][] = ;
}
} glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, , GL_RGBA, , , , GL_RGBA, GL_UNSIGNED_BYTE, texArray);
glEnable(GL_TEXTURE_2D); glColor3f(0.5, 0.5, 0.5); // 与纹理值相乘,因此会发暗
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glTexCoord2f(3.0, 0.0);
glVertex2f(3.0, 0.0);
glTexCoord2f(3.0, 3.0);
glVertex2f(3.0, 3.0);
glTexCoord2f(0.0, 3.0);
glVertex2f(0.0, 3.0);
glEnd(); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(0.5, 0.5, 0.5); // 替换为纹理颜色,则不会像前一个图案那样发暗
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(-4.0, -4.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(-1.0, -4.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(-1.0, -1.0);
glTexCoord2f(0.0, 1.0);
glVertex2f(-4.0, -1.0);
glEnd();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // 不允许纹理坐标超过1,因此小于0的设为0,大于1的设为1
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(-4.0, 0.0);
glTexCoord2f(, 0.0);
glVertex2f(-1.0, 0.0);
glTexCoord2f(, );
glVertex2f(-1.0, 3.0);
glTexCoord2f(0.0, );
glVertex2f(-4.0, 3.0);
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glDisable(GL_TEXTURE_2D);
} void displayFcn (void)
{
glClear(GL_COLOR_BUFFER_BIT); // lineTextureMapping();
surfaceTextureMapping();
xyCoords(); glFlush();
} void winReshapeFcn (GLint newWidth, GLint newHeight)
{
glViewport(, , newWidth, newHeight);
} int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(winWidht, winHeight);
glutInitWindowPosition(, );
glutCreateWindow("texture mapping"); init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutMainLoop(); return ;
}