]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/fg_os_osgviewer.cxx
commradio: improvements for atis speech
[flightgear.git] / src / Viewer / fg_os_osgviewer.cxx
1 // fg_os_osgviewer.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
3 //
4 // Copyright (C) 2007  Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich 
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <algorithm>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29
30 #include <stdlib.h>
31
32 // Boost
33 #include <boost/algorithm/string/case_conv.hpp>
34 #include <boost/foreach.hpp>
35
36 #include <simgear/compiler.h>
37 #include <simgear/structure/exception.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/props/props_io.hxx>
40
41 #include <osg/Camera>
42 #include <osg/GraphicsContext>
43 #include <osg/Group>
44 #include <osg/Matrixd>
45 #include <osg/Viewport>
46 #include <osg/Version>
47 #include <osg/Notify>
48 #include <osg/View>
49 #include <osgViewer/ViewerEventHandlers>
50 #include <osgViewer/Viewer>
51 #include <osgViewer/GraphicsWindow>
52
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"
63
64 // Static linking of OSG needs special macros
65 #ifdef OSG_LIBRARY_STATIC
66 #include <osgDB/Registry>
67 USE_GRAPHICSWINDOW();
68 // Image formats
69 USE_OSGPLUGIN(bmp);
70 USE_OSGPLUGIN(dds);
71 USE_OSGPLUGIN(hdr);
72 USE_OSGPLUGIN(pic);
73 USE_OSGPLUGIN(pnm);
74 USE_OSGPLUGIN(rgb);
75 USE_OSGPLUGIN(tga);
76 #ifdef OSG_JPEG_ENABLED
77   USE_OSGPLUGIN(jpeg);
78 #endif
79 #ifdef OSG_PNG_ENABLED
80   USE_OSGPLUGIN(png);
81 #endif
82 #ifdef OSG_TIFF_ENABLED
83   USE_OSGPLUGIN(tiff);
84 #endif
85 // Model formats
86 USE_OSGPLUGIN(3ds);
87 USE_OSGPLUGIN(ac);
88 USE_OSGPLUGIN(ive);
89 USE_OSGPLUGIN(osg);
90 USE_OSGPLUGIN(txf);
91 #endif
92
93 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
94 // to create the graphics window and run the event/update/render loop.
95
96 //
97 // fg_os implementation
98 //
99
100 using namespace std;    
101 using namespace flightgear;
102 using namespace osg;
103
104 osg::ref_ptr<osgViewer::Viewer> viewer;
105
106 static void setStereoMode( const char * mode )
107 {
108     DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER;
109     bool stereoOn = true;
110
111     if (strcmp(mode,"QUAD_BUFFER")==0)
112     {
113         stereoMode = DisplaySettings::QUAD_BUFFER;
114     }
115     else if (strcmp(mode,"ANAGLYPHIC")==0)
116     {
117         stereoMode = DisplaySettings::ANAGLYPHIC;
118     }
119     else if (strcmp(mode,"HORIZONTAL_SPLIT")==0)
120     {
121         stereoMode = DisplaySettings::HORIZONTAL_SPLIT;
122     }
123     else if (strcmp(mode,"VERTICAL_SPLIT")==0)
124     {
125         stereoMode = DisplaySettings::VERTICAL_SPLIT;
126     }
127     else if (strcmp(mode,"LEFT_EYE")==0)
128     {
129         stereoMode = DisplaySettings::LEFT_EYE;
130     }
131     else if (strcmp(mode,"RIGHT_EYE")==0)
132     {
133         stereoMode = DisplaySettings::RIGHT_EYE;
134     }
135     else if (strcmp(mode,"HORIZONTAL_INTERLACE")==0)
136     {
137         stereoMode = DisplaySettings::HORIZONTAL_INTERLACE;
138     }
139     else if (strcmp(mode,"VERTICAL_INTERLACE")==0)
140     {
141         stereoMode = DisplaySettings::VERTICAL_INTERLACE;
142     }
143     else if (strcmp(mode,"CHECKERBOARD")==0)
144     {
145         stereoMode = DisplaySettings::CHECKERBOARD;
146     } else {
147         stereoOn = false; 
148     }
149     DisplaySettings::instance()->setStereo( stereoOn );
150     DisplaySettings::instance()->setStereoMode( stereoMode );
151 }
152
153 static const char * getStereoMode()
154 {
155     DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode();
156     bool stereoOn = DisplaySettings::instance()->getStereo();
157     if( !stereoOn ) return "OFF";
158     if( stereoMode == DisplaySettings::QUAD_BUFFER ) {
159         return "QUAD_BUFFER";
160     } else if( stereoMode == DisplaySettings::ANAGLYPHIC ) {
161         return "ANAGLYPHIC";
162     } else if( stereoMode == DisplaySettings::HORIZONTAL_SPLIT ) {
163         return "HORIZONTAL_SPLIT";
164     } else if( stereoMode == DisplaySettings::VERTICAL_SPLIT ) {
165         return "VERTICAL_SPLIT";
166     } else if( stereoMode == DisplaySettings::LEFT_EYE ) {
167         return "LEFT_EYE";
168     } else if( stereoMode == DisplaySettings::RIGHT_EYE ) {
169         return "RIGHT_EYE";
170     } else if( stereoMode == DisplaySettings::HORIZONTAL_INTERLACE ) {
171         return "HORIZONTAL_INTERLACE";
172     } else if( stereoMode == DisplaySettings::VERTICAL_INTERLACE ) {
173         return "VERTICAL_INTERLACE";
174     } else if( stereoMode == DisplaySettings::CHECKERBOARD ) {
175         return "CHECKERBOARD";
176     } 
177     return "OFF";
178 }
179
180 /**
181  * merge OSG output into our logging system, so it gets recorded to file,
182  * and so we can display a GUI console with renderer issues, especially
183  * shader compilation warnings and errors.
184  */
185 class NotifyLogger : public osg::NotifyHandler
186 {
187 public:
188   // note this callback will be invoked by OSG from multiple threads.
189   // fortunately our Simgear logging implementation already handles
190   // that internally, so we simply pass the message on.
191   virtual void notify(osg::NotifySeverity severity, const char *message)
192   {
193     SG_LOG(SG_GL, translateSeverity(severity), message);
194
195     // Detect whether a osg::Reference derived object is deleted with a non-zero
196     // reference count. In this case trigger a segfault to get a stack trace.
197     if( strstr(message, "the final reference count was") )
198     {
199
200       int* trigger_segfault = 0;
201       *trigger_segfault = 0;
202     }
203   }
204   
205 private:
206   sgDebugPriority translateSeverity(osg::NotifySeverity severity)
207   {
208     switch (severity) {
209       case osg::ALWAYS:
210       case osg::FATAL:  return SG_ALERT;
211       case osg::WARN:   return SG_WARN;
212       case osg::NOTICE:
213       case osg::INFO:   return SG_INFO;
214       case osg::DEBUG_FP:
215       case osg::DEBUG_INFO: return SG_DEBUG;
216       default: return SG_ALERT;
217     }
218   }
219 };
220
221 class NotifyLevelListener : public SGPropertyChangeListener
222 {
223 public:
224     void valueChanged(SGPropertyNode* node)
225     {
226         osg::NotifySeverity severity = osg::WARN;
227         string val = boost::to_lower_copy(string(node->getStringValue()));
228         
229         if (val == "fatal") {
230             severity = osg::FATAL;
231         } else if (val == "warn") {
232             severity = osg::WARN;
233         } else if (val == "notice") {
234             severity = osg::NOTICE;
235         } else if (val == "info") {
236             severity = osg::INFO;
237         } else if ((val == "debug") || (val == "debug-info")) {
238             severity = osg::DEBUG_INFO;
239         }
240         
241         osg::setNotifyLevel(severity);
242     }
243 };
244
245 void updateOSGNotifyLevel()
246 {
247    }
248
249 void fgOSOpenWindow(bool stencil)
250 {
251     osg::setNotifyHandler(new NotifyLogger);
252     
253     viewer = new osgViewer::Viewer;
254     viewer->setDatabasePager(FGScenery::getPagerSingleton());
255
256     std::string mode;
257     mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
258     if (mode == "AutomaticSelection")
259       viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
260     else if (mode == "CullDrawThreadPerContext")
261       viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
262     else if (mode == "DrawThreadPerContext")
263       viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
264     else if (mode == "CullThreadPerCameraDrawThreadPerContext")
265       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
266     else
267       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
268     WindowBuilder::initWindowBuilder(stencil);
269     CameraGroup::buildDefaultGroup(viewer.get());
270     
271     FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
272     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
273     if (wsa->windows.size() != 1) {
274         manipulator->setResizable(false);
275     }
276     viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
277     viewer->addEventHandler(manipulator);
278     // Let FG handle the escape key with a confirmation
279     viewer->setKeyEventSetsDone(0);
280     // The viewer won't start without some root.
281     viewer->setSceneData(new osg::Group);
282     globals->get_renderer()->setViewer(viewer.get());
283 }
284
285 void fgOSResetProperties()
286 {
287     SGPropertyNode* osgLevel = fgGetNode("/sim/rendering/osg-notify-level", true);
288     NotifyLevelListener* l = new NotifyLevelListener;
289     globals->addListenerToCleanup(l);
290     osgLevel->addChangeListener(l, true);
291     
292     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
293     if (guiCamera) {
294         Viewport* guiViewport = guiCamera->getViewport();
295         fgSetInt("/sim/startup/xsize", guiViewport->width());
296         fgSetInt("/sim/startup/ysize", guiViewport->height());
297     }
298     
299     DisplaySettings * displaySettings = DisplaySettings::instance();
300     fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation );
301     fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance );
302     fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth );
303     fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight );
304     fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode );
305     fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer );
306     fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer );
307     fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB );
308 }
309
310
311 static int status = 0;
312
313 void fgOSExit(int code)
314 {
315     viewer->setDone(true);
316     viewer->getDatabasePager()->cancel();
317     status = code;
318     
319     // otherwise we crash if OSG does logging during static destruction, eg
320     // GraphicsWindowX11, since OSG statics may have been created before the
321     // sglog static, despite our best efforts in boostrap.cxx
322     osg::setNotifyHandler(new osg::StandardNotifyHandler);
323 }
324
325 int fgOSMainLoop()
326 {
327     viewer->setReleaseContextAtEndOfFrameHint(false);
328     if (!viewer->isRealized())
329         viewer->realize();
330     while (!viewer->done()) {
331         fgIdleHandler idleFunc = globals->get_renderer()->getEventHandler()->getIdleHandler();
332         if (idleFunc)
333             (*idleFunc)();
334         globals->get_renderer()->update();
335         viewer->frame( globals->get_sim_time_sec() );
336     }
337     
338     return status;
339 }
340
341 int fgGetKeyModifiers()
342 {
343     if (!globals->get_renderer()) { // happens during shutdown
344       return 0;
345     }
346     
347     return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
348 }
349
350 void fgWarpMouse(int x, int y)
351 {
352     warpGUIPointer(CameraGroup::getDefault(), x, y);
353 }
354
355 void fgOSInit(int* argc, char** argv)
356 {
357     globals->get_renderer()->init();
358     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
359 }
360
361 void fgOSCloseWindow()
362 {
363     FGScenery::resetPagerSingleton();
364     flightgear::CameraGroup::setDefault(NULL);
365     WindowSystemAdapter::setWSA(NULL);
366     viewer = NULL;
367 }
368
369 void fgOSFullScreen()
370 {
371     std::vector<osgViewer::GraphicsWindow*> windows;
372     viewer->getWindows(windows);
373
374     if (windows.empty())
375         return; // Huh?!?
376
377     /* Toggling window fullscreen is only supported for the main GUI window.
378      * The other windows should use fixed setup from the camera.xml file anyway. */
379     osgViewer::GraphicsWindow* window = windows[0];
380
381     {
382         osg::GraphicsContext::WindowingSystemInterface    *wsi = osg::GraphicsContext::getWindowingSystemInterface();
383
384         if (wsi == NULL)
385         {
386             SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
387             return;
388         }
389
390         static int previous_x = 0;
391         static int previous_y = 0;
392         static int previous_width = 800;
393         static int previous_height = 600;
394
395         unsigned int screenWidth;
396         unsigned int screenHeight;
397         wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
398
399         int x;
400         int y;
401         int width;
402         int height;
403         window->getWindowRectangle(x, y, width, height);
404
405         /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with
406          * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width
407          * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a
408          * single display) - so we detect full screen mode using "WindowDecoration" state instead.
409          * "false" - even when a single window is display in fullscreen */
410         //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
411         bool isFullScreen = !window->getWindowDecoration();
412
413         SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
414                << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen
415                << ", number of screens: " << wsi->getNumScreens());
416         if (isFullScreen)
417         {
418             // limit x,y coordinates and window size to screen area
419             if (previous_x + previous_width > (int)screenWidth)
420                 previous_x = 0;
421             if (previous_y + previous_height > (int)screenHeight)
422                 previous_y = 0;
423
424             // disable fullscreen mode, restore previous window size/coordinates
425             x = previous_x;
426             y = previous_y;
427             width = previous_width;
428             height = previous_height;
429         }
430         else
431         {
432             // remember previous setting
433             previous_x = x;
434             previous_y = y;
435             previous_width = width;
436             previous_height = height;
437
438             // enable fullscreen mode, set new width/height
439             x = 0;
440             y = 0;
441             width = screenWidth;
442             height = screenHeight;
443         }
444
445         // set xsize/ysize properties to adapt GUI planes
446         fgSetInt("/sim/startup/xsize", width);
447         fgSetInt("/sim/startup/ysize", height);
448
449         // reconfigure window
450         window->setWindowDecoration(isFullScreen);
451         window->setWindowRectangle(x, y, width, height);
452         window->grabFocusIfPointerInWindow();
453     }
454 }
455
456 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)
457 {
458     if (!gw) {
459         return;
460     }
461   
462     osgViewer::GraphicsWindow::MouseCursor mouseCursor;
463     mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
464     if (cursor == MOUSE_CURSOR_NONE)
465         mouseCursor = osgViewer::GraphicsWindow::NoCursor;
466     else if(cursor == MOUSE_CURSOR_POINTER)
467 #ifdef SG_MAC
468         // osgViewer-Cocoa lacks RightArrowCursor, use Left
469         mouseCursor = osgViewer::GraphicsWindow::LeftArrowCursor;
470 #else
471         mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
472 #endif
473     else if(cursor == MOUSE_CURSOR_WAIT)
474         mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
475     else if(cursor == MOUSE_CURSOR_CROSSHAIR)
476         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
477     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
478         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
479     else if(cursor == MOUSE_CURSOR_TOPSIDE)
480         mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
481     else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
482         mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
483     else if(cursor == MOUSE_CURSOR_LEFTSIDE)
484         mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
485     else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
486         mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
487     else if(cursor == MOUSE_CURSOR_TOPLEFT)
488         mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
489     else if(cursor == MOUSE_CURSOR_TOPRIGHT)
490         mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
491     else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
492         mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
493     else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
494         mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
495
496     gw->setCursor(mouseCursor);
497 }
498
499 static int _cursor = -1;
500
501 void fgSetMouseCursor(int cursor)
502 {
503     _cursor = cursor;
504     if (!viewer)
505         return;
506     
507     std::vector<osgViewer::GraphicsWindow*> windows;
508     viewer->getWindows(windows);
509     BOOST_FOREACH(osgViewer::GraphicsWindow* gw, windows) {
510         setMouseCursor(gw, cursor);
511     }
512 }
513
514 int fgGetMouseCursor()
515 {
516     return _cursor;
517 }