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