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.