1
0
Fork 0

Changed GLUT support to use an embedded osgViewer too

This commit is contained in:
timoore 2007-12-07 09:11:46 +00:00
parent 8380fb4463
commit 3426e3cde3
4 changed files with 114 additions and 67 deletions

View file

@ -18,7 +18,7 @@ FGManipulator::FGManipulator() :
idleHandler(0), drawHandler(0), windowResizeHandler(0), keyHandler(0), idleHandler(0), drawHandler(0), windowResizeHandler(0), keyHandler(0),
mouseClickHandler(0), mouseMotionHandler(0), currentModifiers(0), mouseClickHandler(0), mouseMotionHandler(0), currentModifiers(0),
osgModifiers(0), resizable(true), mouseWarped(false), osgModifiers(0), resizable(true), mouseWarped(false),
scrollButtonPressed(false) scrollButtonPressed(false), useEventModifiers(false)
{ {
using namespace osgGA; using namespace osgGA;
@ -271,7 +271,9 @@ void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) { if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
key = numPadIter->second; key = numPadIter->second;
} }
} else { } else if (useEventModifiers) {
}else {
// Track the modifiers because OSG is currently (2.0) broken // Track the modifiers because OSG is currently (2.0) broken
KeyMaskMap::iterator iter = keyMaskMap.find(key); KeyMaskMap::iterator iter = keyMaskMap.find(key);
if (iter != keyMaskMap.end()) { if (iter != keyMaskMap.end()) {

View file

@ -118,6 +118,8 @@ public:
bool getResizable() { return resizable; } bool getResizable() { return resizable; }
void setResizable(bool _resizable) { resizable = _resizable; } void setResizable(bool _resizable) { resizable = _resizable; }
bool getUseEventModifiers() { return useEventModifiers; }
void setUseEventModifiers(bool val) { useEventModifiers = val; }
protected: protected:
osg::ref_ptr<osg::Node> _node; osg::ref_ptr<osg::Node> _node;
fgIdleHandler idleHandler; fgIdleHandler idleHandler;
@ -140,5 +142,8 @@ protected:
// workaround for osgViewer double scroll events // workaround for osgViewer double scroll events
bool scrollButtonPressed; bool scrollButtonPressed;
int release_keys[128]; int release_keys[128];
// When the viewer is embedded, the host toolkit may deliver a
// valid event mask but not control keys.
bool useEventModifiers;
}; };
#endif #endif

View file

@ -15,13 +15,14 @@ else
THREAD_LIBS = THREAD_LIBS =
endif endif
GFX_COMMON = fg_os_common.cxx fg_os.hxx
if USE_SDL if USE_SDL
GFX_CODE = fg_os_sdl.cxx fg_os_common.cxx fg_os.hxx GFX_CODE = fg_os_sdl.cxx $(GFX_COMMON)
else else
if USE_OSGVIEWER if USE_OSGVIEWER
GFX_CODE = fg_os_osgviewer.cxx fg_os_common.cxx fg_os.hxx GFX_CODE = fg_os_osgviewer.cxx $(GFX_COMMON)
else else
GFX_CODE = fg_os.cxx fg_os.hxx GFX_CODE = fg_os.cxx $(GFX_COMMON)
endif endif
endif endif

View file

@ -7,26 +7,20 @@
# include <config.h> # include <config.h>
#endif #endif
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include SG_GLUT_H #include SG_GLUT_H
#include <plib/pu.h> #include <plib/pu.h>
#include "fg_props.hxx" #include <Scenery/scenery.hxx>
#include "fg_os.hxx" #include "fg_os.hxx"
#include "globals.hxx"
// #include "renderer.hxx"
// fg_os callback registration APIs #include "fg_props.hxx"
// (These are not glut-specific)
//
static fgIdleHandler IdleHandler = 0;
static fgDrawHandler DrawHandler = 0;
static fgWindowResizeHandler WindowResizeHandler = 0;
static fgKeyHandler KeyHandler = 0;
static fgMouseClickHandler MouseClickHandler = 0;
static fgMouseMotionHandler MouseMotionHandler = 0;
// We need to flush all pending mouse move events past a mouse warp to avoid // We need to flush all pending mouse move events past a mouse warp to avoid
// a race condition ending in warping twice and having huge increments for the // a race condition ending in warping twice and having huge increments for the
@ -35,62 +29,76 @@ static fgMouseMotionHandler MouseMotionHandler = 0;
// ignoring mouse move events between a warp mouse and the next frame. // ignoring mouse move events between a warp mouse and the next frame.
static bool mouseWarped = false; static bool mouseWarped = false;
void fgRegisterIdleHandler(fgIdleHandler func)
{
IdleHandler = func;
}
void fgRegisterDrawHandler(fgDrawHandler func)
{
DrawHandler = func;
}
void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
{
WindowResizeHandler = func;
}
void fgRegisterKeyHandler(fgKeyHandler func)
{
KeyHandler = func;
}
void fgRegisterMouseClickHandler(fgMouseClickHandler func)
{
MouseClickHandler = func;
}
void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
{
MouseMotionHandler = func;
}
// //
// Native glut callbacks. // Native glut callbacks.
// These translate the glut event model into fg*Handler callbacks // These translate the glut event model into osgGA events
// //
static osg::ref_ptr<osgViewer::Viewer> viewer;
static osg::ref_ptr<osg::Camera> mainCamera;
static osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw;
static int GlutModifiers = 0; static int GlutModifiers = 0;
static unsigned int getOSGModifiers(int modifiers);
static void callKeyHandler(int k, int mods, int x, int y) static void callKeyHandler(int k, int mods, int x, int y)
{ {
int puiup = mods & KEYMOD_RELEASED ? PU_UP : PU_DOWN;
if(puKeyboard(k, puiup)) int key = 0;
return; switch (k) {
if(KeyHandler) (*KeyHandler)(k, mods, x, y); case 0x1b: key = osgGA::GUIEventAdapter::KEY_Escape; break;
case '\n': key = osgGA::GUIEventAdapter::KEY_Return; break;
case '\b': key = osgGA::GUIEventAdapter::KEY_BackSpace; break;
case 0x7f: key = osgGA::GUIEventAdapter::KEY_Delete; break;
case '\t': key = osgGA::GUIEventAdapter::KEY_Tab; break;
case PU_KEY_LEFT: key = osgGA::GUIEventAdapter::KEY_Left; break;
case PU_KEY_UP: key = osgGA::GUIEventAdapter::KEY_Up; break;
case PU_KEY_RIGHT: key = osgGA::GUIEventAdapter::KEY_Right; break;
case PU_KEY_DOWN: key = osgGA::GUIEventAdapter::KEY_Down; break;
case PU_KEY_PAGE_UP: key = osgGA::GUIEventAdapter::KEY_Page_Up; break;
case PU_KEY_PAGE_DOWN: key = osgGA::GUIEventAdapter::KEY_Page_Down; break;
case PU_KEY_HOME: key = osgGA::GUIEventAdapter::KEY_Home; break;
case PU_KEY_END: key = osgGA::GUIEventAdapter::KEY_End; break;
case PU_KEY_INSERT: key = osgGA::GUIEventAdapter::KEY_Insert; break;
case PU_KEY_F1: key = osgGA::GUIEventAdapter::KEY_F1; break;
case PU_KEY_F2: key = osgGA::GUIEventAdapter::KEY_F2; break;
case PU_KEY_F3: key = osgGA::GUIEventAdapter::KEY_F3; break;
case PU_KEY_F4: key = osgGA::GUIEventAdapter::KEY_F4; break;
case PU_KEY_F5: key = osgGA::GUIEventAdapter::KEY_F5; break;
case PU_KEY_F6: key = osgGA::GUIEventAdapter::KEY_F6; break;
case PU_KEY_F7: key = osgGA::GUIEventAdapter::KEY_F7; break;
case PU_KEY_F8: key = osgGA::GUIEventAdapter::KEY_F8; break;
case PU_KEY_F9: key = osgGA::GUIEventAdapter::KEY_F9; break;
case PU_KEY_F10: key = osgGA::GUIEventAdapter::KEY_F10; break;
case PU_KEY_F11: key = osgGA::GUIEventAdapter::KEY_F11; break;
case PU_KEY_F12: key = osgGA::GUIEventAdapter::KEY_F12; break;
default: key = k; break;
}
unsigned int osgModifiers = getOSGModifiers(mods);
osgGA::EventQueue& queue = *gw->getEventQueue();
queue.getCurrentEventState()->setModKeyMask(osgModifiers);
if (mods & KEYMOD_RELEASED)
queue.keyRelease((osgGA::GUIEventAdapter::KeySymbol)key);
else
queue.keyPress((osgGA::GUIEventAdapter::KeySymbol)key);
} }
static void GLUTmotion (int x, int y) static void GLUTmotion (int x, int y)
{ {
if (mouseWarped) if (mouseWarped || !gw.valid())
return; return;
if(MouseMotionHandler) (*MouseMotionHandler)(x, y); gw->getEventQueue()->mouseMotion(x, y);
} }
static void GLUTmouse (int button, int updown, int x, int y) static void GLUTmouse (int button, int updown, int x, int y)
{ {
GlutModifiers = glutGetModifiers(); GlutModifiers = glutGetModifiers();
if(MouseClickHandler) (*MouseClickHandler)(button, updown, x, y, true, 0); if (gw.valid()) {
if (updown == 0)
gw->getEventQueue()->mouseButtonPress(x, y, button+1);
else
gw->getEventQueue()->mouseButtonRelease(x, y, button+1);
}
} }
static void GLUTspecialkeyup(int k, int x, int y) static void GLUTspecialkeyup(int k, int x, int y)
@ -130,22 +138,20 @@ static void GLUTkey(unsigned char k, int x, int y)
callKeyHandler(k, fgGetKeyModifiers(), x, y); callKeyHandler(k, fgGetKeyModifiers(), x, y);
} }
static void GLUTidle()
{
if(IdleHandler) (*IdleHandler)();
mouseWarped = false;
}
static void GLUTdraw() static void GLUTdraw()
{ {
if(DrawHandler) (*DrawHandler)(); viewer->frame();
glutSwapBuffers(); glutSwapBuffers();
glutPostRedisplay();
mouseWarped = false; mouseWarped = false;
} }
static void GLUTreshape(int w, int h) static void GLUTreshape(int w, int h)
{ {
if(WindowResizeHandler) (*WindowResizeHandler)(w, h); // update the window dimensions, in case the window has been resized.
gw->resized(gw->getTraits()->x, gw->getTraits()->y, w, h);
gw->getEventQueue()->windowResize(gw->getTraits()->x, gw->getTraits()->y,
w, h);
} }
// //
@ -206,9 +212,16 @@ int fgGetKeyModifiers()
return result; return result;
} }
void fgRequestRedraw() static unsigned int getOSGModifiers(int glutModifiers)
{ {
glutPostRedisplay(); unsigned int result = 0;
if (glutModifiers & GLUT_ACTIVE_SHIFT)
result |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
if (glutModifiers & GLUT_ACTIVE_CTRL)
result |= osgGA::GUIEventAdapter::MODKEY_CTRL;
if(glutModifiers & GLUT_ACTIVE_ALT)
result |= osgGA::GUIEventAdapter::MODKEY_ALT;
return result;
} }
void fgOSOpenWindow(int w, int h, int bpp, bool alpha, void fgOSOpenWindow(int w, int h, int bpp, bool alpha,
@ -244,9 +257,35 @@ void fgOSOpenWindow(int w, int h, int bpp, bool alpha,
glutSpecialFunc(GLUTspecialkey); glutSpecialFunc(GLUTspecialkey);
glutKeyboardUpFunc(GLUTkeyup); glutKeyboardUpFunc(GLUTkeyup);
glutKeyboardFunc(GLUTkey); glutKeyboardFunc(GLUTkey);
glutIdleFunc(GLUTidle);
glutDisplayFunc(GLUTdraw); glutDisplayFunc(GLUTdraw);
glutReshapeFunc(GLUTreshape); glutReshapeFunc(GLUTreshape);
// XXX
int realw = w;
int realh = h;
viewer = new osgViewer::Viewer;
gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, realw, realh);
// now the main camera ...
//osg::ref_ptr<osg::Camera> camera = new osg::Camera;
osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
mainCamera = camera;
osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
// If a viewport isn't set on the camera, then it's hard to dig it
// out of the SceneView objects in the viewer, and the coordinates
// of mouse events are somewhat bizzare.
camera->setViewport(new osg::Viewport(0, 0, realw, realh));
camera->setProjectionResizePolicy(rsp);
//viewer->addSlave(camera.get());
globals->get_renderer()->getManipulator()->setUseEventModifiers(true);
viewer->setCameraManipulator(globals->get_renderer()->getManipulator());
// Let FG handle the escape key with a confirmation
viewer->setKeyEventSetsDone(0);
osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
statsHandler->setKeyEventTogglesOnScreenStats('*');
statsHandler->setKeyEventPrintsOutStats(0);
viewer->addEventHandler(statsHandler);
// The viewer won't start without some root.
viewer->setSceneData(new osg::Group);
globals->get_renderer()->setViewer(viewer.get());
} }
// Noop; the graphics context is always current // Noop; the graphics context is always current