我在从 OBJ 文件加载的对象上使用片段着色器进行 Lambert 和雾着色,但我对多边形有一个小问题。当我将相机位置从对象移开时,我的网格中的一些多边形被“剔除”。
例子:
老实说,我不知道为什么会发生这种情况,以及为什么只有当我远离物体时才会发生这种情况。这是我的着色器:
顶点着色器
# version 400
out struct vData {
vec4 pos;
vec4 texcoord;
vec3 normal;
} fdata;
void main() {
fdata.texcoord = gl_MultiTexCoord0;
fdata.normal = normalize(gl_NormalMatrix * gl_Normal);
fdata.pos = gl_Vertex;
gl_Position = gl_ModelViewProjectionMatrix * fdata.pos;
}
片段着色器
# version 400
layout(binding=0) uniform sampler2D mainTexture;
uniform vec4 lightColor;
uniform vec4 lightPos;
in struct vData {
vec4 pos;
vec4 texcoord;
vec3 normal;
} fdata;
vec4 ComputeLambert(const in vec3 lightdir, const in vec4 lightColor, const in vec3 normal, const in vec4 diffuse) {
float nDotL = dot(normal, lightdir);
return diffuse * lightColor * max(nDotL, 0.0);
}
float ComputeFogFactor(const in vec4 coord, const in float density) {
const float LOG2 = 1.442695;
float z = coord.z / coord.w;
float fogFactor = exp2( -density * density * z * z * LOG2 );
return clamp(fogFactor, 0.0, 1.0);
}
void main() {
vec3 mypos = fdata.pos.xyz / fdata.pos.w;
vec3 lightdir = lightPos.xyz / lightPos.w;
vec3 direction = normalize(lightdir - mypos);
vec4 diffuse = texture2D(mainTexture, fdata.texcoord.st);
diffuse = ComputeLambert(direction,lightColor, fdata.normal, diffuse);
float fogFactor = ComputeFogFactor(gl_FragCoord,gl_Fog.density);
vec4 finalcolor = mix(gl_Fog.color, diffuse, fogFactor);
//vec4 finalcolor = vec4(1.0,1.0,1.0,1.0);
finalcolor.a = 1.0;
gl_FragColor = finalcolor;
}
我确保在调用绘图之前禁用面部剔除,所以我很确定这不是问题。有谁知道我能做些什么吗?