]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_os_osgviewer.cxx
Merge branch 'zan/stencil2' into next
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
index 087d29757998f520ae32e28c6fdaafaaf3cf0873..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
 // 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 <iostream>
+#include <sstream>
+#include <string>
+
 #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>
 #include "util.hxx"
 #include "globals.hxx"
 #include "renderer.hxx"
-
-#if (FG_OSG_VERSION >= 19008)
-#define OSG_HAS_MOUSE_CURSOR_PATCH
+#include "CameraGroup.hxx"
+#include "FGEventHandler.hxx"
+#include "WindowBuilder.hxx"
+#include "WindowSystemAdapter.hxx"
+
+// 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
 // fg_os implementation
 //
 
+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
+void fgOSOpenWindow(bool stencil)
 {
-struct fgResizeCallback : public GraphicsContext::ResizedCallback
-{
-    fgResizeCallback(Camera* slaveCamera)
-        : mainSlaveCamera(slaveCamera)
-    {}
-    
-    virtual void resizedImplementation(GraphicsContext* gc, int x, int y,
-                                       int width, int height);
-    ref_ptr<Camera> mainSlaveCamera;
-};
-
-void fgResizeCallback::resizedImplementation(GraphicsContext* gc,
-                                             int x, int y,
-                                             int width, int height)
-{
-    View* view = mainSlaveCamera->getView();
-    View::Slave* slave = (view ?
-                          view->findSlaveForCamera(mainSlaveCamera.get()) : 0);
-    if (slave) {
-        Matrixd projOffset(slave->_projectionOffset);
-        gc->resizedImplementation(x, y, width, height);
-        // Restore projection offsets changed by
-        // GraphicsContext::resizedImplementation
-        slave->_projectionOffset = projOffset;
-    } else {
-        gc->resizedImplementation(x, y, width, height);
-    }
-    
-}
-
-}
-void fgOSOpenWindow(int w, int h, int bpp,
-                    bool alpha, bool stencil, bool fullscreen)
-{
-    osg::GraphicsContext::WindowingSystemInterface* wsi;
-    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")
@@ -118,140 +119,63 @@ void fgOSOpenWindow(int w, int h, int bpp,
       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
     else
       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
-    osg::ref_ptr<osg::GraphicsContext::Traits> 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";
-    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;
-#ifdef WIN32
-       // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
-       traits->x = 100;
-       traits->y = 100;
-#endif
-        traits->supportsResize = true;
-    }
-
-    osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
-
-    // 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();
-    int nCameras = 0;
-    if (fgHasNode("/sim/rendering/camera")) {
-      SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
-      for (int i = 0; i < renderingNode->nChildren(); ++i) {
-        SGPropertyNode* cameraNode = renderingNode->getChild(i);
-        if (strcmp(cameraNode->getName(), "camera") != 0)
-          continue;
-
-       nCameras++;
-        // 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);
-        cameraTraits->hostName
-          = cameraNode->getStringValue("host-name", traits->hostName.c_str());
-        cameraTraits->displayNum
-          = cameraNode->getIntValue("display", traits->displayNum);
-        cameraTraits->screenNum
-          = cameraNode->getIntValue("screen", traits->screenNum);
-        if (cameraNode->getBoolValue("fullscreen", fullscreen)) {
-          unsigned width = 0;
-          unsigned height = 0;
-          wsi->getScreenResolution(*cameraTraits, width, height);
-          cameraTraits->windowDecoration = false;
-          cameraTraits->width = width;
-          cameraTraits->height = height;
-          cameraTraits->supportsResize = false;
-        } else {
-          cameraTraits->windowDecoration = true;
-          cameraTraits->width = cameraNode->getIntValue("width", w);
-          cameraTraits->height = cameraNode->getIntValue("height", h);
-          cameraTraits->supportsResize = true;
+    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* 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);
+            }
         }
-        // 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 ...
-        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
-
-        osg::GraphicsContext* gc;
-        gc = osg::GraphicsContext::createGraphicsContext(cameraTraits.get());
-        gc->realize();
-        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, cameraTraits->width, cameraTraits->height));
-        camera->setProjectionResizePolicy(rsp);
-        viewer->addSlave(camera.get(), osg::Matrix::translate(-shearx, -sheary, 0), osg::Matrix());
-      }
-      if (nCameras > 1)
-       manipulator->setResizable(false);
+        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 ...
-    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
-    mainCamera = camera;
-    osg::GraphicsContext* gc;
-    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));
-    camera->setProjectionResizePolicy(rsp);
-    if (nCameras == 0) {
-        // Only one principal camera
-        gc->setResizedCallback(new fgResizeCallback(camera.get()));
+    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());
     }
-    // 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.get());
-
-    viewer->setCameraManipulator(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->addEventHandler(manipulator);
     // Let FG handle the escape key with a confirmation
     viewer->setKeyEventSetsDone(0);
-    osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
-    statsHandler->setKeyEventTogglesOnScreenStats('*');
-    statsHandler->setKeyEventPrintsOutStats(0);
-    viewer->addEventHandler(statsHandler);
     // 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;
@@ -259,38 +183,42 @@ static int status = 0;
 void fgOSExit(int code)
 {
     viewer->setDone(true);
+    viewer->getDatabasePager()->cancel();
     status = 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)
 {
-    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);
 }
 
-// Noop
 void fgOSInit(int* argc, char** argv)
 {
+    WindowSystemAdapter::setWSA(new WindowSystemAdapter);
 }
 
 // Noop
@@ -298,7 +226,6 @@ void fgOSFullScreen()
 {
 }
 
-#ifdef OSG_HAS_MOUSE_CURSOR_PATCH
 static void setMouseCursor(osg::Camera* camera, int cursor)
 {
     if (!camera)
@@ -323,54 +250,37 @@ 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()
 {
     return _cursor;
 }
-
-void fgMakeCurrent()
-{
-    if (!mainCamera.valid())
-        return;
-    osg::GraphicsContext* gc = mainCamera->getGraphicsContext();
-    if (!gc)
-        return;
-    gc->makeCurrent();
-}
-
-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;
-}