I'd like to experiment RTT and, beyond osgprerender example I found another
approach that looks quite simple but actually, even though it looks correct
to me, it doesn't work...the texture quad that should display the scene view
does not.
Here's the code:
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
osgViewer::Viewer viewer(arguments);
// load the data
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" <<
std::endl;
return 1;
}
// screen dimensions...
int screenWidth = 1280;
int screenHeight = 800;
// RTT view dimensions...
float width = screenWidth * 0.35f;
float height = screenHeight * 0.35f;
// create the texture to render to...
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);
//renderTexture->setImage( osgDB::readImageFile("img.jpg"));
// ...and the geometry to render onto...
osg::ref_ptr<osg::Geometry> screenQuad;
screenQuad = osg::createTexturedQuadGeometry(osg::Vec3(),
osg::Vec3(width, 0.0, 0.0),
osg::Vec3(0.0, height,
0.0));
//screenQuad->setDataVariance( osg::Object::DYNAMIC );
osg::ref_ptr<osg::Geode> quadGeode = new osg::Geode;
quadGeode->addDrawable(screenQuad.get());
osg::StateSet *quadState = quadGeode->getOrCreateStateSet();
quadState->setTextureAttributeAndModes(0, renderTexture,
osg::StateAttribute::ON);
// create the camera that will render to texture...
osg::ref_ptr<osg::Camera> textureCamera = new osg::Camera;
textureCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
textureCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
textureCamera->setViewport(0, 0, screenWidth, screenHeight);
// set up the background color and clear mask.
//textureCamera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
// Frame buffer objects are the best option
textureCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_
OBJECT);
//textureCamera->setRenderTargetImplementation(
osg::Camera::FRAME_BUFFER);
// We need to render to the texture BEFORE we render to the screen
textureCamera->setRenderOrder(osg::Camera::PRE_RENDER);
// The camera will render into the texture that we created earlier
textureCamera->attach(osg::Camera::COLOR_BUFFER, renderTexture);
// Add whatever children you want drawn to the texture
textureCamera->addChild(loadedModel.get());
// create the camera that will display the render to texture view...
osg::ref_ptr<osg::Camera> orthoCamera = new osg::Camera;
// We don't want to apply perspective, just overlay using orthographic
orthoCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, width, 0,
height));
orthoCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
orthoCamera->setViewMatrix(osg::Matrix::identity());
// Choose a good spot on the screen to overlay the quad
float xPos = screenWidth * 0.635f;
float yPos = screenHeight * 0.625f;
orthoCamera->setViewport(xPos, yPos, width, height);
// Make sure to render this after rendering the scene
// in order to overlay the quad on top
orthoCamera->setRenderOrder(osg::Camera::POST_RENDER);
// Render only the quad
orthoCamera->addChild(quadGeode.get());
// setup scenegraph...
osg::ref_ptr<osg::Group> sceneRoot = new osg::Group;
sceneRoot->addChild(textureCamera.get());
sceneRoot->addChild(orthoCamera.get());
sceneRoot->addChild(loadedModel.get());
sceneRoot->addChild(quadGeode.get());
// setup viewer and run...
viewer.setSceneData( sceneRoot.get() );
viewer.realize();
return viewer.run();
}
Please can you tell me where the problem is? Thank you very much.
Kind regards.
Alessandro
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org