Integated the trackball manipulator with the osgproducer demo.

This commit is contained in:
Robert Osfield 2003-01-17 13:34:03 +00:00
parent 097030766d
commit 63fe1433bb
5 changed files with 94 additions and 86 deletions

View File

@ -1,69 +0,0 @@
#ifndef PRODUCER_EXAMPLES_MYKEYBOARD_MOUSE_CALLBACK
#define PRODUCER_EXAMPLES_MYKEYBOARD_MOUSE_CALLBACK
#include <stdio.h>
#include <Producer/RenderSurface> // For definition of KeySymbol
#include <Producer/KeyboardMouse>
#ifdef WIN32
#include <windows.h>
#else
#include <X11/keysym.h>
#endif
class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
{
public:
MyKeyboardMouseCallback(bool &done) :
Producer::KeyboardMouseCallback(),
_mx(0.0f),_my(0.0f),_mbutton(0),
_done(done)
{}
virtual void keyPress( Producer::KeySymbol key )
{
switch( key )
{
#ifdef XK_MISCELLANY // XWindows keydefs
case XK_Escape:
_done = true;
break;
#endif
#ifdef WIN32
case VK_ESCAPE:
_done = true;
break;
#endif
}
}
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));
}
//virtual bool idle( void ) {static c = 0; printf( "IDLE %4d\n", c++ ); return true; }
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;
};
#endif

View File

@ -70,9 +70,9 @@ void ProducerEventAdapter::adaptButtonPress(double time,float x, float y, unsign
_time = time;
_eventType = PUSH;
_button = button;
_button = button-1;
switch(button)
switch(_button)
{
case(0):
_s_accumulatedButtonMask =
@ -92,7 +92,7 @@ void ProducerEventAdapter::adaptButtonPress(double time,float x, float y, unsign
}
_s_mx = (int)(x*(float)(_s_Xmax-_s_Xmin))+_s_Xmin;
_s_my = (int)(y*(float)(_s_Ymax-_s_Ymin))+_s_Ymin;
_s_my = (int)(y*(float)(_s_Ymin-_s_Ymax))+_s_Ymax;
copyStaticVariables();
}
@ -102,9 +102,9 @@ void ProducerEventAdapter::adaptButtonRelease(double time,float x, float y, unsi
_time = time;
_eventType = RELEASE;
_button = button;
_button = button-1;
switch(button)
switch(_button)
{
case(0):
_s_accumulatedButtonMask =
@ -124,7 +124,7 @@ void ProducerEventAdapter::adaptButtonRelease(double time,float x, float y, unsi
}
_s_mx = (int)(x*(float)(_s_Xmax-_s_Xmin))+_s_Xmin;
_s_my = (int)(y*(float)(_s_Ymax-_s_Ymin))+_s_Ymin;
_s_my = (int)(y*(float)(_s_Ymin-_s_Ymax))+_s_Ymax;
copyStaticVariables();
}
@ -135,7 +135,7 @@ void ProducerEventAdapter::adaptMouseMotion(double time, float x, float y)
_eventType = DRAG;
_time = time;
_s_mx = (int)(x*(float)(_s_Xmax-_s_Xmin))+_s_Xmin;
_s_my = (int)(y*(float)(_s_Ymax-_s_Ymin))+_s_Ymin;
_s_my = (int)(y*(float)(_s_Ymin-_s_Ymax))+_s_Ymax;
copyStaticVariables();
}

View File

@ -87,3 +87,12 @@ void ProducerEventCallback::buttonRelease( float mx, float my, unsigned int mbut
_eventQueue.push_back(event);
_eventQueueMutex.unlock();
}
void ProducerEventCallback::getEventQueue(EventQueue& queue)
{
queue.clear();
_eventQueueMutex.lock();
_eventQueue.swap(queue);
_eventQueueMutex.unlock();
}

View File

@ -20,7 +20,7 @@ class ProducerEventCallback : public Producer::KeyboardMouseCallback
_done(done)
{}
virtual ~ProducerEventCallback();
virtual ~ProducerEventCallback() {}
virtual void keyPress( Producer::KeySymbol key );

View File

@ -22,7 +22,17 @@
#include <osgDB/ReadFile>
#include "MyKeyboardMouseCallback"
#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>
#if USE_MY_KEYBOARD_MOUSE_CALLBACK
#include "MyKeyboardMouseCallback"
#else
#include "ProducerEventCallback.h"
#include "ProducerActionAdapter.h"
#endif
#include <list>
static Producer::CameraConfig *BuildConfig(void)
{
@ -100,11 +110,11 @@ int main( int argc, char **argv )
// read the scene.
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(filenameList);
if (!scene) return 1;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(filenameList);
if (!loadedModel) return 1;
osgUtil::Optimizer optimizer;
optimizer.optimize(scene.get());
optimizer.optimize(loadedModel.get());
// set up the keyboard and mouse handling.
@ -113,10 +123,19 @@ int main( int argc, char **argv )
(new Producer::KeyboardMouse(ia)) :
(new Producer::KeyboardMouse(cg->getCamera(0)->getRenderSurface()));
#if USE_MY_KEYBOARD_MOUSE_CALLBACK
bool done = false;
MyKeyboardMouseCallback kbmcb(done);
kbm->setCallback( &kbmcb );
kbm->startThread();
#else
bool done = false;
ProducerEventCallback kbmcb(done);
kbm->setCallback( &kbmcb );
kbm->startThread();
#endif
Producer::Trackball tb;
tb.setOrientation( Producer::Trackball::Y_UP );
@ -124,9 +143,9 @@ int main( int argc, char **argv )
// set the globa state
osg::StateSet* globalStateSet = new osg::StateSet;
osg::ref_ptr<osg::StateSet> globalStateSet = new osg::StateSet;
globalStateSet->setGlobalDefaults();
cg->setGlobalStateSet(globalStateSet);
cg->setGlobalStateSet(globalStateSet.get());
// add either a headlight or sun light to the scene.
@ -136,7 +155,7 @@ int main( int argc, char **argv )
lightsource->setReferenceFrame(osg::LightSource::RELATIVE_TO_ABSOLUTE);
lightsource->setLocalStateSetModes(osg::StateAttribute::ON);
lightsource->addChild(scene.get());
lightsource->addChild(loadedModel.get());
// set the scene to render
@ -159,6 +178,26 @@ int main( int argc, char **argv )
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
// create a camera to use with the manipulators.
osg::ref_ptr<osg::Camera> old_style_osg_camera = new osg::Camera;
osg::ref_ptr<osgGA::TrackballManipulator> trackballManipulator = new osgGA::TrackballManipulator;
trackballManipulator->setCamera(old_style_osg_camera.get());
trackballManipulator->setNode(loadedModel.get());
osg::ref_ptr<osgGA::StateSetManipulator> 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<osgGA::GUIEventHandler> > EventHandlerList;
EventHandlerList eventHandlerList;
eventHandlerList.push_back(trackballManipulator.get());
eventHandlerList.push_back(statesetManipulator.get());
// create a dummy action adapter right now.
ProducerActionAdapter actionAdapter;
while( !done )
{
// syncronize to screen refresh.
@ -172,12 +211,41 @@ int main( int argc, char **argv )
// update the trackball
tb.input( kbmcb.mx(), kbmcb.my(), kbmcb.mbutton() );
// get the event since the last frame.
ProducerEventCallback::EventQueue queue;
kbmcb.getEventQueue(queue);
// create an event to signal the new frame.
osg::ref_ptr<ProducerEventAdapter> frame_event = new ProducerEventAdapter;
frame_event->adaptFrame(frameStamp->getReferenceTime());
queue.push_back(frame_event);
// dispatch the events in order of arrival.
for(ProducerEventCallback::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);
}
}
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
scene->accept(update);
cg->getSceneData()->accept(update);
// update the main camera
// update the main producer camera
#if USE_MY_KEYBOARD_MOUSE_CALLBACK
cg->setView(tb.getMatrix().ptr());
#else
cg->setView(old_style_osg_camera->getModelViewMatrix().ptr());
#endif
// fire off the cull and draw traversals of the scene.
cg->frame();