首页 > 解决方案 > 从 gl_FragColor 切换到“out”值没有渲染

问题描述

我正在升级很多旧的 OpenGL 代码,包括片段着色器,并且尝试删除对gl_FragColor的引用似乎不起作用。

#version 330

varying vec2 uvs;
uniform vec3 cameraPos;
uniform vec4 brushColor;
uniform sampler2D meshTexture;
uniform sampler2D paintTexture;
uniform int paintFboWidth;

layout(location = 0) out vec4 meshColor; // added this

void main() {
    vec2 paintUvs = vec2(gl_FragCoord.x/paintFboWidth, gl_FragCoord.y/paintFboWidth);
    float paintIntensity = texture(paintTexture, paintUvs).r;
    vec4 meshColor = texture(meshTexture, uvs);
    vec3 diffuseColor = mix(meshColor.rgb, brushColor.rgb, paintIntensity);

    meshColor = vec4(diffuseColor, 0); // added this
    //gl_FragData[0] = vec4(diffuseColor, 0); // trying to remove this
}

据我了解,如果我切换到out变量,它将正常工作,但是当我写入meshColor而不是gl_FragData[0]时,我看不到屏幕上显示任何内容,也没有 GL 错误。

我是否需要在着色器中或 C++ 代码外部执行任何其他操作才能使用此输出变量?

标签: openglglsl

解决方案


我想到了。我在着色器中隐藏了一个参数,所以它实际上并没有写入输出变量。

layout(location = 0) out vec4 meshColor;
...
    vec4 meshColor = texture(meshTexture, uvs);
    ...

    meshColor = vec4(diffuseColor, 0); // writing to the local instance
}

推荐阅读