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>
46 #include <osgViewer/ViewerEventHandlers>
47 #include <osgViewer/Viewer>
48 #include <osgViewer/GraphicsWindow>
50 #include <Scenery/scenery.hxx>
51 #include <Main/fg_os.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Main/util.hxx>
54 #include <Main/globals.hxx>
55 #include "renderer.hxx"
56 #include "CameraGroup.hxx"
57 #include "FGEventHandler.hxx"
58 #include "WindowBuilder.hxx"
59 #include "WindowSystemAdapter.hxx"
61 // Static linking of OSG needs special macros
62 #ifdef OSG_LIBRARY_STATIC
63 #include <osgDB/Registry>
73 #ifdef OSG_JPEG_ENABLED
76 #ifdef OSG_PNG_ENABLED
79 #ifdef OSG_TIFF_ENABLED
90 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
91 // to create the graphics window and run the event/update/render loop.
94 // fg_os implementation
98 using namespace flightgear;
101 static osg::ref_ptr<osgViewer::Viewer> viewer;
102 static osg::ref_ptr<osg::Camera> mainCamera;
104 static void setStereoMode( const char * mode )
106 DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER;
107 bool stereoOn = true;
109 if (strcmp(mode,"QUAD_BUFFER")==0)
111 stereoMode = DisplaySettings::QUAD_BUFFER;
113 else if (strcmp(mode,"ANAGLYPHIC")==0)
115 stereoMode = DisplaySettings::ANAGLYPHIC;
117 else if (strcmp(mode,"HORIZONTAL_SPLIT")==0)
119 stereoMode = DisplaySettings::HORIZONTAL_SPLIT;
121 else if (strcmp(mode,"VERTICAL_SPLIT")==0)
123 stereoMode = DisplaySettings::VERTICAL_SPLIT;
125 else if (strcmp(mode,"LEFT_EYE")==0)
127 stereoMode = DisplaySettings::LEFT_EYE;
129 else if (strcmp(mode,"RIGHT_EYE")==0)
131 stereoMode = DisplaySettings::RIGHT_EYE;
133 else if (strcmp(mode,"HORIZONTAL_INTERLACE")==0)
135 stereoMode = DisplaySettings::HORIZONTAL_INTERLACE;
137 else if (strcmp(mode,"VERTICAL_INTERLACE")==0)
139 stereoMode = DisplaySettings::VERTICAL_INTERLACE;
141 else if (strcmp(mode,"CHECKERBOARD")==0)
143 stereoMode = DisplaySettings::CHECKERBOARD;
147 DisplaySettings::instance()->setStereo( stereoOn );
148 DisplaySettings::instance()->setStereoMode( stereoMode );
151 static const char * getStereoMode()
153 DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode();
154 bool stereoOn = DisplaySettings::instance()->getStereo();
155 if( !stereoOn ) return "OFF";
156 if( stereoMode == DisplaySettings::QUAD_BUFFER ) {
157 return "QUAD_BUFFER";
158 } else if( stereoMode == DisplaySettings::ANAGLYPHIC ) {
160 } else if( stereoMode == DisplaySettings::HORIZONTAL_SPLIT ) {
161 return "HORIZONTAL_SPLIT";
162 } else if( stereoMode == DisplaySettings::VERTICAL_SPLIT ) {
163 return "VERTICAL_SPLIT";
164 } else if( stereoMode == DisplaySettings::LEFT_EYE ) {
166 } else if( stereoMode == DisplaySettings::RIGHT_EYE ) {
168 } else if( stereoMode == DisplaySettings::HORIZONTAL_INTERLACE ) {
169 return "HORIZONTAL_INTERLACE";
170 } else if( stereoMode == DisplaySettings::VERTICAL_INTERLACE ) {
171 return "VERTICAL_INTERLACE";
172 } else if( stereoMode == DisplaySettings::CHECKERBOARD ) {
173 return "CHECKERBOARD";
178 void fgOSOpenWindow(bool stencil)
180 viewer = new osgViewer::Viewer;
181 viewer->setDatabasePager(FGScenery::getPagerSingleton());
182 CameraGroup* cameraGroup = 0;
184 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
185 if (mode == "AutomaticSelection")
186 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
187 else if (mode == "CullDrawThreadPerContext")
188 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
189 else if (mode == "DrawThreadPerContext")
190 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
191 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
192 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
194 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
195 WindowBuilder::initWindowBuilder(stencil);
196 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
198 // Look for windows, camera groups, and the old syntax of
200 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
201 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
202 bool oldSyntax = !cgroupNode->hasChild("camera");
204 for (int i = 0; i < renderingNode->nChildren(); ++i) {
205 SGPropertyNode* propNode = renderingNode->getChild(i);
206 const char* propName = propNode->getName();
207 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
208 SGPropertyNode* copiedNode
209 = cgroupNode->getNode(propName, propNode->getIndex(), true);
210 copyProperties(propNode, copiedNode);
213 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
214 SGPropertyNode* masterCamera = 0;
215 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
216 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
217 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
218 masterCamera = camera.ptr();
223 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
224 setValue(masterCamera->getNode("window/name", true),
225 windowBuilder->getDefaultWindowName());
227 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
229 setValue(cgroupNode->getNode("gui/window/name", true),
230 nameNode->getStringValue());
232 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
233 Camera* guiCamera = getGUICamera(cameraGroup);
235 Viewport* guiViewport = guiCamera->getViewport();
236 fgSetInt("/sim/startup/xsize", guiViewport->width());
237 fgSetInt("/sim/startup/ysize", guiViewport->height());
239 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
240 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
241 if (wsa->windows.size() != 1) {
242 manipulator->setResizable(false);
244 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
245 viewer->addEventHandler(manipulator);
246 // Let FG handle the escape key with a confirmation
247 viewer->setKeyEventSetsDone(0);
248 // The viewer won't start without some root.
249 viewer->setSceneData(new osg::Group);
250 globals->get_renderer()->setViewer(viewer.get());
251 CameraGroup::setDefault(cameraGroup);
253 DisplaySettings * displaySettings = DisplaySettings::instance();
254 fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
255 fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
256 fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
257 fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
258 fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
259 fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
260 fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
261 fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
265 static int status = 0;
267 void fgOSExit(int code)
269 viewer->setDone(true);
270 viewer->getDatabasePager()->cancel();
276 ref_ptr<FGEventHandler> manipulator
277 = globals->get_renderer()->getEventHandler();
278 viewer->setReleaseContextAtEndOfFrameHint(false);
279 if (!viewer->isRealized())
281 while (!viewer->done()) {
282 fgIdleHandler idleFunc = manipulator->getIdleHandler();
285 globals->get_renderer()->update();
292 int fgGetKeyModifiers()
294 if (!globals->get_renderer()) { // happens during shutdown
298 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
301 void fgWarpMouse(int x, int y)
303 warpGUIPointer(CameraGroup::getDefault(), x, y);
306 void fgOSInit(int* argc, char** argv)
308 globals->get_renderer()->init();
309 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
312 void fgOSFullScreen()
314 std::vector<osgViewer::GraphicsWindow*> windows;
315 viewer->getWindows(windows);
317 if (windows.size() == 0)
320 /* Toggling window fullscreen is only supported for the main GUI window.
321 * The other windows should use fixed setup from the camera.xml file anyway. */
322 osgViewer::GraphicsWindow* window = windows[0];
325 osg::GraphicsContext::WindowingSystemInterface *wsi = osg::GraphicsContext::getWindowingSystemInterface();
329 SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
333 static int previous_x = 0;
334 static int previous_y = 0;
335 static int previous_width = 800;
336 static int previous_height = 600;
338 unsigned int screenWidth;
339 unsigned int screenHeight;
340 wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
346 window->getWindowRectangle(x, y, width, height);
348 /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
349 * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
350 * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
351 * single display) - so we detect full screen mode using "WindowDecoration" state instead.
352 * "false" - even when a single window is display in fullscreen */
353 //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
354 bool isFullScreen = !window->getWindowDecoration();
356 SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
357 << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
358 << ", number of screens: " << wsi->getNumScreens());
361 // limit x,y coordinates and window size to screen area
362 if (previous_x + previous_width > (int)screenWidth)
364 if (previous_y + previous_height > (int)screenHeight)
367 // disable fullscreen mode, restore previous window size/coordinates
370 width = previous_width;
371 height = previous_height;
375 // remember previous setting
378 previous_width = width;
379 previous_height = height;
381 // enable fullscreen mode, set new width/height
385 height = screenHeight;
388 // set xsize/ysize properties to adapt GUI planes
389 fgSetInt("/sim/startup/xsize", width);
390 fgSetInt("/sim/startup/ysize", height);
392 // reconfigure window
393 window->setWindowDecoration(isFullScreen);
394 window->setWindowRectangle(x, y, width, height);
395 window->grabFocusIfPointerInWindow();
399 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
405 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
406 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
407 if (cursor == MOUSE_CURSOR_NONE)
408 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
409 else if(cursor == MOUSE_CURSOR_POINTER)
411 // osgViewer-Cocoa lacks RightArrowCursor, use Left
412 mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
414 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
416 else if(cursor == MOUSE_CURSOR_WAIT)
417 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
418 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
419 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
420 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
421 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
422 else if(cursor == MOUSE_CURSOR_TOPSIDE)
423 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
424 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
425 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
426 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
427 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
428 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
429 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
430 else if(cursor == MOUSE_CURSOR_TOPLEFT)
431 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
432 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
433 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
434 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
435 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
436 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
437 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
439 gw->setCursor(mouseCursor);
442 static int _cursor = -1;
444 void fgSetMouseCursor(int cursor)
448 std::vector<osgViewer::GraphicsWindow*> windows;
449 viewer->getWindows(windows);
450 BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
451 setMouseCursor(gw, cursor);
455 int fgGetMouseCursor()