2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgcamera.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2005-08-16 21:29:07 +08:00
|
|
|
#include <osgDB/ReadFile>
|
2007-02-26 04:05:23 +08:00
|
|
|
#include <osgDB/WriteFile>
|
2006-12-20 00:12:29 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-10 03:18:24 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2006-12-21 05:13:29 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
2007-01-17 00:01:01 +08:00
|
|
|
#include <osgGA/FlightManipulator>
|
2007-01-03 01:39:31 +08:00
|
|
|
#include <osgGA/AnimationPathManipulator>
|
2006-12-23 01:46:21 +08:00
|
|
|
#include <iostream>
|
2006-12-20 00:12:29 +08:00
|
|
|
|
2007-01-16 00:09:32 +08:00
|
|
|
class ModelHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ModelHandler():
|
|
|
|
_position(0) {}
|
|
|
|
|
|
|
|
typedef std::vector<std::string> Filenames;
|
|
|
|
Filenames _filenames;
|
|
|
|
unsigned int _position;
|
|
|
|
|
|
|
|
void add(const std::string& filename) { _filenames.push_back(filename); }
|
|
|
|
|
|
|
|
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
|
|
|
|
{
|
|
|
|
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
|
|
|
|
if (!viewer) return false;
|
|
|
|
|
|
|
|
if (_filenames.empty()) return false;
|
|
|
|
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::KEYUP):
|
|
|
|
{
|
|
|
|
if (ea.getKey()=='l')
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( _filenames[_position] );
|
|
|
|
++_position;
|
|
|
|
if (_position>=_filenames.size()) _position = 0;
|
|
|
|
|
|
|
|
if (model.valid())
|
|
|
|
{
|
|
|
|
viewer->setSceneData(model.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _done;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-01-03 01:39:31 +08:00
|
|
|
void singleWindowMultipleCameras(osgViewer::Viewer& viewer)
|
2006-12-20 00:12:29 +08:00
|
|
|
{
|
2007-01-02 02:20:10 +08:00
|
|
|
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
|
|
|
|
if (!wsi)
|
|
|
|
{
|
2007-01-16 16:56:33 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
|
2007-01-03 01:39:31 +08:00
|
|
|
return;
|
2007-01-02 02:20:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int width, height;
|
|
|
|
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
|
|
|
traits->x = 0;
|
|
|
|
traits->y = 0;
|
|
|
|
traits->width = width;
|
|
|
|
traits->height = height;
|
|
|
|
traits->windowDecoration = true;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
2007-01-06 00:46:57 +08:00
|
|
|
if (gc.valid())
|
2007-01-02 02:20:10 +08:00
|
|
|
{
|
2007-01-06 00:46:57 +08:00
|
|
|
osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl;
|
2007-01-02 02:20:10 +08:00
|
|
|
|
2007-01-06 00:46:57 +08:00
|
|
|
// need to ensure that the window is cleared make sure that the complete window is set the correct colour
|
|
|
|
// rather than just the parts of the window that are under the camera's viewports
|
|
|
|
gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
|
|
|
|
gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2007-01-02 02:20:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int numCameras = 2;
|
|
|
|
double aspectRatioScale = 1.0;///(double)numCameras;
|
|
|
|
for(unsigned int i=0; i<numCameras;++i)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
|
|
|
|
camera->setGraphicsContext(gc.get());
|
|
|
|
camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
|
|
|
|
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
|
|
|
|
camera->setDrawBuffer(buffer);
|
|
|
|
camera->setReadBuffer(buffer);
|
|
|
|
|
|
|
|
viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
|
|
|
|
}
|
2007-01-03 01:39:31 +08:00
|
|
|
}
|
|
|
|
|
2007-06-10 03:18:24 +08:00
|
|
|
void multipleWindowMultipleCameras(osgViewer::Viewer& viewer, bool multipleScreens)
|
2007-01-03 01:39:31 +08:00
|
|
|
{
|
|
|
|
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
|
|
|
|
if (!wsi)
|
|
|
|
{
|
2007-01-16 16:56:33 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
|
2007-01-03 01:39:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int width, height;
|
|
|
|
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
|
|
|
|
|
|
|
|
|
2007-01-09 03:29:59 +08:00
|
|
|
unsigned int numCameras = 6;
|
2007-01-03 01:39:31 +08:00
|
|
|
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;
|
2007-06-10 03:18:24 +08:00
|
|
|
traits->screenNum = multipleScreens ? i / 3 : 0;
|
2007-01-03 01:39:31 +08:00
|
|
|
traits->x = (i*width)/numCameras;
|
|
|
|
traits->y = 0;
|
|
|
|
traits->width = width/numCameras-1;
|
|
|
|
traits->height = height;
|
|
|
|
traits->windowDecoration = true;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
2007-01-06 00:46:57 +08:00
|
|
|
if (gc.valid())
|
2007-01-03 01:39:31 +08:00
|
|
|
{
|
2007-01-06 00:46:57 +08:00
|
|
|
osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl;
|
2007-01-03 01:39:31 +08:00
|
|
|
}
|
|
|
|
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() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:31:14 +08:00
|
|
|
class EnableVBOVisitor : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EnableVBOVisitor():
|
|
|
|
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
|
|
|
|
|
|
|
void apply(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
for(unsigned int i=0; i<geode.getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
|
2009-11-26 18:12:38 +08:00
|
|
|
if (geom)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Enabling VBO"<<std::endl;
|
|
|
|
geom->setUseVertexBufferObjects(true);
|
|
|
|
}
|
2009-11-26 00:31:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-01-03 01:39:31 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:20:25 +08:00
|
|
|
unsigned int numRepeats = 2;
|
|
|
|
if (arguments.read("--repeat",numRepeats) || arguments.read("-r",numRepeats) || arguments.read("--repeat") || arguments.read("-r"))
|
|
|
|
{
|
2009-11-26 00:31:14 +08:00
|
|
|
|
2009-11-26 00:20:25 +08:00
|
|
|
bool sharedModel = arguments.read("--shared");
|
2009-11-26 00:31:14 +08:00
|
|
|
bool enableVBO = arguments.read("--vbo");
|
|
|
|
|
2009-11-26 00:20:25 +08:00
|
|
|
osg::ref_ptr<osg::Node> model;
|
2009-11-26 00:31:14 +08:00
|
|
|
if (sharedModel)
|
|
|
|
{
|
|
|
|
model = osgDB::readNodeFiles(arguments);
|
|
|
|
if (!model) return 0;
|
|
|
|
|
|
|
|
if (enableVBO)
|
|
|
|
{
|
|
|
|
EnableVBOVisitor enableVBOs;
|
|
|
|
model->accept(enableVBOs);
|
|
|
|
}
|
|
|
|
}
|
2009-11-26 00:20:25 +08:00
|
|
|
|
|
|
|
osgViewer::Viewer::ThreadingModel threadingModel = osgViewer::Viewer::AutomaticSelection;
|
|
|
|
while (arguments.read("-s")) { threadingModel = osgViewer::Viewer::SingleThreaded; }
|
|
|
|
while (arguments.read("-g")) { threadingModel = osgViewer::Viewer::CullDrawThreadPerContext; }
|
|
|
|
while (arguments.read("-d")) { threadingModel = osgViewer::Viewer::DrawThreadPerContext; }
|
|
|
|
while (arguments.read("-c")) { threadingModel = osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext; }
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<numRepeats; ++i)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"+++++++++++++ New viewer ++++++++++++"<<std::endl;
|
|
|
|
|
2009-11-26 00:31:14 +08:00
|
|
|
{
|
2009-11-26 18:12:38 +08:00
|
|
|
osgViewer::Viewer viewer;
|
|
|
|
|
|
|
|
viewer.setThreadingModel(threadingModel);
|
2009-11-26 00:31:14 +08:00
|
|
|
|
2009-11-26 18:12:38 +08:00
|
|
|
if (sharedModel) viewer.setSceneData(model.get());
|
|
|
|
else
|
2009-11-26 00:31:14 +08:00
|
|
|
{
|
2009-11-26 18:12:38 +08:00
|
|
|
osg::ref_ptr<osg::Node> node = osgDB::readNodeFiles(arguments);
|
|
|
|
if (!node) return 0;
|
|
|
|
|
|
|
|
if (enableVBO)
|
|
|
|
{
|
|
|
|
EnableVBOVisitor enableVBOs;
|
|
|
|
node->accept(enableVBOs);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer.setSceneData(node.get());
|
2009-11-26 00:31:14 +08:00
|
|
|
}
|
|
|
|
|
2009-11-26 18:12:38 +08:00
|
|
|
viewer.run();
|
2009-11-26 00:31:14 +08:00
|
|
|
}
|
2009-11-26 00:20:25 +08:00
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)<<"------------ Viewer ended ----------"<<std::endl<<std::endl;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-03 01:39:31 +08:00
|
|
|
|
|
|
|
std::string pathfile;
|
|
|
|
osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
|
|
|
|
while (arguments.read("-p",pathfile))
|
|
|
|
{
|
|
|
|
apm = new osgGA::AnimationPathManipulator(pathfile);
|
2007-01-13 05:05:39 +08:00
|
|
|
if (!apm.valid() || !(apm->valid()) )
|
2007-01-03 01:39:31 +08:00
|
|
|
{
|
|
|
|
apm = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-10 03:18:24 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
2007-01-03 01:39:31 +08:00
|
|
|
|
|
|
|
while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
|
2007-02-06 22:38:08 +08:00
|
|
|
while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); }
|
|
|
|
while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); }
|
|
|
|
while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); }
|
2007-01-05 05:58:37 +08:00
|
|
|
|
|
|
|
bool limitNumberOfFrames = false;
|
|
|
|
unsigned int maxFrames = 10;
|
|
|
|
while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; }
|
|
|
|
|
|
|
|
// alternative viewer window setups.
|
|
|
|
while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); }
|
2007-06-10 03:18:24 +08:00
|
|
|
while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer, false); }
|
|
|
|
while (arguments.read("-3")) { multipleWindowMultipleCameras(viewer, true); }
|
2007-01-03 01:39:31 +08:00
|
|
|
|
|
|
|
if (apm.valid()) viewer.setCameraManipulator(apm.get());
|
|
|
|
else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
|
2007-06-10 03:18:24 +08:00
|
|
|
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
viewer.addEventHandler(new osgViewer::ThreadingHandler);
|
2007-01-17 00:01:01 +08:00
|
|
|
|
2007-02-26 04:05:23 +08:00
|
|
|
std::string configfile;
|
|
|
|
while (arguments.read("--config", configfile))
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Trying to read config file "<<configfile<<std::endl;
|
|
|
|
osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(configfile);
|
|
|
|
osgViewer::View* view = dynamic_cast<osgViewer::View*>(object.get());
|
|
|
|
if (view)
|
|
|
|
{
|
2015-04-13 18:43:56 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Read config file successfully"<<std::endl;
|
2007-02-26 04:05:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Failed to read config file : "<<configfile<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (arguments.read("--write-config", configfile)) { osgDB::writeObjectFile(viewer, configfile); }
|
|
|
|
|
2007-01-03 01:39:31 +08:00
|
|
|
|
2007-06-10 17:51:29 +08:00
|
|
|
if (arguments.read("-m"))
|
2007-01-16 00:09:32 +08:00
|
|
|
{
|
2007-06-10 17:51:29 +08:00
|
|
|
ModelHandler* modelHandler = new ModelHandler;
|
|
|
|
for(int i=1; i<arguments.argc();++i)
|
|
|
|
{
|
|
|
|
modelHandler->add(arguments[i]);
|
|
|
|
}
|
2007-01-16 00:09:32 +08:00
|
|
|
|
2007-06-10 17:51:29 +08:00
|
|
|
viewer.addEventHandler(modelHandler);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load the scene.
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
2007-01-16 00:09:32 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osgt");
|
2007-06-10 03:18:24 +08:00
|
|
|
|
2007-06-10 17:51:29 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2007-06-10 03:18:24 +08:00
|
|
|
|
2007-06-10 17:51:29 +08:00
|
|
|
viewer.setSceneData(loadedModel.get());
|
2007-01-05 05:58:37 +08:00
|
|
|
}
|
2007-06-10 17:51:29 +08:00
|
|
|
|
2007-01-09 00:20:10 +08:00
|
|
|
viewer.realize();
|
2006-12-20 00:12:29 +08:00
|
|
|
|
2006-12-23 05:53:44 +08:00
|
|
|
unsigned int numFrames = 0;
|
2006-12-25 00:40:19 +08:00
|
|
|
while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
|
2006-12-22 00:56:20 +08:00
|
|
|
{
|
|
|
|
viewer.frame();
|
2006-12-23 05:53:44 +08:00
|
|
|
++numFrames;
|
2006-12-20 00:12:29 +08:00
|
|
|
}
|
2006-12-22 00:56:20 +08:00
|
|
|
|
2005-08-16 21:29:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|