How to clear auxiliary buffers?

How do I clear the auxiliary buffers? I’ve set up a simple render-to-texture system following the Firefly sample, but unlike the firefly sample, my background moves. I find that while the main buffer RTPColor, auto-clears with everyframe as expected, the auxiliary buffers, RTPAuxRgba0,1,2 are not getting cleared. Is it my responsibility to clear them (by drawing a blacksquare into each aux buffer everyframe?)

I’ve attached a small sample code that shows this effect using the bufferviewer. The camera movies right and left while looking at the smiley model, but the RTPAuxRgba* buffers are not cleared.

import direct.directbase.DirectStart
from pandac.PandaModules import *
import math
import time

def CreateFBO(strName = "Buffer", intSort = -10, intSizeX = None, intSizeY = None, 
				intAuxRGBA = 0):
	winprops = WindowProperties()
	winprops.setSize( intSizeX , intSizeY )
	fbprops = FrameBufferProperties()
	fbprops.setColorBits(1)
	fbprops.setAlphaBits(1)
	fbprops.setDepthBits(1)
	fbprops.setStencilBits (1)
	if intAuxRGBA:
		fbprops.setAuxRgba( intAuxRGBA )
	objBuffer = base.graphicsEngine.makeOutput(base.pipe, strName, intSort, fbprops, winprops, GraphicsPipe.BFRefuseWindow, base.win.getGsg(), base.win)
	return objBuffer

texDepth = Texture()
texDepth.setFormat(Texture.FDepthStencil)
texBuffer0 = Texture()
texBuffer1 = Texture()
texBuffer2 = Texture()
texBuffer3 = Texture()

rtmbuffer = CreateFBO(strName = "model buffer", intSort = -5, intSizeX = 1024, intSizeY = 1024, intAuxRGBA = 3)
rtmbuffer.addRenderTexture( texDepth,  GraphicsOutput.RTMCopyTexture, GraphicsOutput.RTPDepthStencil )
rtmbuffer.addRenderTexture( texBuffer0, GraphicsOutput.RTMCopyTexture, GraphicsOutput.RTPColor )
rtmbuffer.addRenderTexture( texBuffer1, GraphicsOutput.RTMCopyTexture, GraphicsOutput.RTPAuxRgba0 )
rtmbuffer.addRenderTexture( texBuffer2, GraphicsOutput.RTMCopyTexture, GraphicsOutput.RTPAuxRgba1 )
rtmbuffer.addRenderTexture( texBuffer3, GraphicsOutput.RTMCopyTexture, GraphicsOutput.RTPAuxRgba2 )

rtmcamMask = 1
rtmcam=base.makeCamera( rtmbuffer, sort = 0, lens=base.camLens, scene=render, mask = rtmcamMask, camName = 'TestCam' )
rtmcam.setPos( 0, -20, 0)

shader = loader.loadShader( "testdef.c")

npModel = loader.loadModel( 'smiley.egg' )
npModel.reparentTo( render )
npModel.setShader( shader )

base.cam.setPos(0,-20, 0)
base.bufferViewer.toggleEnable()

def move(task):
	base.cam.setPos( 3*math.sin(time.time()), -20, 0 )
	rtmcam.setPos( 3*math.sin(time.time()), -20, 0 )
	return task.cont

taskMgr.add( move, 'move' )

run()

The shader

//Cg
//
//Cg profile arbvp1 arbfp1

void vshader(float4 vtx_position : POSITION,
             uniform float4x4 mat_modelproj,
			 in float2 vtx_texcoord0 : TEXCOORD0,
			out float2 l_texcoord0 : TEXCOORD0, 
			out float4 l_position : POSITION)
{
  l_position=mul(mat_modelproj, vtx_position);
  l_texcoord0 = vtx_texcoord0;
}

void fshader( in float2 l_texcoord0 : TEXCOORD0,
						uniform sampler2D tex_0 : TEXUNIT0,
						out float4 o_color0: COLOR0,
						out float4 o_color1: COLOR1,
						out float4 o_color2: COLOR2,
						out float4 o_color3: COLOR3 )
{
  o_color0 = float4( 1, 0, 0, 1);
  o_color1 = float4( 0, 1, 0, 1);
  o_color2 = float4( 0, 0, 1, 1);
  o_color3 = float4( 1, 1, 0, 1);
 }

You can enable clear on a buffer by calling:

buffer.setClearColorActive(True)
buffer.setClearDepthActive(True)

Though I’m a little surprised that this isn’t enabled by default.

David

Hi David,

I’ve tried adding those lines to clear the buffer, but it doesn’t help. It’s still only clearing the ‘base’ buffer. The other 3 aux buffers do not get cleared.

Hmm. How about setting it on the DisplayRegion, with something like:

rtmcam.node().getDisplayRegion(0).setClearColorActive(True)

?

Clearing the display region doesn’t seem to work either. Can you replicate the problem on your computer? (Or is it just me? I have a Nvidia gtx 260 )

Ah, my apologies. The correct way to clear the aux buffers is with code like this:

rtmbuffer.setClearActive(rtmbuffer.RTPAuxRgba0, True)
rtmbuffer.setClearActive(rtmbuffer.RTPAuxRgba1, True)
rtmbuffer.setClearActive(rtmbuffer.RTPAuxRgba2, True)

David

Ah! That was the trick! Thanks for your help David!