我尝试在1个片段着色器中访问2个纹理。
为此我有2个纹理,我用以下代码设置:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vbuff);
glTexCoordPointer(2, GL_FLOAT, 0, tbuff);
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture0);
glEnable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, texture1);
glEnable(GL_TEXTURE_2D);
我尝试将它们与以下片段着色器结合使用:
uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
{
vec4 texval1 = texture2D(tex0, TextureCoordOut);
vec4 texval2 = texture2D(tex1, TextureCoordOut);
gl_FragColor = 0.5*(texval1 + texval2)
}
不知怎的,这不起作用,它只显示第二个纹理。
PS:我正在开发Windows7上的着色器,但是想在Android设备上使用C ++代码和着色器与Android NDK。
编辑:我开始工作,我在这里发布代码,以便其他人可以从中受益:
顶点着色器
varying vec2 TextureCoordOut;
void main()
{
gl_Position = ftransform();
TextureCoordOut = vec2(gl_MultiTexCoord0);
}
片段着色器
varying mediump vec2 TextureCoordOut;
uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
{
gl_FragColor = texture2D(tex0, TextureCoordOut) * texture2D(tex1, TextureCoordOut);
}
呈现
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vbuff); //2 dimensional vertex buffer
glTexCoordPointer(2, GL_FLOAT, 0, tbuff); // 2 dimensional textures
glColor4f(1, 1, 1, 1);
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, mapid1);
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, mapid2);
glUseProgram(shader);
glUniform1i(glGetUniformLocation(gfx.prog, "tex0"), 0); //GL_TEXTURE0
glUniform1i(glGetUniformLocation(gfx.prog, "tex1"), 1); //GL_TEXTURE1
render....
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
答案 0 :(得分:7)
仅仅将这些纹理绑定到各个单位是不够的。你还需要告诉着色器从哪里获取纹身为它的sampler2D
制服。这是通过将相应的整数均匀值设置为您要使用的纹理单元来完成的:
//get uniform locations (either init code or render code)
int locTex0 = glGetUniformLocation(program, "tex0");
int locTex1 = glGetUniformLocation(program, "tex1");
...
//bind textures to texture units
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture0);
//bind program and specify texture units for each sampler2D uniform
glUseProgram(program);
glUniform1i(locTex0, 0); //use texture bound to GL_TEXTURE0
glUniform1i(locTex1, 1); //use texture bound to GL_TEXTURE1
...
draw
当然,这假设您知道如何使用着色器并指定统一值。
其他一些评论:
glEnable(GL_TEXTURE_2D)
,因为着色器决定它是否使用纹理。glVertexPointer
或glTexCoordPointer
),但这是一个不同的问题。