]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_os_osgviewer.cxx
Merge branch 'jt/runway' into next
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
index bb09ab9e9c93b93ba4a8253bb61d4d7b64290b27..af92bc82f58f46b647ac924672a4f5830ecdc0f2 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
@@ -19,7 +19,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 #ifdef HAVE_CONFIG_H
-#  include <config.h>
+#include <config.h>
 #endif
 
 #include <algorithm>
 
 #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 <osg/Group>
 #include <osg/Matrixd>
@@ -50,6 +54,9 @@
 #include "util.hxx"
 #include "globals.hxx"
 #include "renderer.hxx"
+#include "CameraGroup.hxx"
+#include "FGEventHandler.hxx"
+#include "WindowBuilder.hxx"
 #include "WindowSystemAdapter.hxx"
 
 #if (FG_OSG_VERSION >= 19008)
@@ -67,113 +74,17 @@ using namespace std;
 using namespace flightgear;
 using namespace osg;
 
-
-
 static osg::ref_ptr<osgViewer::Viewer> viewer;
 static osg::ref_ptr<osg::Camera> mainCamera;
 
-// Callback to prevent the GraphicsContext resized function from messing
-// with the projection matrix of the slave
-
-namespace
-{
-// silly function for making the default window and camera names
-std::string makeName(const string& prefix, int num)
-{
-    std::stringstream stream;
-    stream << prefix << num;
-    return stream.str();
-}
-
-GraphicsContext::Traits*
-makeDefaultTraits(GraphicsContext::WindowingSystemInterface* wsi, bool stencil)
-{
-    int w = fgGetInt("/sim/startup/xsize");
-    int h = fgGetInt("/sim/startup/ysize");
-    int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
-    bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
-    bool fullscreen = fgGetBool("/sim/startup/fullscreen");
-
-    GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
-    traits->readDISPLAY();
-    int cbits = (bpp <= 16) ?  5 :  8;
-    int zbits = (bpp <= 16) ? 16 : 24;
-    traits->red = traits->green = traits->blue = cbits;
-    traits->depth = zbits;
-    if (alpha)
-       traits->alpha = 8;
-    if (stencil)
-       traits->stencil = 8;
-    traits->doubleBuffer = true;
-    traits->mipMapGeneration = true;
-    traits->windowName = "FlightGear";
-    // XXX should check per window too.
-    traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
-    traits->samples = fgGetBool("/sim/rendering/multi-samples", traits->samples);
-    traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
-    if (fullscreen) {
-        unsigned width = 0;
-        unsigned height = 0;
-        wsi->getScreenResolution(*traits, width, height);
-       traits->windowDecoration = false;
-        traits->width = width;
-        traits->height = height;
-        traits->supportsResize = false;
-    } else {
-       traits->windowDecoration = true;
-        traits->width = w;
-        traits->height = h;
-#if defined(WIN32) || defined(__APPLE__)
-        // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
-        // Mac also needs this to show window frame, menubar and Docks
-        traits->x = 100;
-        traits->y = 100;
-#endif
-        traits->supportsResize = true;
-    }
-    return traits;
-}
-
-void setTraitsFromProperties(GraphicsContext::Traits* traits,
-                             const SGPropertyNode* winNode,
-                             GraphicsContext::WindowingSystemInterface* wsi)
-{
-    traits->hostName
-        = winNode->getStringValue("host-name", traits->hostName.c_str());
-    traits->displayNum = winNode->getIntValue("display", traits->displayNum);
-    traits->screenNum = winNode->getIntValue("screen", traits->screenNum);
-    if (winNode->getBoolValue("fullscreen",
-                              fgGetBool("/sim/startup/fullscreen"))) {
-        unsigned width = 0;
-        unsigned height = 0;
-        wsi->getScreenResolution(*traits, width, height);
-        traits->windowDecoration = false;
-        traits->width = width;
-        traits->height = height;
-        traits->supportsResize = false;
-    } else {
-        traits->windowDecoration = winNode->getBoolValue("decoration", true);
-        traits->width = winNode->getIntValue("width", traits->width);
-        traits->height = winNode->getIntValue("height", traits->height);
-        traits->supportsResize = true;
-    }
-    traits->x = winNode->getIntValue("x", traits->x);
-    traits->y = winNode->getIntValue("y", traits->y);
-    if (winNode->hasChild("window-name"))
-        traits->windowName = winNode->getStringValue("window-name");
-    else if (winNode->hasChild("name")) 
-        traits->windowName = winNode->getStringValue("name");
-}
-
-} //namespace
-
 void fgOSOpenWindow(bool stencil)
 {
-    osg::GraphicsContext::WindowingSystemInterface* wsi;
-    wsi = osg::GraphicsContext::getWindowingSystemInterface();
+    osg::GraphicsContext::WindowingSystemInterface* wsi
+        = osg::GraphicsContext::getWindowingSystemInterface();
 
     viewer = new osgViewer::Viewer;
     viewer->setDatabasePager(FGScenery::getPagerSingleton());
+    CameraGroup* cameraGroup = 0;
     std::string mode;
     mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
     if (mode == "AutomaticSelection")
@@ -186,116 +97,63 @@ void fgOSOpenWindow(bool stencil)
       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
     else
       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
-    osg::ref_ptr<osg::GraphicsContext::Traits> traits
-        = makeDefaultTraits(wsi, stencil);
-
-    // Ok, first the children.
-    // that achieves some magic ordering og the slaves so that we end up
-    // in the main window more often.
-    // This can be sorted out better when we got rid of glut and sdl.
-    FGManipulator* manipulator = globals->get_renderer()->getManipulator();
-    string defaultName("slave");
-    WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
-    if (fgHasNode("/sim/rendering/camera")) {
-        SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
+    WindowBuilder::initWindowBuilder(stencil);
+    WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
+
+    // Look for windows, camera groups, and the old syntax of
+    // top-level cameras
+    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* cameraNode = renderingNode->getChild(i);
-            if (strcmp(cameraNode->getName(), "camera") != 0)
-                continue;
-
-            // get a new copy of the traits struct
-            osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
-            cameraTraits = new osg::GraphicsContext::Traits(*traits);
-            double shearx = cameraNode->getDoubleValue("shear-x", 0);
-            double sheary = cameraNode->getDoubleValue("shear-y", 0);
-            setTraitsFromProperties(cameraTraits.get(), cameraNode, wsi);
-            // FIXME, currently this is too much of a problem to route
-            // the resize events. When we do no longer need sdl and
-            // such this can be simplified
-            cameraTraits->supportsResize = false;
-
-            // ok found a camera configuration, add a new slave if possible
-            GraphicsContext* gc
-                = GraphicsContext::createGraphicsContext(cameraTraits.get());
-            if (gc) {
-                gc->realize();
-                Camera *camera = new Camera;
-                camera->setGraphicsContext(gc);
-                // If a viewport isn't set on the camera, then it's
-                // hard to dig it out of the SceneView objects in the
-                // viewer, and the coordinates of mouse events are
-                // somewhat bizzare.
-                camera->setViewport(new Viewport(0, 0, cameraTraits->width,
-                                                 cameraTraits->height));
-                const char* cameraName = cameraNode->getStringValue("name");
-                string cameraNameString = (cameraName ? string(cameraName)
-                                           : makeName(defaultName, i));
-                GraphicsWindow* window = wsa->registerWindow(gc,
-                                                             cameraNameString);
-                Camera3D* cam3D = wsa->registerCamera3D(window, camera,
-                                                        cameraNameString);
-                if (shearx == 0 && sheary == 0)
-                    cam3D->flags |= Camera3D::MASTER;
-                viewer->addSlave(camera, Matrix::translate(-shearx, -sheary, 0),
-                                 Matrix());
-            } else {
-                SG_LOG(SG_GENERAL, SG_WARN,
-                       "Couldn't create graphics context on "
-                       << cameraTraits->hostName << ":"
-                       << cameraTraits->displayNum
-                       << "." << cameraTraits->screenNum);
+            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());
     }
-    // now the main camera ...
-    // XXX mainCamera's purpose is to establish a "main graphics
-    // context" that can be made current (if necessary). But that
-    // should be a context established with a window. It's used to
-    // choose whether to render the GUI and panel camera nodes, but
-    // that's obsolete because the GUI is rendered in its own
-    // slave. And it's used to translate mouse event coordinates, but
-    // that's bogus because mouse clicks should work on any camera. In
-    // short, mainCamera must die :)
-    Camera3DVector::iterator citr
-        = find_if(wsa->cameras.begin(), wsa->cameras.end(),
-                  WindowSystemAdapter::FlagTester<Camera3D>(Camera3D::MASTER));
-    if (citr == wsa->cameras.end()) {
-        // Create a camera aligned with the master camera. Eventually
-        // this will be optional.
-        Camera* camera = new osg::Camera;
-        mainCamera = camera;
-        osg::GraphicsContext* gc
-            = osg::GraphicsContext::createGraphicsContext(traits.get());
-        gc->realize();
-        gc->makeCurrent();
-        camera->setGraphicsContext(gc);
-        // If a viewport isn't set on the camera, then it's hard to dig it
-        // out of the SceneView objects in the viewer, and the coordinates
-        // of mouse events are somewhat bizzare.
-        camera->setViewport(new osg::Viewport(0, 0,
-                                              traits->width, traits->height));
-        GraphicsWindow* window = wsa->registerWindow(gc, string("main"));
-        window->flags |= GraphicsWindow::GUI;
-        Camera3D* camera3d = wsa->registerCamera3D(window, camera,
-                                                   string("main"));
-        camera3d->flags |= Camera3D::MASTER;
-        // Why a slave? It seems to be the easiest way to assign cameras,
-        // for which we've created the graphics context ourselves, to
-        // the viewer. 
-        viewer->addSlave(camera);
-    } else {
-        mainCamera = (*citr)->camera;
+    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());
     }
-    if (wsa->cameras.size() != 1) {
+    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.
     viewer->setSceneData(new osg::Group);
     globals->get_renderer()->setViewer(viewer.get());
+    CameraGroup::setDefault(cameraGroup);
 }
 
 static int status = 0;
@@ -309,28 +167,29 @@ void fgOSExit(int code)
 
 void fgOSMainLoop()
 {
-    viewer->run();
+    ref_ptr<FGEventHandler> manipulator
+        = globals->get_renderer()->getEventHandler();
+    viewer->setReleaseContextAtEndOfFrameHint(false);
+    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)
 {
-    globals->get_renderer()->getManipulator()->setMouseWarped();
-    // Hack, currently the pointer is just recentered. So, we know the
-    // relative coordinates ...
-    if (!mainCamera.valid()) {
-        viewer->requestWarpPointer(0, 0);
-        return;
-    }
-    float xsize = (float)mainCamera->getGraphicsContext()->getTraits()->width;
-    float ysize = (float)mainCamera->getGraphicsContext()->getTraits()->height;
-    viewer->requestWarpPointer(2.0f * (float)x / xsize - 1.0f,
-                               1.0f - 2.0f * (float)y / ysize);
+    warpGUIPointer(CameraGroup::getDefault(), x, y);
 }
 
 void fgOSInit(int* argc, char** argv)
@@ -389,36 +248,3 @@ int fgGetMouseCursor()
 {
     return _cursor;
 }
-
-bool fgOSIsMainContext(const osg::GraphicsContext* context)
-{
-    if (!mainCamera.valid())
-        return false;
-    return context == mainCamera->getGraphicsContext();
-}
-
-bool fgOSIsMainCamera(const osg::Camera* camera)
-{
-  if (!camera)
-    return false;
-  if (camera == mainCamera.get())
-    return true;
-  if (!viewer.valid())
-    return false;
-  if (camera == viewer->getCamera())
-    return true;
-  return false;
-}
-
-GraphicsContext* fgOSGetMainContext()
-{
-    WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
-    WindowVector::iterator contextIter
-        = std::find_if(wsa->windows.begin(), wsa->windows.end(),
-                       WindowSystemAdapter::FlagTester<GraphicsWindow>(GraphicsWindow::GUI));
-    if (contextIter == wsa->windows.end())
-        return 0;
-    else
-        return (*contextIter)->gc.get();
-}
-