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>
49 #include <Include/general.hxx>
50 #include <Scenery/scenery.hxx>
52 #include "fg_props.hxx"
54 #include "globals.hxx"
55 #include "renderer.hxx"
56 #include "CameraGroup.hxx"
57 #include "FGEventHandler.hxx"
58 #include "WindowBuilder.hxx"
59 #include "WindowSystemAdapter.hxx"
61 // Static linking of OSG needs special macros
62 #ifdef OSG_LIBRARY_STATIC
63 #include <osgDB/Registry>
73 #ifdef OSG_JPEG_ENABLED
76 #ifdef OSG_PNG_ENABLED
79 #ifdef OSG_TIFF_ENABLED
90 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
91 // to create the graphics window and run the event/update/render loop.
94 // fg_os implementation
98 using namespace flightgear;
101 static osg::ref_ptr<osgViewer::Viewer> viewer;
102 static osg::ref_ptr<osg::Camera> mainCamera;
104 void fgOSOpenWindow(bool stencil)
106 viewer = new osgViewer::Viewer;
107 viewer->setDatabasePager(FGScenery::getPagerSingleton());
108 CameraGroup* cameraGroup = 0;
110 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
111 if (mode == "AutomaticSelection")
112 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
113 else if (mode == "CullDrawThreadPerContext")
114 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
115 else if (mode == "DrawThreadPerContext")
116 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
117 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
118 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
120 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
121 WindowBuilder::initWindowBuilder(stencil);
122 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
124 // Look for windows, camera groups, and the old syntax of
126 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
127 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
128 bool oldSyntax = !cgroupNode->hasChild("camera");
130 for (int i = 0; i < renderingNode->nChildren(); ++i) {
131 SGPropertyNode* propNode = renderingNode->getChild(i);
132 const char* propName = propNode->getName();
133 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
134 SGPropertyNode* copiedNode
135 = cgroupNode->getNode(propName, propNode->getIndex(), true);
136 copyProperties(propNode, copiedNode);
139 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
140 SGPropertyNode* masterCamera = 0;
141 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
142 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
143 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
144 masterCamera = camera.ptr();
149 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
150 setValue(masterCamera->getNode("window/name", true),
151 windowBuilder->getDefaultWindowName());
153 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
155 setValue(cgroupNode->getNode("gui/window/name", true),
156 nameNode->getStringValue());
158 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
159 Camera* guiCamera = getGUICamera(cameraGroup);
161 Viewport* guiViewport = guiCamera->getViewport();
162 fgSetInt("/sim/startup/xsize", guiViewport->width());
163 fgSetInt("/sim/startup/ysize", guiViewport->height());
165 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
166 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
167 if (wsa->windows.size() != 1) {
168 manipulator->setResizable(false);
170 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
171 viewer->addEventHandler(manipulator);
172 // Let FG handle the escape key with a confirmation
173 viewer->setKeyEventSetsDone(0);
174 // The viewer won't start without some root.
175 viewer->setSceneData(new osg::Group);
176 globals->get_renderer()->setViewer(viewer.get());
177 CameraGroup::setDefault(cameraGroup);
180 static int status = 0;
182 void fgOSExit(int code)
184 viewer->setDone(true);
185 viewer->getDatabasePager()->cancel();
191 ref_ptr<FGEventHandler> manipulator
192 = globals->get_renderer()->getEventHandler();
193 viewer->setReleaseContextAtEndOfFrameHint(false);
194 if (!viewer->isRealized())
196 while (!viewer->done()) {
197 fgIdleHandler idleFunc = manipulator->getIdleHandler();
198 fgDrawHandler drawFunc = manipulator->getDrawHandler();
208 int fgGetKeyModifiers()
210 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
213 void fgWarpMouse(int x, int y)
215 warpGUIPointer(CameraGroup::getDefault(), x, y);
218 void fgOSInit(int* argc, char** argv)
220 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
224 void fgOSFullScreen()
228 static void setMouseCursor(osg::Camera* camera, int cursor)
232 osg::GraphicsContext* gc = camera->getGraphicsContext();
235 osgViewer::GraphicsWindow* gw;
236 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
240 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
241 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
242 if (cursor == MOUSE_CURSOR_NONE)
243 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
244 else if(cursor == MOUSE_CURSOR_POINTER)
245 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
246 else if(cursor == MOUSE_CURSOR_WAIT)
247 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
248 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
249 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
250 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
251 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
252 else if(cursor == MOUSE_CURSOR_TOPSIDE)
253 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
254 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
255 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
256 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
257 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
258 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
259 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
260 else if(cursor == MOUSE_CURSOR_TOPLEFT)
261 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
262 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
263 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
264 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
265 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
266 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
267 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
269 gw->setCursor(mouseCursor);
272 static int _cursor = -1;
274 void fgSetMouseCursor(int cursor)
277 setMouseCursor(viewer->getCamera(), cursor);
278 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
279 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
282 int fgGetMouseCursor()