]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
Merge branch 'topic/tape' into next
[flightgear.git] / src / Main / fg_os_osgviewer.cxx
1 // fg_os_osgviewer.cxx -- common functions for fg_os interface
2 // implemented as an osgViewer
3 //
4 // Copyright (C) 2007  Tim Moore timoore@redhat.com
5 // Copyright (C) 2007 Mathias Froehlich 
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <algorithm>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29
30 #include <stdlib.h>
31
32 #include <boost/foreach.hpp>
33
34 #include <simgear/compiler.h>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/props/props_io.hxx>
38
39 #include <osg/Camera>
40 #include <osg/GraphicsContext>
41 #include <osg/Group>
42 #include <osg/Matrixd>
43 #include <osg/Viewport>
44 #include <osg/Version>
45 #include <osg/View>
46 #include <osgViewer/ViewerEventHandlers>
47 #include <osgViewer/Viewer>
48 #include <osgGA/MatrixManipulator>
49
50 #include <Include/general.hxx>
51 #include <Scenery/scenery.hxx>
52 #include "fg_os.hxx"
53 #include "fg_props.hxx"
54 #include "util.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"
61
62 #if (FG_OSG_VERSION >= 19008)
63 #define OSG_HAS_MOUSE_CURSOR_PATCH
64 #endif
65
66 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
67 // to create the graphics window and run the event/update/render loop.
68
69 //
70 // fg_os implementation
71 //
72
73 using namespace std;    
74 using namespace flightgear;
75 using namespace osg;
76
77 static osg::ref_ptr<osgViewer::Viewer> viewer;
78 static osg::ref_ptr<osg::Camera> mainCamera;
79
80 void fgOSOpenWindow(bool stencil)
81 {
82     osg::GraphicsContext::WindowingSystemInterface* wsi
83         = osg::GraphicsContext::getWindowingSystemInterface();
84
85     viewer = new osgViewer::Viewer;
86     viewer->setDatabasePager(FGScenery::getPagerSingleton());
87     CameraGroup* cameraGroup = 0;
88     std::string mode;
89     mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded");
90     if (mode == "AutomaticSelection")
91       viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection);
92     else if (mode == "CullDrawThreadPerContext")
93       viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext);
94     else if (mode == "DrawThreadPerContext")
95       viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext);
96     else if (mode == "CullThreadPerCameraDrawThreadPerContext")
97       viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext);
98     else
99       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
100     WindowBuilder::initWindowBuilder(stencil);
101     WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
102
103     // Look for windows, camera groups, and the old syntax of
104     // top-level cameras
105     SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
106     SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
107     bool oldSyntax = !cgroupNode->hasChild("camera");
108     if (oldSyntax) {
109         for (int i = 0; i < renderingNode->nChildren(); ++i) {
110             SGPropertyNode* propNode = renderingNode->getChild(i);
111             const char* propName = propNode->getName();
112             if (!strcmp(propName, "window") || !strcmp(propName, "camera")) {
113                 SGPropertyNode* copiedNode
114                     = cgroupNode->getNode(propName, propNode->getIndex(), true);
115                 copyProperties(propNode, copiedNode);
116             }
117         }
118         vector<SGPropertyNode_ptr> cameras = cgroupNode->getChildren("camera");
119         SGPropertyNode* masterCamera = 0;
120         BOOST_FOREACH(SGPropertyNode_ptr& camera, cameras) {
121             if (camera->getDoubleValue("shear-x", 0.0) == 0.0
122                 && camera->getDoubleValue("shear-y", 0.0) == 0.0) {
123                 masterCamera = camera.ptr();
124                 break;
125             }
126         }
127         if (!masterCamera) {
128             masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
129             setValue(masterCamera->getNode("window/name", true),
130                      windowBuilder->getDefaultWindowName());
131         }
132         SGPropertyNode* nameNode = masterCamera->getNode("window/name");
133         if (nameNode)
134             setValue(cgroupNode->getNode("gui/window/name", true),
135                      nameNode->getStringValue());
136     }
137     cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
138     Camera* guiCamera = getGUICamera(cameraGroup);
139     if (guiCamera) {
140         Viewport* guiViewport = guiCamera->getViewport();
141         fgSetInt("/sim/startup/xsize", guiViewport->width());
142         fgSetInt("/sim/startup/ysize", guiViewport->height());
143     }
144     FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
145     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
146     if (wsa->windows.size() != 1) {
147         manipulator->setResizable(false);
148     }
149     viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED);
150     viewer->addEventHandler(manipulator);
151     // Let FG handle the escape key with a confirmation
152     viewer->setKeyEventSetsDone(0);
153     // The viewer won't start without some root.
154     viewer->setSceneData(new osg::Group);
155     globals->get_renderer()->setViewer(viewer.get());
156     CameraGroup::setDefault(cameraGroup);
157 }
158
159 static int status = 0;
160
161 void fgOSExit(int code)
162 {
163     viewer->setDone(true);
164     viewer->getDatabasePager()->cancel();
165     status = code;
166 }
167
168 void fgOSMainLoop()
169 {
170     ref_ptr<FGEventHandler> manipulator
171         = globals->get_renderer()->getEventHandler();
172     viewer->setReleaseContextAtEndOfFrameHint(false);
173     while (!viewer->done()) {
174         fgIdleHandler idleFunc = manipulator->getIdleHandler();
175         fgDrawHandler drawFunc = manipulator->getDrawHandler();
176         if (idleFunc)
177             (*idleFunc)();
178         if (drawFunc)
179             (*drawFunc)();
180         viewer->frame();
181     }
182     fgExit(status);
183 }
184
185 int fgGetKeyModifiers()
186 {
187     return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
188 }
189
190 void fgWarpMouse(int x, int y)
191 {
192     warpGUIPointer(CameraGroup::getDefault(), x, y);
193 }
194
195 void fgOSInit(int* argc, char** argv)
196 {
197     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
198 }
199
200 // Noop
201 void fgOSFullScreen()
202 {
203 }
204
205 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
206 static void setMouseCursor(osg::Camera* camera, int cursor)
207 {
208     if (!camera)
209         return;
210     osg::GraphicsContext* gc = camera->getGraphicsContext();
211     if (!gc)
212         return;
213     osgViewer::GraphicsWindow* gw;
214     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
215     if (!gw)
216         return;
217     
218     osgViewer::GraphicsWindow::MouseCursor mouseCursor;
219     mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
220     if     (cursor == MOUSE_CURSOR_NONE)
221         mouseCursor = osgViewer::GraphicsWindow::NoCursor;
222     else if(cursor == MOUSE_CURSOR_POINTER)
223         mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
224     else if(cursor == MOUSE_CURSOR_WAIT)
225         mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
226     else if(cursor == MOUSE_CURSOR_CROSSHAIR)
227         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
228     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
229         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
230     else if(cursor == MOUSE_CURSOR_TOPSIDE)
231         mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
232     else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
233         mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
234     else if(cursor == MOUSE_CURSOR_LEFTSIDE)
235         mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
236     else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
237         mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
238     else if(cursor == MOUSE_CURSOR_TOPLEFT)
239         mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
240     else if(cursor == MOUSE_CURSOR_TOPRIGHT)
241         mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
242     else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
243         mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
244     else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
245         mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
246
247     gw->setCursor(mouseCursor);
248 }
249 #endif
250
251 static int _cursor = -1;
252
253 void fgSetMouseCursor(int cursor)
254 {
255     _cursor = cursor;
256 #ifdef OSG_HAS_MOUSE_CURSOR_PATCH
257     setMouseCursor(viewer->getCamera(), cursor);
258     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
259         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
260 #endif
261 }
262
263 int fgGetMouseCursor()
264 {
265     return _cursor;
266 }