8 #include <simgear/compiler.h>
9 #include <simgear/structure/exception.hxx>
10 #include <simgear/debug/logstream.hxx>
12 #include <osg/GraphicsContext>
14 #include <osg/Matrixd>
15 #include <osg/Viewport>
16 #include <osg/Version>
17 #include <osgViewer/ViewerEventHandlers>
18 #include <osgViewer/Viewer>
19 #include <osgGA/MatrixManipulator>
22 #include "fg_props.hxx"
24 #include "globals.hxx"
25 #include "renderer.hxx"
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
35 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
36 // to create the graphics window and run the event/update/render loop.
38 // fg_os callback registration APIs
42 // Event handling and scene graph update is all handled by a
43 // manipulator. See FGManipulator.cpp
44 void fgRegisterIdleHandler(fgIdleHandler func)
46 globals->get_renderer()->getManipulator()->setIdleHandler(func);
49 void fgRegisterDrawHandler(fgDrawHandler func)
51 globals->get_renderer()->getManipulator()->setDrawHandler(func);
54 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
56 globals->get_renderer()->getManipulator()->setWindowResizeHandler(func);
59 void fgRegisterKeyHandler(fgKeyHandler func)
61 globals->get_renderer()->getManipulator()->setKeyHandler(func);
64 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
66 globals->get_renderer()->getManipulator()->setMouseClickHandler(func);
69 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
71 globals->get_renderer()->getManipulator()->setMouseMotionHandler(func);
74 // Redraw "happens" every frame whether you want it or not.
75 void fgRequestRedraw()
80 // fg_os implementation
83 static osg::ref_ptr<osgViewer::Viewer> viewer;
84 static osg::ref_ptr<osg::Camera> mainCamera;
86 void fgOSOpenWindow(int w, int h, int bpp,
87 bool alpha, bool stencil, bool fullscreen)
89 osg::GraphicsContext::WindowingSystemInterface* wsi;
90 wsi = osg::GraphicsContext::getWindowingSystemInterface();
92 viewer = new osgViewer::Viewer;
93 // Avoid complications with fg's custom drawables.
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);
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;
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);
126 wsi->getScreenResolution(*traits, width, height);
127 traits->windowDecoration = false;
128 traits->width = width;
129 traits->height = height;
130 traits->supportsResize = false;
132 traits->windowDecoration = true;
136 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
140 traits->supportsResize = true;
143 osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
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)
156 // get a new copy of the traits struct
157 osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
158 cameraTraits = new osg::GraphicsContext::Traits(*traits);
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)) {
168 wsi->getScreenResolution(*cameraTraits, width, height);
169 cameraTraits->windowDecoration = false;
170 cameraTraits->width = width;
171 cameraTraits->height = height;
172 cameraTraits->supportsResize = false;
174 cameraTraits->windowDecoration = true;
175 cameraTraits->width = cameraNode->getIntValue("width", w);
176 cameraTraits->height = cameraNode->getIntValue("height", h);
177 cameraTraits->supportsResize = true;
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
182 cameraTraits->supportsResize = false;
184 // ok found a camera configuration, add a new slave ...
185 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
187 osg::GraphicsContext* gc;
188 gc = osg::GraphicsContext::createGraphicsContext(cameraTraits.get());
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());
200 // now the main camera ...
201 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
203 osg::GraphicsContext* gc;
204 gc = osg::GraphicsContext::createGraphicsContext(traits.get());
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());
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());
227 static int status = 0;
229 void fgOSExit(int code)
231 viewer->setDone(true);
241 int fgGetKeyModifiers()
243 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
246 void fgWarpMouse(int x, int y)
248 // Hack, currently the pointer is just recentered. So, we know the relative coordinates ...
249 viewer->requestWarpPointer(0, 0);
253 void fgOSInit(int* argc, char** argv)
258 void fgOSFullScreen()
262 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
263 static void setMouseCursor(osg::Camera* camera, int cursor)
267 osg::GraphicsContext* gc = camera->getGraphicsContext();
270 osgViewer::GraphicsWindow* gw;
271 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
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;
288 gw->setCursor(mouseCursor);
292 static int _cursor = -1;
294 void fgSetMouseCursor(int 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);
304 int fgGetMouseCursor()
311 if (!mainCamera.valid())
313 osg::GraphicsContext* gc = mainCamera->getGraphicsContext();
319 bool fgOSIsMainContext(const osg::GraphicsContext* context)
321 if (!mainCamera.valid())
323 return context == mainCamera->getGraphicsContext();
326 bool fgOSIsMainCamera(const osg::Camera* camera)
330 if (camera == mainCamera.get())
334 if (camera == viewer->getCamera())