Thread: Depth buffer
View Single Post
  #3  
Old 03-20-2013, 08:29 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
You can use a render node to render the scene to a depth texture and display it on screen. Here is a quick example:
Code:
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.
Reply With Quote