X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FMain%2Ffg_os_osgviewer.cxx;h=b58d60664ba7cacc5fd3b1500868d9f5aa4b8b35;hb=571f7301f2b0b493f90769baa0728f82fa8ea731;hp=12064723789fa38aa0963c2f4908cc655babd6a2;hpb=7443b8b5e8419869e401f95af28cea8eac72450a;p=flightgear.git diff --git a/src/Main/fg_os_osgviewer.cxx b/src/Main/fg_os_osgviewer.cxx index 120647237..b58d60664 100644 --- a/src/Main/fg_os_osgviewer.cxx +++ b/src/Main/fg_os_osgviewer.cxx @@ -1,116 +1,181 @@ +// fg_os_osgviewer.cxx -- common functions for fg_os interface +// implemented as an osgViewer +// +// Copyright (C) 2007 Tim Moore timoore@redhat.com +// Copyright (C) 2007 Mathias Froehlich +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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 +#endif + +#include +#include +#include +#include + #include +#include + #include #include #include +#include +#include #include #include #include #include -#include +#include +#include +#include #include #include +#include +#include #include "fg_os.hxx" +#include "fg_props.hxx" #include "util.hxx" #include "globals.hxx" #include "renderer.hxx" +#include "CameraGroup.hxx" +#include "FGEventHandler.hxx" +#include "WindowBuilder.hxx" +#include "WindowSystemAdapter.hxx" + +// Static linking of OSG needs special macros +#ifdef OSG_LIBRARY_STATIC +#include +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 // to create the graphics window and run the event/update/render loop. -// -// fg_os callback registration APIs -// - - -// Event handling and scene graph update is all handled by a -// manipulator. See FGManipulator.cpp -void fgRegisterIdleHandler(fgIdleHandler func) -{ - globals->get_renderer()->getManipulator()->setIdleHandler(func); -} - -void fgRegisterDrawHandler(fgDrawHandler func) -{ - globals->get_renderer()->getManipulator()->setDrawHandler(func); -} - -void fgRegisterWindowResizeHandler(fgWindowResizeHandler func) -{ - globals->get_renderer()->getManipulator()->setWindowResizeHandler(func); -} - -void fgRegisterKeyHandler(fgKeyHandler func) -{ - globals->get_renderer()->getManipulator()->setKeyHandler(func); -} - -void fgRegisterMouseClickHandler(fgMouseClickHandler func) -{ - globals->get_renderer()->getManipulator()->setMouseClickHandler(func); -} - -void fgRegisterMouseMotionHandler(fgMouseMotionHandler func) -{ - globals->get_renderer()->getManipulator()->setMouseMotionHandler(func); -} - -// Redraw "happens" every frame whether you want it or not. -void fgRequestRedraw() -{ -} // // fg_os implementation // +using namespace std; +using namespace flightgear; +using namespace osg; static osg::ref_ptr viewer; +static osg::ref_ptr mainCamera; -void fgOSOpenWindow(int w, int h, int bpp, - bool alpha, bool stencil, bool fullscreen) +void fgOSOpenWindow(bool stencil) { viewer = new osgViewer::Viewer; - // Avoid complications with fg's custom drawables. - viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); - osg::ref_ptr traits - = new osg::GraphicsContext::Traits; - int cbits = (bpp <= 16) ? 5 : 8; - int zbits = (bpp <= 16) ? 16 : 24; - traits->width = w; - traits->height = h; - traits->red = traits->green = traits->blue = cbits; - traits->depth = zbits; - if (alpha) - traits->alpha = 8; - if (stencil) - traits->stencil = 8; - if (fullscreen) - traits->windowDecoration = false; + viewer->setDatabasePager(FGScenery::getPagerSingleton()); + CameraGroup* cameraGroup = 0; + std::string mode; + mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded"); + if (mode == "AutomaticSelection") + viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection); + else if (mode == "CullDrawThreadPerContext") + viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); + else if (mode == "DrawThreadPerContext") + viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); + else if (mode == "CullThreadPerCameraDrawThreadPerContext") + viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); else - traits->windowDecoration = true; - traits->supportsResize = true; - traits->doubleBuffer = true; - osg::GraphicsContext* gc - = osg::GraphicsContext::createGraphicsContext(traits.get()); - viewer->getCamera()->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. - viewer->getCamera()->setViewport(new osg::Viewport(0, 0, - traits->width, - traits->height)); - viewer->setCameraManipulator(globals->get_renderer()->getManipulator()); + viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); + 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); + } + } + vector 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()); + } + 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()); + } + 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; @@ -118,48 +183,104 @@ static int status = 0; void fgOSExit(int code) { viewer->setDone(true); + viewer->getDatabasePager()->cancel(); status = code; } void fgOSMainLoop() { - viewer->run(); + ref_ptr 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) { - viewer->requestWarpPointer((float)x, (float)y); + warpGUIPointer(CameraGroup::getDefault(), x, y); } -// Noop void fgOSInit(int* argc, char** argv) { + WindowSystemAdapter::setWSA(new WindowSystemAdapter); } +// Noop void fgOSFullScreen() { - // Noop, but is probably doable - } -// No support in OSG yet -void fgSetMouseCursor(int cursor) +static void setMouseCursor(osg::Camera* camera, int cursor) { + if (!camera) + return; + osg::GraphicsContext* gc = camera->getGraphicsContext(); + if (!gc) + return; + osgViewer::GraphicsWindow* gw; + gw = dynamic_cast(gc); + if (!gw) + return; + + osgViewer::GraphicsWindow::MouseCursor mouseCursor; + mouseCursor = osgViewer::GraphicsWindow::InheritCursor; + if (cursor == MOUSE_CURSOR_NONE) + mouseCursor = osgViewer::GraphicsWindow::NoCursor; + else if(cursor == MOUSE_CURSOR_POINTER) + mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor; + else if(cursor == MOUSE_CURSOR_WAIT) + mouseCursor = osgViewer::GraphicsWindow::WaitCursor; + else if(cursor == MOUSE_CURSOR_CROSSHAIR) + 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); } -int fgGetMouseCursor() +static int _cursor = -1; + +void fgSetMouseCursor(int cursor) { - return 0; + _cursor = cursor; + setMouseCursor(viewer->getCamera(), cursor); + for (unsigned i = 0; i < viewer->getNumSlaves(); ++i) + setMouseCursor(viewer->getSlave(i)._camera.get(), cursor); } -void fgMakeCurrent() +int fgGetMouseCursor() { - osg::GraphicsContext* gc = viewer->getCamera()->getGraphicsContext(); - gc->makeCurrent(); + return _cursor; }