PDA

View Full Version : Depth buffer


andrewjworz
03-12-2013, 08:34 AM
Hello,

I need the depth information of each pixel, so I'm wondering if it is possible to render scenes in the z-buffer representation? (SEE figure at top of wiki article http://en.wikipedia.org/wiki/Depth_buffer ).

Thanks,
Andy

andrewjworz
03-12-2013, 09:11 AM
Even a method/plug-in for capturing the depth buffer and writing this data to a file would be helpful...

farshizzo
03-20-2013, 08:29 AM
You can use a render node to render the scene to a depth texture and display it on screen. Here is a quick example:import viz
viz.go()

viz.add('piazza.osgb')

# Add render texture using depth pixel format
depth = viz.addRenderTexture(format=viz.TEX_DEPTH)

# Use render node to render the scene to the depth texture
rn = viz.addRenderNode()
rn.setRenderTexture(depth)

# Display depth texture on the screen
quad = viz.addTexQuad(viz.SCREEN,size=200,align=viz.TEXT_ LEFT_BOTTOM)

# Use shader to display depth texture in linear range
frag = """
uniform sampler2D depthTex;
void main()
{
float d = texture2D(depthTex,gl_TexCoord[0].st).r;
float n = 0.1;
float f = 200.0;
float z = (2 * n) / (f + n - d * (f - n));
gl_FragColor = vec4(z,z,z,1.0);
}
"""
shader = viz.addShader(frag=frag)
quad.apply(viz.addUniformInt('depthTex',0))
quad.apply(shader)
quad.texture(depth)

# Need to specify clip plane here and in shader
viz.clip(0.1,200)

Let me know if anything isn't clear.

andrewjworz
03-22-2013, 04:23 AM
This is great! Thanks!

andrewjworz
03-22-2013, 02:37 PM
Alright, one more question: Is there a way to size the quad so that is fits the exact dimensions of the window? I've tried, but I'm not sure how the 'size' parameter in viz.addTexQuad method translates into window coordinates.

I have tried inserting the following code into what you have written:

# Get window size
winsize = viz.MainWindow.getSize(viz.WINDOW_PIXELS)

# Display depth texture on the screen
quad = viz.addTexQuad(viz.SCREEN, size=1 ,align=viz.TEXT_CENTER_CENTER)
quad.setPosition(.5,.5,0)
quad.scale(winsize[0],winsize[1])

farshizzo
03-22-2013, 02:54 PM
If you want to view it fullscreen, then you're better off implementing the shader as a post-process effect:import viz
viz.go()

viz.add('piazza.osgb')

NEAR = 0.1
FAR = 200.0

from vizfx.postprocess.effect import BaseShaderEffect

class DepthEffect(BaseShaderEffect):

def _getFragmentCode(self):
return """
uniform sampler2D vizpp_InputDepthTex;
uniform float near;
uniform float far;
void main()
{
float d = texture2D(vizpp_InputDepthTex,gl_TexCoord[0].st).r;
float z = (2 * near) / (far + near - d * (far - near));
gl_FragColor = vec4(z,z,z,1.0);
}
"""

def _createUniforms(self):
self.uniforms.addFloat('near',NEAR)
self.uniforms.addFloat('far',FAR)

viz.clip(NEAR,FAR)

import vizfx.postprocess
effect = DepthEffect()
vizfx.postprocess.addEffect(effect)