在进行纹理贴图是,一次是把一副图像应用在个一个多边形上,多重纹理允许应用几个纹理,在纹理操作管线上把它们逐个应用到同一个多边形上使用glGetIntegerv(GL_MAX_TEXTURE_UNITS)查询当前OPENGL最多支持纹理数量
|
多重纹理的步骤 1.建立纹理单元 纹理图像glTexImage2D 纹理过滤 glTexParameteri(…, GL_TEXTURE_MAG_FILTER, …); 纹理环境应用glTexParameteri(…, GL_TEXTURE_WRAP_S, …); 纹理坐标自动生成(一般不需要) 顶点数组指定(如果需要的话) 每个纹理单位根据它的纹理状态,把原来的片段颜色与纹理图像进行混合,然后,把上述操作产生的片断颜色传递给以下个纹理单元 |
2.指定纹理顶点
|
激活纹理 为了向每个纹理单元分配纹理详细,使用glActiveTexture ();选择要修改的纹理单元,再次之glTexImage,glTexParameteri,glTexEnv,glTexGen,glBindTexture只影响当前纹理 |
#include "header.h" static GLubyte texels0[32][32][4]; static GLubyte texels1[16][16][4]; void makeCheckImages(void) { int i, j; for (i = 0; i < 32; i++) { for (j = 0; j < 32; j++) { texels0[i][j][0] = (GLubyte) i; texels0[i][j][1] = (GLubyte) j; texels0[i][j][2] = (GLubyte) (i*j)/255; texels0[i][j][3] = (GLubyte) 255; } } for (i = 0; i < 16; i++) { for (j = 0; j < 16; j++) { texels1[i][j][0] = (GLubyte) 255; texels1[i][j][1] = (GLubyte) i; texels1[i][j][2] = (GLubyte) j; texels1[i][j][3] = (GLubyte) 255; } } } void init(void) { GLuint texNames[2]; glClearColor (1.0, 1.0, 1.0, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); makeCheckImages(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(2, texNames); glBindTexture(GL_TEXTURE_2D, texNames[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, texNames[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); /* Use the two texture objects to define two texture units * for use in multitexturing */ glActiveTexture (GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texNames[0]); glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glMatrixMode (GL_TEXTURE); glLoadIdentity(); glTranslatef(0.5f, 0.5f, 0.0f); glRotatef(45.0f, 0.0f, 0.0f, 1.0f); glTranslatef(-0.5f, -0.5f, 0.0f); glMatrixMode (GL_MODELVIEW); glActiveTextureARB (GL_TEXTURE1_ARB); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texNames[1]); glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0.0, 0.0); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 1.0, 0.0); glVertex2f(0.0, 0.0); glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0.5, 1.0); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 0.5, 0.0); glVertex2f(50.0, 100.0); glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 1.0, 0.0); glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 1.0, 1.0); glVertex2f(100.0, 0.0); glEnd(); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D(0.0, 100.0, 0.0, 100.0 * (GLdouble)h/(GLdouble)w); else gluOrtho2D(0.0, 100.0 * (GLdouble)w/(GLdouble)h, 0.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(250, 250); glutInitWindowPosition(100, 100); glutCreateWindow(“多重纹理”); glewInit(); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; }