您需要將每個紋理綁定到不同的紋理單元,然後使用多紋理座標。它會看起來像這樣(假設你已經有紋理):
glActiveTexture (GL_TEXTURE0); // First texture is going into texture unit 0
glBindTexture (GL_TEXTURE_2D, tex0);
glEnable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE1); // Second texture is going into texture unit 1
glBindTexture (GL_TEXTURE2D, tex1);
glEnable (GL_TEXTURE_2D);
glUseProgram (yourGLSLProgramID);
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1
///... set the rest of your uniforms...
glBegin (GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
glVertexCoord2f(0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
glVertexCoord2f(width, 0.0);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
glVertexCoord2f(width, height);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
glVertexCoord2f(0.0, height);
glEnd();
[OpenGL Wiki有一篇關於如何工作的文章](http://www.opengl.org/wiki/Sampler_%28GLSL% 29個#Binding_textures_to_samplers)。 –
@NicolBolas你知道他們爲什麼在這些例子中使用GL_TEXTURE0 + x而不是'GL_TEXTURE0','GL_TEXTURE2'等。 – user1118321
@ user1118321由於'GL_TEXTURE#'只能達到31,而*實際*限制可能要高得多。你應該總是使用'GL_TEXTURE0 + x'。 –