Merged in f5soh/librepilot/LP-575_fedora_package (pull request #491)
[librepilot.git] / ground / gcs / src / libs / osgearth / osgearth.cpp
blobce1ab407a22389326f1c235fa509093f4d705aa8
1 /**
2 ******************************************************************************
4 * @file osgearth.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * @addtogroup
7 * @{
8 * @addtogroup
9 * @{
10 * @brief
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "osgearth.h"
30 #include "utils/utility.h"
31 #include "utils/qtwindowingsystem.h"
33 #include "utils/pathutils.h"
35 #include <osg/DisplaySettings>
36 #include <osg/GraphicsContext>
37 #include <osg/Version>
38 #include <osg/Notify>
39 #include <osgDB/Registry>
41 #ifdef USE_OSGEARTH
42 #include <osgEarth/Version>
43 #include <osgEarth/Cache>
44 #include <osgEarth/Capabilities>
45 #include <osgEarth/Registry>
46 #include <osgEarthDrivers/cache_filesystem/FileSystemCache>
47 #endif
49 #include <QDebug>
51 #ifdef OSG_USE_QT_PRIVATE
52 #include <QtGui/private/qguiapplication_p.h>
53 #include <QtGui/qpa/qplatformintegration.h>
54 #endif
56 #include <deque>
58 bool OsgEarth::registered = false;
59 bool OsgEarth::initialized = false;
61 class OSGEARTH_LIB_EXPORT QtNotifyHandler : public osg::NotifyHandler {
62 public:
63 void notify(osg::NotifySeverity severity, const char *message);
66 void OsgEarth::registerQmlTypes()
68 // TOOO not thread safe...
69 if (registered) {
70 return;
72 registered = true;
74 // use "export OSG_NOTIFY_LEVEL=DEBUG" on command line to enable osg logging
75 // redirect osg logging to Qt
76 osg::setNotifyHandler(new QtNotifyHandler());
78 // initialize();
80 // Register Qml types
81 qDebug() << "OsgEarth::registerQmlTypes - registering Qml types...";
82 osgQtQuick::registerTypes();
85 void OsgEarth::initialize()
87 // TOOO not thread safe...
88 if (initialized) {
89 return;
91 initialized = true;
93 qDebug() << "OsgEarth::initialize - initializing osgearth...";
95 // increase cache (default is 300);
96 // setenv("OSG_MAX_PAGEDLOD", "500", 0);
98 // setenv("OSG_ASSIGN_PBO_TO_IMAGES", "on", 0);
100 // Number of threads in the DatbasePager set up, inclusive of the number of http dedicated threads.
101 osg::DisplaySettings::instance()->setNumOfDatabaseThreadsHint(8);
102 osg::DisplaySettings::instance()->setNumOfHttpDatabaseThreadsHint(4);
104 initializePathes();
106 initWindowingSystem();
108 initializeCache();
110 #ifdef OSG_VERBOSE
111 displayInfo();
112 #endif
115 void OsgEarth::initializePathes()
117 // initialize data file path list
118 osgDB::FilePathList &dataFilePathList = osgDB::Registry::instance()->getDataFilePathList();
120 dataFilePathList.push_front((Utils::GetDataPath() + "osgearth").toStdString());
121 dataFilePathList.push_front((Utils::GetDataPath() + "osgearth/data").toStdString());
123 // initialize library file path list
124 osgDB::FilePathList &libraryFilePathList = osgDB::Registry::instance()->getLibraryFilePathList();
125 libraryFilePathList.push_front((Utils::GetLibraryPath() + "osg").toStdString());
128 void OsgEarth::initializeCache()
130 #ifdef USE_OSGEARTH
132 QString cachePath = Utils::GetStoragePath() + "osgearth/cache";
134 qputenv("OSGEARTH_CACHE_PATH", cachePath.toLatin1());
136 const osgEarth::CachePolicy cachePolicy(osgEarth::CachePolicy::USAGE_READ_WRITE);
138 // The default cache policy used when no policy is set elsewhere
139 osgEarth::Registry::instance()->setDefaultCachePolicy(cachePolicy);
141 // The override cache policy (overrides all others if set)
142 // osgEarth::Registry::instance()->setOverrideCachePolicy(cachePolicy);
144 #endif // ifdef USE_OSGEARTH
147 void OsgEarth::displayInfo()
149 qDebug() << "Using osg version :" << osgGetVersion();
150 #ifdef USE_OSGEARTH
151 qDebug() << "Using osgEarth version :" << osgEarthGetVersion();
152 #endif
154 // library file path list
155 osgDB::FilePathList &libraryFilePathList = osgDB::Registry::instance()->getLibraryFilePathList();
156 osgDB::FilePathList::iterator it = libraryFilePathList.begin();
157 while (it != libraryFilePathList.end()) {
158 qDebug() << "osg library file path:" << QString::fromStdString(*it);
159 it++;
162 // data file path list
163 osgDB::FilePathList &dataFilePathList = osgDB::Registry::instance()->getDataFilePathList();
164 it = dataFilePathList.begin();
165 while (it != dataFilePathList.end()) {
166 qDebug() << "osg data file path:" << QString::fromStdString(*it);
167 it++;
170 qDebug() << "osg database threads:" << osg::DisplaySettings::instance()->getNumOfDatabaseThreadsHint();
171 qDebug() << "osg http database threads:" << osg::DisplaySettings::instance()->getNumOfHttpDatabaseThreadsHint();
173 // qDebug() << "Platform supports GLSL:" << osgEarth::Registry::capabilities().supportsGLSL();
175 #ifdef OSG_USE_QT_PRIVATE
176 bool threadedOpenGL = QGuiApplicationPrivate::platform_integration->hasCapability(QPlatformIntegration::ThreadedOpenGL);
177 qDebug() << "Platform supports threaded OpenGL:" << threadedOpenGL;
178 #endif
180 #ifdef USE_OSGEARTH
181 osgQtQuick::capabilitiesInfo(osgEarth::Registry::capabilities());
182 #endif
185 void QtNotifyHandler::notify(osg::NotifySeverity severity, const char *message)
187 QString msg(message);
189 // right trim message...
190 int n = msg.size() - 1;
192 for (; n >= 0; --n) {
193 if (!msg.at(n).isSpace()) {
194 break;
197 msg = msg.left(n + 1);
199 switch (severity) {
200 case osg::ALWAYS:
201 qDebug().noquote() << "[OSG]" << msg;
202 break;
203 case osg::FATAL:
204 qCritical().noquote() << "[OSG FATAL]" << msg;
205 break;
206 case osg::WARN:
207 qWarning().noquote() << "[OSG WARN]" << msg;
208 break;
209 case osg::NOTICE:
210 qDebug().noquote() << "[OSG NOTICE]" << msg;
211 break;
212 case osg::INFO:
213 qDebug().noquote() << "[OSG]" << msg;
214 break;
215 case osg::DEBUG_INFO:
216 qDebug().noquote() << "[OSG DEBUG INFO]" << msg;
217 break;
218 case osg::DEBUG_FP:
219 qDebug().noquote() << "[OSG DEBUG FP]" << msg;
220 break;
224 #if OSG_VERSION_GREATER_OR_EQUAL(3, 5, 3)
225 REGISTER_WINDOWINGSYSTEMINTERFACE(MyQt, QtWindowingSystem)
226 #endif
228 void OsgEarth::initWindowingSystem()
230 #if OSG_VERSION_LESS_THAN(3, 5, 3)
231 osg::GraphicsContext::setWindowingSystemInterface(QtWindowingSystem::getInterface());
232 #endif