GLSL纹理不起作用

时间:2012-09-17 22:43:25

标签: c++ opengl glsl

请考虑以下事项:

virtual void Draw()
{
    _texture->Bind(0);

    _shader->Begin();
    _shader->SetUniform("WVPMatrix", _mvp);
    _shader->SetUniform("InColor", _color);
    _shader->SetUniformImm("InTexture", 0);
    _vbo.GetDeclaration().Activate();
    _vbo.Render(GL_QUADS, _ibo, 4);
    _shader->End();
}

void Texture2D::Bind(int index)
{
    if (index != -1)
    {
        glActiveTexture(GL_TEXTURE0 + index);
    }

    glBindTexture(GL_TEXTURE_2D, _textureID);
}

调用_shader-> Begin(),_ shader-> SetUniform,_shader-> SetUniformImm,_vbo.GetDeclaration()。激活和_vbo.Render就像我设置片段着色器以呈现静态一样颜色(甚至穿制服的颜色)也很好。

现在我正在尝试渲染一个texture2d(它加载得很好,我用立即模式测试)然后我得到一个黑屏,为什么?

顶点:

#version 330

layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec2 VertexTexCoord;

uniform mat4 WVPMatrix;

out vec2 TexCoord0;

void main()
{
    TexCoord0 = VertexTexCoord;

    gl_Position = WVPMatrix * vec4(VertexPosition, 1);
}

片段:

#version 330

in vec2 TexCoord0;

uniform sampler2D InTexture;
uniform vec4 InColor;

out vec4 OutFragColor;

void main()
{
    OutFragColor = texture(InTexture, TexCoord0) * InColor;
}

PS:InColor是白色的,所以它不会弄乱纹理颜色,TexCoord0也有效,因为我已经将OutFragColor上的R和B设置为TexCoord0的S和T来调试它的值。

1 个答案:

答案 0 :(得分:5)

(发布簿记答案)

提问者忘记为他的纹理设置缩小过滤器,因此纹理不完整且不显示。

需要致电glTexParameteri(GL_TEXTURE_2D, GL_MIN_FILTER, GL_LINEAR);(或GL_NEAREST)