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;
206 default: return SG_ALERT;
211 void fgOSOpenWindow(bool stencil)
213 osg::setNotifyHandler(new NotifyLogger);
214 //osg::setNotifyLevel(osg::DEBUG_INFO);
216 viewer = new osgViewer::Viewer;
217 viewer->setDatabasePager(FGScenery::getPagerSingleton());
218 CameraGroup* cameraGroup = 0;
220 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
221 if (mode == "AutomaticSelection")
222 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
223 else if (mode == "CullDrawThreadPerContext")
224 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
225 else if (mode == "DrawThreadPerContext")
226 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
227 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
228 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
230 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
231 WindowBuilder::initWindowBuilder(stencil);
232 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
234 // Look for windows, camera groups, and the old syntax of
236 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
237 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
238 bool oldSyntax = !cgroupNode->hasChild("camera");
240 for (int i = 0; i < renderingNode->nChildren(); ++i) {
241 SGPropertyNode* propNode = renderingNode->getChild(i);
242 const char* propName = propNode->getName();
243 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
244 SGPropertyNode* copiedNode
245 = cgroupNode->getNode(propName, propNode->getIndex(), true);
246 copyProperties(propNode, copiedNode);
249 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
250 SGPropertyNode* masterCamera = 0;
251 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
252 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
253 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
254 masterCamera = camera.ptr();
259 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
260 setValue(masterCamera->getNode("window/name", true),
261 windowBuilder->getDefaultWindowName());
263 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
265 setValue(cgroupNode->getNode("gui/window/name", true),
266 nameNode->getStringValue());
268 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
269 Camera* guiCamera = getGUICamera(cameraGroup);
271 Viewport* guiViewport = guiCamera->getViewport();
272 fgSetInt("/sim/startup/xsize", guiViewport->width());
273 fgSetInt("/sim/startup/ysize", guiViewport->height());
275 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
276 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
277 if (wsa->windows.size() != 1) {
278 manipulator->setResizable(false);
280 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
281 viewer->addEventHandler(manipulator);
282 // Let FG handle the escape key with a confirmation
283 viewer->setKeyEventSetsDone(0);
284 // The viewer won't start without some root.
285 viewer->setSceneData(new osg::Group);
286 globals->get_renderer()->setViewer(viewer.get());
287 CameraGroup::setDefault(cameraGroup);
289 DisplaySettings * displaySettings = DisplaySettings::instance();
290 fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
291 fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
292 fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
293 fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
294 fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
295 fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
296 fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
297 fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
301 static int status = 0;
303 void fgOSExit(int code)
305 viewer->setDone(true);
306 viewer->getDatabasePager()->cancel();
312 ref_ptr<FGEventHandler> manipulator
313 = globals->get_renderer()->getEventHandler();
314 viewer->setReleaseContextAtEndOfFrameHint(false);
315 if (!viewer->isRealized())
317 while (!viewer->done()) {
318 fgIdleHandler idleFunc = manipulator->getIdleHandler();
321 globals->get_renderer()->update();
322 viewer->frame( globals->get_sim_time_sec() );
328 int fgGetKeyModifiers()
330 if (!globals->get_renderer()) { // happens during shutdown
334 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
337 void fgWarpMouse(int x, int y)
339 warpGUIPointer(CameraGroup::getDefault(), x, y);
342 void fgOSInit(int* argc, char** argv)
344 globals->get_renderer()->init();
345 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
348 void fgOSFullScreen()
350 std::vector<osgViewer::GraphicsWindow*> windows;
351 viewer->getWindows(windows);
356 /* Toggling window fullscreen is only supported for the main GUI window.
357 * The other windows should use fixed setup from the camera.xml file anyway. */
358 osgViewer::GraphicsWindow* window = windows[0];
361 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
365 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
369 static int previous_x = 0;
370 static int previous_y = 0;
371 static int previous_width = 800;
372 static int previous_height = 600;
374 unsigned int screenWidth;
375 unsigned int screenHeight;
376 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
382 window->getWindowRectangle(x, y, width, height);
384 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
385 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
386 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
387 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
388 * "false" - even when a single window is display in fullscreen */
389 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
390 bool isFullScreen = !window->getWindowDecoration();
392 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
393 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
394 << ", number of screens: " << wsi->getNumScreens());
397 // limit x,y coordinates and window size to screen area
398 if (previous_x + previous_width > (int)screenWidth)
400 if (previous_y + previous_height > (int)screenHeight)
403 // disable fullscreen mode, restore previous window size/coordinates
406 width = previous_width;
407 height = previous_height;
411 // remember previous setting
414 previous_width = width;
415 previous_height = height;
417 // enable fullscreen mode, set new width/height
421 height = screenHeight;
424 // set xsize/ysize properties to adapt GUI planes
425 fgSetInt("/sim/startup/xsize", width);
426 fgSetInt("/sim/startup/ysize", height);
428 // reconfigure window
429 window->setWindowDecoration(isFullScreen);
430 window->setWindowRectangle(x, y, width, height);
431 window->grabFocusIfPointerInWindow();
435 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
441 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
442 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
443 if (cursor == MOUSE_CURSOR_NONE)
444 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
445 else if(cursor == MOUSE_CURSOR_POINTER)
447 // osgViewer-Cocoa lacks RightArrowCursor, use Left
448 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
450 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
452 else if(cursor == MOUSE_CURSOR_WAIT)
453 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
454 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
455 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
456 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
457 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
458 else if(cursor == MOUSE_CURSOR_TOPSIDE)
459 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
460 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
461 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
462 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
463 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
464 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
465 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
466 else if(cursor == MOUSE_CURSOR_TOPLEFT)
467 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
468 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
469 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
470 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
471 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
472 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
473 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
475 gw->setCursor(mouseCursor);
478 static int _cursor = -1;
480 void fgSetMouseCursor(int cursor)
484 std::vector<osgViewer::GraphicsWindow*> windows;
485 viewer->getWindows(windows);
486 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
487 setMouseCursor(gw, cursor);
491 int fgGetMouseCursor()