I use a fragment shader to shade Lambert and Fog on objects loaded from an OBJ file, but I have a little problem with polygons. Some polygons in my grid are “rejected” when I move the position of the camera away from the object.
Example:

I honestly don’t know why this happens, and why it only happens when I move away from the object. Here are my shaders:
Vertex Shader
Fragment shader
# 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; }
I made sure I turned off Face Culling before my call for drawing, so I’m pretty sure that this is not a problem. Does anyone know if there is anything I can do about this?
source share