opengl - GLSL - How to avoid polygons overlapping? -
i'm making per pixel lighting shader in glsl. pictured grid of cubes where, depending on order they're drawn, sides should obscured rendered on top of others. not problem when switch fixed function opengl lighting setup. causing it, how fix it?
vertex shader:
varying vec4 ecpos; varying vec3 normal; void main() { gl_texcoord[0] = gl_texturematrix[0] * gl_multitexcoord0; /* first transform normal eye space , normalize result */ normal = normalize(gl_normalmatrix * gl_normal); /* compute vertex position in camera space. */ ecpos = gl_modelviewmatrix * gl_vertex; gl_position = ftransform(); }
fragment shader:
varying vec4 ecpos; varying vec3 normal; uniform sampler2d tex; uniform vec2 resolution; void main() { vec3 n,halfv,lightdir; float ndotl,ndothv; vec4 color = gl_lightmodel.ambient; vec4 texc = texture2d(tex,gl_texcoord[0].st) * gl_lightsource[0].diffuse; float att, dist; /* fragment shader can't write verying variable, hence need new variable store normalized interpolated normal */ n = normalize(normal); // compute ligt direction lightdir = vec3(gl_lightsource[0].position-ecpos); /* compute distance light source varying variable*/ dist = length(lightdir); /* compute dot product between normal , ldir */ ndotl = max(dot(n,normalize(lightdir)),0.0); if (ndotl > 0.0) { att = 1.0 / (gl_lightsource[0].constantattenuation + gl_lightsource[0].linearattenuation * dist + gl_lightsource[0].quadraticattenuation * dist * dist); color += att * (texc * ndotl + gl_lightsource[0].ambient); halfv = normalize(gl_lightsource[0].halfvector.xyz); ndothv = max(dot(n,halfv),0.0); color += att * gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv,gl_frontmaterial.shininess); } gl_fragcolor = color; }
the shaders modified versions of these http://www.lighthouse3d.com/tutorials/glsl-tutorial/point-light-per-pixel/
well you've forgot enable gl_depth_test
.
you need call glenable(gl_depth_test);
enabled opengl, test depth of different primitives. when opengl rendering won't randomly render things on top of other things needs behind!
Comments
Post a Comment