1

I want some special process on depth test. First of all, I want to know the correct depth calculation in shader by self instead of GPU depth test.

If I disable depth test, and write depth buffer to achieve same effect as depth test:

Pixel Shader:

void main(void) {
    float depth = (position.z / position.w + 1.0) * 0.5
    gl_FragColor = vec4(depth, depth, depth, 1.0);
}

It's correct implement? And how about the different depth length? (depth16 or depth24)

Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47
  • The depth calculation is quite arbitrary in modern OpenGL. In the vertex shader, it is you, the programmer, who sets the clip space position of the vertex (`gl_Position`) using a projection transform. The hardware, after clipping in this clip space, does the perspective divide, resulting in 3D points. These are then interpolated in a perspective-correct manner for every fragment of a given triangle; the resulting z values goes in to the depth buffer. Which part of this process do you want to alter? – legends2k Dec 31 '15 at 08:32
  • As you comment, the depth cal maybe little different in different GPU, so I should calculate any depth by self algorithm? My case is , 2 meshes, for example, a man with a cloth, sometimes body cross cloth during bone animation. It's hard to make collision test on vertex, we just want to fix this fault by depth calculation. – Jiangshanbin Dec 31 '15 at 09:06
  • @Jiangshanbin: Ir is unclear what `position` actually is. Assuming it is a clip space position, it might be OK. However, this code will never achieve the same effect as the depth _test_, since there is no testing involved now, so each objects just overwrites the color buffer with some grayscale value, no matter how far away they are. – derhass Dec 31 '15 at 13:31
  • 1
    @Jiangshanbin: However, the main question is: what are you actually trying to achieve? GL already supports different depth formats. And GL also allows writing to the depth value in the shader. It also supports rendering to depth textures in case you need those values in a later pass. So why are you trying to put the depth values in a color buffer? – derhass Dec 31 '15 at 13:35
  • _"It's hard to make collision test on vertex, we just want to fix this fault by depth calculation."_ It's even harder to implement collision detection per-pixel, by the way. You'd probably be better off using an occlusion query and a couple frame latency. – Andon M. Coleman Dec 31 '15 at 16:22

0 Answers0