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.
33 #include <boost/algorithm/string/case_conv.hpp>
34 #include <boost/foreach.hpp>
36 #include <simgear/compiler.h>
37 #include <simgear/structure/exception.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/props/props_io.hxx>
42 #include <osg/GraphicsContext>
44 #include <osg/Matrixd>
45 #include <osg/Viewport>
46 #include <osg/Version>
49 #include <osgViewer/ViewerEventHandlers>
50 #include <osgViewer/Viewer>
51 #include <osgViewer/GraphicsWindow>
53 #include <Scenery/scenery.hxx>
54 #include <Main/fg_os.hxx>
55 #include <Main/fg_props.hxx>
56 #include <Main/util.hxx>
57 #include <Main/globals.hxx>
58 #include "renderer.hxx"
59 #include "CameraGroup.hxx"
60 #include "FGEventHandler.hxx"
61 #include "WindowBuilder.hxx"
62 #include "WindowSystemAdapter.hxx"
64 // Static linking of OSG needs special macros
65 #ifdef OSG_LIBRARY_STATIC
66 #include <osgDB/Registry>
76 #ifdef OSG_JPEG_ENABLED
79 #ifdef OSG_PNG_ENABLED
82 #ifdef OSG_TIFF_ENABLED
93 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
94 // to create the graphics window and run the event/update/render loop.
97 // fg_os implementation
101 using namespace flightgear;
104 osg::ref_ptr<osgViewer::Viewer> viewer;
106 static void setStereoMode( const char * mode )
108 DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER;
109 bool stereoOn = true;
111 if (strcmp(mode,"QUAD_BUFFER")==0)
113 stereoMode = DisplaySettings::QUAD_BUFFER;
115 else if (strcmp(mode,"ANAGLYPHIC")==0)
117 stereoMode = DisplaySettings::ANAGLYPHIC;
119 else if (strcmp(mode,"HORIZONTAL_SPLIT")==0)
121 stereoMode = DisplaySettings::HORIZONTAL_SPLIT;
123 else if (strcmp(mode,"VERTICAL_SPLIT")==0)
125 stereoMode = DisplaySettings::VERTICAL_SPLIT;
127 else if (strcmp(mode,"LEFT_EYE")==0)
129 stereoMode = DisplaySettings::LEFT_EYE;
131 else if (strcmp(mode,"RIGHT_EYE")==0)
133 stereoMode = DisplaySettings::RIGHT_EYE;
135 else if (strcmp(mode,"HORIZONTAL_INTERLACE")==0)
137 stereoMode = DisplaySettings::HORIZONTAL_INTERLACE;
139 else if (strcmp(mode,"VERTICAL_INTERLACE")==0)
141 stereoMode = DisplaySettings::VERTICAL_INTERLACE;
143 else if (strcmp(mode,"CHECKERBOARD")==0)
145 stereoMode = DisplaySettings::CHECKERBOARD;
149 DisplaySettings::instance()->setStereo( stereoOn );
150 DisplaySettings::instance()->setStereoMode( stereoMode );
153 static const char * getStereoMode()
155 DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode();
156 bool stereoOn = DisplaySettings::instance()->getStereo();
157 if( !stereoOn ) return "OFF";
158 if( stereoMode == DisplaySettings::QUAD_BUFFER ) {
159 return "QUAD_BUFFER";
160 } else if( stereoMode == DisplaySettings::ANAGLYPHIC ) {
162 } else if( stereoMode == DisplaySettings::HORIZONTAL_SPLIT ) {
163 return "HORIZONTAL_SPLIT";
164 } else if( stereoMode == DisplaySettings::VERTICAL_SPLIT ) {
165 return "VERTICAL_SPLIT";
166 } else if( stereoMode == DisplaySettings::LEFT_EYE ) {
168 } else if( stereoMode == DisplaySettings::RIGHT_EYE ) {
170 } else if( stereoMode == DisplaySettings::HORIZONTAL_INTERLACE ) {
171 return "HORIZONTAL_INTERLACE";
172 } else if( stereoMode == DisplaySettings::VERTICAL_INTERLACE ) {
173 return "VERTICAL_INTERLACE";
174 } else if( stereoMode == DisplaySettings::CHECKERBOARD ) {
175 return "CHECKERBOARD";
181 * merge OSG output into our logging system, so it gets recorded to file,
182 * and so we can display a GUI console with renderer issues, especially
183 * shader compilation warnings and errors.
185 class NotifyLogger : public osg::NotifyHandler
188 // note this callback will be invoked by OSG from multiple threads.
189 // fortunately our Simgear logging implementation already handles
190 // that internally, so we simply pass the message on.
191 virtual void notify(osg::NotifySeverity severity, const char *message)
193 // Detect whether a osg::Reference derived object is deleted with a non-zero
194 // reference count. In this case trigger a segfault to get a stack trace.
195 if( strstr(message, "the final reference count was") )
197 // as this is going to segfault ignore the translation of severity and always output the message.
198 SG_LOG(SG_GL, SG_ALERT, message);
199 int* trigger_segfault = 0;
200 *trigger_segfault = 0;
203 SG_LOG(SG_GL, translateSeverity(severity), message);
207 sgDebugPriority translateSeverity(osg::NotifySeverity severity)
211 case osg::FATAL: return SG_ALERT;
212 case osg::WARN: return SG_WARN;
214 case osg::INFO: return SG_INFO;
216 case osg::DEBUG_INFO: return SG_DEBUG;
217 default: return SG_ALERT;
222 class NotifyLevelListener : public SGPropertyChangeListener
225 void valueChanged(SGPropertyNode* node)
227 osg::NotifySeverity severity = osg::WARN;
228 string val = boost::to_lower_copy(string(node->getStringValue()));
230 if (val == "fatal") {
231 severity = osg::FATAL;
232 } else if (val == "warn") {
233 severity = osg::WARN;
234 } else if (val == "notice") {
235 severity = osg::NOTICE;
236 } else if (val == "info") {
237 severity = osg::INFO;
238 } else if ((val == "debug") || (val == "debug-info")) {
239 severity = osg::DEBUG_INFO;
242 osg::setNotifyLevel(severity);
246 void updateOSGNotifyLevel()
250 void fgOSOpenWindow(bool stencil)
252 osg::setNotifyHandler(new NotifyLogger);
254 viewer = new osgViewer::Viewer;
255 viewer->setDatabasePager(FGScenery::getPagerSingleton());
258 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
259 if (mode == "AutomaticSelection")
260 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
261 else if (mode == "CullDrawThreadPerContext")
262 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
263 else if (mode == "DrawThreadPerContext")
264 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
265 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
266 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
268 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
269 WindowBuilder::initWindowBuilder(stencil);
270 CameraGroup::buildDefaultGroup(viewer.get());
272 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
273 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
274 if (wsa->windows.size() != 1) {
275 manipulator->setResizable(false);
277 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
278 viewer->addEventHandler(manipulator);
279 // Let FG handle the escape key with a confirmation
280 viewer->setKeyEventSetsDone(0);
281 // The viewer won't start without some root.
282 viewer->setSceneData(new osg::Group);
283 globals->get_renderer()->setViewer(viewer.get());
286 void fgOSResetProperties()
288 SGPropertyNode* osgLevel = fgGetNode("/sim/rendering/osg-notify-level", true);
289 NotifyLevelListener* l = new NotifyLevelListener;
290 globals->addListenerToCleanup(l);
291 osgLevel->addChangeListener(l, true);
293 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
295 Viewport* guiViewport = guiCamera->getViewport();
296 fgSetInt("/sim/startup/xsize", guiViewport->width());
297 fgSetInt("/sim/startup/ysize", guiViewport->height());
300 DisplaySettings * displaySettings = DisplaySettings::instance();
301 fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
302 fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
303 fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
304 fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
305 fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
306 fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
307 fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
308 fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
312 static int status = 0;
314 void fgOSExit(int code)
316 viewer->setDone(true);
317 viewer->getDatabasePager()->cancel();
320 // otherwise we crash if OSG does logging during static destruction, eg
321 // GraphicsWindowX11, since OSG statics may have been created before the
322 // sglog static, despite our best efforts in boostrap.cxx
323 osg::setNotifyHandler(new osg::StandardNotifyHandler);
328 viewer->setReleaseContextAtEndOfFrameHint(false);
329 if (!viewer->isRealized())
331 while (!viewer->done()) {
332 fgIdleHandler idleFunc = globals->get_renderer()->getEventHandler()->getIdleHandler();
335 globals->get_renderer()->update();
336 viewer->frame( globals->get_sim_time_sec() );
342 int fgGetKeyModifiers()
344 if (!globals->get_renderer()) { // happens during shutdown
348 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
351 void fgWarpMouse(int x, int y)
353 warpGUIPointer(CameraGroup::getDefault(), x, y);
356 void fgOSInit(int* argc, char** argv)
358 globals->get_renderer()->init();
359 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
362 void fgOSCloseWindow()
364 FGScenery::resetPagerSingleton();
365 flightgear::CameraGroup::setDefault(NULL);
366 WindowSystemAdapter::setWSA(NULL);
370 void fgOSFullScreen()
372 std::vector<osgViewer::GraphicsWindow*> windows;
373 viewer->getWindows(windows);
378 /* Toggling window fullscreen is only supported for the main GUI window.
379 * The other windows should use fixed setup from the camera.xml file anyway. */
380 osgViewer::GraphicsWindow* window = windows[0];
383 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
387 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
391 static int previous_x = 0;
392 static int previous_y = 0;
393 static int previous_width = 800;
394 static int previous_height = 600;
396 unsigned int screenWidth;
397 unsigned int screenHeight;
398 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
404 window->getWindowRectangle(x, y, width, height);
406 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
407 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
408 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
409 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
410 * "false" - even when a single window is display in fullscreen */
411 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
412 bool isFullScreen = !window->getWindowDecoration();
414 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
415 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
416 << ", number of screens: " << wsi->getNumScreens());
419 // limit x,y coordinates and window size to screen area
420 if (previous_x + previous_width > (int)screenWidth)
422 if (previous_y + previous_height > (int)screenHeight)
425 // disable fullscreen mode, restore previous window size/coordinates
428 width = previous_width;
429 height = previous_height;
433 // remember previous setting
436 previous_width = width;
437 previous_height = height;
439 // enable fullscreen mode, set new width/height
443 height = screenHeight;
446 // set xsize/ysize properties to adapt GUI planes
447 fgSetInt("/sim/startup/xsize", width);
448 fgSetInt("/sim/startup/ysize", height);
449 fgSetBool("/sim/startup/fullscreen", !isFullScreen);
451 // reconfigure window
452 window->setWindowDecoration(isFullScreen);
453 window->setWindowRectangle(x, y, width, height);
454 window->grabFocusIfPointerInWindow();
458 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
464 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
465 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
466 if (cursor == MOUSE_CURSOR_NONE)
467 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
468 else if(cursor == MOUSE_CURSOR_POINTER)
470 // osgViewer-Cocoa lacks RightArrowCursor, use Left
471 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
473 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
475 else if(cursor == MOUSE_CURSOR_WAIT)
476 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
477 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
478 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
479 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
480 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
481 else if(cursor == MOUSE_CURSOR_TOPSIDE)
482 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
483 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
484 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
485 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
486 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
487 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
488 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
489 else if(cursor == MOUSE_CURSOR_TOPLEFT)
490 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
491 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
492 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
493 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
494 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
495 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
496 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
498 gw->setCursor(mouseCursor);
501 static int _cursor = -1;
503 void fgSetMouseCursor(int cursor)
509 std::vector<osgViewer::GraphicsWindow*> windows;
510 viewer->getWindows(windows);
511 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
512 setMouseCursor(gw, cursor);
516 int fgGetMouseCursor()