1

I want to know how can I adjust the depth value of each fragment (using gl_FragDepth). I render point primitives with gl_PointSize=5.0.

I know that in fragment shader, I can access to each fragment via gl.PointCoord.xy, but i failed to reset different depth values to each fragment?

Any help will be appreciated.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
nina
  • 13
  • 7
  • when using point primitives, all fragements of this point have the same depth value, i want to adjust depth value and give different depth value to each fragment of this point. when i use gl_fragDepth i get this error: gl_fragDepth undeclared identifier. ??? what is the problem – nina Feb 20 '18 at 18:51
  • Yes, i know is exactly what i did but, is it work with point primitives or only with triangle primitives? – nina Feb 20 '18 at 19:06

1 Answers1

3

In WebGL 1.0 the extension EXT_frag_depth has to be enabeld to access gl_FragDepth:

You can write to the built in uniform out float gl_FragDepth; in the fragment shader. The value of gl_FragDepth is set to the depth buffer. Since the depth is set in the fragment shader it affects a single fragment.

The valid range for the depth is set by glDepthRange. By default the depth range is from 0 to 1. 0 is near and 1 is far.

If gl_FragDepth is not set, then the z component of gl_FragCoord will be set to the depth buffer.

The depth of a vertex position of a primitve (in your case a point) is specified by setting the z component of gl_Position in the vertex shader. If -gl_Position.w < gl_Position.z and gl_Position.z < gl_Position.w then the depth (z component) is in the clip space.

Th clip space coordinate is transformed to the normalized device space coordinate by dividing by the w component of gl_Position (perspective divide, beacause gl_Position is a Homogeneous coordinat). So the z components, between the near and the far plane of the normalized device space, are in the range [-1, 1].

Finally the z component of the normalized device space (in range [-1, 1]) is mapped to the depth range, which is by default in [0, 1].


Note, in mediump vec2 gl_PointCoord is 2 dimensional input variable to the fragment shader. You cannot write to it.

See OpenGL ES Shading Language 1.00 Specification, 7.2 Fragment Shader Special Variables, page 60:

The fragment shader has access to the read-only built-in variable gl_PointCoord. The values in gl_PointCoord are two-dimensional coordinates indicating where within a point primitive the current fragment is located. They range from 0.0 to 1.0 across the point.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • thanks for the reply, what if i do the rendering in floating textures and i change the depth of each fragment of the point manually? I get different values for each fragment or the same values when using gl.Pointcoord? – nina Feb 20 '18 at 19:56
  • If a point has a size of more the 1.0 (e.g. by setting `gl_PointSize=5.0.`) then the fragment shader is executed more then once, becaus the "point" takes palce over more than one fragment. The depth of this fragments can be set individually. – Rabbid76 Feb 20 '18 at 20:00