View Single Post
  #2  
Old 04-29-2008, 07:54 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I've attached a sample script that renders the scene in stereo to a texture and displays it onto a tessellated quad. You can implement the _distortTexCoord function to return distorted texture coordinates, right now it simply returns undistorted coordinates.
Code:
import viz
viz.go(viz.STEREO)

#Initialize environment
viz.clearcolor(viz.GRAY)
viz.add('gallery.ive')

#Hide scene so it is not rendered to frame buffer
viz.MainScene.visible(0,viz.WORLD)

#Distortion grid constants
GRID_WIDTH = 100
GRID_HEIGHT = 100
GRID_STEP = 5

def CreateLensDistortion(eye):
	
	#Create render texture
	tex = viz.addRenderTexture()

	#Determine size of window
	width,height = viz.window.getSize()
	width /= 2

	#Create node to render scene to a texture
	lens = viz.addRenderNode(size=[width,height])
	lens.attachTexture(tex)
	lens.disable(eye)
	
	#Create node to displayed rendered texture to screen
	overlay = viz.addRenderNode()
	overlay.setOrder(viz.POST_RENDER)
	overlay.setHUD(0,GRID_WIDTH,0,GRID_HEIGHT)
	overlay.disable(eye)
	overlay.disable(viz.LIGHTING)

	def _distortTexCoord(x,y):
		#TODO: Distort texture coordinates given (x,y) vertex coordinates
		return x / float(GRID_WIDTH) , y / float(GRID_HEIGHT)
	
	#Create quad with distorted texture coordinates to display render texture
	viz.startlayer(viz.QUADS)
	for i in range(0,GRID_WIDTH,GRID_STEP):
		for j in range(0,GRID_HEIGHT,GRID_STEP):
	
			viz.texcoord(_distortTexCoord(i,j))
			viz.vertex(i,j,0)
			
			viz.texcoord(_distortTexCoord(i+GRID_STEP,j))
			viz.vertex(i+GRID_STEP,j,0)
			
			viz.texcoord(_distortTexCoord(i+GRID_STEP,j+GRID_STEP))
			viz.vertex(i+GRID_STEP,j+GRID_STEP,0)
			
			viz.texcoord(_distortTexCoord(i,j+GRID_STEP))
			viz.vertex(i,j+GRID_STEP,0)
			
	quad = viz.endlayer(parent=overlay)
	quad.texture(tex)

#Create lens distortion for left/right eyes
CreateLensDistortion(viz.RENDER_LEFT)
CreateLensDistortion(viz.RENDER_RIGHT)
Reply With Quote