]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
header cleanups
[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 <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/GraphicsContext>
37 #include <osg/Group>
38 #include <osg/Matrixd>
39 #include <osg/Viewport>
40 #include <osg/Version>
41 #include <osg/View>
42 #include <osgViewer/ViewerEventHandlers>
43 #include <osgViewer/Viewer>
44 #include <osgGA/MatrixManipulator>
45
46 #include <Include/general.hxx>
47 #include <Scenery/scenery.hxx>
48 #include "fg_os.hxx"
49 #include "fg_props.hxx"
50 #include "util.hxx"
51 #include "globals.hxx"
52 #include "renderer.hxx"
53 #include "WindowSystemAdapter.hxx"
54
55 #if (FG_OSG_VERSION >= 19008)
56 #define OSG_HAS_MOUSE_CURSOR_PATCH
57 #endif
58
59 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
60 // to create the graphics window and run the event/update/render loop.
61
62 //
63 // fg_os implementation
64 //
65
66 using namespace std;    
67 using namespace flightgear;
68 using namespace osg;
69
70
71
72 static osg::ref_ptr<osgViewer::Viewer> viewer;
73 static osg::ref_ptr<osg::Camera> mainCamera;
74
75 // Callback to prevent the GraphicsContext resized function from messing
76 // with the projection matrix of the slave
77
78 namespace
79 {
80 // silly function for making the default window and camera names
81 std::string makeName(const string& prefix, int num)
82 {
83     std::stringstream stream;
84     stream << prefix << num;
85     return stream.str();
86 }
87
88 GraphicsContext::Traits*
89 makeDefaultTraits(GraphicsContext::WindowingSystemInterface* wsi, bool stencil)
90 {
91     int w = fgGetInt("/sim/startup/xsize");
92     int h = fgGetInt("/sim/startup/ysize");
93     int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
94     bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
95     bool fullscreen = fgGetBool("/sim/startup/fullscreen");
96
97     GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
98     traits->readDISPLAY();
99     int cbits = (bpp <= 16) ?  5 :  8;
100     int zbits = (bpp <= 16) ? 16 : 24;
101     traits->red = traits->green = traits->blue = cbits;
102     traits->depth = zbits;
103     if (alpha)
104         traits->alpha = 8;
105     if (stencil)
106         traits->stencil = 8;
107     traits->doubleBuffer = true;
108     traits->mipMapGeneration = true;
109     traits->windowName = "FlightGear";
110     // XXX should check per window too.
111     traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
112     traits->samples = fgGetBool("/sim/rendering/multi-samples", traits->samples);
113     traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
114     if (fullscreen) {
115         unsigned width = 0;
116         unsigned height = 0;
117         wsi->getScreenResolution(*traits, width, height);
118         traits->windowDecoration = false;
119         traits->width = width;
120         traits->height = height;
121         traits->supportsResize = false;
122     } else {
123         traits->windowDecoration = true;
124         traits->width = w;
125         traits->height = h;
126 #if defined(WIN32) || defined(__APPLE__)
127         // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
128         // Mac also needs this to show window frame, menubar and Docks
129         traits->x = 100;
130         traits->y = 100;
131 #endif
132         traits->supportsResize = true;
133     }
134     return traits;
135 }
136
137 void setTraitsFromProperties(GraphicsContext::Traits* traits,
138                              const SGPropertyNode* winNode,
139                              GraphicsContext::WindowingSystemInterface* wsi)
140 {
141     traits->hostName
142         = winNode->getStringValue("host-name", traits->hostName.c_str());
143     traits->displayNum = winNode->getIntValue("display", traits->displayNum);
144     traits->screenNum = winNode->getIntValue("screen", traits->screenNum);
145     if (winNode->getBoolValue("fullscreen",
146                               fgGetBool("/sim/startup/fullscreen"))) {
147         unsigned width = 0;
148         unsigned height = 0;
149         wsi->getScreenResolution(*traits, width, height);
150         traits->windowDecoration = false;
151         traits->width = width;
152         traits->height = height;
153         traits->supportsResize = false;
154     } else {
155         traits->windowDecoration = winNode->getBoolValue("decoration", true);
156         traits->width = winNode->getIntValue("width", traits->width);
157         traits->height = winNode->getIntValue("height", traits->height);
158         traits->supportsResize = true;
159     }
160     traits->x = winNode->getIntValue("x", traits->x);
161     traits->y = winNode->getIntValue("y", traits->y);
162     if (winNode->hasChild("window-name"))
163         traits->windowName = winNode->getStringValue("window-name");
164     else if (winNode->hasChild("name")) 
165         traits->windowName = winNode->getStringValue("name");
166 }
167
168 } //namespace
169
170 void fgOSOpenWindow(bool stencil)
171 {
172     osg::GraphicsContext::WindowingSystemInterface* wsi;
173     wsi = osg::GraphicsContext::getWindowingSystemInterface();
174
175     viewer = new osgViewer::Viewer;
176     viewer->setDatabasePager(FGScenery::getPagerSingleton());
177     std::string mode;
178     mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
179     if (mode == "AutomaticSelection")
180       viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
181     else if (mode == "CullDrawThreadPerContext")
182       viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
183     else if (mode == "DrawThreadPerContext")
184       viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
185     else if (mode == "CullThreadPerCameraDrawThreadPerContext")
186       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
187     else
188       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
189     osg::ref_ptr<osg::GraphicsContext::Traits> traits
190         = makeDefaultTraits(wsi, stencil);
191
192     // Ok, first the children.
193     // that achieves some magic ordering og the slaves so that we end up
194     // in the main window more often.
195     // This can be sorted out better when we got rid of glut and sdl.
196     FGManipulator* manipulator = globals->get_renderer()->getManipulator();
197     string defaultName("slave");
198     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
199     if (fgHasNode("/sim/rendering/camera")) {
200         SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
201         for (int i = 0; i < renderingNode->nChildren(); ++i) {
202             SGPropertyNode* cameraNode = renderingNode->getChild(i);
203             if (strcmp(cameraNode->getName(), "camera") != 0)
204                 continue;
205
206             // get a new copy of the traits struct
207             osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
208             cameraTraits = new osg::GraphicsContext::Traits(*traits);
209             double shearx = cameraNode->getDoubleValue("shear-x", 0);
210             double sheary = cameraNode->getDoubleValue("shear-y", 0);
211             double heading = cameraNode->getDoubleValue("heading-deg", 0);
212             setTraitsFromProperties(cameraTraits.get(), cameraNode, wsi);
213             // FIXME, currently this is too much of a problem to route
214             // the resize events. When we do no longer need sdl and
215             // such this can be simplified
216             cameraTraits->supportsResize = false;
217
218             // ok found a camera configuration, add a new slave if possible
219             GraphicsContext* gc
220                 = GraphicsContext::createGraphicsContext(cameraTraits.get());
221             if (gc) {
222                 gc->realize();
223                 Camera *camera = new Camera;
224                 camera->setGraphicsContext(gc);
225                 // If a viewport isn't set on the camera, then it's
226                 // hard to dig it out of the SceneView objects in the
227                 // viewer, and the coordinates of mouse events are
228                 // somewhat bizzare.
229                 camera->setViewport(new Viewport(0, 0, cameraTraits->width,
230                                                  cameraTraits->height));
231                 const char* cameraName = cameraNode->getStringValue("name");
232                 string cameraNameString = (cameraName ? string(cameraName)
233                                            : makeName(defaultName, i));
234                 GraphicsWindow* window = wsa->registerWindow(gc,
235                                                              cameraNameString);
236                 Camera3D* cam3D = wsa->registerCamera3D(window, camera,
237                                                         cameraNameString);
238                 if (shearx == 0 && sheary == 0)
239                     cam3D->flags |= Camera3D::MASTER;
240                 
241                 osg::Matrix pOff = osg::Matrix::translate(-shearx, -sheary, 0);
242                 osg::Matrix vOff;
243                 vOff.makeRotate(SGMiscd::deg2rad(heading), osg::Vec3(0, 1, 0));
244                 viewer->addSlave(camera, pOff, vOff);
245             } else {
246                 SG_LOG(SG_GENERAL, SG_WARN,
247                        "Couldn't create graphics context on "
248                        << cameraTraits->hostName << ":"
249                        << cameraTraits->displayNum
250                        << "." << cameraTraits->screenNum);
251             }
252         }
253     }
254     // now the main camera ...
255     // XXX mainCamera's purpose is to establish a "main graphics
256     // context" that can be made current (if necessary). But that
257     // should be a context established with a window. It's used to
258     // choose whether to render the GUI and panel camera nodes, but
259     // that's obsolete because the GUI is rendered in its own
260     // slave. And it's used to translate mouse event coordinates, but
261     // that's bogus because mouse clicks should work on any camera. In
262     // short, mainCamera must die :)
263     Camera3DVector::iterator citr
264         = find_if(wsa->cameras.begin(), wsa->cameras.end(),
265                   WindowSystemAdapter::FlagTester<Camera3D>(Camera3D::MASTER));
266     if (citr == wsa->cameras.end()) {
267         // Create a camera aligned with the master camera. Eventually
268         // this will be optional.
269         Camera* camera = new osg::Camera;
270         mainCamera = camera;
271         osg::GraphicsContext* gc
272             = osg::GraphicsContext::createGraphicsContext(traits.get());
273         gc->realize();
274         gc->makeCurrent();
275         camera->setGraphicsContext(gc);
276         // If a viewport isn't set on the camera, then it's hard to dig it
277         // out of the SceneView objects in the viewer, and the coordinates
278         // of mouse events are somewhat bizzare.
279         camera->setViewport(new osg::Viewport(0, 0,
280                                               traits->width, traits->height));
281         GraphicsWindow* window = wsa->registerWindow(gc, string("main"));
282         window->flags |= GraphicsWindow::GUI;
283         Camera3D* camera3d = wsa->registerCamera3D(window, camera,
284                                                    string("main"));
285         camera3d->flags |= Camera3D::MASTER;
286         // Why a slave? It seems to be the easiest way to assign cameras,
287         // for which we've created the graphics context ourselves, to
288         // the viewer. 
289         viewer->addSlave(camera);
290     } else {
291         mainCamera = (*citr)->camera;
292     }
293     if (wsa->cameras.size() != 1) {
294         manipulator->setResizable(false);
295     }
296     viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
297     viewer->setCameraManipulator(manipulator);
298     // Let FG handle the escape key with a confirmation
299     viewer->setKeyEventSetsDone(0);
300     // The viewer won't start without some root.
301     viewer->setSceneData(new osg::Group);
302     globals->get_renderer()->setViewer(viewer.get());
303 }
304
305 static int status = 0;
306
307 void fgOSExit(int code)
308 {
309     viewer->setDone(true);
310     viewer->getDatabasePager()->cancel();
311     status = code;
312 }
313
314 void fgOSMainLoop()
315 {
316     viewer->run();
317     fgExit(status);
318 }
319
320 int fgGetKeyModifiers()
321 {
322     return globals->get_renderer()->getManipulator()->getCurrentModifiers();
323 }
324
325 void fgWarpMouse(int x, int y)
326 {
327     globals->get_renderer()->getManipulator()->setMouseWarped();
328     // Hack, currently the pointer is just recentered. So, we know the
329     // relative coordinates ...
330     if (!mainCamera.valid()) {
331         viewer->requestWarpPointer(0, 0);
332         return;
333     }
334     float xsize = (float)mainCamera->getGraphicsContext()->getTraits()->width;
335     float ysize = (float)mainCamera->getGraphicsContext()->getTraits()->height;
336     viewer->requestWarpPointer(2.0f * (float)x / xsize - 1.0f,
337                                1.0f - 2.0f * (float)y / ysize);
338 }
339
340 void fgOSInit(int* argc, char** argv)
341 {
342     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
343 }
344
345 // Noop
346 void fgOSFullScreen()
347 {
348 }
349
350 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
351 static void setMouseCursor(osg::Camera* camera, int cursor)
352 {
353     if (!camera)
354         return;
355     osg::GraphicsContext* gc = camera->getGraphicsContext();
356     if (!gc)
357         return;
358     osgViewer::GraphicsWindow* gw;
359     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
360     if (!gw)
361         return;
362     
363     osgViewer::GraphicsWindow::MouseCursor mouseCursor;
364     mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
365     if     (cursor == MOUSE_CURSOR_NONE)
366         mouseCursor = osgViewer::GraphicsWindow::NoCursor;
367     else if(cursor == MOUSE_CURSOR_POINTER)
368         mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
369     else if(cursor == MOUSE_CURSOR_WAIT)
370         mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
371     else if(cursor == MOUSE_CURSOR_CROSSHAIR)
372         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
373     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
374         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
375
376     gw->setCursor(mouseCursor);
377 }
378 #endif
379
380 static int _cursor = -1;
381
382 void fgSetMouseCursor(int cursor)
383 {
384     _cursor = cursor;
385 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
386     setMouseCursor(viewer->getCamera(), cursor);
387     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
388         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
389 #endif
390 }
391
392 int fgGetMouseCursor()
393 {
394     return _cursor;
395 }
396
397 bool fgOSIsMainContext(const osg::GraphicsContext* context)
398 {
399     if (!mainCamera.valid())
400         return false;
401     return context == mainCamera->getGraphicsContext();
402 }
403
404 bool fgOSIsMainCamera(const osg::Camera* camera)
405 {
406   if (!camera)
407     return false;
408   if (camera == mainCamera.get())
409     return true;
410   if (!viewer.valid())
411     return false;
412   if (camera == viewer->getCamera())
413     return true;
414   return false;
415 }
416
417 GraphicsContext* fgOSGetMainContext()
418 {
419     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
420     WindowVector::iterator contextIter
421         = std::find_if(wsa->windows.begin(), wsa->windows.end(),
422                        WindowSystemAdapter::FlagTester<GraphicsWindow>(GraphicsWindow::GUI));
423     if (contextIter == wsa->windows.end())
424         return 0;
425     else
426         return (*contextIter)->gc.get();
427 }
428