]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_osgviewer.cxx
Merge branch 'ehofman/particle' 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 // Static linking of OSG needs special macros
63 #ifdef OSG_LIBRARY_STATIC
64 #include <osgDB/Registry>
65 USE_GRAPHICSWINDOW();
66 // Image formats
67 USE_OSGPLUGIN(bmp);
68 USE_OSGPLUGIN(dds);
69 USE_OSGPLUGIN(hdr);
70 USE_OSGPLUGIN(pic);
71 USE_OSGPLUGIN(pnm);
72 USE_OSGPLUGIN(rgb);
73 USE_OSGPLUGIN(tga);
74 #ifdef OSG_JPEG_ENABLED
75   USE_OSGPLUGIN(jpeg);
76 #endif
77 #ifdef OSG_PNG_ENABLED
78   USE_OSGPLUGIN(png);
79 #endif
80 #ifdef OSG_TIFF_ENABLED
81   USE_OSGPLUGIN(tiff);
82 #endif
83 // Model formats
84 USE_OSGPLUGIN(3ds);
85 USE_OSGPLUGIN(ac);
86 USE_OSGPLUGIN(ive);
87 USE_OSGPLUGIN(osg);
88 USE_OSGPLUGIN(txf);
89 #endif
90
91 // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class
92 // to create the graphics window and run the event/update/render loop.
93
94 //
95 // fg_os implementation
96 //
97
98 using namespace std;    
99 using namespace flightgear;
100 using namespace osg;
101
102 static osg::ref_ptr<osgViewer::Viewer> viewer;
103 static osg::ref_ptr<osg::Camera> mainCamera;
104
105 void fgOSOpenWindow(bool stencil)
106 {
107     viewer = new osgViewer::Viewer;
108     viewer->setDatabasePager(FGScenery::getPagerSingleton());
109     CameraGroup* cameraGroup = 0;
110     std::string mode;
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);
120     else
121       viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
122     WindowBuilder::initWindowBuilder(stencil);
123     WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder();
124
125     // Look for windows, camera groups, and the old syntax of
126     // top-level cameras
127     SGPropertyNode* renderingNode = fgGetNode("/sim/rendering");
128     SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true);
129     bool oldSyntax = !cgroupNode->hasChild("camera");
130     if (oldSyntax) {
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);
138             }
139         }
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();
146                 break;
147             }
148         }
149         if (!masterCamera) {
150             masterCamera = cgroupNode->getChild("camera", cameras.size(), true);
151             setValue(masterCamera->getNode("window/name", true),
152                      windowBuilder->getDefaultWindowName());
153         }
154         SGPropertyNode* nameNode = masterCamera->getNode("window/name");
155         if (nameNode)
156             setValue(cgroupNode->getNode("gui/window/name", true),
157                      nameNode->getStringValue());
158     }
159     cameraGroup = CameraGroup::buildCameraGroup(viewer.get(), cgroupNode);
160     Camera* guiCamera = getGUICamera(cameraGroup);
161     if (guiCamera) {
162         Viewport* guiViewport = guiCamera->getViewport();
163         fgSetInt("/sim/startup/xsize", guiViewport->width());
164         fgSetInt("/sim/startup/ysize", guiViewport->height());
165     }
166     FGEventHandler* manipulator = globals->get_renderer()->getEventHandler();
167     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
168     if (wsa->windows.size() != 1) {
169         manipulator->setResizable(false);
170     }
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);
179 }
180
181 static int status = 0;
182
183 void fgOSExit(int code)
184 {
185     viewer->setDone(true);
186     viewer->getDatabasePager()->cancel();
187     status = code;
188 }
189
190 void fgOSMainLoop()
191 {
192     ref_ptr<FGEventHandler> manipulator
193         = globals->get_renderer()->getEventHandler();
194     viewer->setReleaseContextAtEndOfFrameHint(false);
195     if (!viewer->isRealized())
196         viewer->realize();
197     while (!viewer->done()) {
198         fgIdleHandler idleFunc = manipulator->getIdleHandler();
199         fgDrawHandler drawFunc = manipulator->getDrawHandler();
200         if (idleFunc)
201             (*idleFunc)();
202         if (drawFunc)
203             (*drawFunc)();
204         viewer->frame();
205     }
206     fgExit(status);
207 }
208
209 int fgGetKeyModifiers()
210 {
211     return globals->get_renderer()->getEventHandler()->getCurrentModifiers();
212 }
213
214 void fgWarpMouse(int x, int y)
215 {
216     warpGUIPointer(CameraGroup::getDefault(), x, y);
217 }
218
219 void fgOSInit(int* argc, char** argv)
220 {
221     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
222 }
223
224 // Noop
225 void fgOSFullScreen()
226 {
227 }
228
229 static void setMouseCursor(osg::Camera* camera, int cursor)
230 {
231     if (!camera)
232         return;
233     osg::GraphicsContext* gc = camera->getGraphicsContext();
234     if (!gc)
235         return;
236     osgViewer::GraphicsWindow* gw;
237     gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc);
238     if (!gw)
239         return;
240     
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;
269
270     gw->setCursor(mouseCursor);
271 }
272
273 static int _cursor = -1;
274
275 void fgSetMouseCursor(int cursor)
276 {
277     _cursor = cursor;
278     setMouseCursor(viewer->getCamera(), cursor);
279     for (unsigned i = 0; i < viewer->getNumSlaves(); ++i)
280         setMouseCursor(viewer->getSlave(i)._camera.get(), cursor);
281 }
282
283 int fgGetMouseCursor()
284 {
285     return _cursor;
286 }