]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
9749645f7c706a32318f4346114792f79e7cabaf
[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 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
63 // to create the graphics window and run the event/update/render loop.
64
65 //
66 // fg_os implementation
67 //
68
69 using namespace std;    
70 using namespace flightgear;
71 using namespace osg;
72
73 static osg::ref_ptr<osgViewer::Viewer> viewer;
74 static osg::ref_ptr<osg::Camera> mainCamera;
75
76 void fgOSOpenWindow(bool stencil)
77 {
78     viewer = new osgViewer::Viewer;
79     viewer->setDatabasePager(FGScenery::getPagerSingleton());
80     CameraGroup* cameraGroup = 0;
81     std::string mode;
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);
91     else
92       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
93     WindowBuilder::initWindowBuilder(stencil);
94     WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
95
96     // Look for windows, camera groups, and the old syntax of
97     // top-level cameras
98     SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
99     SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
100     bool oldSyntax = !cgroupNode->hasChild("camera");
101     if (oldSyntax) {
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);
109             }
110         }
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();
117                 break;
118             }
119         }
120         if (!masterCamera) {
121             masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
122             setValue(masterCamera->getNode("window/name", true),
123                      windowBuilder->getDefaultWindowName());
124         }
125         SGPropertyNode* nameNode = masterCamera->getNode("window/name");
126         if (nameNode)
127             setValue(cgroupNode->getNode("gui/window/name", true),
128                      nameNode->getStringValue());
129     }
130     cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
131     Camera* guiCamera = getGUICamera(cameraGroup);
132     if (guiCamera) {
133         Viewport* guiViewport = guiCamera->getViewport();
134         fgSetInt("/sim/startup/xsize", guiViewport->width());
135         fgSetInt("/sim/startup/ysize", guiViewport->height());
136     }
137     FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
138     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
139     if (wsa->windows.size() != 1) {
140         manipulator->setResizable(false);
141     }
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);
150     viewer->setLightingMode(View::NO_LIGHT);
151 }
152
153 static int status = 0;
154
155 void fgOSExit(int code)
156 {
157     viewer->setDone(true);
158     viewer->getDatabasePager()->cancel();
159     status = code;
160 }
161
162 void fgOSMainLoop()
163 {
164     ref_ptr<FGEventHandler> manipulator
165         = globals->get_renderer()->getEventHandler();
166     viewer->setReleaseContextAtEndOfFrameHint(false);
167     if (!viewer->isRealized())
168         viewer->realize();
169     while (!viewer->done()) {
170         fgIdleHandler idleFunc = manipulator->getIdleHandler();
171         fgDrawHandler drawFunc = manipulator->getDrawHandler();
172         if (idleFunc)
173             (*idleFunc)();
174         if (drawFunc)
175             (*drawFunc)();
176         viewer->frame();
177     }
178     fgExit(status);
179 }
180
181 int fgGetKeyModifiers()
182 {
183     return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
184 }
185
186 void fgWarpMouse(int x, int y)
187 {
188     warpGUIPointer(CameraGroup::getDefault(), x, y);
189 }
190
191 void fgOSInit(int* argc, char** argv)
192 {
193     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
194 }
195
196 // Noop
197 void fgOSFullScreen()
198 {
199 }
200
201 static void setMouseCursor(osg::Camera* camera, int cursor)
202 {
203     if (!camera)
204         return;
205     osg::GraphicsContext* gc = camera->getGraphicsContext();
206     if (!gc)
207         return;
208     osgViewer::GraphicsWindow* gw;
209     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
210     if (!gw)
211         return;
212     
213     osgViewer::GraphicsWindow::MouseCursor mouseCursor;
214     mouseCursor = osgViewer::GraphicsWindow::InheritCursor;
215     if     (cursor == MOUSE_CURSOR_NONE)
216         mouseCursor = osgViewer::GraphicsWindow::NoCursor;
217     else if(cursor == MOUSE_CURSOR_POINTER)
218         mouseCursor = osgViewer::GraphicsWindow::RightArrowCursor;
219     else if(cursor == MOUSE_CURSOR_WAIT)
220         mouseCursor = osgViewer::GraphicsWindow::WaitCursor;
221     else if(cursor == MOUSE_CURSOR_CROSSHAIR)
222         mouseCursor = osgViewer::GraphicsWindow::CrosshairCursor;
223     else if(cursor == MOUSE_CURSOR_LEFTRIGHT)
224         mouseCursor = osgViewer::GraphicsWindow::LeftRightCursor;
225     else if(cursor == MOUSE_CURSOR_TOPSIDE)
226         mouseCursor = osgViewer::GraphicsWindow::TopSideCursor;
227     else if(cursor == MOUSE_CURSOR_BOTTOMSIDE)
228         mouseCursor = osgViewer::GraphicsWindow::BottomSideCursor;
229     else if(cursor == MOUSE_CURSOR_LEFTSIDE)
230         mouseCursor = osgViewer::GraphicsWindow::LeftSideCursor;
231     else if(cursor == MOUSE_CURSOR_RIGHTSIDE)
232         mouseCursor = osgViewer::GraphicsWindow::RightSideCursor;
233     else if(cursor == MOUSE_CURSOR_TOPLEFT)
234         mouseCursor = osgViewer::GraphicsWindow::TopLeftCorner;
235     else if(cursor == MOUSE_CURSOR_TOPRIGHT)
236         mouseCursor = osgViewer::GraphicsWindow::TopRightCorner;
237     else if(cursor == MOUSE_CURSOR_BOTTOMLEFT)
238         mouseCursor = osgViewer::GraphicsWindow::BottomLeftCorner;
239     else if(cursor == MOUSE_CURSOR_BOTTOMRIGHT)
240         mouseCursor = osgViewer::GraphicsWindow::BottomRightCorner;
241
242     gw->setCursor(mouseCursor);
243 }
244
245 static int _cursor = -1;
246
247 void fgSetMouseCursor(int cursor)
248 {
249     _cursor = cursor;
250     setMouseCursor(viewer->getCamera(), cursor);
251     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
252         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
253 }
254
255 int fgGetMouseCursor()
256 {
257     return _cursor;
258 }