Display background texture

Vizard provides the option of changing the background color of the scene, but some users need the ability to display an image instead of a single color. This article describes how to replace the background color of your scene with any texture object, including images and videos.

There are a few different ways to render a background texture in Vizard. The technique in this article uses render node objects that display a full screen quad before the main scene renders. This allows all the scene objects to render on top of the texture.

The addBackgroundQuad function below will setup the render node objects and return a handle to the background quad:

def addBackgroundQuad(scene=viz.MainScene):
    """Returns a quad that will display in the background of the scene"""

    #Create group node containing render nodes for left/right eye quads
    group = viz.addGroup(scene=scene)
    group.leftQuad = viz.addRenderNode()
    group.leftQuad.disable(viz.RENDER_RIGHT)
    group.rightQuad = viz.addRenderNode()
    group.rightQuad.disable(viz.RENDER_LEFT)
   
    #Setup render nodes to display behind scene
    nodes = viz.ObjectGroup([group.leftQuad,group.rightQuad])
    nodes.setHUD(-1,1,-1,1,True)
    nodes.setOrder(viz.MAIN_RENDER)
    nodes.parent(group)
    group.drawOrder(-10000)
    group.polyMode(viz.POLY_FILL)
   
    return group

The following code shows how to use the addBackgroundQuad function and apply a texture to the returned node object:

background = addBackgroundQuad()
texture = viz.add('lake3.jpg')
background.texture(texture)

Here is a complete script you can run to see the background texture in action:

import viz
viz.go()

#Add models to scene
platform = viz.add('platform.osg')
viz.add('mini.osgx',parent=platform)

#Spin platform
platform.addAction(vizact.spin(0,1,0,10,viz.FOREVER))

#Add pivot camera
import vizcam
cam = vizcam.PivotNavigate(distance=20)
cam.rotateUp(15)

def addBackgroundQuad(scene=viz.MainScene):
    """Returns a quad that will display in the background of the scene"""

    #Create group node containing render nodes for left/right eye quads
    group = viz.addGroup(scene=scene)
    group.leftQuad = viz.addRenderNode()
    group.leftQuad.disable(viz.RENDER_RIGHT)
    group.rightQuad = viz.addRenderNode()
    group.rightQuad.disable(viz.RENDER_LEFT)
   
    #Setup render nodes to display behind scene
    nodes = viz.ObjectGroup([group.leftQuad,group.rightQuad])
    nodes.setHUD(-1,1,-1,1,True)
    nodes.setOrder(viz.MAIN_RENDER)
    nodes.parent(group)
    group.drawOrder(-10000)
    group.polyMode(viz.POLY_FILL)
   
    return group

#Create background quad and apply a texture
background = addBackgroundQuad()
texture = viz.add('lake3.jpg')
background.texture(texture)

If you are running your script in stereo and want to display a different background texture to each eye, then you can use the following code:

background = addBackgroundQuad()
left_texture = viz.add('image1.jpg')
right_texture = viz.add('image2.jpg')
background.leftQuad.texture(left_texture)
background.rightQuad.texture(right_texture)