1 // fg_os_common.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
4 // Copyright (C) 2007 Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich
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.
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.
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.
27 #include <simgear/compiler.h>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/debug/logstream.hxx>
31 #include <osg/GraphicsContext>
33 #include <osg/Matrixd>
34 #include <osg/Viewport>
35 #include <osg/Version>
36 #include <osgViewer/ViewerEventHandlers>
37 #include <osgViewer/Viewer>
38 #include <osgGA/MatrixManipulator>
40 #include <Include/general.hxx>
41 #include <Scenery/scenery.hxx>
43 #include "fg_props.hxx"
45 #include "globals.hxx"
46 #include "renderer.hxx"
48 #if (FG_OSG_VERSION >= 19008)
49 #define OSG_HAS_MOUSE_CURSOR_PATCH
52 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
53 // to create the graphics window and run the event/update/render loop.
56 // fg_os implementation
59 static osg::ref_ptr<osgViewer::Viewer> viewer;
60 static osg::ref_ptr<osg::Camera> mainCamera;
62 void fgOSOpenWindow(int w, int h, int bpp,
63 bool alpha, bool stencil, bool fullscreen)
65 osg::GraphicsContext::WindowingSystemInterface* wsi;
66 wsi = osg::GraphicsContext::getWindowingSystemInterface();
68 viewer = new osgViewer::Viewer;
69 viewer->setDatabasePager(FGScenery::getPagerSingleton());
70 // Avoid complications with fg's custom drawables.
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);
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;
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);
103 wsi->getScreenResolution(*traits, width, height);
104 traits->windowDecoration = false;
105 traits->width = width;
106 traits->height = height;
107 traits->supportsResize = false;
109 traits->windowDecoration = true;
113 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
117 traits->supportsResize = true;
120 osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
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();
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)
136 // get a new copy of the traits struct
137 osg::ref_ptr<osg::GraphicsContext::Traits> cameraTraits;
138 cameraTraits = new osg::GraphicsContext::Traits(*traits);
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)) {
148 wsi->getScreenResolution(*cameraTraits, width, height);
149 cameraTraits->windowDecoration = false;
150 cameraTraits->width = width;
151 cameraTraits->height = height;
152 cameraTraits->supportsResize = false;
154 cameraTraits->windowDecoration = true;
155 cameraTraits->width = cameraNode->getIntValue("width", w);
156 cameraTraits->height = cameraNode->getIntValue("height", h);
157 cameraTraits->supportsResize = true;
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
162 cameraTraits->supportsResize = false;
164 // ok found a camera configuration, add a new slave ...
165 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
167 osg::GraphicsContext* gc;
168 gc = osg::GraphicsContext::createGraphicsContext(cameraTraits.get());
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());
179 manipulator->setResizable(false);
182 // now the main camera ...
183 osg::ref_ptr<osg::Camera> camera = new osg::Camera;
185 osg::GraphicsContext* gc;
186 gc = osg::GraphicsContext::createGraphicsContext(traits.get());
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());
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());
209 static int status = 0;
211 void fgOSExit(int code)
213 viewer->setDone(true);
223 int fgGetKeyModifiers()
225 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
228 void fgWarpMouse(int x, int y)
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);
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);
244 void fgOSInit(int* argc, char** argv)
249 void fgOSFullScreen()
253 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
254 static void setMouseCursor(osg::Camera* camera, int cursor)
258 osg::GraphicsContext* gc = camera->getGraphicsContext();
261 osgViewer::GraphicsWindow* gw;
262 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
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;
279 gw->setCursor(mouseCursor);
283 static int _cursor = -1;
285 void fgSetMouseCursor(int 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);
295 int fgGetMouseCursor()
302 if (!mainCamera.valid())
304 osg::GraphicsContext* gc = mainCamera->getGraphicsContext();
310 bool fgOSIsMainContext(const osg::GraphicsContext* context)
312 if (!mainCamera.valid())
314 return context == mainCamera->getGraphicsContext();
317 bool fgOSIsMainCamera(const osg::Camera* camera)
321 if (camera == mainCamera.get())
325 if (camera == viewer->getCamera())