(Demo) Depth Buffer Visualization

GeeXLab demo - Depth Buffer Visualization - OpenGL / GLSL

Here is an update of the demo for the article about the visualization of the depth buffer I wrote long time ago. The demo of the original article is no longer available that’s why I coded it again.

The demo is available in the OpenGL 2.1 demopack in the following folder:

Load the main.xml file into GeeXLab (drag and drop is the fastest way) and that’s all.

GeeXLab demo - Depth Buffer Visualization - OpenGL / GLSL

The 3D scene (torus and cubes) is rendered into a render target. The render target textures (color and depth) are inputs of the pixel shader used for the visualization. In this shader, only the depth texture is used. The left part of the screen if the raw reading of the depth buffer and the right part is the linearized value of the depth buffer.

Here is the pixel shader for visualizing the depth buffer.

#version 120
varying vec4 v_uv;
uniform sampler2D color_tex; 
uniform sampler2D depth_tex; 
uniform vec4 camera_params; 
uniform vec2 mouse; 

float LinearizeDepth(float z)
  float n = camera_params.z; // camera z near
  float f = camera_params.w; // camera z far
  return (2.0 * n) / (f + n - z * (f - n));	

void main() 
  vec2 uv = v_uv.xy; 
  vec4 fragcol = vec4(1.0, 0.0, 0.0, 1.0);
  float d = texture2D(depth_tex,uv).r; 

  if (uv.x < (mouse.x-0.0025))
    fragcol.rgb = vec3(d);
  else if (uv.x > (mouse.x+0.0025))
    float z = LinearizeDepth(d);
    fragcol.rgb = vec3(z) * 10.0;

  gl_FragColor.rgb = fragcol.rgb;
  gl_FragColor.a = 1.0; 

Leave a Comment

Your email address will not be published. Required fields are marked *