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);
196 sgDebugPriority translateSeverity(osg::NotifySeverity severity)
200 case osg::FATAL: return SG_ALERT;
201 case osg::WARN: return SG_WARN;
203 case osg::INFO: return SG_INFO;
205 case osg::DEBUG_INFO: return SG_DEBUG;
210 void fgOSOpenWindow(bool stencil)
212 osg::setNotifyHandler(new NotifyLogger);
213 //osg::setNotifyLevel(osg::DEBUG_INFO);
215 viewer = new osgViewer::Viewer;
216 viewer->setDatabasePager(FGScenery::getPagerSingleton());
217 CameraGroup* cameraGroup = 0;
219 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
220 if (mode == "AutomaticSelection")
221 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
222 else if (mode == "CullDrawThreadPerContext")
223 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
224 else if (mode == "DrawThreadPerContext")
225 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
226 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
227 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
229 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
230 WindowBuilder::initWindowBuilder(stencil);
231 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
233 // Look for windows, camera groups, and the old syntax of
235 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
236 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
237 bool oldSyntax = !cgroupNode->hasChild("camera");
239 for (int i = 0; i < renderingNode->nChildren(); ++i) {
240 SGPropertyNode* propNode = renderingNode->getChild(i);
241 const char* propName = propNode->getName();
242 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
243 SGPropertyNode* copiedNode
244 = cgroupNode->getNode(propName, propNode->getIndex(), true);
245 copyProperties(propNode, copiedNode);
248 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
249 SGPropertyNode* masterCamera = 0;
250 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
251 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
252 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
253 masterCamera = camera.ptr();
258 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
259 setValue(masterCamera->getNode("window/name", true),
260 windowBuilder->getDefaultWindowName());
262 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
264 setValue(cgroupNode->getNode("gui/window/name", true),
265 nameNode->getStringValue());
267 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
268 Camera* guiCamera = getGUICamera(cameraGroup);
270 Viewport* guiViewport = guiCamera->getViewport();
271 fgSetInt("/sim/startup/xsize", guiViewport->width());
272 fgSetInt("/sim/startup/ysize", guiViewport->height());
274 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
275 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
276 if (wsa->windows.size() != 1) {
277 manipulator->setResizable(false);
279 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
280 viewer->addEventHandler(manipulator);
281 // Let FG handle the escape key with a confirmation
282 viewer->setKeyEventSetsDone(0);
283 // The viewer won't start without some root.
284 viewer->setSceneData(new osg::Group);
285 globals->get_renderer()->setViewer(viewer.get());
286 CameraGroup::setDefault(cameraGroup);
288 DisplaySettings * displaySettings = DisplaySettings::instance();
289 fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
290 fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
291 fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
292 fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
293 fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
294 fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
295 fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
296 fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
300 static int status = 0;
302 void fgOSExit(int code)
304 viewer->setDone(true);
305 viewer->getDatabasePager()->cancel();
311 ref_ptr<FGEventHandler> manipulator
312 = globals->get_renderer()->getEventHandler();
313 viewer->setReleaseContextAtEndOfFrameHint(false);
314 if (!viewer->isRealized())
316 while (!viewer->done()) {
317 fgIdleHandler idleFunc = manipulator->getIdleHandler();
320 globals->get_renderer()->update();
321 viewer->frame( globals->get_sim_time_sec() );
327 int fgGetKeyModifiers()
329 if (!globals->get_renderer()) { // happens during shutdown
333 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
336 void fgWarpMouse(int x, int y)
338 warpGUIPointer(CameraGroup::getDefault(), x, y);
341 void fgOSInit(int* argc, char** argv)
343 globals->get_renderer()->init();
344 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
347 void fgOSFullScreen()
349 std::vector<osgViewer::GraphicsWindow*> windows;
350 viewer->getWindows(windows);
352 if (windows.size() == 0)
355 /* Toggling window fullscreen is only supported for the main GUI window.
356 * The other windows should use fixed setup from the camera.xml file anyway. */
357 osgViewer::GraphicsWindow* window = windows[0];
360 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
364 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
368 static int previous_x = 0;
369 static int previous_y = 0;
370 static int previous_width = 800;
371 static int previous_height = 600;
373 unsigned int screenWidth;
374 unsigned int screenHeight;
375 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
381 window->getWindowRectangle(x, y, width, height);
383 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
384 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
385 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
386 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
387 * "false" - even when a single window is display in fullscreen */
388 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
389 bool isFullScreen = !window->getWindowDecoration();
391 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
392 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
393 << ", number of screens: " << wsi->getNumScreens());
396 // limit x,y coordinates and window size to screen area
397 if (previous_x + previous_width > (int)screenWidth)
399 if (previous_y + previous_height > (int)screenHeight)
402 // disable fullscreen mode, restore previous window size/coordinates
405 width = previous_width;
406 height = previous_height;
410 // remember previous setting
413 previous_width = width;
414 previous_height = height;
416 // enable fullscreen mode, set new width/height
420 height = screenHeight;
423 // set xsize/ysize properties to adapt GUI planes
424 fgSetInt("/sim/startup/xsize", width);
425 fgSetInt("/sim/startup/ysize", height);
427 // reconfigure window
428 window->setWindowDecoration(isFullScreen);
429 window->setWindowRectangle(x, y, width, height);
430 window->grabFocusIfPointerInWindow();
434 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
440 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
441 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
442 if (cursor == MOUSE_CURSOR_NONE)
443 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
444 else if(cursor == MOUSE_CURSOR_POINTER)
446 // osgViewer-Cocoa lacks RightArrowCursor, use Left
447 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
449 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
451 else if(cursor == MOUSE_CURSOR_WAIT)
452 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
453 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
454 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
455 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
456 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
457 else if(cursor == MOUSE_CURSOR_TOPSIDE)
458 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
459 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
460 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
461 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
462 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
463 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
464 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
465 else if(cursor == MOUSE_CURSOR_TOPLEFT)
466 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
467 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
468 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
469 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
470 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
471 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
472 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
474 gw->setCursor(mouseCursor);
477 static int _cursor = -1;
479 void fgSetMouseCursor(int cursor)
483 std::vector<osgViewer::GraphicsWindow*> windows;
484 viewer->getWindows(windows);
485 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
486 setMouseCursor(gw, cursor);
490 int fgGetMouseCursor()