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 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
63 // to create the graphics window and run the event/update/render loop.
66 // fg_os implementation
70 using namespace flightgear;
73 static osg::ref_ptr<osgViewer::Viewer> viewer;
74 static osg::ref_ptr<osg::Camera> mainCamera;
76 void fgOSOpenWindow(bool stencil)
78 viewer = new osgViewer::Viewer;
79 viewer->setDatabasePager(FGScenery::getPagerSingleton());
80 CameraGroup* cameraGroup = 0;
82 mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
83 if (mode == "AutomaticSelection")
84 viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
85 else if (mode == "CullDrawThreadPerContext")
86 viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
87 else if (mode == "DrawThreadPerContext")
88 viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
89 else if (mode == "CullThreadPerCameraDrawThreadPerContext")
90 viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
92 viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
93 WindowBuilder::initWindowBuilder(stencil);
94 WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
96 // Look for windows, camera groups, and the old syntax of
98 SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
99 SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
100 bool oldSyntax = !cgroupNode->hasChild("camera");
102 for (int i = 0; i < renderingNode->nChildren(); ++i) {
103 SGPropertyNode* propNode = renderingNode->getChild(i);
104 const char* propName = propNode->getName();
105 if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
106 SGPropertyNode* copiedNode
107 = cgroupNode->getNode(propName, propNode->getIndex(), true);
108 copyProperties(propNode, copiedNode);
111 vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
112 SGPropertyNode* masterCamera = 0;
113 BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
114 if (camera->getDoubleValue("shear-x", 0.0) == 0.0
115 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
116 masterCamera = camera.ptr();
121 masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
122 setValue(masterCamera->getNode("window/name", true),
123 windowBuilder->getDefaultWindowName());
125 SGPropertyNode* nameNode = masterCamera->getNode("window/name");
127 setValue(cgroupNode->getNode("gui/window/name", true),
128 nameNode->getStringValue());
130 cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
131 Camera* guiCamera = getGUICamera(cameraGroup);
133 Viewport* guiViewport = guiCamera->getViewport();
134 fgSetInt("/sim/startup/xsize", guiViewport->width());
135 fgSetInt("/sim/startup/ysize", guiViewport->height());
137 FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
138 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
139 if (wsa->windows.size() != 1) {
140 manipulator->setResizable(false);
142 viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
143 viewer->addEventHandler(manipulator);
144 // Let FG handle the escape key with a confirmation
145 viewer->setKeyEventSetsDone(0);
146 // The viewer won't start without some root.
147 viewer->setSceneData(new osg::Group);
148 globals->get_renderer()->setViewer(viewer.get());
149 CameraGroup::setDefault(cameraGroup);
152 static int status = 0;
154 void fgOSExit(int code)
156 viewer->setDone(true);
157 viewer->getDatabasePager()->cancel();
163 ref_ptr<FGEventHandler> manipulator
164 = globals->get_renderer()->getEventHandler();
165 viewer->setReleaseContextAtEndOfFrameHint(false);
166 if (!viewer->isRealized())
168 while (!viewer->done()) {
169 fgIdleHandler idleFunc = manipulator->getIdleHandler();
170 fgDrawHandler drawFunc = manipulator->getDrawHandler();
180 int fgGetKeyModifiers()
182 return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
185 void fgWarpMouse(int x, int y)
187 warpGUIPointer(CameraGroup::getDefault(), x, y);
190 void fgOSInit(int* argc, char** argv)
192 WindowSystemAdapter::setWSA(new WindowSystemAdapter);
196 void fgOSFullScreen()
200 static void setMouseCursor(osg::Camera* camera, int cursor)
204 osg::GraphicsContext* gc = camera->getGraphicsContext();
207 osgViewer::GraphicsWindow* gw;
208 gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
212 osgViewer::GraphicsWindow::MouseCursor mouseCursor;
213 mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
214 if (cursor == MOUSE_CURSOR_NONE)
215 mouseCursor = osgViewer::GraphicsWindow::NoCursor;
216 else if(cursor == MOUSE_CURSOR_POINTER)
217 mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
218 else if(cursor == MOUSE_CURSOR_WAIT)
219 mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
220 else if(cursor == MOUSE_CURSOR_CROSSHAIR)
221 mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
222 else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
223 mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
224 else if(cursor == MOUSE_CURSOR_TOPSIDE)
225 mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
226 else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
227 mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
228 else if(cursor == MOUSE_CURSOR_LEFTSIDE)
229 mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
230 else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
231 mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
232 else if(cursor == MOUSE_CURSOR_TOPLEFT)
233 mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
234 else if(cursor == MOUSE_CURSOR_TOPRIGHT)
235 mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
236 else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
237 mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
238 else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
239 mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
241 gw->setCursor(mouseCursor);
244 static int _cursor = -1;
246 void fgSetMouseCursor(int cursor)
249 setMouseCursor(viewer->getCamera(), cursor);
250 for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
251 setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
254 int fgGetMouseCursor()