2003-02-21 22:05:39 +08:00
|
|
|
//C++ source file - Open Producer - Copyright (C) 2002 Don Burns
|
|
|
|
//Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// Simple example of use of Producer::RenderSurface
|
|
|
|
// The myGraphics class is a simple sample of how one would implement
|
|
|
|
// graphics drawing with Producer::RenderSurface
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <Producer/Camera>
|
|
|
|
#include <Producer/CameraConfig>
|
|
|
|
#include <Producer/InputArea>
|
|
|
|
#include <Producer/KeyboardMouse>
|
2003-04-10 23:23:49 +08:00
|
|
|
#include <Producer/Trackball>
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
#include <osg/Timer>
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
#include <osgUtil/UpdateVisitor>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
|
2003-02-25 20:28:16 +08:00
|
|
|
#include <osgProducer/OsgCameraGroup>
|
|
|
|
#include <osgProducer/OsgSceneHandler>
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
|
2003-02-21 22:05:39 +08:00
|
|
|
{
|
2003-04-10 23:23:49 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
MyKeyboardMouseCallback() :
|
|
|
|
Producer::KeyboardMouseCallback(),
|
|
|
|
_mx(0.0f),_my(0.0f),_mbutton(0),
|
|
|
|
_done(false)
|
|
|
|
{}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
virtual void specialKeyPress( Producer::KeyCharacter key )
|
|
|
|
{
|
|
|
|
if (key==Producer::KeyChar_Escape)
|
|
|
|
shutdown();
|
|
|
|
}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
virtual void shutdown()
|
|
|
|
{
|
|
|
|
_done = true;
|
|
|
|
}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
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;
|
|
|
|
};
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
// 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");
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// create the camera group.
|
2003-04-10 23:23:49 +08:00
|
|
|
osgProducer::OsgCameraGroup cg(arguments);
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
// if user request help write it out to cout.
|
|
|
|
if (arguments.read("-h") || arguments.read("--help"))
|
|
|
|
{
|
|
|
|
arguments.getApplicationUsage()->write(std::cout);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// read the scene.
|
2003-04-10 23:23:49 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
osgUtil::Optimizer optimizer;
|
|
|
|
optimizer.optimize(loadedModel.get());
|
|
|
|
|
|
|
|
// set up the keyboard and mouse handling.
|
2003-04-10 23:23:49 +08:00
|
|
|
Producer::InputArea *ia = cg.getCameraConfig()->getInputArea();
|
2003-02-21 22:05:39 +08:00
|
|
|
Producer::KeyboardMouse *kbm = ia ?
|
|
|
|
(new Producer::KeyboardMouse(ia)) :
|
2003-04-10 23:23:49 +08:00
|
|
|
(new Producer::KeyboardMouse(cg.getCamera(0)->getRenderSurface()));
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
MyKeyboardMouseCallback kbmcb;
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// register the callback with the keyboard mouse manger.
|
|
|
|
kbm->setCallback( &kbmcb );
|
|
|
|
kbm->startThread();
|
|
|
|
|
|
|
|
// set the scene to render
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.setSceneData(loadedModel.get());
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-17 19:14:25 +08:00
|
|
|
|
|
|
|
const osg::BoundingSphere& bs = loadedModel->getBound();
|
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
Producer::Trackball tb;
|
2003-04-13 21:26:41 +08:00
|
|
|
tb.setOrientation( Producer::Trackball::Z_UP );
|
2003-04-17 19:14:25 +08:00
|
|
|
tb.setDistance(bs.radius()*3.0f);
|
|
|
|
tb.translate(-bs.center().x(),-bs.center().y(),-bs.center().z());
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// create the windows and run the threads.
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.realize();
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
osgUtil::UpdateVisitor update;
|
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
while( !kbmcb.done() )
|
2003-02-21 22:05:39 +08:00
|
|
|
{
|
2003-04-13 21:26:41 +08:00
|
|
|
// syncronize to the when cull and draw threads have completed.
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.sync();
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// update the scene by traversing it with the the update visitor which will
|
|
|
|
// call all node update callbacks and animations.
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.getSceneData()->accept(update);
|
2003-02-21 22:05:39 +08:00
|
|
|
|
2003-04-10 23:23:49 +08:00
|
|
|
tb.input( kbmcb.mx(), kbmcb.my(), kbmcb.mbutton() );
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// update the main producer camera
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.setViewByMatrix(tb.getMatrix());
|
2003-02-21 22:05:39 +08:00
|
|
|
|
|
|
|
// fire off the cull and draw traversals of the scene.
|
2003-04-10 23:23:49 +08:00
|
|
|
cg.frame();
|
2003-02-21 22:05:39 +08:00
|
|
|
}
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
// syncronize to the when cull and draw threads have completed.
|
|
|
|
cg.sync();
|
|
|
|
|
2003-02-21 22:05:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|