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 <osg/Version>
12 #include <osgViewer/StatsHandler>
13 #include <osgViewer/Viewer>
14 #include <osgGA/MatrixManipulator>
17 #include "fg_props.hxx"
19 #include "globals.hxx"
20 #include "renderer.hxx"
22 #if ((1 == OSG_VERSION_MAJOR) && (9 == OSG_VERSION_MINOR) && \
23 (8 <= OSG_VERSION_PATCH)) || \
24 ((1 == OSG_VERSION_MAJOR) && (9 < OSG_VERSION_MINOR)) || \
25 (1 < OSG_VERSION_MAJOR)
26 #define OSG_HAS_MOUSE_CURSOR_PATCH
29 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
30 // to create the graphics window and run the event/update/render loop.
32 // fg_os callback registration APIs
36 // Event handling and scene graph update is all handled by a
37 // manipulator. See FGManipulator.cpp
38 void fgRegisterIdleHandler(fgIdleHandler func)
40 globals->get_renderer()->getManipulator()->setIdleHandler(func);
43 void fgRegisterDrawHandler(fgDrawHandler func)
45 globals->get_renderer()->getManipulator()->setDrawHandler(func);
48 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
50 globals->get_renderer()->getManipulator()->setWindowResizeHandler(func);
53 void fgRegisterKeyHandler(fgKeyHandler func)
55 globals->get_renderer()->getManipulator()->setKeyHandler(func);
58 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
60 globals->get_renderer()->getManipulator()->setMouseClickHandler(func);
63 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
65 globals->get_renderer()->getManipulator()->setMouseMotionHandler(func);
68 // Redraw "happens" every frame whether you want it or not.
69 void fgRequestRedraw()
74 // fg_os implementation
77 static osg::ref_ptr<osgViewer::Viewer> viewer;
78 static osg::ref_ptr<osg::Camera> mainCamera;
80 void fgOSOpenWindow(int w, int h, int bpp,
81 bool alpha, bool stencil, bool fullscreen)
83 osg::GraphicsContext::WindowingSystemInterface* wsi;
84 wsi = osg::GraphicsContext::getWindowingSystemInterface();
86 viewer = new osgViewer::Viewer;
87 // Avoid complications with fg's custom drawables.
89 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
90 if (mode == "AutomaticSelection")
91 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
92 else if (mode == "CullDrawThreadPerContext")
93 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
94 else if (mode == "DrawThreadPerContext")
95 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
96 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
97 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
99 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
100 osg::ref_ptr<osg::GraphicsContext::Traits> traits;
101 traits = new osg::GraphicsContext::Traits;
102 int cbits = (bpp <= 16) ? 5 : 8;
103 int zbits = (bpp <= 16) ? 16 : 24;
104 traits->red = traits->green = traits->blue = cbits;
105 traits->depth = zbits;
110 traits->doubleBuffer = true;
111 traits->mipMapGeneration = true;
112 traits->windowName = "FlightGear";
113 traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
114 traits->samples = fgGetBool("/sim/rendering/multi-samples", traits->samples);
115 traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
120 wsi->getScreenResolution(*traits, width, height);
121 traits->windowDecoration = false;
122 traits->width = width;
123 traits->height = height;
124 traits->supportsResize = false;
126 traits->windowDecoration = true;
130 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
134 traits->supportsResize = true;
137 osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
139 // Ok, first the children.
140 // that achieves some magic ordering og the slaves so that we end up
141 // in the main window more often.
142 // This can be sorted out better when we got rid of glut and sdl.
143 if (fgHasNode("/sim/rendering/camera")) {
144 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
145 for (int i = 0; i < renderingNode->nChildren(); ++i) {
146 SGPropertyNode* cameraNode = renderingNode->getChild(i);
147 if (strcmp(cameraNode->getName(), "camera") != 0)
150 // get a new copy of the traits struct
151 osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
152 cameraTraits = new osg::GraphicsContext::Traits(*traits);
154 double shearx = cameraNode->getDoubleValue("shear-x", 0);
155 double sheary = cameraNode->getDoubleValue("shear-y", 0);
156 cameraTraits->hostName = cameraNode->getStringValue("host-name", "");
157 cameraTraits->displayNum = cameraNode->getIntValue("display", 0);
158 cameraTraits->screenNum = cameraNode->getIntValue("screen", 0);
159 if (cameraNode->getBoolValue("fullscreen", fullscreen)) {
162 wsi->getScreenResolution(*cameraTraits, width, height);
163 cameraTraits->windowDecoration = false;
164 cameraTraits->width = width;
165 cameraTraits->height = height;
166 cameraTraits->supportsResize = false;
168 cameraTraits->windowDecoration = true;
169 cameraTraits->width = cameraNode->getIntValue("width", w);
170 cameraTraits->height = cameraNode->getIntValue("height", h);
171 cameraTraits->supportsResize = true;
173 // FIXME, currently this is too much of a problem to route the resize
174 // events. When we do no longer need sdl and such this
176 cameraTraits->supportsResize = false;
178 // ok found a camera configuration, add a new slave ...
179 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
181 osg::GraphicsContext* gc;
182 gc = osg::GraphicsContext::createGraphicsContext(cameraTraits.get());
184 camera->setGraphicsContext(gc);
185 // If a viewport isn't set on the camera, then it's hard to dig it
186 // out of the SceneView objects in the viewer, and the coordinates
187 // of mouse events are somewhat bizzare.
188 camera->setViewport(new osg::Viewport(0, 0, cameraTraits->width, cameraTraits->height));
189 camera->setProjectionResizePolicy(rsp);
190 viewer->addSlave(camera.get(), osg::Matrix::translate(-shearx, -sheary, 0), osg::Matrix());
194 // now the main camera ...
195 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
197 osg::GraphicsContext* gc;
198 gc = osg::GraphicsContext::createGraphicsContext(traits.get());
201 camera->setGraphicsContext(gc);
202 // If a viewport isn't set on the camera, then it's hard to dig it
203 // out of the SceneView objects in the viewer, and the coordinates
204 // of mouse events are somewhat bizzare.
205 camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
206 camera->setProjectionResizePolicy(rsp);
207 viewer->addSlave(camera.get());
209 viewer->setCameraManipulator(globals->get_renderer()->getManipulator());
210 // Let FG handle the escape key with a confirmation
211 viewer->setKeyEventSetsDone(0);
212 osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
213 statsHandler->setKeyEventTogglesOnScreenStats('*');
214 statsHandler->setKeyEventPrintsOutStats(0);
215 viewer->addEventHandler(statsHandler);
216 // The viewer won't start without some root.
217 viewer->setSceneData(new osg::Group);
218 globals->get_renderer()->setViewer(viewer.get());
221 static int status = 0;
223 void fgOSExit(int code)
225 viewer->setDone(true);
235 int fgGetKeyModifiers()
237 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
240 void fgWarpMouse(int x, int y)
242 // Hack, currently the pointer is just recentered. So, we know the relative coordinates ...
243 viewer->requestWarpPointer(0, 0);
247 void fgOSInit(int* argc, char** argv)
252 void fgOSFullScreen()
256 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
257 static void setMouseCursor(osg::Camera* camera, int cursor)
261 osg::GraphicsContext* gc = camera->getGraphicsContext();
264 osgViewer::GraphicsWindow* gw;
265 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
269 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
270 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
271 if (cursor == MOUSE_CURSOR_NONE)
272 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
273 else if(cursor == MOUSE_CURSOR_POINTER)
274 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
275 else if(cursor == MOUSE_CURSOR_WAIT)
276 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
277 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
278 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
279 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
280 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
282 gw->setCursor(mouseCursor);
286 static int _cursor = -1;
288 void fgSetMouseCursor(int cursor)
291 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
292 setMouseCursor(viewer->getCamera(), cursor);
293 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
294 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
298 int fgGetMouseCursor()
305 if (!mainCamera.valid())
307 osg::GraphicsContext* gc = mainCamera->getGraphicsContext();
313 bool fgOSIsMainContext(const osg::GraphicsContext* context)
315 if (!mainCamera.valid())
317 return context == mainCamera->getGraphicsContext();
320 bool fgOSIsMainCamera(const osg::Camera* camera)
324 if (camera == mainCamera.get())
328 if (camera == viewer->getCamera())