以下是我的片段着色器代码,用于在地板和球体上生成纹理,并在其上方生成雾化效果。
我收到一条错误消息,指出“片段着色器会写入多种类型的输出gl_FragData,gl_FragColor或用户绑定的碎片数据。”当我尝试编译它时。
out vec4 color;
out vec2 texCoord;
uniform int fogType;
uniform int enableFloorTexture;
uniform int sphereTexture;
uniform int objectType;
uniform sampler2D texture;
uniform sampler1D stexture;
void main()
{
vec4 finColor = color;
if (objectType == 1)
{
if(enableFloorTexture == 1)
{
finColor = color * texture2D( texture, texCoord );
}
}
else if (objectType == 2)
{
if(sphereTexture != 0)
{
finColor = color * texture1D( stexture, texCoord.x );
}
}
float fogFactor = 0.0;
float fogDepth = (18.0 - 0.0);
float fogDensity = 0.09;
vec4 fogColor = vec4(0.7, 0.7, 0.7, 0.5);
float z = gl_FragCoord.z / gl_FragCoord.w;
if(fogType == 1)
{
fogFactor = (18.0 - z) / fogDepth;
}
else if (fogType == 2)
{
fogFactor = exp( -fogDensity * z );
}
else if (fogType == 3)
{
fogFactor = exp( -fogDensity * fogDensity * z * z );
}
fogFactor = clamp(fogFactor, 0.0, 1.0);
if(fogType == 0)
{
gl_FragColor = finColor;
}
else
{
gl_FragColor = mix(fogColor, finColor, fogFactor);
}
}
最佳答案
使您的程序感到困惑的是,您正在使用color
作为输出,但是在程序中使用了它。这只是笔误吗?
如果要使用#version 400
输出格式,则输出应如下所示:
layout(location = 0) out vec4 color;