Added support for different threading models in osgViewer::Viewer
This commit is contained in:
@@ -7,42 +7,16 @@
|
||||
#include <osgDB/ReadFile>
|
||||
#include <osgViewer/Viewer>
|
||||
#include <osgGA/TrackballManipulator>
|
||||
#include <osgGA/AnimationPathManipulator>
|
||||
#include <iostream>
|
||||
|
||||
int main( int argc, char **argv )
|
||||
void singleWindowMultipleCameras(osgViewer::Viewer& viewer)
|
||||
{
|
||||
if (argc<2)
|
||||
{
|
||||
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2);
|
||||
osg::Referenced::setThreadSafeReferenceCounting(true);
|
||||
|
||||
// load the scene.
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
|
||||
if (!loadedModel)
|
||||
{
|
||||
std::cout << argv[0] <<": No data loaded." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
osgViewer::Viewer viewer;
|
||||
|
||||
viewer.setSceneData(loadedModel.get());
|
||||
|
||||
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
|
||||
viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f));
|
||||
|
||||
#if 0
|
||||
|
||||
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
|
||||
if (!wsi)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int width, height;
|
||||
@@ -91,6 +65,119 @@ int main( int argc, char **argv )
|
||||
|
||||
viewer.setUpRenderingSupport();
|
||||
viewer.assignSceneDataToCameras();
|
||||
}
|
||||
|
||||
void multipleWindowMultipleCameras(osgViewer::Viewer& viewer)
|
||||
{
|
||||
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
|
||||
if (!wsi)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int width, height;
|
||||
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
|
||||
|
||||
|
||||
unsigned int numCameras = 2;
|
||||
double aspectRatioScale = (double)numCameras;
|
||||
double translate_x = double(numCameras)-1;
|
||||
for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0)
|
||||
{
|
||||
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
||||
traits->x = (i*width)/numCameras;
|
||||
traits->y = 0;
|
||||
traits->width = width/numCameras-1;
|
||||
traits->height = height;
|
||||
#if 1
|
||||
traits->windowDecoration = false;
|
||||
#else
|
||||
traits->windowDecoration = true;
|
||||
#endif
|
||||
traits->doubleBuffer = true;
|
||||
traits->sharedContext = 0;
|
||||
|
||||
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
||||
|
||||
osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
|
||||
if (gw)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<" GraphicsWindow has been created successfully."<<gw<<std::endl;
|
||||
|
||||
gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, traits->width, traits->height );
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl;
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
|
||||
camera->setGraphicsContext(gc.get());
|
||||
camera->setViewport(new osg::Viewport(0,0, width/numCameras, height));
|
||||
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
|
||||
camera->setDrawBuffer(buffer);
|
||||
camera->setReadBuffer(buffer);
|
||||
|
||||
viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() );
|
||||
}
|
||||
|
||||
viewer.setUpRenderingSupport();
|
||||
viewer.assignSceneDataToCameras();
|
||||
}
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
// use an ArgumentParser object to manage the program arguments.
|
||||
osg::ArgumentParser arguments(&argc,argv);
|
||||
|
||||
if (argc<2)
|
||||
{
|
||||
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
std::string pathfile;
|
||||
osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
|
||||
while (arguments.read("-p",pathfile))
|
||||
{
|
||||
apm = new osgGA::AnimationPathManipulator(pathfile);
|
||||
if(!apm.valid() || !(apm->valid()) )
|
||||
{
|
||||
apm = 0;
|
||||
}
|
||||
}
|
||||
|
||||
osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2);
|
||||
osg::Referenced::setThreadSafeReferenceCounting(true);
|
||||
|
||||
// load the scene.
|
||||
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
||||
if (!loadedModel)
|
||||
{
|
||||
std::cout << argv[0] <<": No data loaded." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
osgViewer::Viewer viewer;
|
||||
|
||||
while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
|
||||
while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); }
|
||||
while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); }
|
||||
|
||||
viewer.setSceneData(loadedModel.get());
|
||||
|
||||
if (apm.valid()) viewer.setCameraManipulator(apm.get());
|
||||
else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
|
||||
viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f));
|
||||
|
||||
#if 1
|
||||
|
||||
// singleWindowMultipleCameras(viewer);
|
||||
|
||||
multipleWindowMultipleCameras(viewer);
|
||||
|
||||
|
||||
#else
|
||||
|
||||
|
||||
Reference in New Issue
Block a user