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