Additions since the CVS back up was made.

This commit is contained in:
Robert Osfield
2003-06-24 21:57:13 +00:00
parent 15f88f35b2
commit d70ab592ed
53 changed files with 2630 additions and 124 deletions

View File

@@ -0,0 +1,19 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgdemeter.cpp\
LIBS += -lDemeterOSG -lDemeter -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgdemeter
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,14 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgviewer.cpp\
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgviewer
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,205 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
#include <osgDB/Registry>
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
#include <Demeter/Terrain.h>
#include <Demeter/Loader.h>
#include <Demeter/DemeterDrawable.h>
Demeter::Terrain* loadTerrain()
{
#ifdef _WIN32
char fileSeparator = '\\';
#else
char fileSeparator = '/';
#endif
char szMediaPath[17];
sprintf(szMediaPath,"..%cdata%cLlano",fileSeparator,fileSeparator);
Demeter::Settings::GetInstance()->SetMediaPath(szMediaPath);
// Load a terrain that was generated in the Demeter Texture Editor
Demeter::Terrain* pTerrain = new Demeter::Terrain();
try
{
#ifdef _DEBUG
Demeter::Loader::GetInstance()->LoadElevations("DemeterElevationLoaderDebug","Llano.terrain",pTerrain);
Demeter::Loader::GetInstance()->LoadTerrainTexture("DemeterTextureLoaderDebug","Llano.terrain",pTerrain);
#else
Demeter::Loader::GetInstance()->LoadElevations("DemeterElevationLoader","Llano.terrain",pTerrain);
Demeter::Loader::GetInstance()->LoadTerrainTexture("DemeterTextureLoader","Llano.terrain",pTerrain);
#endif
}
catch(Demeter::DemeterException* pEx)
{
std::cerr<<pEx->GetErrorMessage()<<std::endl;
return 0;
}
return pTerrain;
}
osg::Node* createSceneWithTerrain(Demeter::Terrain* pTerrain)
{
Demeter::DemeterDrawable* pDrawable = new Demeter::DemeterDrawable;
pDrawable->SetTerrain(pTerrain);
osg::Geode* pGeode = new osg::Geode;
pGeode->addDrawable(pDrawable);
float detailThreshold = 9.0f;
pTerrain->SetDetailThreshold(detailThreshold);
return pGeode;
}
class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
KeyboardEventHandler(Demeter::Terrain* pTerrain):
_pTerrain(pTerrain) {}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='n')
{
_pTerrain->SetDetailThreshold(_pTerrain->GetDetailThreshold()+0.1f);
std::cout << "_pTerrain->GetDetailThreshold() = "<<_pTerrain->GetDetailThreshold() << std::endl;
return true;
}
else if (ea.getKey()=='n')
{
_pTerrain->SetDetailThreshold(_pTerrain->GetDetailThreshold()-0.1f);
std::cout << "_pTerrain->GetDetailThreshold() = "<<_pTerrain->GetDetailThreshold() << std::endl;
return true;
}
break;
}
default:
break;
}
return false;
}
virtual void accept(osgGA::GUIEventHandlerVisitor& v)
{
v.visit(*this);
}
Demeter::Terrain* _pTerrain;
};
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
//
// if (arguments.argc()<=1)
// {
// arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
// return 1;
// }
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
Demeter::Terrain* pTerrain = loadTerrain();
// read the scene from the list of file specified commandline args.
osg::ref_ptr<osg::Node> loadedModel = createSceneWithTerrain(pTerrain);
// if no model has been successfully loaded report failure.
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
osg::Timer_t end_tick = timer.tick();
std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
// set the scene to render
viewer.setSceneData(loadedModel.get());
viewer.getEventHandlerList().push_front(new KeyboardEventHandler(pTerrain));
// create the windows and run the threads.
viewer.realize();
while( !viewer.done() )
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}

View File

@@ -0,0 +1,19 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgpoints.cpp\
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgpoints
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,14 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgpoints.cpp\
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgpoints
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,180 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
#include <osg/Point>
class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
KeyboardEventHandler(osg::StateSet* stateset):
_stateset(stateset)
{
_point = new osg::Point;
_point->setDistanceAttenuation(osg::Vec3(0.0,0.0005,0.0f));
_point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.000005f));
_stateset->setAttribute(_point.get());
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
{
changePointSize(1.0f);
return true;
}
else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
{
changePointSize(-1.0f);
return true;
}
break;
}
default:
break;
}
return false;
}
virtual void accept(osgGA::GUIEventHandlerVisitor& v)
{
v.visit(*this);
}
float getPointSize() const
{
return _point->getSize();
}
void setPointSize(float psize)
{
if (psize>0.0)
{
_point->setSize(psize);
}
std::cout<<"Point size "<<psize<<std::endl;
}
void changePointSize(float delta)
{
setPointSize(getPointSize()+delta);
}
osg::ref_ptr<osg::StateSet> _stateset;
osg::ref_ptr<osg::Point> _point;
};
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
if (arguments.argc()<=1)
{
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
// read the scene from the list of file specified commandline args.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
// if no model has been successfully loaded report failure.
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
osg::Timer_t end_tick = timer.tick();
std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
// optimize the scene graph, remove rendundent nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
// set the scene to render
viewer.setSceneData(loadedModel.get());
// register the handler for modifying the point size
viewer.getEventHandlerList().push_front(new KeyboardEventHandler(viewer.getGlobalStateSet()));
// create the windows and run the threads.
viewer.realize();
while( !viewer.done() )
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}

View File

@@ -0,0 +1,18 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgprerendercubemap.cpp\
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgprerendercubemap
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,14 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgprerendercubemap.cpp\
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgprerendercubemap
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,491 @@
#include <osg/GLExtensions>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Matrix>
#include <osg/Quat>
#include <osg/StateSet>
#include <osg/TextureCubeMap>
#include <osg/TexGen>
#include <osg/TexMat>
#include <osg/TexEnvCombine>
#include <osg/ShapeDrawable>
#include <osg/PositionAttitudeTransform>
#include <osgUtil/RenderToTextureStage>
#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgDB/Registry>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgProducer/Viewer>
#include <iostream>
#include <string>
#include <vector>
//#define UPDATE_ONE_IMAGE_PER_FRAME 1
class PrerenderAppCallback : public osg::NodeCallback
{
public:
PrerenderAppCallback(osg::Node* subgraph):
_subgraph(subgraph) {}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
// traverse the subgraph to update any nodes.
if (_subgraph.valid()) _subgraph->accept(*nv);
// must traverse the Node's subgraph
traverse(node,nv);
}
osg::ref_ptr<osg::Node> _subgraph;
};
class PrerenderCullCallback : public osg::NodeCallback
{
public:
PrerenderCullCallback(osg::Node* subgraph, osg::TextureCubeMap* cubemap, osg::TexMat* texmat):
_subgraph(subgraph),
_cubemap(cubemap),
_texmat(texmat)
{
_updateCubemapFace = 0;
_clearColor = osg::Vec4(1,1,1,1);
_localState[0] = new osg::StateSet;
_localState[1] = new osg::StateSet;
_localState[2] = new osg::StateSet;
_localState[3] = new osg::StateSet;
_localState[4] = new osg::StateSet;
_localState[5] = new osg::StateSet;
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv && _cubemap.valid() && _subgraph.valid())
{
const osg::Vec4 clearColArray[] =
{
osg::Vec4(0, 0, 1, 1), // +X
osg::Vec4(1, 0.7f, 0, 1), // -X
osg::Vec4(0, 1, 1, 1), // +Y
osg::Vec4(1, 1, 0, 1), // -Y
osg::Vec4(1, 0, 0, 1), // +Z
osg::Vec4(0, 1, 0, 1) // -Z
};
osg::Quat q;
q.set(cv->getModelViewMatrix());
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texmat->setMatrix(C);
#if UPDATE_ONE_IMAGE_PER_FRAME
if ((_updateCubemapFace >= 0) && (_updateCubemapFace <= 5))
{
_clearColor = clearColArray[_updateCubemapFace];
doPreRender(*cv, _updateCubemapFace++);
}
#else
while (_updateCubemapFace<6)
{
_clearColor = clearColArray[_updateCubemapFace];
doPreRender(*cv, _updateCubemapFace++);
}
#endif
}
// must traverse the subgraph
traverse(node,nv);
}
void doPreRender(osgUtil::CullVisitor& cv, const int nFace);
struct ImageData
{
ImageData(const osg::Vec3& dir, const osg::Vec3& up) : _dir(dir), _up(up) {}
osg::Vec3 _dir;
osg::Vec3 _up;
};
osg::ref_ptr<osg::Node> _subgraph;
osg::ref_ptr<osg::TextureCubeMap> _cubemap;
osg::ref_ptr<osg::StateSet> _localState[6];
osg::ref_ptr<osg::TexMat> _texmat;
osg::Vec4 _clearColor;
int _updateCubemapFace;
};
void PrerenderCullCallback::doPreRender(osgUtil::CullVisitor& cv, const int nFace)
{
const ImageData id[] =
{
ImageData( osg::Vec3( 1, 0, 0), osg::Vec3( 0, -1, 0) ), // +X
ImageData( osg::Vec3(-1, 0, 0), osg::Vec3( 0, -1, 0) ), // -X
ImageData( osg::Vec3( 0, 1, 0), osg::Vec3( 0, 0, 1) ), // +Y
ImageData( osg::Vec3( 0, -1, 0), osg::Vec3( 0, 0, -1) ), // -Y
ImageData( osg::Vec3( 0, 0, 1), osg::Vec3( 0, -1, 0) ), // +Z
ImageData( osg::Vec3( 0, 0, -1), osg::Vec3( 0, -1, 0) ) // -Z
};
osg::Image* image = _cubemap->getImage((osg::TextureCubeMap::Face)nFace);
osg::Vec3 dir = id[nFace]._dir;
osg::Vec3 up = id[nFace]._up;
const osg::BoundingSphere& bs = _subgraph->getBound();
if (!bs.valid())
{
osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()<<std::endl;
return;
}
// create the render to texture stage.
osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage;
// set up lighting.
// currently ignore lights in the scene graph itself..
// will do later.
osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->_stage;
// set up the background color and clear mask.
rtts->setClearColor(_clearColor);
// ABJ: use default (color+depth)
rtts->setClearMask(previous_stage->getClearMask());
// set up to charge the same RenderStageLighting is the parent previous stage.
rtts->setRenderStageLighting(previous_stage->getRenderStageLighting());
// record the render bin, to be restored after creation
// of the render to text
osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin();
// set the current renderbin to be the newly created stage.
cv.setCurrentRenderBin(rtts.get());
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
znear *= 0.9f;
zfar *= 1.1f;
// set up projection.
const double fovy = 90.0;
const double aspectRatio = 1.0;
osg::RefMatrix* projection = new osg::RefMatrix;
projection->makePerspective(fovy, aspectRatio, znear, zfar);
cv.pushProjectionMatrix(projection);
osg::RefMatrix* matrix = new osg::RefMatrix;
osg::Vec3 eye = bs.center(); eye.z() = 0.0f;
osg::Vec3 center = eye + dir;
matrix->makeLookAt(eye, center, up);
cv.pushModelViewMatrix(matrix);
cv.pushStateSet(_localState[nFace].get());
{
// traverse the subgraph
_subgraph->accept(cv);
}
cv.popStateSet();
// restore the previous model view matrix.
cv.popModelViewMatrix();
// restore the previous model view matrix.
cv.popProjectionMatrix();
// restore the previous renderbin.
cv.setCurrentRenderBin(previousRenderBin);
if (rtts->_renderGraphList.size()==0 && rtts->_bins.size()==0)
{
// getting to this point means that all the subgraph has been
// culled by small feature culling or is beyond LOD ranges.
return;
}
int height = 128;
int width = 128;
const osg::Viewport& viewport = *cv.getViewport();
// offset the impostor viewport from the center of the main window
// viewport as often the edges of the viewport might be obscured by
// other windows, which can cause image/reading writing problems.
int center_x = viewport.x()+viewport.width()/2;
int center_y = viewport.y()+viewport.height()/2;
osg::Viewport* new_viewport = new osg::Viewport;
new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height);
rtts->setViewport(new_viewport);
_localState[nFace]->setAttribute(new_viewport);
// and the render to texture stage to the current stages
// dependancy list.
cv.getCurrentRenderBin()->_stage->addToDependencyList(rtts.get());
// if one exist attach image to the RenderToTextureStage.
// if (_image.valid()) rtts->setImage(_image.get());
if (image) rtts->setImage(image);
}
osg::Drawable* makeGeometry()
{
const float radius = 20;
return new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius));
}
osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
{
if (!subgraph) return 0;
// create the quad to visualize.
osg::Drawable* geom = makeGeometry();
geom->setSupportsDisplayList(false);
// new we need to add the texture to the Drawable, we do so by creating a
// StateSet to contain the Texture StateAttribute.
osg::StateSet* stateset = new osg::StateSet;
osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;
// set up the textures
osg::Image* imagePosX = new osg::Image;
osg::Image* imageNegX = new osg::Image;
osg::Image* imagePosY = new osg::Image;
osg::Image* imageNegY = new osg::Image;
osg::Image* imagePosZ = new osg::Image;
osg::Image* imageNegZ = new osg::Image;
imagePosX->setInternalTextureFormat(GL_RGB);
imageNegX->setInternalTextureFormat(GL_RGB);
imagePosY->setInternalTextureFormat(GL_RGB);
imageNegY->setInternalTextureFormat(GL_RGB);
imagePosZ->setInternalTextureFormat(GL_RGB);
imageNegZ->setInternalTextureFormat(GL_RGB);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
cubemap->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
cubemap->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
stateset->setTextureAttributeAndModes(0, cubemap, osg::StateAttribute::ON);
osg::TexGen *texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::REFLECTION_MAP);
stateset->setTextureAttributeAndModes(0, texgen, osg::StateAttribute::ON);
osg::TexMat* texmat = new osg::TexMat;
stateset->setTextureAttribute(0, texmat);
stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
geom->setStateSet(stateset);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(geom);
// Geodes can't have cull callback so create extra Group to attach cullcallback.
osg::Group* parent = new osg::Group;
parent->setUpdateCallback(new PrerenderAppCallback(subgraph));
parent->setCullCallback(new PrerenderCullCallback(subgraph, cubemap, texmat));
parent->addChild(geode);
return parent;
}
struct DrawableCullCallback : public osg::Drawable::CullCallback
{
DrawableCullCallback(osg::TexMat* texmat) : _texmat(texmat)
{}
virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* /*drawable*/, osg::State* /*state*/) const
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
if (cv)
{
osg::Quat q;
q.set(cv->getModelViewMatrix());
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texmat->setMatrix(C);
}
return false;
}
mutable osg::ref_ptr<osg::TexMat> _texmat;
};
osg::Node* createReferenceSphere()
{
const float radius = 10;
osg::Drawable* sphere = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius));
osg::StateSet* stateset = new osg::StateSet;
sphere->setStateSet(stateset);
osg::TextureCubeMap* cubemap = new osg::TextureCubeMap;
#define CUBEMAP_FILENAME(face) "Cubemap_axis/" #face ".png"
osg::Image* imagePosX = osgDB::readImageFile(CUBEMAP_FILENAME(posx));
osg::Image* imageNegX = osgDB::readImageFile(CUBEMAP_FILENAME(negx));
osg::Image* imagePosY = osgDB::readImageFile(CUBEMAP_FILENAME(posy));
osg::Image* imageNegY = osgDB::readImageFile(CUBEMAP_FILENAME(negy));
osg::Image* imagePosZ = osgDB::readImageFile(CUBEMAP_FILENAME(posz));
osg::Image* imageNegZ = osgDB::readImageFile(CUBEMAP_FILENAME(negz));
if (imagePosX && imageNegX && imagePosY && imageNegY && imagePosZ && imageNegZ)
{
cubemap->setImage(osg::TextureCubeMap::POSITIVE_X, imagePosX);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_X, imageNegX);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Y, imagePosY);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Y, imageNegY);
cubemap->setImage(osg::TextureCubeMap::POSITIVE_Z, imagePosZ);
cubemap->setImage(osg::TextureCubeMap::NEGATIVE_Z, imageNegZ);
cubemap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
cubemap->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
cubemap->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
cubemap->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
stateset->setTextureAttributeAndModes(0, cubemap, osg::StateAttribute::ON);
}
osg::TexGen *texgen = new osg::TexGen;
texgen->setMode(osg::TexGen::REFLECTION_MAP);
stateset->setTextureAttributeAndModes(0, texgen, osg::StateAttribute::ON);
osg::TexMat* texmat = new osg::TexMat;
stateset->setTextureAttribute(0, texmat);
stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
sphere->setCullCallback(new DrawableCullCallback(texmat));
osg::Geode* geode = new osg::Geode();
geode->addDrawable(sphere);
return geode;
}
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates pre rendering of scene to a texture, and then apply this texture to geometry.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
/*
if (arguments.argc()<=1)
{
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}
*/
osg::Group* rootNode = new osg::Group();
#if 1
osg::Node* sky = osgDB::readNodeFile("skydome.osg");
if (sky)
rootNode->addChild(createPreRenderSubGraph(sky));
#endif
#if 1
osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
pat->setPosition(osg::Vec3(0,0,50));
pat->addChild(createReferenceSphere());
rootNode->addChild(pat);
#endif
// load the nodes from the commandline arguments.
osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
if (loadedModel)
rootNode->addChild(loadedModel);
// add model to the viewer.
viewer.setSceneData( rootNode );
// create the windows and run the threads.
viewer.realize();
while( !viewer.done() )
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}

View File

@@ -34,10 +34,13 @@ osg::Geode* createShapes()
float radius = 0.8f;
float height = 1.0f;
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius)));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius)));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height)));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height)));
osg::TessellationHints* hints = new osg::TessellationHints;
hints->setDetailRatio(0.5f);
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f,0.0f,0.0f),radius),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(2.0f,0.0f,0.0f),2*radius),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cone(osg::Vec3(4.0f,0.0f,0.0f),radius,height),hints));
geode->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(6.0f,0.0f,0.0f),radius,height),hints));
osg::Grid* grid = new osg::Grid;
grid->allocateGrid(38,39);

View File

@@ -0,0 +1,131 @@
#include <osgDB/ReadFile>
#include <osg/Switch>
#include <osg/Texture2D>
#include <osg/MatrixTransform>
#include <osg/Quat>
#include <osg/Geometry>
#include <osg/Geode>
osg::MatrixTransform* createPositionedAndScaledModel(osg::Node* model,const osg::Vec3& pos, float radius, osg::Quat rotation)
{
osg::MatrixTransform* transform = new osg::MatrixTransform;
const osg::BoundingSphere& bs = model->getBound();
transform->setDataVariance(osg::Object::STATIC);
transform->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(radius/bs.radius(),radius/bs.radius(),radius/bs.radius())*
osg::Matrix::rotate(rotation)*
osg::Matrix::translate(pos));
transform->addChild(model);
return transform;
}
osg::Node* createDefaultPresentation()
{
osg::Switch* presentation = new osg::Switch;
presentation->setName("Presentation default");
float width = 1280.0f;
float height = 1024.0f;
osg::Geometry* backgroundQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
osg::Vec3(width,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,height));
osg::Geode* background = new osg::Geode;
background->getOrCreateStateSet()->setTextureAttributeAndModes(0,
new osg::Texture2D(osgDB::readImageFile("lz.rgb")),
osg::StateAttribute::ON);
background->addDrawable(backgroundQuad);
osg::Geode* background2 = new osg::Geode;
background2->getOrCreateStateSet()->setTextureAttributeAndModes(0,
new osg::Texture2D(osgDB::readImageFile("reflect.rgb")),
osg::StateAttribute::ON);
background2->addDrawable(backgroundQuad);
{
osg::Switch* slide = new osg::Switch;
slide->setName("Slide 0");
{
osg::Group* group = new osg::Group;
group->setName("Layer 0");
group->addChild(background);
slide->addChild(group);
}
{
osg::Group* group = new osg::Group;
group->setName("Layer 1");
group->addChild(background2);
slide->addChild(group);
}
{
osg::Group* group = new osg::Group;
group->setName("Layer 2");
group->addChild(background);
group->addChild(createPositionedAndScaledModel(osgDB::readNodeFile("cow.osg"),
osg::Vec3(600.0f,0.0f,600.0f),500.0f,
osg::Quat()));
slide->addChild(group);
}
presentation->addChild(slide);
}
{
osg::Switch* slide = new osg::Switch;
slide->setName("Slide 1");
{
osg::Group* group = new osg::Group;
group->setName("Layer 0");
group->addChild(background);
group->addChild(createPositionedAndScaledModel(osgDB::readNodeFile("glider.osg"),
osg::Vec3(600.0f,0.0f,600.0f),500.0f,
osg::Quat()));
slide->addChild(group);
}
{
osg::Group* group = new osg::Group;
group->setName("Layer 1");
group->addChild(background2);
group->addChild(createPositionedAndScaledModel(osgDB::readNodeFile("Town.osg"),
osg::Vec3(600.0f,0.0f,600.0f),500.0f,
osg::Quat()));
slide->addChild(group);
}
presentation->addChild(slide);
}
return presentation;
}

View File

@@ -0,0 +1,21 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
SlideEventHandler.cpp\
DefaultPresentation.cpp\
osgslideshow.cpp\
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgslideshow
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,16 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
SlideEventHandler.cpp\
DefaultPresentation.cpp\
osgviewer.cpp\
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgslideshow
INC += $(PRODUCER_INCLUDE_DIR) -I/usr/X11R6/include
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -0,0 +1,274 @@
#include "SlideEventHandler.h"
class FindNamedSwitchVisitor : public osg::NodeVisitor
{
public:
FindNamedSwitchVisitor(const std::string& name):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_name(name),
_switch(0) {}
void apply(osg::Switch& sw)
{
if (sw.getName().find(_name)!=std::string::npos)
{
_switch = &sw;
return; // note, no need to do traverse now we've located the relevant switch
}
traverse(sw);
}
std::string _name;
osg::Switch* _switch;
};
SlideEventHandler::SlideEventHandler():
_presentationSwitch(0),
_activeSlide(0),
_slideSwitch(0),
_activeLayer(0),
_firstTraversal(true),
_previousTime(-1.0f),
_timePerSlide(1.0),
_autoSteppingActive(false),
_loopPresentation(true),
_pause(false)
{
}
void SlideEventHandler::set(osg::Node* model)
{
FindNamedSwitchVisitor findPresentation("Presentation");
model->accept(findPresentation);
if (findPresentation._switch)
{
std::cout<<"Found presenation '"<<model->getName()<<"'"<<std::endl;
_presentationSwitch = findPresentation._switch;
selectSlide(0);
}
else
{
std::cout<<"Not found presenation "<<std::endl;
_presentationSwitch = 0;
_activeSlide = 0;
FindNamedSwitchVisitor findSlide("Slide");
model->accept(findSlide);
if (findSlide._switch)
{
std::cout<<"Found presenation slide"<<findSlide._switch->getName()<<std::endl;
_slideSwitch = findSlide._switch;
selectLayer(0);
}
else
{
std::cout<<"Not found slide either "<<std::endl;
}
}
}
bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::FRAME):
{
if (_autoSteppingActive)
{
double time = ea.time();
if (_firstTraversal)
{
_firstTraversal = false;
_previousTime = time;
}
else if (time-_previousTime>_timePerSlide)
{
_previousTime = time;
nextLayerOrSlide();
}
}
}
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='a')
{
_autoSteppingActive = !_autoSteppingActive;
_previousTime = ea.time();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Home ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home)
{
_autoSteppingActive = false;
selectSlide(0);
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_End ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End)
{
_autoSteppingActive = false;
selectSlide(LAST_POSITION,LAST_POSITION);
return true;
}
else if (ea.getKey()=='n' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
{
_autoSteppingActive = false;
nextLayerOrSlide();
return true;
}
else if (ea.getKey()=='p' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
{
_autoSteppingActive = false;
previousLayerOrSlide();
return true;
}
else if (ea.getKey()=='N' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Down ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Down)
{
_autoSteppingActive = false;
nextSlide();
return true;
}
else if (ea.getKey()=='P' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Up ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Up)
{
_autoSteppingActive = false;
previousSlide();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Pause)
{
_pause = !_pause;
if (_pause) std::cout<<"Pause"<<std::endl;
else std::cout<<"End Pause"<<std::endl;
}
return false;
}
default:
return false;
}
}
void SlideEventHandler::getUsage(osg::ApplicationUsage& usage) const
{
usage.addKeyboardMouseBinding("a","Toggle on/off the automatic advancement for image to image");
usage.addKeyboardMouseBinding("n","Advance to next layer or slide");
usage.addKeyboardMouseBinding("p","Move to previous layer or slide");
}
bool SlideEventHandler::selectSlide(unsigned int slideNum,unsigned int layerNum)
{
if (!_presentationSwitch) return false;
if (slideNum==LAST_POSITION && _presentationSwitch->getNumChildren()>0)
{
slideNum = _presentationSwitch->getNumChildren()-1;
}
if (slideNum>=_presentationSwitch->getNumChildren()) return false;
_activeSlide = slideNum;
_presentationSwitch->setSingleChildOn(_activeSlide);
//std::cout<<"Selected slide '"<<_presentationSwitch->getChild(_activeSlide)->getName()<<"'"<<std::endl;
FindNamedSwitchVisitor findSlide("Slide");
_presentationSwitch->getChild(_activeSlide)->accept(findSlide);
if (findSlide._switch)
{
//std::cout<<"Found slide '"<<findSlide._switch->getName()<<"'"<<std::endl;
_slideSwitch = findSlide._switch;
return selectLayer(layerNum);
}
else
{
//std::cout<<"Not found slide"<<std::endl;
}
return false;
}
bool SlideEventHandler::selectLayer(unsigned int layerNum)
{
if (!_slideSwitch) return false;
if (layerNum==LAST_POSITION && _slideSwitch->getNumChildren()>0)
{
layerNum = _slideSwitch->getNumChildren()-1;
}
if (layerNum>=_slideSwitch->getNumChildren()) return false;
_activeLayer = layerNum;
_slideSwitch->setSingleChildOn(_activeLayer);
//std::cout<<"Selected layer '"<<_slideSwitch->getChild(_activeLayer)->getName()<<"' num="<<_activeLayer<< std::endl;
return true;
}
bool SlideEventHandler::nextLayerOrSlide()
{
if (nextLayer()) return true;
else return nextSlide();
}
bool SlideEventHandler::previousLayerOrSlide()
{
if (previousLayer()) return true;
else return previousSlide();
}
bool SlideEventHandler::nextSlide()
{
if (selectSlide(_activeSlide+1)) return true;
else if (_loopPresentation) return selectSlide(0);
else return false;
}
bool SlideEventHandler::previousSlide()
{
if (_activeSlide>0) return selectSlide(_activeSlide-1,LAST_POSITION);
else if (_loopPresentation && _presentationSwitch.valid()) return selectSlide(_presentationSwitch->getNumChildren()-1,LAST_POSITION);
else return false;
}
bool SlideEventHandler::nextLayer()
{
return selectLayer(_activeLayer+1);
}
bool SlideEventHandler::previousLayer()
{
if (_activeLayer>0) return selectLayer(_activeLayer-1);
else return false;
}

View File

@@ -0,0 +1,78 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef SLIDEEVENTHANDLER
#define SLIDEEVENTHANDLER 1
#include <osg/Switch>
#include <osgGA/GUIEventHandler>
class SlideEventHandler : public osgGA::GUIEventHandler
{
public:
SlideEventHandler();
META_Object(osgslideshowApp,SlideEventHandler);
void set(osg::Node* model);
virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); }
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
virtual void getUsage(osg::ApplicationUsage& usage) const;
enum WhichPosition
{
FIRST_POSITION = 0,
LAST_POSITION = 0xffffffff,
};
bool selectSlide(unsigned int slideNum,unsigned int layerNum=FIRST_POSITION);
bool selectLayer(unsigned int layerNum);
bool nextLayerOrSlide();
bool previousLayerOrSlide();
bool nextSlide();
bool previousSlide();
bool nextLayer();
bool previousLayer();
protected:
~SlideEventHandler() {}
SlideEventHandler(const SlideEventHandler&,const osg::CopyOp&) {}
osg::ref_ptr<osg::Switch> _presentationSwitch;
unsigned int _activeSlide;
osg::ref_ptr<osg::Switch> _slideSwitch;
unsigned int _activeLayer;
bool _firstTraversal;
double _previousTime;
double _timePerSlide;
bool _autoSteppingActive;
bool _loopPresentation;
bool _pause;
};
#endif

View File

@@ -0,0 +1,119 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
#include "SlideEventHandler.h"
extern osg::Node* createDefaultPresentation();
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// register the slide event handler - which moves the presentation from slide to slide, layer to layer.
SlideEventHandler* seh = new SlideEventHandler;
viewer.getEventHandlerList().push_front(seh);
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
// read the scene from the list of file specified commandline args.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
// if no model loaded create a default presentation.
if (!loadedModel)
{
loadedModel = createDefaultPresentation();
}
// if no model has been successfully loaded report failure.
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
osg::Timer_t end_tick = timer.tick();
std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
// optimize the scene graph, remove rendundent nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
// pass the model to the slide event handler so it knows which to manipulate.
seh->set(loadedModel.get());
// set the scene to render
viewer.setSceneData(loadedModel.get());
// create the windows and run the threads.
viewer.realize();
while( !viewer.done() )
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}