1 // fg_os_common.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
4 // Copyright (C) 2007 Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/compiler.h>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/debug/logstream.hxx>
33 #include <osg/GraphicsContext>
35 #include <osg/Matrixd>
36 #include <osg/Viewport>
37 #include <osg/Version>
39 #include <osgViewer/ViewerEventHandlers>
40 #include <osgViewer/Viewer>
41 #include <osgGA/MatrixManipulator>
43 #include <Include/general.hxx>
44 #include <Scenery/scenery.hxx>
46 #include "fg_props.hxx"
48 #include "globals.hxx"
49 #include "renderer.hxx"
50 #include "CameraGroup.hxx"
51 #include "WindowBuilder.hxx"
52 #include "WindowSystemAdapter.hxx"
54 #if (FG_OSG_VERSION >= 19008)
55 #define OSG_HAS_MOUSE_CURSOR_PATCH
58 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
59 // to create the graphics window and run the event/update/render loop.
62 // fg_os implementation
66 using namespace flightgear;
69 static osg::ref_ptr<osgViewer::Viewer> viewer;
70 static osg::ref_ptr<osg::Camera> mainCamera;
74 // If a camera group isn't specified, build one from the top-level
75 // camera specs and then add a camera aligned with the master camera
76 // if it doesn't seem to exist.
77 CameraGroup* buildDefaultCameraGroup(osgViewer::Viewer* viewer,
78 const SGPropertyNode* gnode)
80 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
81 CameraGroup* cgroup = CameraGroup::buildCameraGroup(viewer, gnode);
82 // Look for a camera with no shear
83 Camera* masterCamera = 0;
84 for (CameraGroup::CameraIterator iter = cgroup->camerasBegin(),
85 e = cgroup->camerasEnd();
88 const View::Slave& slave = viewer->getSlave((*iter)->slaveIndex);
89 if (slave._projectionOffset.isIdentity()) {
90 masterCamera = (*iter)->camera.get();
95 // No master camera found; better add one.
96 GraphicsWindow* window
97 = WindowBuilder::getWindowBuilder()->getDefaultWindow();
98 masterCamera = new Camera();
99 masterCamera->setGraphicsContext(window->gc.get());
100 const GraphicsContext::Traits *traits = window->gc->getTraits();
101 masterCamera->setViewport(new Viewport(0, 0,
102 traits->width, traits->height));
103 cgroup->addCamera(CameraGroup::DO_INTERSECTION_TEST, masterCamera,
106 // Find window on which the GUI is drawn.
107 WindowVector::iterator iter = wsa->windows.begin();
108 WindowVector::iterator end = wsa->windows.end();
109 for (; iter != end; ++iter) {
110 if ((*iter)->gc.get() == masterCamera->getGraphicsContext())
113 if (iter != end) { // Better not happen
114 (*iter)->flags |= GraphicsWindow::GUI;
115 cgroup->buildGUICamera(0, iter->get());
121 void fgOSOpenWindow(bool stencil)
123 osg::GraphicsContext::WindowingSystemInterface* wsi
124 = osg::GraphicsContext::getWindowingSystemInterface();
126 viewer = new osgViewer::Viewer;
127 viewer->setDatabasePager(FGScenery::getPagerSingleton());
128 CameraGroup* cameraGroup = 0;
130 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
131 if (mode == "AutomaticSelection")
132 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
133 else if (mode == "CullDrawThreadPerContext")
134 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
135 else if (mode == "DrawThreadPerContext")
136 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
137 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
138 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
140 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
141 WindowBuilder::initWindowBuilder(stencil);
142 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
144 // Look for windows, camera groups, and the old syntax of
146 const SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
147 for (int i = 0; i < renderingNode->nChildren(); ++i) {
148 const SGPropertyNode* propNode = renderingNode->getChild(i);
149 const char* propName = propNode->getName();
150 if (!strcmp(propName, "window")) {
151 windowBuilder->buildWindow(propNode);
152 } else if (!strcmp(propName, "camera-group")) {
153 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), propNode);
157 cameraGroup = buildDefaultCameraGroup(viewer.get(), renderingNode);
158 Camera* guiCamera = getGUICamera(cameraGroup);
160 Viewport* guiViewport = guiCamera->getViewport();
161 fgSetInt("/sim/startup/xsize", guiViewport->width());
162 fgSetInt("/sim/startup/ysize", guiViewport->height());
164 FGManipulator* manipulator = globals->get_renderer()->getManipulator();
165 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
166 if (wsa->windows.size() != 1) {
167 manipulator->setResizable(false);
169 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
170 viewer->setCameraManipulator(manipulator);
171 // Let FG handle the escape key with a confirmation
172 viewer->setKeyEventSetsDone(0);
173 // The viewer won't start without some root.
174 viewer->setSceneData(new osg::Group);
175 globals->get_renderer()->setViewer(viewer.get());
176 CameraGroup::setDefault(cameraGroup);
179 static int status = 0;
181 void fgOSExit(int code)
183 viewer->setDone(true);
184 viewer->getDatabasePager()->cancel();
194 int fgGetKeyModifiers()
196 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
199 void fgWarpMouse(int x, int y)
201 warpGUIPointer(CameraGroup::getDefault(), x, y);
204 void fgOSInit(int* argc, char** argv)
206 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
210 void fgOSFullScreen()
214 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
215 static void setMouseCursor(osg::Camera* camera, int cursor)
219 osg::GraphicsContext* gc = camera->getGraphicsContext();
222 osgViewer::GraphicsWindow* gw;
223 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
227 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
228 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
229 if (cursor == MOUSE_CURSOR_NONE)
230 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
231 else if(cursor == MOUSE_CURSOR_POINTER)
232 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
233 else if(cursor == MOUSE_CURSOR_WAIT)
234 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
235 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
236 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
237 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
238 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
240 gw->setCursor(mouseCursor);
244 static int _cursor = -1;
246 void fgSetMouseCursor(int cursor)
249 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
250 setMouseCursor(viewer->getCamera(), cursor);
251 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
252 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
256 int fgGetMouseCursor()