1 // fg_os_osgviewer.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 <boost/foreach.hpp>
34 #include <simgear/compiler.h>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/props/props_io.hxx>
40 #include <osg/GraphicsContext>
42 #include <osg/Matrixd>
43 #include <osg/Viewport>
44 #include <osg/Version>
46 #include <osgViewer/ViewerEventHandlers>
47 #include <osgViewer/Viewer>
48 #include <osgGA/MatrixManipulator>
50 #include <Include/general.hxx>
51 #include <Scenery/scenery.hxx>
53 #include "fg_props.hxx"
55 #include "globals.hxx"
56 #include "renderer.hxx"
57 #include "CameraGroup.hxx"
58 #include "FGEventHandler.hxx"
59 #include "WindowBuilder.hxx"
60 #include "WindowSystemAdapter.hxx"
62 // Static linking of OSG needs special macros
63 #ifdef OSG_LIBRARY_STATIC
64 #include <osgDB/Registry>
74 #ifdef OSG_JPEG_ENABLED
77 #ifdef OSG_PNG_ENABLED
80 #ifdef OSG_TIFF_ENABLED
91 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
92 // to create the graphics window and run the event/update/render loop.
95 // fg_os implementation
99 using namespace flightgear;
102 static osg::ref_ptr<osgViewer::Viewer> viewer;
103 static osg::ref_ptr<osg::Camera> mainCamera;
105 void fgOSOpenWindow(bool stencil)
107 viewer = new osgViewer::Viewer;
108 viewer->setDatabasePager(FGScenery::getPagerSingleton());
109 CameraGroup* cameraGroup = 0;
111 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
112 if (mode == "AutomaticSelection")
113 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
114 else if (mode == "CullDrawThreadPerContext")
115 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
116 else if (mode == "DrawThreadPerContext")
117 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
118 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
119 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
121 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
122 WindowBuilder::initWindowBuilder(stencil);
123 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
125 // Look for windows, camera groups, and the old syntax of
127 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
128 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
129 bool oldSyntax = !cgroupNode->hasChild("camera");
131 for (int i = 0; i < renderingNode->nChildren(); ++i) {
132 SGPropertyNode* propNode = renderingNode->getChild(i);
133 const char* propName = propNode->getName();
134 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
135 SGPropertyNode* copiedNode
136 = cgroupNode->getNode(propName, propNode->getIndex(), true);
137 copyProperties(propNode, copiedNode);
140 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
141 SGPropertyNode* masterCamera = 0;
142 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
143 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
144 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
145 masterCamera = camera.ptr();
150 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
151 setValue(masterCamera->getNode("window/name", true),
152 windowBuilder->getDefaultWindowName());
154 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
156 setValue(cgroupNode->getNode("gui/window/name", true),
157 nameNode->getStringValue());
159 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
160 Camera* guiCamera = getGUICamera(cameraGroup);
162 Viewport* guiViewport = guiCamera->getViewport();
163 fgSetInt("/sim/startup/xsize", guiViewport->width());
164 fgSetInt("/sim/startup/ysize", guiViewport->height());
166 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
167 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
168 if (wsa->windows.size() != 1) {
169 manipulator->setResizable(false);
171 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
172 viewer->addEventHandler(manipulator);
173 // Let FG handle the escape key with a confirmation
174 viewer->setKeyEventSetsDone(0);
175 // The viewer won't start without some root.
176 viewer->setSceneData(new osg::Group);
177 globals->get_renderer()->setViewer(viewer.get());
178 CameraGroup::setDefault(cameraGroup);
181 static int status = 0;
183 void fgOSExit(int code)
185 viewer->setDone(true);
186 viewer->getDatabasePager()->cancel();
192 ref_ptr<FGEventHandler> manipulator
193 = globals->get_renderer()->getEventHandler();
194 viewer->setReleaseContextAtEndOfFrameHint(false);
195 if (!viewer->isRealized())
197 while (!viewer->done()) {
198 fgIdleHandler idleFunc = manipulator->getIdleHandler();
199 fgDrawHandler drawFunc = manipulator->getDrawHandler();
209 int fgGetKeyModifiers()
211 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
214 void fgWarpMouse(int x, int y)
216 warpGUIPointer(CameraGroup::getDefault(), x, y);
219 void fgOSInit(int* argc, char** argv)
221 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
225 void fgOSFullScreen()
229 static void setMouseCursor(osg::Camera* camera, int cursor)
233 osg::GraphicsContext* gc = camera->getGraphicsContext();
236 osgViewer::GraphicsWindow* gw;
237 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
241 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
242 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
243 if (cursor == MOUSE_CURSOR_NONE)
244 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
245 else if(cursor == MOUSE_CURSOR_POINTER)
246 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
247 else if(cursor == MOUSE_CURSOR_WAIT)
248 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
249 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
250 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
251 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
252 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
253 else if(cursor == MOUSE_CURSOR_TOPSIDE)
254 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
255 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
256 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
257 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
258 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
259 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
260 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
261 else if(cursor == MOUSE_CURSOR_TOPLEFT)
262 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
263 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
264 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
265 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
266 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
267 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
268 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
270 gw->setCursor(mouseCursor);
273 static int _cursor = -1;
275 void fgSetMouseCursor(int cursor)
278 setMouseCursor(viewer->getCamera(), cursor);
279 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
280 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
283 int fgGetMouseCursor()