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 osgLevel->addChangeListener(new NotifyLevelListener, true);
291 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
293 Viewport* guiViewport = guiCamera->getViewport();
294 fgSetInt("/sim/startup/xsize", guiViewport->width());
295 fgSetInt("/sim/startup/ysize", guiViewport->height());
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 viewer->setReleaseContextAtEndOfFrameHint(false);
322 if (!viewer->isRealized())
324 while (!viewer->done()) {
325 fgIdleHandler idleFunc = globals->get_renderer()->getEventHandler()->getIdleHandler();
328 globals->get_renderer()->update();
329 viewer->frame( globals->get_sim_time_sec() );
335 int fgGetKeyModifiers()
337 if (!globals->get_renderer()) { // happens during shutdown
341 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
344 void fgWarpMouse(int x, int y)
346 warpGUIPointer(CameraGroup::getDefault(), x, y);
349 void fgOSInit(int* argc, char** argv)
351 globals->get_renderer()->init();
352 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
355 void fgOSCloseWindow()
357 FGScenery::resetPagerSingleton();
359 flightgear::CameraGroup::setDefault(NULL);
360 WindowSystemAdapter::setWSA(NULL);
364 void fgOSFullScreen()
366 std::vector<osgViewer::GraphicsWindow*> windows;
367 viewer->getWindows(windows);
372 /* Toggling window fullscreen is only supported for the main GUI window.
373 * The other windows should use fixed setup from the camera.xml file anyway. */
374 osgViewer::GraphicsWindow* window = windows[0];
377 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
381 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
385 static int previous_x = 0;
386 static int previous_y = 0;
387 static int previous_width = 800;
388 static int previous_height = 600;
390 unsigned int screenWidth;
391 unsigned int screenHeight;
392 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
398 window->getWindowRectangle(x, y, width, height);
400 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
401 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
402 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
403 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
404 * "false" - even when a single window is display in fullscreen */
405 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
406 bool isFullScreen = !window->getWindowDecoration();
408 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
409 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
410 << ", number of screens: " << wsi->getNumScreens());
413 // limit x,y coordinates and window size to screen area
414 if (previous_x + previous_width > (int)screenWidth)
416 if (previous_y + previous_height > (int)screenHeight)
419 // disable fullscreen mode, restore previous window size/coordinates
422 width = previous_width;
423 height = previous_height;
427 // remember previous setting
430 previous_width = width;
431 previous_height = height;
433 // enable fullscreen mode, set new width/height
437 height = screenHeight;
440 // set xsize/ysize properties to adapt GUI planes
441 fgSetInt("/sim/startup/xsize", width);
442 fgSetInt("/sim/startup/ysize", height);
444 // reconfigure window
445 window->setWindowDecoration(isFullScreen);
446 window->setWindowRectangle(x, y, width, height);
447 window->grabFocusIfPointerInWindow();
451 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
457 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
458 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
459 if (cursor == MOUSE_CURSOR_NONE)
460 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
461 else if(cursor == MOUSE_CURSOR_POINTER)
463 // osgViewer-Cocoa lacks RightArrowCursor, use Left
464 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
466 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
468 else if(cursor == MOUSE_CURSOR_WAIT)
469 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
470 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
471 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
472 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
473 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
474 else if(cursor == MOUSE_CURSOR_TOPSIDE)
475 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
476 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
477 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
478 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
479 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
480 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
481 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
482 else if(cursor == MOUSE_CURSOR_TOPLEFT)
483 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
484 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
485 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
486 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
487 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
488 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
489 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
491 gw->setCursor(mouseCursor);
494 static int _cursor = -1;
496 void fgSetMouseCursor(int cursor)
502 std::vector<osgViewer::GraphicsWindow*> windows;
503 viewer->getWindows(windows);
504 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
505 setMouseCursor(gw, cursor);
509 int fgGetMouseCursor()