我正在使用片段着色器对我从OBJ文件中加载的物体进行Lambert和Fog着色,但我的模型中有一些小问题。当我将相机位置远离物体时,我的网格中的某些多边形被“剔除”。
例如:
老实说,我不知道为什么会发生这种情况,以及为什么只会在我远离物体时发生。以下是我的着色器:
顶点着色器
例如:
![](https://i.imgur.com/f9gjy2a.png)
顶点着色器
# 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;
}
在我的绘图调用之前,我确保禁用了面剔除,所以我相当确定这不是问题。有人知道我能做些什么吗?