1 // fg_os_osgviewer.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.
32 #include <simgear/compiler.h>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/debug/logstream.hxx>
37 #include <osg/GraphicsContext>
39 #include <osg/Matrixd>
40 #include <osg/Viewport>
41 #include <osg/Version>
43 #include <osgViewer/ViewerEventHandlers>
44 #include <osgViewer/Viewer>
45 #include <osgGA/MatrixManipulator>
47 #include <Include/general.hxx>
48 #include <Scenery/scenery.hxx>
50 #include "fg_props.hxx"
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "CameraGroup.hxx"
55 #include "WindowBuilder.hxx"
56 #include "WindowSystemAdapter.hxx"
58 #if (FG_OSG_VERSION >= 19008)
59 #define OSG_HAS_MOUSE_CURSOR_PATCH
62 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
63 // to create the graphics window and run the event/update/render loop.
66 // fg_os implementation
70 using namespace flightgear;
73 static osg::ref_ptr<osgViewer::Viewer> viewer;
74 static osg::ref_ptr<osg::Camera> mainCamera;
78 // If a camera group isn't specified, build one from the top-level
79 // camera specs and then add a camera aligned with the master camera
80 // if it doesn't seem to exist.
81 CameraGroup* buildDefaultCameraGroup(osgViewer::Viewer* viewer,
82 const SGPropertyNode* gnode)
84 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
85 CameraGroup* cgroup = CameraGroup::buildCameraGroup(viewer, gnode);
86 // Look for a camera with no shear
87 Camera* masterCamera = 0;
88 for (CameraGroup::CameraIterator citer = cgroup->camerasBegin(),
89 e = cgroup->camerasEnd();
92 const View::Slave& slave = viewer->getSlave((*citer)->slaveIndex);
93 if (slave._projectionOffset.isIdentity()) {
94 masterCamera = (*citer)->camera.get();
99 // No master camera found; better add one.
100 GraphicsWindow* window
101 = WindowBuilder::getWindowBuilder()->getDefaultWindow();
102 masterCamera = new Camera();
103 masterCamera->setGraphicsContext(window->gc.get());
104 const GraphicsContext::Traits *traits = window->gc->getTraits();
105 masterCamera->setViewport(new Viewport(0, 0,
106 traits->width, traits->height));
107 cgroup->addCamera(CameraGroup::DO_INTERSECTION_TEST, masterCamera,
110 // Find window on which the GUI is drawn.
111 WindowVector::iterator iter = wsa->windows.begin();
112 WindowVector::iterator end = wsa->windows.end();
113 for (; iter != end; ++iter) {
114 if ((*iter)->gc.get() == masterCamera->getGraphicsContext())
117 if (iter != end) { // Better not happen
118 (*iter)->flags |= GraphicsWindow::GUI;
119 cgroup->buildGUICamera(0, iter->get());
125 void fgOSOpenWindow(bool stencil)
127 osg::GraphicsContext::WindowingSystemInterface* wsi
128 = osg::GraphicsContext::getWindowingSystemInterface();
130 viewer = new osgViewer::Viewer;
131 viewer->setDatabasePager(FGScenery::getPagerSingleton());
132 CameraGroup* cameraGroup = 0;
134 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
135 if (mode == "AutomaticSelection")
136 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
137 else if (mode == "CullDrawThreadPerContext")
138 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
139 else if (mode == "DrawThreadPerContext")
140 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
141 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
142 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
144 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
145 WindowBuilder::initWindowBuilder(stencil);
146 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
148 // Look for windows, camera groups, and the old syntax of
150 const SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
151 for (int i = 0; i < renderingNode->nChildren(); ++i) {
152 const SGPropertyNode* propNode = renderingNode->getChild(i);
153 const char* propName = propNode->getName();
154 if (!strcmp(propName, "window")) {
155 windowBuilder->buildWindow(propNode);
156 } else if (!strcmp(propName, "camera-group")) {
157 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), propNode);
161 cameraGroup = buildDefaultCameraGroup(viewer.get(), renderingNode);
162 Camera* guiCamera = getGUICamera(cameraGroup);
164 Viewport* guiViewport = guiCamera->getViewport();
165 fgSetInt("/sim/startup/xsize", guiViewport->width());
166 fgSetInt("/sim/startup/ysize", guiViewport->height());
168 FGManipulator* manipulator = globals->get_renderer()->getManipulator();
169 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
170 if (wsa->windows.size() != 1) {
171 manipulator->setResizable(false);
173 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
174 viewer->setCameraManipulator(manipulator);
175 // Let FG handle the escape key with a confirmation
176 viewer->setKeyEventSetsDone(0);
177 // The viewer won't start without some root.
178 viewer->setSceneData(new osg::Group);
179 globals->get_renderer()->setViewer(viewer.get());
180 CameraGroup::setDefault(cameraGroup);
183 static int status = 0;
185 void fgOSExit(int code)
187 viewer->setDone(true);
188 viewer->getDatabasePager()->cancel();
198 int fgGetKeyModifiers()
200 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
203 void fgWarpMouse(int x, int y)
205 warpGUIPointer(CameraGroup::getDefault(), x, y);
208 void fgOSInit(int* argc, char** argv)
210 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
214 void fgOSFullScreen()
218 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
219 static void setMouseCursor(osg::Camera* camera, int cursor)
223 osg::GraphicsContext* gc = camera->getGraphicsContext();
226 osgViewer::GraphicsWindow* gw;
227 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
231 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
232 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
233 if (cursor == MOUSE_CURSOR_NONE)
234 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
235 else if(cursor == MOUSE_CURSOR_POINTER)
236 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
237 else if(cursor == MOUSE_CURSOR_WAIT)
238 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
239 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
240 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
241 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
242 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
244 gw->setCursor(mouseCursor);
248 static int _cursor = -1;
250 void fgSetMouseCursor(int cursor)
253 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
254 setMouseCursor(viewer->getCamera(), cursor);
255 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
256 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
260 int fgGetMouseCursor()