]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
52e2b6d1e414a47b25eeb8fd797c837c27c791ef
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
1 // fg_os_common.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 <stdlib.h>
26
27 #include <simgear/compiler.h>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/debug/logstream.hxx>
30
31 #include <osg/GraphicsContext>
32 #include <osg/Group>
33 #include <osg/Matrixd>
34 #include <osg/Viewport>
35 #include <osg/Version>
36 #include <osgViewer/ViewerEventHandlers>
37 #include <osgViewer/Viewer>
38 #include <osgGA/MatrixManipulator>
39
40 #include <Include/general.hxx>
41 #include <Scenery/scenery.hxx>
42 #include "fg_os.hxx"
43 #include "fg_props.hxx"
44 #include "util.hxx"
45 #include "globals.hxx"
46 #include "renderer.hxx"
47
48 #if (FG_OSG_VERSION >= 19008)
49 #define OSG_HAS_MOUSE_CURSOR_PATCH
50 #endif
51
52 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
53 // to create the graphics window and run the event/update/render loop.
54
55 //
56 // fg_os implementation
57 //
58
59 static osg::ref_ptr<osgViewer::Viewer> viewer;
60 static osg::ref_ptr<osg::Camera> mainCamera;
61
62 void fgOSOpenWindow(int w, int h, int bpp,
63                     bool alpha, bool stencil, bool fullscreen)
64 {
65     osg::GraphicsContext::WindowingSystemInterface* wsi;
66     wsi = osg::GraphicsContext::getWindowingSystemInterface();
67
68     viewer = new osgViewer::Viewer;
69     viewer->setDatabasePager(FGScenery::getPagerSingleton());
70     // Avoid complications with fg's custom drawables.
71     std::string mode;
72     mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
73     if (mode == "AutomaticSelection")
74       viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
75     else if (mode == "CullDrawThreadPerContext")
76       viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
77     else if (mode == "DrawThreadPerContext")
78       viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
79     else if (mode == "CullThreadPerCameraDrawThreadPerContext")
80       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
81     else
82       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
83     osg::ref_ptr<osg::GraphicsContext::Traits> traits;
84     traits = new osg::GraphicsContext::Traits;
85     int cbits = (bpp <= 16) ?  5 :  8;
86     int zbits = (bpp <= 16) ? 16 : 24;
87     traits->red = traits->green = traits->blue = cbits;
88     traits->depth = zbits;
89     if (alpha)
90         traits->alpha = 8;
91     if (stencil)
92         traits->stencil = 8;
93     traits->doubleBuffer = true;
94     traits->mipMapGeneration = true;
95     traits->windowName = "FlightGear";
96     traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
97     traits->samples = fgGetBool("/sim/rendering/multi-samples", traits->samples);
98     traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
99
100     if (fullscreen) {
101         unsigned width = 0;
102         unsigned height = 0;
103         wsi->getScreenResolution(*traits, width, height);
104         traits->windowDecoration = false;
105         traits->width = width;
106         traits->height = height;
107         traits->supportsResize = false;
108     } else {
109         traits->windowDecoration = true;
110         traits->width = w;
111         traits->height = h;
112 #ifdef WIN32
113         // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
114         traits->x = 100;
115         traits->y = 100;
116 #endif
117         traits->supportsResize = true;
118     }
119
120     osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
121
122     // Ok, first the children.
123     // that achieves some magic ordering og the slaves so that we end up
124     // in the main window more often.
125     // This can be sorted out better when we got rid of glut and sdl.
126     FGManipulator* manipulator = globals->get_renderer()->getManipulator();
127     int nCameras = 0;
128     if (fgHasNode("/sim/rendering/camera")) {
129       SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
130       for (int i = 0; i < renderingNode->nChildren(); ++i) {
131         SGPropertyNode* cameraNode = renderingNode->getChild(i);
132         if (strcmp(cameraNode->getName(), "camera") != 0)
133           continue;
134
135         nCameras++;
136         // get a new copy of the traits struct
137         osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
138         cameraTraits = new osg::GraphicsContext::Traits(*traits);
139
140         double shearx = cameraNode->getDoubleValue("shear-x", 0);
141         double sheary = cameraNode->getDoubleValue("shear-y", 0);
142         cameraTraits->hostName = cameraNode->getStringValue("host-name", "");
143         cameraTraits->displayNum = cameraNode->getIntValue("display", 0);
144         cameraTraits->screenNum = cameraNode->getIntValue("screen", 0);
145         if (cameraNode->getBoolValue("fullscreen", fullscreen)) {
146           unsigned width = 0;
147           unsigned height = 0;
148           wsi->getScreenResolution(*cameraTraits, width, height);
149           cameraTraits->windowDecoration = false;
150           cameraTraits->width = width;
151           cameraTraits->height = height;
152           cameraTraits->supportsResize = false;
153         } else {
154           cameraTraits->windowDecoration = true;
155           cameraTraits->width = cameraNode->getIntValue("width", w);
156           cameraTraits->height = cameraNode->getIntValue("height", h);
157           cameraTraits->supportsResize = true;
158         }
159         // FIXME, currently this is too much of a problem to route the resize
160         // events. When we do no longer need sdl and such this
161         // can be simplified
162         cameraTraits->supportsResize = false;
163
164         // ok found a camera configuration, add a new slave ...
165         osg::ref_ptr<osg::Camera> camera = new osg::Camera;
166
167         osg::GraphicsContext* gc;
168         gc = osg::GraphicsContext::createGraphicsContext(cameraTraits.get());
169         gc->realize();
170         camera->setGraphicsContext(gc);
171         // If a viewport isn't set on the camera, then it's hard to dig it
172         // out of the SceneView objects in the viewer, and the coordinates
173         // of mouse events are somewhat bizzare.
174         camera->setViewport(new osg::Viewport(0, 0, cameraTraits->width, cameraTraits->height));
175         camera->setProjectionResizePolicy(rsp);
176         viewer->addSlave(camera.get(), osg::Matrix::translate(-shearx, -sheary, 0), osg::Matrix());
177       }
178       if (nCameras > 1)
179         manipulator->setResizable(false);
180     }
181
182     // now the main camera ...
183     osg::ref_ptr<osg::Camera> camera = new osg::Camera;
184     mainCamera = camera;
185     osg::GraphicsContext* gc;
186     gc = osg::GraphicsContext::createGraphicsContext(traits.get());
187     gc->realize();
188     gc->makeCurrent();
189     camera->setGraphicsContext(gc);
190     // If a viewport isn't set on the camera, then it's hard to dig it
191     // out of the SceneView objects in the viewer, and the coordinates
192     // of mouse events are somewhat bizzare.
193     camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
194     camera->setProjectionResizePolicy(rsp);
195     viewer->addSlave(camera.get());
196
197     viewer->setCameraManipulator(globals->get_renderer()->getManipulator());
198     // Let FG handle the escape key with a confirmation
199     viewer->setKeyEventSetsDone(0);
200     osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
201     statsHandler->setKeyEventTogglesOnScreenStats('*');
202     statsHandler->setKeyEventPrintsOutStats(0);
203     viewer->addEventHandler(statsHandler);
204     // The viewer won't start without some root.
205     viewer->setSceneData(new osg::Group);
206     globals->get_renderer()->setViewer(viewer.get());
207 }
208
209 static int status = 0;
210
211 void fgOSExit(int code)
212 {
213     viewer->setDone(true);
214     status = code;
215 }
216
217 void fgOSMainLoop()
218 {
219     viewer->run();
220     fgExit(status);
221 }
222
223 int fgGetKeyModifiers()
224 {
225     return globals->get_renderer()->getManipulator()->getCurrentModifiers();
226 }
227
228 void fgWarpMouse(int x, int y)
229 {
230     globals->get_renderer()->getManipulator()->setMouseWarped();
231     // Hack, currently the pointer is just recentered. So, we know the
232     // relative coordinates ...
233     if (!mainCamera.valid()) {
234         viewer->requestWarpPointer(0, 0);
235         return;
236     }
237     float xsize = (float)mainCamera->getGraphicsContext()->getTraits()->width;
238     float ysize = (float)mainCamera->getGraphicsContext()->getTraits()->height;
239     viewer->requestWarpPointer(2.0f * (float)x / xsize - 1.0f,
240                                1.0f - 2.0f * (float)y / ysize);
241 }
242
243 // Noop
244 void fgOSInit(int* argc, char** argv)
245 {
246 }
247
248 // Noop
249 void fgOSFullScreen()
250 {
251 }
252
253 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
254 static void setMouseCursor(osg::Camera* camera, int cursor)
255 {
256     if (!camera)
257         return;
258     osg::GraphicsContext* gc = camera->getGraphicsContext();
259     if (!gc)
260         return;
261     osgViewer::GraphicsWindow* gw;
262     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
263     if (!gw)
264         return;
265     
266     osgViewer::GraphicsWindow::MouseCursor mouseCursor;
267     mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
268     if     (cursor == MOUSE_CURSOR_NONE)
269         mouseCursor = osgViewer::GraphicsWindow::NoCursor;
270     else if(cursor == MOUSE_CURSOR_POINTER)
271         mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
272     else if(cursor == MOUSE_CURSOR_WAIT)
273         mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
274     else if(cursor == MOUSE_CURSOR_CROSSHAIR)
275         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
276     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
277         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
278
279     gw->setCursor(mouseCursor);
280 }
281 #endif
282
283 static int _cursor = -1;
284
285 void fgSetMouseCursor(int cursor)
286 {
287     _cursor = cursor;
288 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
289     setMouseCursor(viewer->getCamera(), cursor);
290     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
291         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
292 #endif
293 }
294
295 int fgGetMouseCursor()
296 {
297     return _cursor;
298 }
299
300 void fgMakeCurrent()
301 {
302     if (!mainCamera.valid())
303         return;
304     osg::GraphicsContext* gc = mainCamera->getGraphicsContext();
305     if (!gc)
306         return;
307     gc->makeCurrent();
308 }
309
310 bool fgOSIsMainContext(const osg::GraphicsContext* context)
311 {
312     if (!mainCamera.valid())
313         return false;
314     return context == mainCamera->getGraphicsContext();
315 }
316
317 bool fgOSIsMainCamera(const osg::Camera* camera)
318 {
319   if (!camera)
320     return false;
321   if (camera == mainCamera.get())
322     return true;
323   if (!viewer.valid())
324     return false;
325   if (camera == viewer->getCamera())
326     return true;
327   return false;
328 }