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;
105 static osg::ref_ptr<osg::Camera> mainCamera;
107 static void setStereoMode( const char * mode )
109 DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER;
110 bool stereoOn = true;
112 if (strcmp(mode,"QUAD_BUFFER")==0)
114 stereoMode = DisplaySettings::QUAD_BUFFER;
116 else if (strcmp(mode,"ANAGLYPHIC")==0)
118 stereoMode = DisplaySettings::ANAGLYPHIC;
120 else if (strcmp(mode,"HORIZONTAL_SPLIT")==0)
122 stereoMode = DisplaySettings::HORIZONTAL_SPLIT;
124 else if (strcmp(mode,"VERTICAL_SPLIT")==0)
126 stereoMode = DisplaySettings::VERTICAL_SPLIT;
128 else if (strcmp(mode,"LEFT_EYE")==0)
130 stereoMode = DisplaySettings::LEFT_EYE;
132 else if (strcmp(mode,"RIGHT_EYE")==0)
134 stereoMode = DisplaySettings::RIGHT_EYE;
136 else if (strcmp(mode,"HORIZONTAL_INTERLACE")==0)
138 stereoMode = DisplaySettings::HORIZONTAL_INTERLACE;
140 else if (strcmp(mode,"VERTICAL_INTERLACE")==0)
142 stereoMode = DisplaySettings::VERTICAL_INTERLACE;
144 else if (strcmp(mode,"CHECKERBOARD")==0)
146 stereoMode = DisplaySettings::CHECKERBOARD;
150 DisplaySettings::instance()->setStereo( stereoOn );
151 DisplaySettings::instance()->setStereoMode( stereoMode );
154 static const char * getStereoMode()
156 DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode();
157 bool stereoOn = DisplaySettings::instance()->getStereo();
158 if( !stereoOn ) return "OFF";
159 if( stereoMode == DisplaySettings::QUAD_BUFFER ) {
160 return "QUAD_BUFFER";
161 } else if( stereoMode == DisplaySettings::ANAGLYPHIC ) {
163 } else if( stereoMode == DisplaySettings::HORIZONTAL_SPLIT ) {
164 return "HORIZONTAL_SPLIT";
165 } else if( stereoMode == DisplaySettings::VERTICAL_SPLIT ) {
166 return "VERTICAL_SPLIT";
167 } else if( stereoMode == DisplaySettings::LEFT_EYE ) {
169 } else if( stereoMode == DisplaySettings::RIGHT_EYE ) {
171 } else if( stereoMode == DisplaySettings::HORIZONTAL_INTERLACE ) {
172 return "HORIZONTAL_INTERLACE";
173 } else if( stereoMode == DisplaySettings::VERTICAL_INTERLACE ) {
174 return "VERTICAL_INTERLACE";
175 } else if( stereoMode == DisplaySettings::CHECKERBOARD ) {
176 return "CHECKERBOARD";
182 * merge OSG output into our logging system, so it gets recorded to file,
183 * and so we can display a GUI console with renderer issues, especially
184 * shader compilation warnings and errors.
186 class NotifyLogger : public osg::NotifyHandler
189 // note this callback will be invoked by OSG from multiple threads.
190 // fortunately our Simgear logging implementation already handles
191 // that internally, so we simply pass the message on.
192 virtual void notify(osg::NotifySeverity severity, const char *message)
194 SG_LOG(SG_GL, translateSeverity(severity), message);
196 // Detect whether a osg::Reference derived object is deleted with a non-zero
197 // reference count. In this case trigger a segfault to get a stack trace.
198 if( strstr(message, "the final reference count was") )
201 int* trigger_segfault = 0;
202 *trigger_segfault = 0;
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();
323 viewer->setReleaseContextAtEndOfFrameHint(false);
324 if (!viewer->isRealized())
326 while (!viewer->done()) {
327 fgIdleHandler idleFunc = globals->get_renderer()->getEventHandler()->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 fgOSCloseWindow()
359 FGScenery::resetPagerSingleton();
361 flightgear::CameraGroup::setDefault(NULL);
362 WindowSystemAdapter::setWSA(NULL);
366 void fgOSFullScreen()
368 std::vector<osgViewer::GraphicsWindow*> windows;
369 viewer->getWindows(windows);
374 /* Toggling window fullscreen is only supported for the main GUI window.
375 * The other windows should use fixed setup from the camera.xml file anyway. */
376 osgViewer::GraphicsWindow* window = windows[0];
379 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
383 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
387 static int previous_x = 0;
388 static int previous_y = 0;
389 static int previous_width = 800;
390 static int previous_height = 600;
392 unsigned int screenWidth;
393 unsigned int screenHeight;
394 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
400 window->getWindowRectangle(x, y, width, height);
402 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
403 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
404 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
405 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
406 * "false" - even when a single window is display in fullscreen */
407 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
408 bool isFullScreen = !window->getWindowDecoration();
410 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
411 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
412 << ", number of screens: " << wsi->getNumScreens());
415 // limit x,y coordinates and window size to screen area
416 if (previous_x + previous_width > (int)screenWidth)
418 if (previous_y + previous_height > (int)screenHeight)
421 // disable fullscreen mode, restore previous window size/coordinates
424 width = previous_width;
425 height = previous_height;
429 // remember previous setting
432 previous_width = width;
433 previous_height = height;
435 // enable fullscreen mode, set new width/height
439 height = screenHeight;
442 // set xsize/ysize properties to adapt GUI planes
443 fgSetInt("/sim/startup/xsize", width);
444 fgSetInt("/sim/startup/ysize", height);
446 // reconfigure window
447 window->setWindowDecoration(isFullScreen);
448 window->setWindowRectangle(x, y, width, height);
449 window->grabFocusIfPointerInWindow();
453 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
459 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
460 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
461 if (cursor == MOUSE_CURSOR_NONE)
462 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
463 else if(cursor == MOUSE_CURSOR_POINTER)
465 // osgViewer-Cocoa lacks RightArrowCursor, use Left
466 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
468 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
470 else if(cursor == MOUSE_CURSOR_WAIT)
471 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
472 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
473 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
474 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
475 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
476 else if(cursor == MOUSE_CURSOR_TOPSIDE)
477 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
478 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
479 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
480 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
481 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
482 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
483 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
484 else if(cursor == MOUSE_CURSOR_TOPLEFT)
485 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
486 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
487 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
488 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
489 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
490 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
491 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
493 gw->setCursor(mouseCursor);
496 static int _cursor = -1;
498 void fgSetMouseCursor(int cursor)
504 std::vector<osgViewer::GraphicsWindow*> windows;
505 viewer->getWindows(windows);
506 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
507 setMouseCursor(gw, cursor);
511 int fgGetMouseCursor()