每次写入后OpenGL Frag Shader纹理消失

时间:2018-08-08 10:05:24

标签: opengl shader

我有一个采用以下纹理的程序:

enter image description here

通过FreeType2生成的

。基本上,它会为我要求绘制的每个字符创建一个纹理图集。如您所见,字符明亮而清晰。实际上,当我在RenderDoc中检查纹理时,可以看到小写字母“ i”的最左上角像素的值为71(在255中)或0.7098

接下来,引擎将字母组合成一个帧缓冲对象。这是通过纹理四边形完成的。顶点着色器:

#version 330

layout(location=0) in vec2 inVertexPosition;
layout(location=1) in vec2 inTexelCoords;
layout(location=2) in float inDepth;

out vec2 texelCoords;
out float depth;

void main()
{
    gl_Position = vec4(inVertexPosition.x,-inVertexPosition.y, 0.0, 1.0);
    texelCoords = vec2(inTexelCoords.x,1-inTexelCoords.y);
    depth = inDepth;
}

还有碎片着色器:

#version 330

layout(location=0) out vec4 frag_colour;

in vec2 texelCoords;
in float depth;

uniform sampler2D uTexture;
uniform vec4 uTextColor;

void main()
{
    vec4 c = texture(uTexture,texelCoords);
    frag_colour = uTextColor * c.r;
    gl_FragDepth = depth;
}

如您所见,它将像素颜色设置为红色通道的一个因素。

但是,当我通过RenderDoc查看FBO的内容并保存到此处时,您会看到:

enter image description here

如果您不透明地看待它(只是在Gimp下添加了第二层以更好地说明):

enter image description here

您可以看到与以前相比,该文本有些褪色。如果您查看小写字母“ i”的最左上角像素,则它现在的值为50.2,或者对于0-1的范围,它的值为0.50196(通过RenderDoc)。

接下来,当FBO通过另一个纹理四边形最终放置在屏幕上时,其褪色甚至更大。首先是顶点着色器:

#version 330

layout(location=0) in vec2 inVertexPosition;
layout(location=1) in vec2 inTexelCoords;

varying vec2 texelCoords;

void main()
{
    gl_Position = vec4(inVertexPosition.x,-inVertexPosition.y, 0.0, 1.0);
    texelCoords = vec2(inTexelCoords.x,1-inTexelCoords.y);
}

和片段着色器:

#version 330

precision highp float;

layout(location=0) out vec4 frag_colour;

varying vec2 texelCoords;

uniform sampler2D uTexture;

void main()
{
    vec4 c = texture(uTexture,texelCoords);
    frag_colour = c;
}

正如我所说,结果比以前更加褪色:

原件: enter image description here

为了清楚起见,使用gimp背景:

enter image description here

现在该像素的值为25.10.05139

每次渲染后导致这种褪色的原因是什么?

我认为必须注意,较亮的区域不会褪色。

我的Framebuffer创建代码

glGenFramebuffers(1, &m_framebuffer);
glGenTextures(1, &m_fboColorAttachment);
glGenTextures(1, &m_fboAdditionalInfo);
glGenTextures(1, &m_fboDepthStencil);

glCall(glBindFramebuffer,GL_FRAMEBUFFER, m_framebuffer);

    /* setup color output 0 */
    glBindTexture(GL_TEXTURE_2D, m_fboColorAttachment);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        glCall(glTexImage2D, GL_TEXTURE_2D, 0, GL_RGBA8, screenDimensionsX, screenDimensionsY, 0, GL_BGRA, GL_UNSIGNED_BYTE, nullptr);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fboColorAttachment, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    /* setup color output 1 */
    glBindTexture(GL_TEXTURE_2D, m_fboAdditionalInfo);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        glCall(glTexImage2D, GL_TEXTURE_2D, 0, GL_R32UI, screenDimensionsX, screenDimensionsY, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_fboAdditionalInfo, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    /* setup depth and stencil */
    glBindTexture(GL_TEXTURE_2D, m_fboDepthStencil);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        glCall(glTexImage2D, GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, screenDimensionsX, screenDimensionsY, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_fboDepthStencil, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

初始(红色)纹理创建:

glActiveTexture(GL_TEXTURE0);
glGenTextures(1,&textureData.texture);
glBindTexture(GL_TEXTURE_2D,textureData.texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 500, 500, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

我的混合完成

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

1 个答案:

答案 0 :(得分:0)

  

的值为71(在255中)或0.7098

不知道,您在这里的意思是什么。 71/255将是0.278。从0.7098中将181归一化255。看来您的“满分为255”仅仅是百分比值,超出了100%。

  

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

您将红色通道中的颜色值也用作alpha,因此,启用混合后,最终会得到0.7098*0.7098=.5038。由于结果将四舍五入为最接近的可代表值,因此我们宁愿将181/255 * 181/255 = .50382舍入为128/255

  

现在它的值为50.2,或者在0-1的范围内是0.50196

128/2550.50196078...

因此解决方案是:在不需要时禁用所有步骤的混合。或者,如果需要,请设置有用的Alpha值。

旁注:

  

现在该像素的值为25.10.05139

不知道这是什么意思,25.10.05139毫无明显关系,您必须再次确定地切换这些值的含义。

相关问题