GL_TEXTURE0之后的每个纹理(插槽0上的纹理)都是黑色。
片段着色器(通过更改权重来检查不同纹理的示例代码):
#version 330
uniform sampler2D g_ColorTex;
uniform sampler2D g_DepthTex;
uniform sampler2D g_DofNearBlurTex;
uniform sampler2D g_DofDownBlurTex;
in vec4 g_VertexPosition;
in vec2 g_ScreenCoords;
layout (location = 0) out vec4 FragColor;
void main()
{
// Changing weights to check textures
FragColor = 1.00001f * vec4(texture(g_ColorTex , g_ScreenCoords).rgb, 1.0f);
FragColor += 0.00001f * vec4(texture(g_DepthTex , g_ScreenCoords).rgb, 1.0f);
FragColor += 0.00001f * vec4(texture(g_DofNearBlurTex, g_ScreenCoords).rgb, 1.0f);
FragColor += 0.00001f * vec4(texture(g_DofDownBlurTex, g_ScreenCoords).rgb, 1.0f);
}
我上传这样的纹理(摘录!):
::glUseProgram(ShaderHandle);
unsigned int TextureIndex;
TextureIndex = ::glGetUniformLocation(ShaderHandle, "g_ColorTex");
::glUniform1i(TextureIndex, 0);
::glActiveTexture(GL_TEXTURE0);
::glBindTexture(GL_TEXTURE_2D, TextureHandle);
::glBindSampler(GL_TEXTURE0, SamplerHandle);
TextureIndex = ::glGetUniformLocation(ShaderHandle, "g_DepthTex");
::glUniform1i(TextureIndex, 1);
::glActiveTexture(GL_TEXTURE1);
::glBindTexture(GL_TEXTURE_2D, TextureHandle);
::glBindSampler(GL_TEXTURE1, SamplerHandle);
TextureIndex = ::glGetUniformLocation(ShaderHandle, "g_DofNearBlurTex");
::glUniform1i(TextureIndex, 2);
::glActiveTexture(GL_TEXTURE2);
::glBindTexture(GL_TEXTURE_2D, TextureHandle);
::glBindSampler(GL_TEXTURE2, SamplerHandle);
TextureIndex = ::glGetUniformLocation(ShaderHandle, "g_DofDownBlurTex");
::glUniform1i(TextureIndex, 3);
::glActiveTexture(GL_TEXTURE3);
::glBindTexture(GL_TEXTURE_2D, TextureHandle);
::glBindSampler(GL_TEXTURE3, SamplerHandle);
//...
::glDrawElements(GL_TRIANGLES, Mesh->m_Indices.size() * 3, GL_UNSIGNED_INT, 0);
我尝试更改纹理插槽并上传不同的纹理,但我总是得到黑屏,直到我在GL_TEXTURE0上使用纹理。
答案 0 :(得分:6)
::glBindSampler(GL_TEXTURE0, SamplerHandle);
那是错的。 glBindSampler
采用纹理图像单元0
,而不是GL_TEXTURE0
。
处理此问题的最佳方法如下:
glUniform1i(TextureIndex, index);
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, TextureHandle);
glBindSampler(index, SamplerHandle);