Reading buffer contents?

I’m a bit confused on how to read a buffer’s content to a PNMImage. The contents of my pnmimage seem to be the main window’s content instead of my buffer. The test code and related files can be downloaded from here

from direct.showbase.DirectObject import *
from pandac.PandaModules import Texture, PNMImage, GraphicsOutput, NodePath, Filename, TextureStage, Vec3, PandaNode, Point3
from direct.interval.IntervalGlobal import *
import sys

class ShaderPaintTest(DirectObject):

    def __init__(self):
        self.smiley = loader.loadModel('models/smiley')
        self.smiley.setPos(-3, 0, 10)
        self.frown = loader.loadModel('models/frowney')
        self.frown.setPos(3, 0, 10)
       , -40, 15),0,5)
        frownTurn = LerpHprInterval(self.frown, 5, Vec3(360,0,0))
        smileTurn = LerpHprInterval(self.smiley, 5, Vec3(360,0,0))
        camPan = LerpPosInterval(, 10, Point3(0, -40, 10))
        taskMgr.add(self.paintTask, "Paint Task")
        self.isPainting = False
        self.accept('mouse1', setattr, [self, 'isPainting', True])
        self.accept('mouse1-up', setattr, [self, 'isPainting', False])
        self.accept('[', self.writeImg)
        self.pickLayer = PNMImage()
        self.windowX =
        self.windowY =
        self.accept('v', base.bufferViewer.toggleEnable)
    def makeStages(self):
        self.paintTexture = loader.loadTexture('index-512-256.png')
        self.painterStage = TextureStage('Painter')
        render.setTexture(self.painterStage, self.paintTexture)
        self.frownStage = TextureStage('Frown')
        self.frown.setTexture(self.frownStage, self.frown.findTexture("**"))
        self.smileyStage = TextureStage('Smiley')
        self.smiley.setTexture(self.smileyStage, self.smiley.findTexture("**"))
    def makeBuffer(self):
        self.pickTex = Texture()
        self.pickLayer = PNMImage()
        self.buffer ="pickBuffer",,
        #self.buffer.addRenderTexture(self.pickTex, GraphicsOutput.RTMCopyRam)
        self.buffCam = base.makeCamera(self.buffer,
        self.shader = loader.loadShader('painter.sha')
        tempnode = NodePath(PandaNode("temp node"))

    def paintTask(self, task):
        if not base.mouseWatcherNode.hasMouse():
            return task.cont
        if self.isPainting is False:
            return task.cont
        if self.buffer.getScreenshot(self.pickLayer) is False:
            return task.cont

        print "PNIMage size", self.pickLayer.getXSize(),self.pickLayer.getYSize()
        mpos = base.mouseWatcherNode.getMouse()
        mx = int(((mpos.getX()+1)/2)*self.windowX)
        my = self.windowY - int(((mpos.getY()+1)/2)*self.windowY)
        # get the color below the mousepick from the rendered frame
        r = self.pickLayer.getRedVal(mx,my)
        g = self.pickLayer.getGreenVal(mx,my)
        b = self.pickLayer.getBlueVal(mx,my)
        # calculate uv-texture position from the color
        x = r + ((b%16)*256)
        y = g + ((b//16)*256)
        print "UV Coordinates:", x, y
        return task.cont
    def writeImg(self):

if __name__ == "__main__":
    import direct.directbase.DirectStart
    s = ShaderPaintTest()



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

void fshader(
    uniform sampler2D tex_0 : TEXUNIT0,
    uniform sampler2D tex_1 : TEXUNIT1,
    in float2 l_my : TEXCOORD0,
    out float4 o_color : COLOR)
    o_color = tex2D(tex_1, l_my);

The problem is if Panda chooses to implement your buffer with a ParasiteBuffer, which shares the framebuffer data with your main window, then by the time you sample the buffer contents they have already been overwritten with the main window contents. There are two possible solutions.

(1) Don’t use a ParasiteBuffer. Put:

prefer-parasite-buffer 0

in your Config.prc file. Note that if something is wrong with your driver support for offscreen buffers, Panda may fail over to a ParasiteBuffer anyway.

(2) Get the buffer image from your texture instead of from your buffer. The image has been copied to the texture, so this will be a viable option; however, you still need to copy the image from the graphics memory into your system RAM. You can do this by specifying toRam = True on your makeTextureBuffer call to do this every frame (you’ll need to pass in a default Texture object to get to the toRam parameter), or you can call base.graphicsEngine.extractTextureData() to get the texture data in a one-shot pull. Then you can use to save the texture data to your PNMImage.


The second suggestion fixed it. Thanks drwr.