Tech Forums > General Discussion

Accessing the depth buffer in GLSL

(1/4) > >>

Hi! I am currently trying to implement DOF in GeeXLab 0.1.14, but I am unable to access the Depth Buffer. I started from a shader of the shader library, and that's what my code looks like:

       <render_texture name="sceneBuffer" type="FRAMEBUFFER" >
      <attach_texture_depth pixel_format="DEPTH"/>
      <attach_render_buffer type="DEPTH" />

   <post_processing name="MainPostProcessing">
      <effect name="Dream_Effect">
         <phase name="Dream_Effect_Phase">
            <step name="_1" target="FRAMEBUFFER" gpu_shader="DreamShader" >
               <texture render_texture="sceneBuffer" render_texture_type="DEPTH" render_texture_index="0" />

   <shader_program name="DreamShader" >
      <constant_1i name="sceneTex" value="0" />
void main(void)
  gl_Position = ftransform();
  gl_TexCoord[0] = gl_MultiTexCoord0;
uniform sampler2D sceneTex; // 0
void main()
  vec2 uv = gl_TexCoord[0].xy;
  float c = texture2D(sceneTex, uv);
  gl_FragColor = c == 0.0 ? vec4(1.0) : vec4(0.0);

My screen is completely white, which means that the depth of every pixel is equal to 0... Could someone see if I made an error here, or send me a piece of code that can access the depth buffer ? Thank you for your help!

I'm preparing a small demo to display the depth buffer...

Demo posted here:

Hope this will help you  ;)

Wow thank you! This really helped me out! I finally achieved the effect I desired ; a dynamic DOF using 5x5 Gaussian blur. Here is a screenshot of the result:

There is still some optimizations to do, I must find a way to diminish the bleeding around near objects, and I will try manual down sampling to get better performances, but that's a start !

If someone wants to try it, here is the code:

Absolutely nice work man!
I think I'll publish your demo officially on Geeks3D front page.

I recommend you to manage window resizing with a SIZE script like this:

--- Code: ---<script name="resize" run_mode="SIZE" >
  local w, h = HYP_Scene.GetWindowSize()
  id = HYP_GPUShader.GetId("HorizontalBlur")
  HYP_GPUShader.SetConstant_1f(id, "texWidth", w)
  id = HYP_GPUShader.GetId("VerticalBlur")
  HYP_GPUShader.SetConstant_1f(id, "texHeight", h)

--- End code ---


[0] Message Index

[#] Next page

Go to full version