2007-11-30 00:00:36 +00:00
|
|
|
// fg_os_common.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.
|
2007-06-16 16:15:49 +00:00
|
|
|
|
2008-05-19 21:21:03 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2007-05-21 17:50:02 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
|
2008-08-01 15:57:29 +00:00
|
|
|
#include <osg/Camera>
|
2007-05-21 17:50:02 +00:00
|
|
|
#include <osg/GraphicsContext>
|
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Matrixd>
|
|
|
|
#include <osg/Viewport>
|
2007-06-08 07:14:56 +00:00
|
|
|
#include <osg/Version>
|
2008-01-25 18:44:45 +00:00
|
|
|
#include <osg/View>
|
2007-06-16 16:15:49 +00:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2007-05-21 17:50:02 +00:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgGA/MatrixManipulator>
|
|
|
|
|
2007-10-04 20:40:29 +00:00
|
|
|
#include <Include/general.hxx>
|
2007-12-14 22:51:56 +00:00
|
|
|
#include <Scenery/scenery.hxx>
|
2007-05-21 17:50:02 +00:00
|
|
|
#include "fg_os.hxx"
|
2007-05-26 13:53:46 +00:00
|
|
|
#include "fg_props.hxx"
|
2007-05-21 17:50:02 +00:00
|
|
|
#include "util.hxx"
|
|
|
|
#include "globals.hxx"
|
|
|
|
#include "renderer.hxx"
|
2008-08-01 15:57:29 +00:00
|
|
|
#include "CameraGroup.hxx"
|
|
|
|
#include "WindowBuilder.hxx"
|
2008-05-19 21:21:03 +00:00
|
|
|
#include "WindowSystemAdapter.hxx"
|
2007-05-21 17:50:02 +00:00
|
|
|
|
2007-10-04 20:40:29 +00:00
|
|
|
#if (FG_OSG_VERSION >= 19008)
|
2007-06-08 07:14:56 +00:00
|
|
|
#define OSG_HAS_MOUSE_CURSOR_PATCH
|
|
|
|
#endif
|
|
|
|
|
2007-05-21 17:50:02 +00:00
|
|
|
// fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
|
|
|
|
// to create the graphics window and run the event/update/render loop.
|
|
|
|
|
|
|
|
//
|
|
|
|
// fg_os implementation
|
|
|
|
//
|
|
|
|
|
2008-05-19 21:21:03 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace flightgear;
|
2008-01-25 18:44:45 +00:00
|
|
|
using namespace osg;
|
|
|
|
|
2007-05-21 17:50:02 +00:00
|
|
|
static osg::ref_ptr<osgViewer::Viewer> viewer;
|
2007-05-26 13:53:46 +00:00
|
|
|
static osg::ref_ptr<osg::Camera> mainCamera;
|
2007-05-21 17:50:02 +00:00
|
|
|
|
2008-01-25 18:44:45 +00:00
|
|
|
namespace
|
|
|
|
{
|
2008-08-01 15:57:29 +00:00
|
|
|
// 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)
|
2008-01-25 18:44:45 +00:00
|
|
|
{
|
2008-08-01 15:57:29 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-05-26 13:53:46 +00:00
|
|
|
}
|
2008-08-01 15:57:29 +00:00
|
|
|
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());
|
2008-05-19 21:21:03 +00:00
|
|
|
}
|
2008-08-01 15:57:29 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2008-05-19 21:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fgOSOpenWindow(bool stencil)
|
|
|
|
{
|
2008-08-01 15:57:29 +00:00
|
|
|
osg::GraphicsContext::WindowingSystemInterface* wsi
|
|
|
|
= osg::GraphicsContext::getWindowingSystemInterface();
|
2007-05-26 13:53:46 +00:00
|
|
|
|
2008-05-19 21:21:03 +00:00
|
|
|
viewer = new osgViewer::Viewer;
|
|
|
|
viewer->setDatabasePager(FGScenery::getPagerSingleton());
|
2008-08-01 15:57:29 +00:00
|
|
|
CameraGroup* cameraGroup = 0;
|
2008-05-19 21:21:03 +00:00
|
|
|
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
|
|
|
|
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2008-08-01 15:57:29 +00:00
|
|
|
WindowBuilder::initWindowBuilder(stencil);
|
|
|
|
WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
|
|
|
|
|
|
|
|
// 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);
|
2008-04-19 10:28:07 +00:00
|
|
|
}
|
2007-05-26 13:53:46 +00:00
|
|
|
}
|
2008-08-01 15:57:29 +00:00
|
|
|
if (!cameraGroup)
|
|
|
|
cameraGroup = buildDefaultCameraGroup(viewer.get(), renderingNode);
|
|
|
|
Camera* guiCamera = getGUICamera(cameraGroup);
|
|
|
|
if (guiCamera) {
|
|
|
|
Viewport* guiViewport = guiCamera->getViewport();
|
|
|
|
fgSetInt("/sim/startup/xsize", guiViewport->width());
|
|
|
|
fgSetInt("/sim/startup/ysize", guiViewport->height());
|
2008-01-25 18:44:45 +00:00
|
|
|
}
|
2008-08-01 15:57:29 +00:00
|
|
|
FGManipulator* manipulator = globals->get_renderer()->getManipulator();
|
|
|
|
WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
|
|
|
|
if (wsa->windows.size() != 1) {
|
2008-05-19 21:21:03 +00:00
|
|
|
manipulator->setResizable(false);
|
|
|
|
}
|
|
|
|
viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
|
|
|
|
viewer->setCameraManipulator(manipulator);
|
2007-05-21 17:50:02 +00:00
|
|
|
// 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());
|
2008-08-01 15:57:29 +00:00
|
|
|
CameraGroup::setDefault(cameraGroup);
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int status = 0;
|
|
|
|
|
|
|
|
void fgOSExit(int code)
|
|
|
|
{
|
|
|
|
viewer->setDone(true);
|
2008-03-22 09:31:06 +00:00
|
|
|
viewer->getDatabasePager()->cancel();
|
2007-05-21 17:50:02 +00:00
|
|
|
status = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fgOSMainLoop()
|
|
|
|
{
|
|
|
|
viewer->run();
|
|
|
|
fgExit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fgGetKeyModifiers()
|
|
|
|
{
|
|
|
|
return globals->get_renderer()->getManipulator()->getCurrentModifiers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fgWarpMouse(int x, int y)
|
|
|
|
{
|
2008-08-01 15:57:29 +00:00
|
|
|
warpGUIPointer(CameraGroup::getDefault(), x, y);
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void fgOSInit(int* argc, char** argv)
|
|
|
|
{
|
2008-05-19 21:21:03 +00:00
|
|
|
WindowSystemAdapter::setWSA(new WindowSystemAdapter);
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
2007-05-26 13:53:46 +00:00
|
|
|
// Noop
|
2007-05-21 17:50:02 +00:00
|
|
|
void fgOSFullScreen()
|
|
|
|
{
|
2007-05-26 13:53:46 +00:00
|
|
|
}
|
2007-05-21 17:50:02 +00:00
|
|
|
|
2007-05-26 13:53:46 +00:00
|
|
|
#ifdef OSG_HAS_MOUSE_CURSOR_PATCH
|
|
|
|
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<osgViewer::GraphicsWindow*>(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;
|
|
|
|
|
|
|
|
gw->setCursor(mouseCursor);
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|
2007-05-26 13:53:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int _cursor = -1;
|
2007-05-21 17:50:02 +00:00
|
|
|
|
|
|
|
void fgSetMouseCursor(int cursor)
|
|
|
|
{
|
2007-05-26 13:53:46 +00:00
|
|
|
_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
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fgGetMouseCursor()
|
|
|
|
{
|
2007-05-26 13:53:46 +00:00
|
|
|
return _cursor;
|
2007-05-21 17:50:02 +00:00
|
|
|
}
|