]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
bfee9f57ba8c10d66b65c250728ba56380d14803
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
1 // fg_os_osgviewer.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
3 //
4 // Copyright (C) 2007  Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich 
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <algorithm>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29
30 #include <stdlib.h>
31
32 #include <simgear/compiler.h>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/debug/logstream.hxx>
35
36 #include <osg/Camera>
37 #include <osg/GraphicsContext>
38 #include <osg/Group>
39 #include <osg/Matrixd>
40 #include <osg/Viewport>
41 #include <osg/Version>
42 #include <osg/View>
43 #include <osgViewer/ViewerEventHandlers>
44 #include <osgViewer/Viewer>
45 #include <osgGA/MatrixManipulator>
46
47 #include <Include/general.hxx>
48 #include <Scenery/scenery.hxx>
49 #include "fg_os.hxx"
50 #include "fg_props.hxx"
51 #include "util.hxx"
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "CameraGroup.hxx"
55 #include "WindowBuilder.hxx"
56 #include "WindowSystemAdapter.hxx"
57
58 #if (FG_OSG_VERSION >= 19008)
59 #define OSG_HAS_MOUSE_CURSOR_PATCH
60 #endif
61
62 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
63 // to create the graphics window and run the event/update/render loop.
64
65 //
66 // fg_os implementation
67 //
68
69 using namespace std;    
70 using namespace flightgear;
71 using namespace osg;
72
73 static osg::ref_ptr<osgViewer::Viewer> viewer;
74 static osg::ref_ptr<osg::Camera> mainCamera;
75
76 namespace
77 {
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)
83 {
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();
90          citer != e;
91          ++citer) {
92         const View::Slave& slave = viewer->getSlave((*citer)->slaveIndex);
93         if (slave._projectionOffset.isIdentity()) {
94             masterCamera = (*citer)->camera.get();
95             break;
96         }
97     }
98     if (!masterCamera) {
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,
108                           Matrix(), Matrix());
109     }
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())
115             break;
116     }
117     if (iter != end) {            // Better not happen
118         (*iter)->flags |= GraphicsWindow::GUI;
119         cgroup->buildGUICamera(0, iter->get());
120     }
121     return cgroup;
122 }
123 }
124
125 void fgOSOpenWindow(bool stencil)
126 {
127     osg::GraphicsContext::WindowingSystemInterface* wsi
128         = osg::GraphicsContext::getWindowingSystemInterface();
129
130     viewer = new osgViewer::Viewer;
131     viewer->setDatabasePager(FGScenery::getPagerSingleton());
132     CameraGroup* cameraGroup = 0;
133     std::string mode;
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);
143     else
144       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
145     WindowBuilder::initWindowBuilder(stencil);
146     WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
147
148     // Look for windows, camera groups, and the old syntax of
149     // top-level cameras
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);
158         }
159     }
160     if (!cameraGroup)
161         cameraGroup = buildDefaultCameraGroup(viewer.get(), renderingNode);
162     Camera* guiCamera = getGUICamera(cameraGroup);
163     if (guiCamera) {
164         Viewport* guiViewport = guiCamera->getViewport();
165         fgSetInt("/sim/startup/xsize", guiViewport->width());
166         fgSetInt("/sim/startup/ysize", guiViewport->height());
167     }
168     FGManipulator* manipulator = globals->get_renderer()->getManipulator();
169     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
170     if (wsa->windows.size() != 1) {
171         manipulator->setResizable(false);
172     }
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);
181 }
182
183 static int status = 0;
184
185 void fgOSExit(int code)
186 {
187     viewer->setDone(true);
188     viewer->getDatabasePager()->cancel();
189     status = code;
190 }
191
192 void fgOSMainLoop()
193 {
194     viewer->run();
195     fgExit(status);
196 }
197
198 int fgGetKeyModifiers()
199 {
200     return globals->get_renderer()->getManipulator()->getCurrentModifiers();
201 }
202
203 void fgWarpMouse(int x, int y)
204 {
205     warpGUIPointer(CameraGroup::getDefault(), x, y);
206 }
207
208 void fgOSInit(int* argc, char** argv)
209 {
210     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
211 }
212
213 // Noop
214 void fgOSFullScreen()
215 {
216 }
217
218 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
219 static void setMouseCursor(osg::Camera* camera, int cursor)
220 {
221     if (!camera)
222         return;
223     osg::GraphicsContext* gc = camera->getGraphicsContext();
224     if (!gc)
225         return;
226     osgViewer::GraphicsWindow* gw;
227     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
228     if (!gw)
229         return;
230     
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;
243
244     gw->setCursor(mouseCursor);
245 }
246 #endif
247
248 static int _cursor = -1;
249
250 void fgSetMouseCursor(int cursor)
251 {
252     _cursor = 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);
257 #endif
258 }
259
260 int fgGetMouseCursor()
261 {
262     return _cursor;
263 }