]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_os_osgviewer.cxx
Merge branch 'curt/replay' into next
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
index ee7367ef02d19fe69ad453d523556bbf8032286f..b58d60664ba7cacc5fd3b1500868d9f5aa4b8b35 100644 (file)
@@ -1,4 +1,4 @@
-// fg_os_common.cxx -- common functions for fg_os interface
+// fg_os_osgviewer.cxx -- common functions for fg_os interface
 // implemented as an osgViewer
 //
 // Copyright (C) 2007  Tim Moore timoore@redhat.com
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <algorithm>
 #include <iostream>
 #include <sstream>
 
 #include <stdlib.h>
 
+#include <boost/foreach.hpp>
+
 #include <simgear/compiler.h>
 #include <simgear/structure/exception.hxx>
 #include <simgear/debug/logstream.hxx>
+#include <simgear/props/props_io.hxx>
 
 #include <osg/Camera>
 #include <osg/GraphicsContext>
 #include "globals.hxx"
 #include "renderer.hxx"
 #include "CameraGroup.hxx"
+#include "FGEventHandler.hxx"
 #include "WindowBuilder.hxx"
 #include "WindowSystemAdapter.hxx"
 
-#if (FG_OSG_VERSION >= 19008)
-#define OSG_HAS_MOUSE_CURSOR_PATCH
+// Static linking of OSG needs special macros
+#ifdef OSG_LIBRARY_STATIC
+#include <osgDB/Registry>
+USE_GRAPHICSWINDOW();
+// Image formats
+USE_OSGPLUGIN(bmp);
+USE_OSGPLUGIN(dds);
+USE_OSGPLUGIN(hdr);
+USE_OSGPLUGIN(pic);
+USE_OSGPLUGIN(pnm);
+USE_OSGPLUGIN(rgb);
+USE_OSGPLUGIN(tga);
+#ifdef OSG_JPEG_ENABLED
+  USE_OSGPLUGIN(jpeg);
+#endif
+#ifdef OSG_PNG_ENABLED
+  USE_OSGPLUGIN(png);
+#endif
+#ifdef OSG_TIFF_ENABLED
+  USE_OSGPLUGIN(tiff);
+#endif
+// Model formats
+USE_OSGPLUGIN(3ds);
+USE_OSGPLUGIN(ac);
+USE_OSGPLUGIN(ive);
+USE_OSGPLUGIN(osg);
+USE_OSGPLUGIN(txf);
 #endif
 
 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
@@ -69,60 +102,8 @@ using namespace osg;
 static osg::ref_ptr<osgViewer::Viewer> viewer;
 static osg::ref_ptr<osg::Camera> mainCamera;
 
-namespace
-{
-// If a camera group isn't specified, build one from the top-level
-// camera specs and then add a camera aligned with the master camera
-// if it doesn't seem to exist.
-CameraGroup* buildDefaultCameraGroup(osgViewer::Viewer* viewer,
-                                     const SGPropertyNode* gnode)
-{
-    WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
-    CameraGroup* cgroup = CameraGroup::buildCameraGroup(viewer, gnode);
-    // Look for a camera with no shear
-    Camera* masterCamera = 0;
-    for (CameraGroup::CameraIterator iter = cgroup->camerasBegin(),
-             e = cgroup->camerasEnd();
-         iter != e;
-         ++iter) {
-        const View::Slave& slave = viewer->getSlave((*iter)->slaveIndex);
-        if (slave._projectionOffset.isIdentity()) {
-            masterCamera = (*iter)->camera.get();
-            break;
-        }
-    }
-    if (!masterCamera) {
-        // No master camera found; better add one.
-        GraphicsWindow* window
-            = WindowBuilder::getWindowBuilder()->getDefaultWindow();
-        masterCamera = new Camera();
-        masterCamera->setGraphicsContext(window->gc.get());
-        const GraphicsContext::Traits *traits = window->gc->getTraits();
-        masterCamera->setViewport(new Viewport(0, 0,
-                                               traits->width, traits->height));
-        cgroup->addCamera(CameraGroup::DO_INTERSECTION_TEST, masterCamera,
-                          Matrix(), Matrix());
-    }
-    // Find window on which the GUI is drawn.
-    WindowVector::iterator iter = wsa->windows.begin();
-    WindowVector::iterator end = wsa->windows.end();
-    for (; iter != end; ++iter) {
-        if ((*iter)->gc.get() == masterCamera->getGraphicsContext())
-            break;
-    }
-    if (iter != end) {            // Better not happen
-        (*iter)->flags |= GraphicsWindow::GUI;
-        cgroup->buildGUICamera(0, iter->get());
-    }
-    return cgroup;
-}
-}
-
 void fgOSOpenWindow(bool stencil)
 {
-    osg::GraphicsContext::WindowingSystemInterface* wsi
-        = osg::GraphicsContext::getWindowingSystemInterface();
-
     viewer = new osgViewer::Viewer;
     viewer->setDatabasePager(FGScenery::getPagerSingleton());
     CameraGroup* cameraGroup = 0;
@@ -143,31 +124,52 @@ void fgOSOpenWindow(bool stencil)
 
     // Look for windows, camera groups, and the old syntax of
     // top-level cameras
-    const SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
-    for (int i = 0; i < renderingNode->nChildren(); ++i) {
-        const SGPropertyNode* propNode = renderingNode->getChild(i);
-        const char* propName = propNode->getName();
-        if (!strcmp(propName, "window")) {
-            windowBuilder->buildWindow(propNode);
-        } else if (!strcmp(propName, "camera-group")) {
-            cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), propNode);
+    SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
+    SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
+    bool oldSyntax = !cgroupNode->hasChild("camera");
+    if (oldSyntax) {
+        for (int i = 0; i < renderingNode->nChildren(); ++i) {
+            SGPropertyNode* propNode = renderingNode->getChild(i);
+            const char* propName = propNode->getName();
+            if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
+                SGPropertyNode* copiedNode
+                    = cgroupNode->getNode(propName, propNode->getIndex(), true);
+                copyProperties(propNode, copiedNode);
+            }
         }
+        vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
+        SGPropertyNode* masterCamera = 0;
+        BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
+            if (camera->getDoubleValue("shear-x", 0.0) == 0.0
+                && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
+                masterCamera = camera.ptr();
+                break;
+            }
+        }
+        if (!masterCamera) {
+            masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
+            setValue(masterCamera->getNode("window/name", true),
+                     windowBuilder->getDefaultWindowName());
+        }
+        SGPropertyNode* nameNode = masterCamera->getNode("window/name");
+        if (nameNode)
+            setValue(cgroupNode->getNode("gui/window/name", true),
+                     nameNode->getStringValue());
     }
-    if (!cameraGroup)
-        cameraGroup = buildDefaultCameraGroup(viewer.get(), renderingNode);
+    cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
     Camera* guiCamera = getGUICamera(cameraGroup);
     if (guiCamera) {
         Viewport* guiViewport = guiCamera->getViewport();
         fgSetInt("/sim/startup/xsize", guiViewport->width());
         fgSetInt("/sim/startup/ysize", guiViewport->height());
     }
-    FGManipulator* manipulator = globals->get_renderer()->getManipulator();
+    FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
     if (wsa->windows.size() != 1) {
         manipulator->setResizable(false);
     }
     viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
-    viewer->setCameraManipulator(manipulator);
+    viewer->addEventHandler(manipulator);
     // Let FG handle the escape key with a confirmation
     viewer->setKeyEventSetsDone(0);
     // The viewer won't start without some root.
@@ -187,13 +189,26 @@ void fgOSExit(int code)
 
 void fgOSMainLoop()
 {
-    viewer->run();
+    ref_ptr<FGEventHandler> manipulator
+        = globals->get_renderer()->getEventHandler();
+    viewer->setReleaseContextAtEndOfFrameHint(false);
+    if (!viewer->isRealized())
+        viewer->realize();
+    while (!viewer->done()) {
+        fgIdleHandler idleFunc = manipulator->getIdleHandler();
+        fgDrawHandler drawFunc = manipulator->getDrawHandler();
+        if (idleFunc)
+            (*idleFunc)();
+        if (drawFunc)
+            (*drawFunc)();
+        viewer->frame();
+    }
     fgExit(status);
 }
 
 int fgGetKeyModifiers()
 {
-    return globals->get_renderer()->getManipulator()->getCurrentModifiers();
+    return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
 }
 
 void fgWarpMouse(int x, int y)
@@ -211,7 +226,6 @@ void fgOSFullScreen()
 {
 }
 
-#ifdef OSG_HAS_MOUSE_CURSOR_PATCH
 static void setMouseCursor(osg::Camera* camera, int cursor)
 {
     if (!camera)
@@ -236,21 +250,34 @@ static void setMouseCursor(osg::Camera* camera, int cursor)
         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
+    else if(cursor == MOUSE_CURSOR_TOPSIDE)
+        mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
+    else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
+        mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
+    else if(cursor == MOUSE_CURSOR_LEFTSIDE)
+        mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
+    else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
+        mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
+    else if(cursor == MOUSE_CURSOR_TOPLEFT)
+        mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
+    else if(cursor == MOUSE_CURSOR_TOPRIGHT)
+        mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
+    else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
+        mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
+    else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
+        mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
 
     gw->setCursor(mouseCursor);
 }
-#endif
 
 static int _cursor = -1;
 
 void fgSetMouseCursor(int cursor)
 {
     _cursor = cursor;
-#ifdef OSG_HAS_MOUSE_CURSOR_PATCH
     setMouseCursor(viewer->getCamera(), cursor);
     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
-#endif
 }
 
 int fgGetMouseCursor()