GLSL-如何避免多边形重叠?

永恒的

我在GLSL中制作每个像素的照明着色器。图中是一个多维数据集网格,其中根据绘制顺序,将应遮挡的面呈现在其他面的上方。当我切换到固定功能的opengl照明设置时,这不是问题。是什么原因造成的,我该如何解决?

看起来像什么

顶点着色器:

varying vec4 ecPos;
varying vec3 normal;

void main()
{ 

gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

/* first transform the normal into eye space and normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);

/* compute the vertex position  in camera space. */
ecPos = gl_ModelViewMatrix * gl_Vertex;


gl_Position = ftransform();

} 

片段着色器:

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;

/* a fragment shader can't write a verying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

// Compute the ligt direction
lightDir = vec3(gl_LightSource[0].position-ecPos);

/* compute the distance to the light source to a varying variable*/
dist = length(lightDir);


/* compute the dot product between normal and 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;
}

着色器大多是这些http://www.lighthouse3d.com/tutorials/glsl-tutorial/point-light-per-pixel/的修改版本

瓦伦丁

那么您可能已经忘记启用了GL_DEPTH_TEST

您需要调用glEnable(GL_DEPTH_TEST);此已启用的OpenGL,以测试不同基元的深度。因此,当OpenGL渲染时,它不会在其他实际需要落后的东西之上随机渲染东西!

http://www.opengl.org/sdk/docs/man/xhtml/glEnable.xml

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章