2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgwindows.
|
|
|
|
*
|
|
|
|
* 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.
|
2003-04-07 18:20:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgUtil/Optimizer>
|
2007-01-11 23:19:59 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-12 22:20:16 +08:00
|
|
|
|
|
|
|
|
2007-01-11 23:53:32 +08:00
|
|
|
#include <iostream>
|
2003-04-07 18:20:34 +08:00
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
// read the scene from the list of file specified commandline args.
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
2003-04-07 18:20:34 +08:00
|
|
|
|
2007-06-08 23:37:20 +08:00
|
|
|
// if not loaded assume no arguments passed in, try use default mode instead.
|
2011-06-15 00:54:20 +08:00
|
|
|
if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osgt");
|
2007-06-08 23:37:20 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
// if no model has been successfully loaded report failure.
|
|
|
|
if (!loadedModel)
|
2003-04-07 18:20:34 +08:00
|
|
|
{
|
2007-01-11 23:19:59 +08:00
|
|
|
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
|
2003-04-07 18:20:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::Viewer viewer;
|
2003-04-07 18:20:34 +08:00
|
|
|
|
2007-07-04 01:32:19 +08:00
|
|
|
int xoffset = 40;
|
|
|
|
int yoffset = 40;
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
// left window + left slave camera
|
2003-04-07 18:20:34 +08:00
|
|
|
{
|
2007-01-11 23:19:59 +08:00
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
2007-07-04 01:32:19 +08:00
|
|
|
traits->x = xoffset + 0;
|
|
|
|
traits->y = yoffset + 0;
|
|
|
|
traits->width = 600;
|
2007-01-11 23:19:59 +08:00
|
|
|
traits->height = 480;
|
|
|
|
traits->windowDecoration = true;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
|
|
|
|
camera->setGraphicsContext(gc.get());
|
|
|
|
camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
|
|
|
|
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
|
|
|
|
camera->setDrawBuffer(buffer);
|
|
|
|
camera->setReadBuffer(buffer);
|
|
|
|
|
2007-07-04 01:32:19 +08:00
|
|
|
// add this slave camera to the viewer, with a shift left of the projection matrix
|
2007-01-11 23:19:59 +08:00
|
|
|
viewer.addSlave(camera.get(), osg::Matrixd::translate(1.0,0.0,0.0), osg::Matrixd());
|
2003-04-07 18:20:34 +08:00
|
|
|
}
|
|
|
|
|
2007-07-04 01:32:19 +08:00
|
|
|
// right window + right slave camera
|
2003-04-07 18:20:34 +08:00
|
|
|
{
|
2007-01-11 23:19:59 +08:00
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
2007-07-04 01:32:19 +08:00
|
|
|
traits->x = xoffset + 600;
|
|
|
|
traits->y = yoffset + 0;
|
|
|
|
traits->width = 600;
|
2007-01-11 23:19:59 +08:00
|
|
|
traits->height = 480;
|
|
|
|
traits->windowDecoration = true;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
|
|
|
|
camera->setGraphicsContext(gc.get());
|
|
|
|
camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
|
|
|
|
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
|
|
|
|
camera->setDrawBuffer(buffer);
|
|
|
|
camera->setReadBuffer(buffer);
|
|
|
|
|
2007-07-04 01:32:19 +08:00
|
|
|
// add this slave camera to the viewer, with a shift right of the projection matrix
|
2007-01-11 23:19:59 +08:00
|
|
|
viewer.addSlave(camera.get(), osg::Matrixd::translate(-1.0,0.0,0.0), osg::Matrixd());
|
2003-04-07 18:20:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-13 18:43:56 +08:00
|
|
|
// optimize the scene graph, remove redundant nodes and state etc.
|
2003-04-07 18:20:34 +08:00
|
|
|
osgUtil::Optimizer optimizer;
|
|
|
|
optimizer.optimize(loadedModel.get());
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(loadedModel.get());
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
return viewer.run();
|
2003-04-07 18:20:34 +08:00
|
|
|
}
|
|
|
|
|