I started playing with RTT cameras and, having looked at some OSG examples, I
tried my own code but when I switch from fullscreen to windowed mode the
rendered texture doesn't appear as expected: it seems not to cover the whole
quad as it does while in fullscreen, only a part of it is updated and as you
stretch the window the rendered texture is stretched too (refer to attached
images).
Here's my code:
Code:
int main( int argc, char **argv )
{
osg::ArgumentParser arguments(&argc,argv);
osgViewer::Viewer viewer(arguments);
// add stats and window size handlers...
viewer.addEventHandler( new osgViewer::StatsHandler() );
viewer.addEventHandler( new osgViewer::WindowSizeHandler() );
int screenWidth = 1366;
int screenHeight = 768;
// create the texture to render to...
osg::ref_ptr<osg::Texture2D> texture_RTT = new osg::Texture2D;
texture_RTT->setTextureSize(screenWidth, screenHeight);
texture_RTT->setInternalFormat(GL_RGBA);
texture_RTT->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
texture_RTT->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
texture_RTT->setResizeNonPowerOfTwoHint(false);
// create the quad...
osg::ref_ptr<osg::Geometry> quad_RTT = osg::createTexturedQuadGeometry(
osg::Vec3(0.0, 0.0, 0.0),
osg::Vec3(screenWidth, 0.0, 0.0),
osg::Vec3(0.0, screenHeight,
0.0));
quad_RTT->setDataVariance( osg::Object::DYNAMIC );
osg::ref_ptr<osg::Geode> quad_geode_RTT = new osg::Geode;
quad_geode_RTT->addDrawable(quad_RTT.get());
quad_geode_RTT->setDataVariance( osg::Object::DYNAMIC );
osg::StateSet *quadState = quad_geode_RTT->getOrCreateStateSet();
quadState->setTextureAttributeAndModes(0, texture_RTT.get(),
osg::StateAttribute::ON);
quadState->setMode( GL_LIGHTING, osg::StateAttribute::OFF |
osg::StateAttribute::PROTECTED );
// create the camera...
osg::ref_ptr<osg::Camera> camera_RTT = new osg::Camera;
#ifdef USE_IMAGE
osg::ref_ptr<osg::Image> image_RTT = new osg::Image;
image_RTT->allocateImage(screenWidth, screenHeight, 1, GL_RGBA,
GL_UNSIGNED_BYTE);
camera_RTT->attach(osg::Camera::COLOR_BUFFER, image_RTT.get());
texture_RTT->setImage(0, image_RTT.get());
#else
camera_RTT->attach(osg::Camera::COLOR_BUFFER, texture_RTT.get());
#endif
//camera_RTT->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT,
osg::Camera::FRAME_BUFFER);
camera_RTT->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER);
camera_RTT->setRenderOrder(osg::Camera::PRE_RENDER);
camera_RTT->setReferenceFrame( osg::Camera::ABSOLUTE_RF );
camera_RTT->setClearColor( osg::Vec4( 0., 0., 1., 1. ) );
camera_RTT->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// add whatever children you want drawn to the texture
osg::ref_ptr< osg::Node > node = osgDB::readNodeFile("cow.osg");
if(node.valid())
{
const osg::BoundingSphere& bs = node->getBound();
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
float proj_top = 0.25f*znear;
float proj_right = 0.5f*znear;
znear *= 0.9f;
zfar *= 1.1f;
camera_RTT->setProjectionMatrixAsFrustum(-proj_right,proj_right,-proj_top,proj_top,znear,zfar);
camera_RTT->setViewMatrixAsLookAt(bs.center()-osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
camera_RTT->addChild(node.get());
}
else
osg::notify(osg::FATAL)<<"model not loaded"<<std::endl;
// add camera and quad to root...
osg::Group* rootNode = new osg::Group();
rootNode->addChild(camera_RTT.get());
rootNode->addChild(quad_geode_RTT.get());
// add model to the viewer.
viewer.setSceneData( rootNode );
return viewer.run();
}
Sorry if the code doesn't visualize fine (I'm attaching a .cpp FYC).
The situation does not change if I use FRAME_BUFFER_OBJECT target instead of
FRAME_BUFFER. On the other hand, it changes if I use an osg::Image (by defining
USE_IMAGE in the above code, but performances are not so good (9 fps) as when
using the texture).
What am I missing?
Cheers.
Alessandro
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=41214#41214
Attachments:
http://forum.openscenegraph.org//files/rtt_windowed_using_image_712.png
http://forum.openscenegraph.org//files/rtt_windowed_919.png
http://forum.openscenegraph.org//files/rtt_fullscreen_175.png
http://forum.openscenegraph.org//files/test_rtt_603.cpp
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org