The perspective version of optical illusion is great, but the problem remains until an object touches the ground or another object!
1.) ... The first example with the solution is difficult to use!
2.) ... The second example is easy to understand!
3.) ... What's the point of all this stuff with gl_FragDepth, gl_DepthRange, ....?
1.) The first example with solution is difficult to use!
This doesn't behave like an object intended for the depth buffer. As far as that goes, it's just a circle!Here's the solution: https://paroj.github.io/gltut/Illumination/Tut13%20Deceit%20in%20Depth.html
Part of the fragment shader's output is a depth value. If you don't write one, OpenGL likes to use gl_FragCoord.z , the fragment shader's depth output. This value is subjected to a depth test against the current depth value and, if the test passes, is written to the depth buffer.
https://paroj.github.io/gltut/Illumination/Tutorial%2013.html
(Image-1) How can I in OpenGL! |
![]() |

Related:
https://www.cl.cam.ac.uk/teaching/1718/FGraphics/6.%20Advanced%20Shader%20Techniques.pdf
2.) The second example is easy to understand!
https://www.raywenderlich.com/2323-opengl-es-pixel-shaders-tutorial
(Bild-2) Gl_FragDepth Manipulation! |
![]() |
When working with a 3D sphere, vector normalization makes equations much easier, and the same applies to procedural textures, especially noise. Functions like smoothing and interpolation are much easier when performed on a square grid.

3.) Why all this with gl_FragDepth, gl_DepthRange, ....?
It's always good to know, as it allows you to display 3D objects in a different way. You must never forget that no matter how you go about it, 3D doesn't exist and is only an illusion. Whether you use triangles or other formulas is irrelevant; only the result and the improved appearance count here!
As with everything else, the simplest is usually the best!
Further links:
Source code for MS Visual Studio 10:
http://tubafun.bplaced.net/public/sphere_shader.zip
Description:
http://11235813tdd.blogspot.com/2013/04/raycasted-spheres-and-point-sprites-vs.html !
Calculation of gl_FragDepth ;-)
/*something*/ = Position in space
https://microeducate.tech/glsl-gl_fragcoord-z-calculation-and-setting-gl_fragdepth/
https://stackoverflow.com/questions/10264949/glsl-gl-fragcoord-z-calculation-and-setting-gl-fragdepth
Also good for understanding:
►► http://www.songho.ca/opengl/gl_transform.html
float far=gl_DepthRange.far; float near=gl_DepthRange.near;
vec4 eye_space_pos = gl_ModelViewMatrix * /*something*/
vec4 clip_space_pos = gl_ProjectionMatrix * eye_space_pos;
float ndc_depth = clip_space_pos.z / clip_space_pos.w;
float depth = (((far-near) * ndc_depth) + near + far) / 2.0;
gl_FragDepth = depth;
Implementing Soft Particles in WebGL and OpenGL Particles are one of the easiest ways to enhance the visual appearance of a scene. Using gl_FragDepth, gl_FragCoord.z, and gl_FragCoord.w, you can significantly save processing power!
https://keaukraine.medium.com/implementing-soft-particles-in-webgl-and-opengl-es-b968d61133b0
(Image-3) Gl_FragDepth manipulation, deception also for particle effects! |
![]() |

https://github.com/ssloy/glsltuto
(Image-4) Gl_FragDepth Manipulation, deception and effects! |
![]() |
