WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-12-2013, 08:34 AM
andrewjworz andrewjworz is offline
Member
 
Join Date: Apr 2011
Posts: 11
Depth buffer

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
Reply With Quote
  #2  
Old 03-12-2013, 09:11 AM
andrewjworz andrewjworz is offline
Member
 
Join Date: Apr 2011
Posts: 11
Even a method/plug-in for capturing the depth buffer and writing this data to a file would be helpful...
Reply With Quote
  #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
  #4  
Old 03-22-2013, 04:23 AM
andrewjworz andrewjworz is offline
Member
 
Join Date: Apr 2011
Posts: 11
This is great! Thanks!
Reply With Quote
  #5  
Old 03-22-2013, 02:37 PM
andrewjworz andrewjworz is offline
Member
 
Join Date: Apr 2011
Posts: 11
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])
Reply With Quote
  #6  
Old 03-22-2013, 02:54 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
If you want to view it fullscreen, then you're better off implementing the shader as a post-process effect:
Code:
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)
Reply With Quote
Reply

Tags
depth buffer

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
eMagin HMD Quad buffer problem Shay Vizard 1 11-30-2012 12:00 PM
Issue Getting Quad Buffer Working nabrahamson Vizard 2 08-30-2012 09:43 AM
Does the vrpn7.dle tracker plug-in clear the udp buffer? michaelrepucci Vizard 2 09-19-2008 10:22 AM
depth perception for a DTI 3D monitor timbo Vizard 5 07-16-2008 12:23 PM
accessing screen buffer hotspur1 Vizard 3 08-22-2003 03:31 PM


All times are GMT -7. The time now is 05:23 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC