1 // fg_os_osgviewer.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
4 // Copyright (C) 2007 Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <boost/foreach.hpp>
34 #include <simgear/compiler.h>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/props/props_io.hxx>
40 #include <osg/GraphicsContext>
42 #include <osg/Matrixd>
43 #include <osg/Viewport>
44 #include <osg/Version>
47 #include <osgViewer/ViewerEventHandlers>
48 #include <osgViewer/Viewer>
49 #include <osgViewer/GraphicsWindow>
51 #include <Scenery/scenery.hxx>
52 #include <Main/fg_os.hxx>
53 #include <Main/fg_props.hxx>
54 #include <Main/util.hxx>
55 #include <Main/globals.hxx>
56 #include "renderer.hxx"
57 #include "CameraGroup.hxx"
58 #include "FGEventHandler.hxx"
59 #include "WindowBuilder.hxx"
60 #include "WindowSystemAdapter.hxx"
62 // Static linking of OSG needs special macros
63 #ifdef OSG_LIBRARY_STATIC
64 #include <osgDB/Registry>
74 #ifdef OSG_JPEG_ENABLED
77 #ifdef OSG_PNG_ENABLED
80 #ifdef OSG_TIFF_ENABLED
91 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
92 // to create the graphics window and run the event/update/render loop.
95 // fg_os implementation
99 using namespace flightgear;
102 static osg::ref_ptr<osgViewer::Viewer> viewer;
103 static osg::ref_ptr<osg::Camera> mainCamera;
105 static void setStereoMode( const char * mode )
107 DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER;
108 bool stereoOn = true;
110 if (strcmp(mode,"QUAD_BUFFER")==0)
112 stereoMode = DisplaySettings::QUAD_BUFFER;
114 else if (strcmp(mode,"ANAGLYPHIC")==0)
116 stereoMode = DisplaySettings::ANAGLYPHIC;
118 else if (strcmp(mode,"HORIZONTAL_SPLIT")==0)
120 stereoMode = DisplaySettings::HORIZONTAL_SPLIT;
122 else if (strcmp(mode,"VERTICAL_SPLIT")==0)
124 stereoMode = DisplaySettings::VERTICAL_SPLIT;
126 else if (strcmp(mode,"LEFT_EYE")==0)
128 stereoMode = DisplaySettings::LEFT_EYE;
130 else if (strcmp(mode,"RIGHT_EYE")==0)
132 stereoMode = DisplaySettings::RIGHT_EYE;
134 else if (strcmp(mode,"HORIZONTAL_INTERLACE")==0)
136 stereoMode = DisplaySettings::HORIZONTAL_INTERLACE;
138 else if (strcmp(mode,"VERTICAL_INTERLACE")==0)
140 stereoMode = DisplaySettings::VERTICAL_INTERLACE;
142 else if (strcmp(mode,"CHECKERBOARD")==0)
144 stereoMode = DisplaySettings::CHECKERBOARD;
148 DisplaySettings::instance()->setStereo( stereoOn );
149 DisplaySettings::instance()->setStereoMode( stereoMode );
152 static const char * getStereoMode()
154 DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode();
155 bool stereoOn = DisplaySettings::instance()->getStereo();
156 if( !stereoOn ) return "OFF";
157 if( stereoMode == DisplaySettings::QUAD_BUFFER ) {
158 return "QUAD_BUFFER";
159 } else if( stereoMode == DisplaySettings::ANAGLYPHIC ) {
161 } else if( stereoMode == DisplaySettings::HORIZONTAL_SPLIT ) {
162 return "HORIZONTAL_SPLIT";
163 } else if( stereoMode == DisplaySettings::VERTICAL_SPLIT ) {
164 return "VERTICAL_SPLIT";
165 } else if( stereoMode == DisplaySettings::LEFT_EYE ) {
167 } else if( stereoMode == DisplaySettings::RIGHT_EYE ) {
169 } else if( stereoMode == DisplaySettings::HORIZONTAL_INTERLACE ) {
170 return "HORIZONTAL_INTERLACE";
171 } else if( stereoMode == DisplaySettings::VERTICAL_INTERLACE ) {
172 return "VERTICAL_INTERLACE";
173 } else if( stereoMode == DisplaySettings::CHECKERBOARD ) {
174 return "CHECKERBOARD";
180 * merge OSG output into our logging system, so it gets recorded to file,
181 * and so we can display a GUI console with renderer issues, especially
182 * shader compilation warnings and errors.
184 class NotifyLogger : public osg::NotifyHandler
187 // note this callback will be invoked by OSG from multiple threads.
188 // fortunately our Simgear logging implementation already handles
189 // that internally, so we simply pass the message on.
190 virtual void notify(osg::NotifySeverity severity, const char *message)
192 SG_LOG(SG_GL, translateSeverity(severity), message);
194 // Detect whether a osg::Reference derived object is deleted with a non-zero
195 // reference count. In this case trigger a segfault to get a stack trace.
196 if( strstr(message, "the final reference count was") )
199 int* trigger_segfault = 0;
200 *trigger_segfault = 0;
205 sgDebugPriority translateSeverity(osg::NotifySeverity severity)
209 case osg::FATAL: return SG_ALERT;
210 case osg::WARN: return SG_WARN;
212 case osg::INFO: return SG_INFO;
214 case osg::DEBUG_INFO: return SG_DEBUG;
215 default: return SG_ALERT;
220 void fgOSOpenWindow(bool stencil)
222 osg::setNotifyHandler(new NotifyLogger);
223 //osg::setNotifyLevel(osg::DEBUG_INFO);
225 viewer = new osgViewer::Viewer;
226 viewer->setDatabasePager(FGScenery::getPagerSingleton());
227 CameraGroup* cameraGroup = 0;
229 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
230 if (mode == "AutomaticSelection")
231 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
232 else if (mode == "CullDrawThreadPerContext")
233 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
234 else if (mode == "DrawThreadPerContext")
235 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
236 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
237 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
239 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
240 WindowBuilder::initWindowBuilder(stencil);
241 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
243 // Look for windows, camera groups, and the old syntax of
245 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
246 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
247 bool oldSyntax = !cgroupNode->hasChild("camera");
249 for (int i = 0; i < renderingNode->nChildren(); ++i) {
250 SGPropertyNode* propNode = renderingNode->getChild(i);
251 const char* propName = propNode->getName();
252 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
253 SGPropertyNode* copiedNode
254 = cgroupNode->getNode(propName, propNode->getIndex(), true);
255 copyProperties(propNode, copiedNode);
258 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
259 SGPropertyNode* masterCamera = 0;
260 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
261 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
262 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
263 masterCamera = camera.ptr();
268 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
269 setValue(masterCamera->getNode("window/name", true),
270 windowBuilder->getDefaultWindowName());
272 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
274 setValue(cgroupNode->getNode("gui/window/name", true),
275 nameNode->getStringValue());
277 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
278 Camera* guiCamera = getGUICamera(cameraGroup);
280 Viewport* guiViewport = guiCamera->getViewport();
281 fgSetInt("/sim/startup/xsize", guiViewport->width());
282 fgSetInt("/sim/startup/ysize", guiViewport->height());
284 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
285 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
286 if (wsa->windows.size() != 1) {
287 manipulator->setResizable(false);
289 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
290 viewer->addEventHandler(manipulator);
291 // Let FG handle the escape key with a confirmation
292 viewer->setKeyEventSetsDone(0);
293 // The viewer won't start without some root.
294 viewer->setSceneData(new osg::Group);
295 globals->get_renderer()->setViewer(viewer.get());
296 CameraGroup::setDefault(cameraGroup);
298 DisplaySettings * displaySettings = DisplaySettings::instance();
299 fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
300 fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
301 fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
302 fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
303 fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
304 fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
305 fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
306 fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
310 static int status = 0;
312 void fgOSExit(int code)
314 viewer->setDone(true);
315 viewer->getDatabasePager()->cancel();
321 ref_ptr<FGEventHandler> manipulator
322 = globals->get_renderer()->getEventHandler();
323 viewer->setReleaseContextAtEndOfFrameHint(false);
324 if (!viewer->isRealized())
326 while (!viewer->done()) {
327 fgIdleHandler idleFunc = manipulator->getIdleHandler();
330 globals->get_renderer()->update();
331 viewer->frame( globals->get_sim_time_sec() );
337 int fgGetKeyModifiers()
339 if (!globals->get_renderer()) { // happens during shutdown
343 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
346 void fgWarpMouse(int x, int y)
348 warpGUIPointer(CameraGroup::getDefault(), x, y);
351 void fgOSInit(int* argc, char** argv)
353 globals->get_renderer()->init();
354 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
357 void fgOSFullScreen()
359 std::vector<osgViewer::GraphicsWindow*> windows;
360 viewer->getWindows(windows);
365 /* Toggling window fullscreen is only supported for the main GUI window.
366 * The other windows should use fixed setup from the camera.xml file anyway. */
367 osgViewer::GraphicsWindow* window = windows[0];
370 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
374 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
378 static int previous_x = 0;
379 static int previous_y = 0;
380 static int previous_width = 800;
381 static int previous_height = 600;
383 unsigned int screenWidth;
384 unsigned int screenHeight;
385 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
391 window->getWindowRectangle(x, y, width, height);
393 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
394 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
395 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
396 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
397 * "false" - even when a single window is display in fullscreen */
398 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
399 bool isFullScreen = !window->getWindowDecoration();
401 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
402 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
403 << ", number of screens: " << wsi->getNumScreens());
406 // limit x,y coordinates and window size to screen area
407 if (previous_x + previous_width > (int)screenWidth)
409 if (previous_y + previous_height > (int)screenHeight)
412 // disable fullscreen mode, restore previous window size/coordinates
415 width = previous_width;
416 height = previous_height;
420 // remember previous setting
423 previous_width = width;
424 previous_height = height;
426 // enable fullscreen mode, set new width/height
430 height = screenHeight;
433 // set xsize/ysize properties to adapt GUI planes
434 fgSetInt("/sim/startup/xsize", width);
435 fgSetInt("/sim/startup/ysize", height);
437 // reconfigure window
438 window->setWindowDecoration(isFullScreen);
439 window->setWindowRectangle(x, y, width, height);
440 window->grabFocusIfPointerInWindow();
444 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
450 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
451 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
452 if (cursor == MOUSE_CURSOR_NONE)
453 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
454 else if(cursor == MOUSE_CURSOR_POINTER)
456 // osgViewer-Cocoa lacks RightArrowCursor, use Left
457 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
459 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
461 else if(cursor == MOUSE_CURSOR_WAIT)
462 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
463 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
464 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
465 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
466 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
467 else if(cursor == MOUSE_CURSOR_TOPSIDE)
468 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
469 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
470 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
471 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
472 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
473 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
474 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
475 else if(cursor == MOUSE_CURSOR_TOPLEFT)
476 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
477 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
478 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
479 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
480 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
481 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
482 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
484 gw->setCursor(mouseCursor);
487 static int _cursor = -1;
489 void fgSetMouseCursor(int cursor)
493 std::vector<osgViewer::GraphicsWindow*> windows;
494 viewer->getWindows(windows);
495 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
496 setMouseCursor(gw, cursor);
500 int fgGetMouseCursor()