3 #include <simgear/compiler.h>
4 #include <simgear/structure/exception.hxx>
5 #include <simgear/debug/logstream.hxx>
7 #include <osg/GraphicsContext>
10 #include <osg/Viewport>
11 #include <osgViewer/StatsHandler>
12 #include <osgViewer/Viewer>
13 #include <osgGA/MatrixManipulator>
17 #include "globals.hxx"
18 #include "renderer.hxx"
20 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
21 // to create the graphics window and run the event/update/render loop.
23 // fg_os callback registration APIs
27 // Event handling and scene graph update is all handled by a
28 // manipulator. See FGManipulator.cpp
29 void fgRegisterIdleHandler(fgIdleHandler func)
31 globals->get_renderer()->getManipulator()->setIdleHandler(func);
34 void fgRegisterDrawHandler(fgDrawHandler func)
36 globals->get_renderer()->getManipulator()->setDrawHandler(func);
39 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
41 globals->get_renderer()->getManipulator()->setWindowResizeHandler(func);
44 void fgRegisterKeyHandler(fgKeyHandler func)
46 globals->get_renderer()->getManipulator()->setKeyHandler(func);
49 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
51 globals->get_renderer()->getManipulator()->setMouseClickHandler(func);
54 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
56 globals->get_renderer()->getManipulator()->setMouseMotionHandler(func);
59 // Redraw "happens" every frame whether you want it or not.
60 void fgRequestRedraw()
65 // fg_os implementation
69 static osg::ref_ptr<osgViewer::Viewer> viewer;
71 void fgOSOpenWindow(int w, int h, int bpp,
72 bool alpha, bool stencil, bool fullscreen)
74 viewer = new osgViewer::Viewer;
75 // Avoid complications with fg's custom drawables.
76 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
77 osg::ref_ptr<osg::GraphicsContext::Traits> traits
78 = new osg::GraphicsContext::Traits;
79 int cbits = (bpp <= 16) ? 5 : 8;
80 int zbits = (bpp <= 16) ? 16 : 24;
83 traits->red = traits->green = traits->blue = cbits;
84 traits->depth = zbits;
90 traits->windowDecoration = false;
92 traits->windowDecoration = true;
93 traits->supportsResize = true;
94 traits->doubleBuffer = true;
95 osg::GraphicsContext* gc
96 = osg::GraphicsContext::createGraphicsContext(traits.get());
97 viewer->getCamera()->setGraphicsContext(gc);
98 // If a viewport isn't set on the camera, then it's hard to dig it
99 // out of the SceneView objects in the viewer, and the coordinates
100 // of mouse events are somewhat bizzare.
101 viewer->getCamera()->setViewport(new osg::Viewport(0, 0,
104 viewer->setCameraManipulator(globals->get_renderer()->getManipulator());
105 // Let FG handle the escape key with a confirmation
106 viewer->setKeyEventSetsDone(0);
107 osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
108 statsHandler->setKeyEventTogglesOnScreenStats('*');
109 statsHandler->setKeyEventPrintsOutStats(0);
110 viewer->addEventHandler(statsHandler);
111 // The viewer won't start without some root.
112 viewer->setSceneData(new osg::Group);
113 globals->get_renderer()->setViewer(viewer.get());
116 static int status = 0;
118 void fgOSExit(int code)
120 viewer->setDone(true);
130 int fgGetKeyModifiers()
132 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
135 void fgWarpMouse(int x, int y)
137 viewer->requestWarpPointer((float)x, (float)y);
141 void fgOSInit(int* argc, char** argv)
145 void fgOSFullScreen()
147 // Noop, but is probably doable
151 // No support in OSG yet
152 void fgSetMouseCursor(int cursor)
156 int fgGetMouseCursor()
163 osg::GraphicsContext* gc = viewer->getCamera()->getGraphicsContext();