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.
32 #include <simgear/compiler.h>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/debug/logstream.hxx>
36 #include <osg/GraphicsContext>
38 #include <osg/Matrixd>
39 #include <osg/Viewport>
40 #include <osg/Version>
42 #include <osgViewer/ViewerEventHandlers>
43 #include <osgViewer/Viewer>
44 #include <osgGA/MatrixManipulator>
46 #include <Include/general.hxx>
47 #include <Scenery/scenery.hxx>
49 #include "fg_props.hxx"
51 #include "globals.hxx"
52 #include "renderer.hxx"
53 #include "WindowSystemAdapter.hxx"
55 #if (FG_OSG_VERSION >= 19008)
56 #define OSG_HAS_MOUSE_CURSOR_PATCH
59 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
60 // to create the graphics window and run the event/update/render loop.
63 // fg_os implementation
67 using namespace flightgear;
72 static osg::ref_ptr<osgViewer::Viewer> viewer;
73 static osg::ref_ptr<osg::Camera> mainCamera;
75 // Callback to prevent the GraphicsContext resized function from messing
76 // with the projection matrix of the slave
80 // silly function for making the default window and camera names
81 std::string makeName(const string& prefix, int num)
83 std::stringstream stream;
84 stream << prefix << num;
88 GraphicsContext::Traits*
89 makeDefaultTraits(GraphicsContext::WindowingSystemInterface* wsi, bool stencil)
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");
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;
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);
117 wsi->getScreenResolution(*traits, width, height);
118 traits->windowDecoration = false;
119 traits->width = width;
120 traits->height = height;
121 traits->supportsResize = false;
123 traits->windowDecoration = true;
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
132 traits->supportsResize = true;
137 void setTraitsFromProperties(GraphicsContext::Traits* traits,
138 const SGPropertyNode* winNode,
139 GraphicsContext::WindowingSystemInterface* wsi)
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"))) {
149 wsi->getScreenResolution(*traits, width, height);
150 traits->windowDecoration = false;
151 traits->width = width;
152 traits->height = height;
153 traits->supportsResize = false;
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;
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");
170 void fgOSOpenWindow(bool stencil)
172 osg::GraphicsContext::WindowingSystemInterface* wsi;
173 wsi = osg::GraphicsContext::getWindowingSystemInterface();
175 viewer = new osgViewer::Viewer;
176 viewer->setDatabasePager(FGScenery::getPagerSingleton());
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);
188 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
189 osg::ref_ptr<osg::GraphicsContext::Traits> traits
190 = makeDefaultTraits(wsi, stencil);
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)
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;
218 // ok found a camera configuration, add a new slave if possible
220 = GraphicsContext::createGraphicsContext(cameraTraits.get());
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
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,
236 Camera3D* cam3D = wsa->registerCamera3D(window, camera,
238 if (shearx == 0 && sheary == 0)
239 cam3D->flags |= Camera3D::MASTER;
241 osg::Matrix pOff = osg::Matrix::translate(-shearx, -sheary, 0);
243 vOff.makeRotate(SGMiscd::deg2rad(heading), osg::Vec3(0, 1, 0));
244 viewer->addSlave(camera, pOff, vOff);
246 SG_LOG(SG_GENERAL, SG_WARN,
247 "Couldn't create graphics context on "
248 << cameraTraits->hostName << ":"
249 << cameraTraits->displayNum
250 << "." << cameraTraits->screenNum);
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;
271 osg::GraphicsContext* gc
272 = osg::GraphicsContext::createGraphicsContext(traits.get());
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,
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
289 viewer->addSlave(camera);
291 mainCamera = (*citr)->camera;
293 if (wsa->cameras.size() != 1) {
294 manipulator->setResizable(false);
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());
305 static int status = 0;
307 void fgOSExit(int code)
309 viewer->setDone(true);
310 viewer->getDatabasePager()->cancel();
320 int fgGetKeyModifiers()
322 return globals->get_renderer()->getManipulator()->getCurrentModifiers();
325 void fgWarpMouse(int x, int y)
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);
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);
340 void fgOSInit(int* argc, char** argv)
342 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
346 void fgOSFullScreen()
350 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
351 static void setMouseCursor(osg::Camera* camera, int cursor)
355 osg::GraphicsContext* gc = camera->getGraphicsContext();
358 osgViewer::GraphicsWindow* gw;
359 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
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;
376 gw->setCursor(mouseCursor);
380 static int _cursor = -1;
382 void fgSetMouseCursor(int 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);
392 int fgGetMouseCursor()
397 bool fgOSIsMainContext(const osg::GraphicsContext* context)
399 if (!mainCamera.valid())
401 return context == mainCamera->getGraphicsContext();
404 bool fgOSIsMainCamera(const osg::Camera* camera)
408 if (camera == mainCamera.get())
412 if (camera == viewer->getCamera())
417 GraphicsContext* fgOSGetMainContext()
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())
426 return (*contextIter)->gc.get();