Hi,
I have some problems with render to texture, I made a code and I don't 
understand why it didn't work. I try to take a screenshot with a render to 
texture. This is my code

the first part : 

Code:

int     osgBuilder::start()
{
        osg::ref_ptr<osgViewer::Viewer>                 viewer = new 
osgViewer::Viewer;
        osg::ref_ptr<myKeyboardEventHandler>    kbHdl = new 
myKeyboardEventHandler;
        osg::Matrix                                                             
cameraPosition;

        cameraPosition.translate(0., -100., 0.);

        viewer->getCamera()->setViewMatrix(cameraPosition);
        viewer->getCamera()->setClearColor(osg::Vec4(0.3, 0.2, 0.5, 1.));
        viewer->addEventHandler(kbHdl);
        viewer->setUpViewInWindow(200, 200, 800, 600);

        osg::ref_ptr<osg::Group>        root = new osg::Group;
        osg::ref_ptr<osg::Group>        worldParent = new osg::Group;

        osg::ref_ptr<osg::Group>        world = this->createScene(); //my world 
content
        root->addChild(worldParent);
        worldParent->addChild(world);

        osg::ref_ptr<osg::Camera> rttCamera = kbHdl->CreateRttCam(viewer, 
world); // Here I create the rtt camera
        root->addChild(rttCamera);

        viewer->setSceneData(root);

        return (viewer->run());
}





Code:

osg::ref_ptr<osg::Camera>       
myKeyboardEventHandler::CreateRttCam(osg::ref_ptr<osgViewer::Viewer> viewer, 
osg::ref_ptr<osg::Group> worldNode)
{
        if (worldNode == 0)
                return 0;

        //get the screen size
        const int screenWidth = 
viewer->getCamera()->getGraphicsContext()->getTraits()->width;
        const int screenHeight = 
viewer->getCamera()->getGraphicsContext()->getTraits()->height;

        //Create the texture to render to
        osg::ref_ptr<osg::Texture2D> renderTexture = new osg::Texture2D();
        renderTexture->setTextureSize(screenWidth, screenHeight);
        renderTexture->setInternalFormat(GL_RGBA);
        renderTexture->setFilter(osg::Texture2D::MIN_FILTER, 
osg::Texture2D::LINEAR);
        renderTexture->setFilter(osg::Texture2D::MAG_FILTER, 
osg::Texture2D::LINEAR);

//the osg::Image that is an attribute
        this->textureImage = new osg::Image;
        textureImage->allocateImage(screenWidth, screenHeight, 1, GL_RGBA, 
GL_UNSIGNED_BYTE);
        renderTexture->setImage(0, textureImage);

//copy the viewer camera attributes into the rttCamera
        osg::ref_ptr<osg::Camera> textureCamera = new 
osg::Camera(*viewer->getCamera());
//to be sure
textureCamera->setViewMatrix(viewer->getCamera()->getViewMatrix());
        textureCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        textureCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        textureCamera->setViewport(0, 0, screenWidth, screenHeight);
        
textureCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);

        textureCamera->setRenderOrder(osg::Camera::PRE_RENDER);

        textureCamera->attach(osg::Camera::COLOR_BUFFER, renderTexture.get());

        textureCamera->attach(osg::Camera::COLOR_BUFFER, 
this->textureImage.get());

        textureCamera->setPostDrawCallback(new 
MyCameraPostDrawCallBack(this->textureImage));

        textureCamera->addChild(worldNode.get());

        return (textureCamera);
}




My event handler will call a function when the touch 'p' is pressed and 
register the osg::image on disk but the image is empty. I also don't understand 
why my PostDrawCallback on the rttCamera isn't call.
Any clue ?

ps : Sorry for my english...
ps2 : I'm a beginner in OSG

Thank's a lot !

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=27227#27227





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to