From 52d2d8eaff06fc29a53cf0733cc6f922489cf67e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 10 Apr 2003 15:23:49 +0000 Subject: [PATCH] Moved the osgcameragroup example across to being just based on osgProducer::OsgCameraGroup with no usage of osgGA manipulators. Removed redundent files from osgProducer. --- VisualStudio/osgProducer/osgProducer.dsp | 12 -- examples/osgcameragroup/osgcameragroup.cpp | 239 +++++++++------------ include/osgProducer/ActionAdapter | 35 --- include/osgProducer/FrameStatsHandler | 168 --------------- include/osgProducer/StatsEventHandler | 42 ---- src/osgProducer/GNUmakefile | 1 - src/osgProducer/StatsEventHandler.cpp | 41 ---- src/osgProducer/Viewer.cpp | 2 - 8 files changed, 104 insertions(+), 436 deletions(-) delete mode 100644 include/osgProducer/ActionAdapter delete mode 100644 include/osgProducer/FrameStatsHandler delete mode 100644 include/osgProducer/StatsEventHandler delete mode 100644 src/osgProducer/StatsEventHandler.cpp diff --git a/VisualStudio/osgProducer/osgProducer.dsp b/VisualStudio/osgProducer/osgProducer.dsp index 8ceff7e9c..598902bc6 100755 --- a/VisualStudio/osgProducer/osgProducer.dsp +++ b/VisualStudio/osgProducer/osgProducer.dsp @@ -113,10 +113,6 @@ SOURCE=..\..\src\osgProducer\EventAdapter.cpp # End Source File # Begin Source File -SOURCE=..\..\src\osgProducer\StatsEventHandler.cpp -# End Source File -# Begin Source File - SOURCE=..\..\src\osgProducer\ViewerEventHandler.cpp # End Source File # Begin Source File @@ -149,18 +145,10 @@ SOURCE=..\..\Include\osgProducer\Viewer # End Source File # Begin Source File -SOURCE=..\..\Include\osgProducer\FrameStatsHandler -# End Source File -# Begin Source File - SOURCE=..\..\Include\osgProducer\KeyboardMouseCallback # End Source File # Begin Source File -SOURCE=..\..\Include\osgProducer\StatsEventHandler -# End Source File -# Begin Source File - SOURCE=..\..\Include\osgProducer\ViewerEventHandler # End Source File # Begin Source File diff --git a/examples/osgcameragroup/osgcameragroup.cpp b/examples/osgcameragroup/osgcameragroup.cpp index 16cb645d5..3d519bea5 100644 --- a/examples/osgcameragroup/osgcameragroup.cpp +++ b/examples/osgcameragroup/osgcameragroup.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -20,187 +21,155 @@ #include -#include -#include -#include -#include -#include #include #include -#include -#include -#include +class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback +{ + public: + + MyKeyboardMouseCallback() : + Producer::KeyboardMouseCallback(), + _mx(0.0f),_my(0.0f),_mbutton(0), + _done(false) + {} + + virtual void specialKeyPress( Producer::KeyCharacter key ) + { + if (key==Producer::KeyChar_Escape) + shutdown(); + } + + virtual void shutdown() + { + _done = true; + } + + virtual void keyPress( Producer::KeyCharacter ) + { + } + + virtual void mouseMotion( float mx, float my ) + { + _mx = mx; + _my = my; + } + virtual void buttonPress( float mx, float my, unsigned int mbutton ) + { + _mx = mx; + _my = my; + _mbutton |= (1<<(mbutton-1)); + } + virtual void buttonRelease( float mx, float my, unsigned int mbutton ) + { + _mx = mx; + _my = my; + _mbutton &= ~(1<<(mbutton-1)); + } + + bool done() { return _done; } + float mx() { return _mx; } + float my() { return _my; } + unsigned int mbutton() { return _mbutton; } + + private: + + float _mx, _my; + unsigned int _mbutton; + bool _done; +}; int main( int argc, char **argv ) { - // threading model. - Producer::CameraGroup::ThreadingModel threadingModel = Producer::CameraGroup::SingleThreaded; - threadingModel = Producer::CameraGroup::ThreadPerCamera; - - // configuration file. - std::string configFile; - //configFile = "twoWindows.cfg"; - - // set up the database files to read. - std::vector filenameList; - if (argc>1) filenameList.push_back(argv[1]); - else filenameList.push_back("cow.osg"); - - + // 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 example of using osgProducer::CameraGroup."); + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); // create the camera group. - osgProducer::OsgCameraGroup *cg = configFile.empty() ? - (new osgProducer::OsgCameraGroup()): - (new osgProducer::OsgCameraGroup(configFile)); + osgProducer::OsgCameraGroup cg(arguments); - // set up the maximum number of graphics contexts, before loading the scene graph - // to ensure that texture objects and display buffers are configured to the correct size. - osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts( cg->getNumberOfCameras() ); + // 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; + } // read the scene. - osg::ref_ptr loadedModel = osgDB::readNodeFiles(filenameList); - if (!loadedModel) return 1; + osg::ref_ptr loadedModel = osgDB::readNodeFiles(arguments); + if (!loadedModel) + { + std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; + return 1; + } osgUtil::Optimizer optimizer; optimizer.optimize(loadedModel.get()); - // set up the keyboard and mouse handling. - Producer::InputArea *ia = cg->getCameraConfig()->getInputArea(); + Producer::InputArea *ia = cg.getCameraConfig()->getInputArea(); Producer::KeyboardMouse *kbm = ia ? (new Producer::KeyboardMouse(ia)) : - (new Producer::KeyboardMouse(cg->getCamera(0)->getRenderSurface())); + (new Producer::KeyboardMouse(cg.getCamera(0)->getRenderSurface())); - // set up the time and frame counter. - unsigned int frameNumber = 0; - osg::Timer timer; - osg::Timer_t start_tick = timer.tick(); - // set the keyboard mouse callback to catch the events from the windows. - bool done = false; - osgProducer::KeyboardMouseCallback kbmcb(kbm,done); - kbmcb.setStartTick(start_tick); + MyKeyboardMouseCallback kbmcb; // register the callback with the keyboard mouse manger. kbm->setCallback( &kbmcb ); - //kbm->allowContinuousMouseMotionUpdate(true); kbm->startThread(); - - - - // set the globa state - osg::ref_ptr globalStateSet = new osg::StateSet; - globalStateSet->setGlobalDefaults(); - cg->setGlobalStateSet(globalStateSet.get()); - - - // add either a headlight or sun light to the scene. - osg::LightSource* lightsource = new osg::LightSource; - osg::Light* light = new osg::Light; - lightsource->setLight(light); - lightsource->setReferenceFrame(osg::LightSource::RELATIVE_TO_ABSOLUTE); // headlight. - lightsource->setLocalStateSetModes(osg::StateAttribute::ON); - - lightsource->addChild(loadedModel.get()); - // set the scene to render -// cg->setSceneData(scene.get()); - cg->setSceneData(lightsource); + cg.setSceneData(loadedModel.get()); - // set up the pthread stack size to large enough to run into problems. - cg->setStackSize( 20*1024*1024); + Producer::Trackball tb; + tb.setOrientation( Producer::Trackball::Y_UP ); // create the windows and run the threads. - cg->realize(threadingModel); - - osg::ref_ptr frameStamp = cg->getFrameStamp(); + cg.realize(); osgUtil::UpdateVisitor update; - update.setFrameStamp(frameStamp.get()); - - - // create a camera to use with the manipulators. - osg::ref_ptr old_style_osg_camera = new osg::Camera; - - osg::ref_ptr keyswitchManipulator = new osgGA::KeySwitchCameraManipulator; - keyswitchManipulator->addNumberedCameraManipulator(new osgGA::TrackballManipulator); - keyswitchManipulator->addNumberedCameraManipulator(new osgGA::FlightManipulator); - keyswitchManipulator->addNumberedCameraManipulator(new osgGA::DriveManipulator); - - keyswitchManipulator->setCamera(old_style_osg_camera.get()); - keyswitchManipulator->setNode(loadedModel.get()); - - - osg::ref_ptr statesetManipulator = new osgGA::StateSetManipulator; - statesetManipulator->setStateSet(globalStateSet.get()); - - // create an event handler list, we'll dispatch our event to these.. - typedef std::list< osg::ref_ptr > EventHandlerList; - EventHandlerList eventHandlerList; - eventHandlerList.push_back(keyswitchManipulator.get()); - eventHandlerList.push_back(statesetManipulator.get()); - - // create a dummy action adapter right now. - osgProducer::ActionAdapter actionAdapter; - - osg::ref_ptr init_event = new osgProducer::EventAdapter; - init_event->adaptFrame(0.0); - keyswitchManipulator->getCurrentCameraManipulator()->home(*init_event,actionAdapter); - - while( !done ) + while( !kbmcb.done() ) { // syncronize to screen refresh. - cg->sync(); - - // set the frame stamp for the new frame. - double time_since_start = timer.delta_s(start_tick,timer.tick()); - frameStamp->setFrameNumber(frameNumber); - frameStamp->setReferenceTime(time_since_start); - - - // get the event since the last frame. - osgProducer::KeyboardMouseCallback::EventQueue queue; - kbmcb.getEventQueue(queue); - - // create an event to signal the new frame. - osg::ref_ptr frame_event = new osgProducer::EventAdapter; - frame_event->adaptFrame(frameStamp->getReferenceTime()); - queue.push_back(frame_event); - - // dispatch the events in order of arrival. - for(osgProducer::KeyboardMouseCallback::EventQueue::iterator event_itr=queue.begin(); - event_itr!=queue.end(); - ++event_itr) - { - bool handled = false; - for(EventHandlerList::iterator handler_itr=eventHandlerList.begin(); - handler_itr!=eventHandlerList.end() && !handled; - ++handler_itr) - { - handled = (*handler_itr)->handle(*(*event_itr),actionAdapter); - } - } + cg.sync(); // update the scene by traversing it with the the update visitor which will // call all node update callbacks and animations. - cg->getSceneData()->accept(update); + cg.getSceneData()->accept(update); + tb.input( kbmcb.mx(), kbmcb.my(), kbmcb.mbutton() ); // update the main producer camera - cg->setView(old_style_osg_camera->getModelViewMatrix()); + cg.setViewByMatrix(tb.getMatrix()); // fire off the cull and draw traversals of the scene. - cg->frame(); - - // increment the frame number ready for the next frame - ++frameNumber; + cg.frame(); } return 0; } diff --git a/include/osgProducer/ActionAdapter b/include/osgProducer/ActionAdapter deleted file mode 100644 index 64e3ad8dd..000000000 --- a/include/osgProducer/ActionAdapter +++ /dev/null @@ -1,35 +0,0 @@ -/* -*-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 PRODUCERACTIONADAPTER -#define PRODUCERGUIACTIONADAPTER 1 - -#include - -namespace osgProducer { - -class ActionAdapter : public osgGA::GUIActionAdapter -{ - public: - - void requestRedraw() {} - - void requestContinuousUpdate(bool) {} - - void requestWarpPointer(float ,float ) {} - -}; - -} - -#endif diff --git a/include/osgProducer/FrameStatsHandler b/include/osgProducer/FrameStatsHandler deleted file mode 100644 index e8b86d7b4..000000000 --- a/include/osgProducer/FrameStatsHandler +++ /dev/null @@ -1,168 +0,0 @@ -/* -*-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 OSGPRODUCER_FRAME_STATS_HANDLER -#define OSGPRODUCER_FRAME_STATS_HANDLER 1 - -#include - -#include -#include - -namespace osgProducer { - -class FrameStatsHandler : public Producer::CameraGroup::StatsHandler, public Producer::Camera::Callback -{ - public: - FrameStatsHandler() - { - _fs.resize(6); - _index = 0; - } - - void setArraySize(unsigned int size) { _fs.resize(size); } - - unsigned int getArraySize() { return _fs.size(); } - - void operator() (const Producer::CameraGroup &cg ) - { - _index = (_index + 1) % _fs.size(); - _fs[_index] = cg.getFrameStats(); - } - - void operator() (const Producer::Camera &camera) - { - if (!camera.getInstrumentationMode()) return; - - int x,y; - unsigned int width,height; - camera.getProjectionRect(x,y,width,height); - - glViewport( x, y, width, height ); - - // Set up the Orthographic view - glMatrixMode( GL_PROJECTION ); - glPushMatrix(); - glLoadIdentity(); - glOrtho( -.01, .128, 600.0, -10.0, -1.0, 1.0 ); - - glPushAttrib( GL_ENABLE_BIT ); - glDisable( GL_LIGHTING ); - glDisable( GL_DEPTH_TEST ); - glEnable( GL_BLEND ); - - glMatrixMode( GL_MODELVIEW ); - glPushMatrix(); - glLoadIdentity(); - - unsigned int lindex = (_index + 1) % _fs.size(); - Producer::Camera::TimeStamp zero = _fs[lindex]._startOfFrame; - unsigned int i; - double x1=0.0, x2=0.0, y1=0.0, y2=0.0; - for(unsigned int frame = 0; frame < _fs.size(); frame++ ) - { - Producer::CameraGroup::FrameStats fs = _fs[(lindex + frame) % _fs.size()]; - y1 = 0.0; - y2 = y1 + 10; - x1 = fs._startOfUpdate - zero; - x2 = fs._endOfUpdate - zero; - - glBegin( GL_QUADS ); - - // Draw Update length - glColor4f( 0.0, 1.0, 0.0, 0.5 ); - glVertex2d( x1, y1); - glVertex2d( x2, y1); - glVertex2d( x2, y2); - glVertex2d( x1, y2); - - for( i = 0; i < fs.getNumFrameTimeStampSets(); i++ ) - { - Producer::Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i); - y1 += 13.0; - y2 = y1 + 10.0; - x1 = fts[Producer::Camera::BeginCull] - zero; - x2 = fts[Producer::Camera::EndCull] - zero; - - glColor4f( 0.0, 0.0, 1.0, 0.5 ); - glVertex2d( x1, y1); - glVertex2d( x2, y1); - glVertex2d( x2, y2); - glVertex2d( x1, y2); - - x1 = fts[Producer::Camera::BeginDraw] - zero; - x2 = fts[Producer::Camera::EndDraw] - zero; - - glColor4f( 1.0, 0.0, 0.0, 0.5 ); - glVertex2d( x1, y1); - glVertex2d( x2, y1); - glVertex2d( x2, y2); - glVertex2d( x1, y2); - - } - glEnd(); - - glBegin( GL_LINES ); - glColor4f( 1, 1, 1, 0.5 ); - glVertex2d( fs._startOfFrame - zero , 0.0 ); - y1 = fs.getNumFrameTimeStampSets() * 13.0 + 10.0; - glVertex2d( fs._startOfFrame - zero, y1 ); - - y1 = 12.5; - for( i = 0; i < fs.getNumFrameTimeStampSets(); i++ ) - { - y2 = y1 + 11; - Producer::Camera::FrameTimeStampSet fts = fs.getFrameTimeStampSet(i); - Producer::Camera::TimeStamp vsync = fts[Producer::Camera::Vsync]; - double x1 = vsync - zero; - glColor4f( 1.0, 1.0, 0.0, 0.5 ); - glVertex2d( x1, y1 ); - glVertex2d( x1, y2 ); - y1 += 13.0; - } - glEnd(); - } - - glBegin( GL_LINES ); - - glColor4f( 1, 1, 1, 0.5 ); - for( i = 0; i < 128; i++ ) - { - glVertex2d((GLdouble)i*.001, y1); - - if( !(i%10) ) - glVertex2d((GLdouble)i*.001, y1 - 5.0); - else if( !(i%5) ) - glVertex2d((GLdouble)i*.001, y1 - 3.0); - else - glVertex2d((GLdouble)i*.001, y1 - 1.0); - } - - glEnd(); - - glPopMatrix(); - glMatrixMode( GL_PROJECTION ); - glPopMatrix(); - glMatrixMode( GL_MODELVIEW ); - - glPopAttrib(); - } - - private: - std::vector _fs; - unsigned int _index; -}; - -} - -#endif diff --git a/include/osgProducer/StatsEventHandler b/include/osgProducer/StatsEventHandler deleted file mode 100644 index e45c76e3e..000000000 --- a/include/osgProducer/StatsEventHandler +++ /dev/null @@ -1,42 +0,0 @@ -/* -*-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 OSGPRODUCER_STATSEVENTHANDLER -#define OSGPRODUCER_STATSEVENTHANDLER 1 - -#include -#include - -namespace osgProducer { - -class StatsEventHandler : public osgGA::GUIEventHandler -{ - public: - - StatsEventHandler(osgProducer::Viewer* cg):_cg(cg) {} - - virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); - - virtual void accept(osgGA::GUIEventHandlerVisitor& gehv); - - /** Get the keyboard and mouse usage of this manipulator.*/ - virtual void getUsage(osg::ApplicationUsage& usage) const; - - protected: - - osgProducer::Viewer* _cg; -}; - -} - -#endif diff --git a/src/osgProducer/GNUmakefile b/src/osgProducer/GNUmakefile index 81bec2d6b..ca4fb9dd8 100644 --- a/src/osgProducer/GNUmakefile +++ b/src/osgProducer/GNUmakefile @@ -6,7 +6,6 @@ CXXFILES =\ KeyboardMouseCallback.cpp\ OsgCameraGroup.cpp\ OsgSceneHandler.cpp\ - StatsEventHandler.cpp\ ViewerEventHandler.cpp\ Viewer.cpp\ diff --git a/src/osgProducer/StatsEventHandler.cpp b/src/osgProducer/StatsEventHandler.cpp deleted file mode 100644 index d31a713f5..000000000 --- a/src/osgProducer/StatsEventHandler.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include - -using namespace osgProducer; - -bool StatsEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) -{ - if(!_cg) return false; - - if(ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN) - { - - switch( ea.getKey() ) - { - case 's' : - _cg->setInstrumentationMode(!_cg->getInstrumentationMode()); - return true; - - case 'v' : - _cg->setBlockOnVsync(!_cg->getBlockOnVsync()); - //std::cout<<"_cg->getBlockOnVsync()="<<_cg->getBlockOnVsync()< #include -#include -#include #include using namespace osgProducer;