深度测试问题

luke1985

我有一个简单的OpenGL应用程序,我想绘制一个多维数据集,但深度缓冲区似乎以某种方式无法正常工作。我正在将SFML用于windowin系统,并将GLEW作为库。

主要内容如下:https : //pastebin.com/tq5t0TJN

...code...

网格类如下所示:https : //pastebin.com/YiWH8dWH

...code...

加载函数如下所示:https : //pastebin.com/vUNLn0gu-但它们似乎都能正常工作

...code...

现在我有了一个顶点着色器:https : //pastebin.com/EeP4RXHy

#version 330 core
layout (location = 0) in vec3 location_attrib;
layout (location = 1) in vec2 texcoord_attrib;
layout (location = 2) in vec3 normal_attrib;


out vec3 location;
out vec3 normal;


void main()
{
    normal = normal_attrib;
    gl_Position = vec4(location_attrib.x / 2, location_attrib.y  / 2, location_attrib.z  / 2, 1.0);
}

还有片段着色器:https : //pastebin.com/E9krMFZq

#version 330 core
out vec4 gl_FragColor;
//out float gl_FragDepth ;

in vec3 location;
in vec3 normal;

void main()
{
    vec3 normal_normalized = normalize(normal);
    vec3 light_dir = vec3(0, -1, 0);

    float light_amount = dot(normal_normalized, light_dir);

    vec3 color = vec3(1.0f, 0.5f, 0.2f);
    color = color * light_amount;
    color = clamp(color, vec3(0,0,0), vec3(1,1,1));
    color.r += 0.1;
    color.g += 0.1;
    color.b += 0.1;
    //gl_FragColor = vec4(color.r, color.g, color.b , 1.0);
    gl_FragDepth = location.z / 1000;
    gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0);
}

但是以这种方式获得的图像如下所示:

投递框

Can anybody tell what is the issue?

I tried to figure out what is wrong with the depth test, tried different combinations, but it all seems to not work.

Rabbid76

The depth of the fragment is explicitly set in the fragment shader:

gl_FragDepth = location.z / 1000;

The depth buffer can represent values in the range [0.0, 1.0] and the accuracy of the depth buffer is limited. In your case the depth buffer has 24 bits (settings.depthBits = 24;).
If the values ​​are outside this range or if the difference between the values ​​is so small that it cannot be represented with the accuracy of the depth buffer, then the depth test will not work correctly.

Anyway, the vertex shader does not write to the output variable location. Hence location.z is 0.0 for all fragments.


如果要将深度值映射到[0.0,1.0]的子范围,则可以用于glDepthRange指定映射。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章