| 1 | // renderer.cxx -- top level sim routines |
| 2 | // |
| 3 | // Written by Curtis Olson, started May 1997. |
| 4 | // This file contains parts of main.cxx prior to october 2004 |
| 5 | // |
| 6 | // Copyright (C) 1997 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | |
| 22 | #include <config.h> |
| 23 | |
| 24 | #ifdef HAVE_WINDOWS_H |
| 25 | # include <windows.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <simgear/compiler.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <iostream> |
| 32 | #include <map> |
| 33 | #include <vector> |
| 34 | #include <typeinfo> |
| 35 | |
| 36 | #include <osg/ref_ptr> |
| 37 | #include <osg/AlphaFunc> |
| 38 | #include <osg/BlendFunc> |
| 39 | #include <osg/Camera> |
| 40 | #include <osg/CullFace> |
| 41 | #include <osg/CullStack> |
| 42 | #include <osg/Depth> |
| 43 | #include <osg/Fog> |
| 44 | #include <osg/Group> |
| 45 | #include <osg/Hint> |
| 46 | #include <osg/Light> |
| 47 | #include <osg/LightModel> |
| 48 | #include <osg/LightSource> |
| 49 | #include <osg/Material> |
| 50 | #include <osg/Math> |
| 51 | #include <osg/NodeCallback> |
| 52 | #include <osg/Notify> |
| 53 | #include <osg/PolygonMode> |
| 54 | #include <osg/PolygonOffset> |
| 55 | #include <osg/Program> |
| 56 | #include <osg/Version> |
| 57 | #include <osg/TexEnv> |
| 58 | |
| 59 | #include <osgUtil/LineSegmentIntersector> |
| 60 | |
| 61 | #include <osg/io_utils> |
| 62 | #include <osgDB/WriteFile> |
| 63 | #include <osgViewer/Renderer> |
| 64 | |
| 65 | #include <simgear/scene/material/matlib.hxx> |
| 66 | #include <simgear/scene/material/EffectCullVisitor.hxx> |
| 67 | #include <simgear/scene/material/Effect.hxx> |
| 68 | #include <simgear/scene/material/EffectGeode.hxx> |
| 69 | #include <simgear/scene/material/EffectBuilder.hxx> |
| 70 | #include <simgear/scene/model/animation.hxx> |
| 71 | #include <simgear/scene/model/placement.hxx> |
| 72 | #include <simgear/scene/sky/sky.hxx> |
| 73 | #include <simgear/scene/util/DeletionManager.hxx> |
| 74 | #include <simgear/scene/util/SGUpdateVisitor.hxx> |
| 75 | #include <simgear/scene/util/RenderConstants.hxx> |
| 76 | #include <simgear/scene/util/SGSceneUserData.hxx> |
| 77 | #include <simgear/scene/util/OsgUtils.hxx> |
| 78 | #include <simgear/scene/tgdb/GroundLightManager.hxx> |
| 79 | #include <simgear/scene/tgdb/pt_lights.hxx> |
| 80 | #include <simgear/scene/tgdb/userdata.hxx> |
| 81 | #include <simgear/props/props.hxx> |
| 82 | #include <simgear/timing/sg_time.hxx> |
| 83 | #include <simgear/ephemeris/ephemeris.hxx> |
| 84 | #include <simgear/math/sg_random.hxx> |
| 85 | |
| 86 | #include <Time/light.hxx> |
| 87 | #include <Time/light.hxx> |
| 88 | #include <Cockpit/panel.hxx> |
| 89 | |
| 90 | #include <Model/panelnode.hxx> |
| 91 | #include <Model/modelmgr.hxx> |
| 92 | #include <Model/acmodel.hxx> |
| 93 | #include <Scenery/scenery.hxx> |
| 94 | #include <Scenery/redout.hxx> |
| 95 | #include <GUI/new_gui.hxx> |
| 96 | #include <GUI/gui.h> |
| 97 | #include <GUI/Highlight.hxx> |
| 98 | |
| 99 | #ifdef ENABLE_HUD |
| 100 | # include <Instrumentation/HUD/HUD.hxx> |
| 101 | #endif |
| 102 | #include <Environment/precipitation_mgr.hxx> |
| 103 | #include <Environment/environment_mgr.hxx> |
| 104 | #include <Environment/ephemeris.hxx> |
| 105 | |
| 106 | //#include <Main/main.hxx> |
| 107 | #include "view.hxx" |
| 108 | #include "viewmgr.hxx" |
| 109 | #include "splash.hxx" |
| 110 | #include "renderer.hxx" |
| 111 | #include "CameraGroup.hxx" |
| 112 | #include "FGEventHandler.hxx" |
| 113 | #include <Main/sentryIntegration.hxx> |
| 114 | |
| 115 | #if defined(ENABLE_QQ_UI) |
| 116 | #include <GUI/QQuickDrawable.hxx> |
| 117 | #endif |
| 118 | |
| 119 | #if defined(HAVE_PUI) |
| 120 | #include <Viewer/PUICamera.hxx> |
| 121 | #endif |
| 122 | |
| 123 | using namespace osg; |
| 124 | using namespace flightgear; |
| 125 | |
| 126 | class FGHintUpdateCallback : public osg::StateAttribute::Callback { |
| 127 | public: |
| 128 | FGHintUpdateCallback(const char* configNode) : |
| 129 | mConfigNode(fgGetNode(configNode, true)) |
| 130 | { } |
| 131 | virtual void operator()(osg::StateAttribute* stateAttribute, |
| 132 | osg::NodeVisitor*) |
| 133 | { |
| 134 | assert(dynamic_cast<osg::Hint*>(stateAttribute)); |
| 135 | osg::Hint* hint = static_cast<osg::Hint*>(stateAttribute); |
| 136 | |
| 137 | std::string value = mConfigNode->getStringValue(); |
| 138 | if (value.empty()) |
| 139 | hint->setMode(GL_DONT_CARE); |
| 140 | else if (value == "nicest") |
| 141 | hint->setMode(GL_NICEST); |
| 142 | else if (value == "fastest") |
| 143 | hint->setMode(GL_FASTEST); |
| 144 | else |
| 145 | hint->setMode(GL_DONT_CARE); |
| 146 | } |
| 147 | private: |
| 148 | SGPropertyNode_ptr mConfigNode; |
| 149 | }; |
| 150 | |
| 151 | #ifdef ENABLE_HUD |
| 152 | |
| 153 | class SGHUDDrawable : public osg::Drawable { |
| 154 | public: |
| 155 | SGHUDDrawable() |
| 156 | { |
| 157 | // Dynamic stuff, do not store geometry |
| 158 | setUseDisplayList(false); |
| 159 | setDataVariance(Object::DYNAMIC); |
| 160 | |
| 161 | osg::StateSet* stateSet = getOrCreateStateSet(); |
| 162 | stateSet->setRenderBinDetails(1000, "RenderBin"); |
| 163 | |
| 164 | // speed optimization? |
| 165 | stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); |
| 166 | stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA)); |
| 167 | stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); |
| 168 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 169 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
| 170 | |
| 171 | stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE)); |
| 172 | } |
| 173 | virtual void drawImplementation(osg::RenderInfo& renderInfo) const |
| 174 | { drawImplementation(*renderInfo.getState()); } |
| 175 | void drawImplementation(osg::State& state) const |
| 176 | { |
| 177 | state.setActiveTextureUnit(0); |
| 178 | state.setClientActiveTextureUnit(0); |
| 179 | state.disableAllVertexArrays(); |
| 180 | |
| 181 | glPushAttrib(GL_ALL_ATTRIB_BITS); |
| 182 | glPushClientAttrib(~0u); |
| 183 | |
| 184 | // HUD can be NULL |
| 185 | auto hud = globals->get_subsystem<HUD>(); |
| 186 | if (hud) { |
| 187 | hud->draw(state); |
| 188 | } |
| 189 | |
| 190 | glPopClientAttrib(); |
| 191 | glPopAttrib(); |
| 192 | } |
| 193 | |
| 194 | virtual osg::Object* cloneType() const { return new SGHUDDrawable; } |
| 195 | virtual osg::Object* clone(const osg::CopyOp&) const { return new SGHUDDrawable; } |
| 196 | |
| 197 | private: |
| 198 | }; |
| 199 | |
| 200 | #endif |
| 201 | |
| 202 | class FGLightSourceUpdateCallback : public osg::NodeCallback { |
| 203 | public: |
| 204 | |
| 205 | /** |
| 206 | * @param isSun true if the light is the actual sun i.e., for |
| 207 | * illuminating the moon. |
| 208 | */ |
| 209 | FGLightSourceUpdateCallback(bool isSun = false) : _isSun(isSun) {} |
| 210 | FGLightSourceUpdateCallback(const FGLightSourceUpdateCallback& nc, |
| 211 | const CopyOp& op) |
| 212 | : NodeCallback(nc, op), _isSun(nc._isSun) |
| 213 | {} |
| 214 | META_Object(flightgear,FGLightSourceUpdateCallback); |
| 215 | |
| 216 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
| 217 | { |
| 218 | assert(dynamic_cast<osg::LightSource*>(node)); |
| 219 | osg::LightSource* lightSource = static_cast<osg::LightSource*>(node); |
| 220 | osg::Light* light = lightSource->getLight(); |
| 221 | |
| 222 | auto l = globals->get_subsystem<FGLight>(); |
| 223 | if (!l) { |
| 224 | // lighting is down during re-init |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (_isSun) { |
| 229 | light->setAmbient(Vec4(0.0f, 0.0f, 0.0f, 0.0f)); |
| 230 | light->setDiffuse(Vec4(1.0f, 1.0f, 1.0f, 1.0f)); |
| 231 | light->setSpecular(Vec4(0.0f, 0.0f, 0.0f, 0.0f)); |
| 232 | } else { |
| 233 | light->setAmbient(toOsg(l->scene_ambient())); |
| 234 | light->setDiffuse(toOsg(l->scene_diffuse())); |
| 235 | light->setSpecular(toOsg(l->scene_specular())); |
| 236 | } |
| 237 | osg::Vec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0); |
| 238 | light->setPosition(position); |
| 239 | |
| 240 | traverse(node, nv); |
| 241 | } |
| 242 | private: |
| 243 | const bool _isSun; |
| 244 | }; |
| 245 | |
| 246 | class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback { |
| 247 | public: |
| 248 | FGWireFrameModeUpdateCallback() : |
| 249 | mWireframe(fgGetNode("/sim/rendering/wireframe", true)) |
| 250 | { } |
| 251 | virtual void operator()(osg::StateAttribute* stateAttribute, |
| 252 | osg::NodeVisitor*) |
| 253 | { |
| 254 | assert(dynamic_cast<osg::PolygonMode*>(stateAttribute)); |
| 255 | osg::PolygonMode* polygonMode; |
| 256 | polygonMode = static_cast<osg::PolygonMode*>(stateAttribute); |
| 257 | |
| 258 | if (mWireframe->getBoolValue()) |
| 259 | polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, |
| 260 | osg::PolygonMode::LINE); |
| 261 | else |
| 262 | polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, |
| 263 | osg::PolygonMode::FILL); |
| 264 | } |
| 265 | private: |
| 266 | SGPropertyNode_ptr mWireframe; |
| 267 | }; |
| 268 | |
| 269 | class FGLightModelUpdateCallback : public osg::StateAttribute::Callback { |
| 270 | public: |
| 271 | FGLightModelUpdateCallback() : |
| 272 | mHighlights(fgGetNode("/sim/rendering/specular-highlight", true)) |
| 273 | { } |
| 274 | virtual void operator()(osg::StateAttribute* stateAttribute, |
| 275 | osg::NodeVisitor*) |
| 276 | { |
| 277 | assert(dynamic_cast<osg::LightModel*>(stateAttribute)); |
| 278 | osg::LightModel* lightModel; |
| 279 | lightModel = static_cast<osg::LightModel*>(stateAttribute); |
| 280 | |
| 281 | #if 0 |
| 282 | auto l = globals->get_subsystem<FGLight>(); |
| 283 | lightModel->setAmbientIntensity(toOsg(l->scene_ambient()); |
| 284 | #else |
| 285 | lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1)); |
| 286 | #endif |
| 287 | lightModel->setTwoSided(true); |
| 288 | lightModel->setLocalViewer(false); |
| 289 | |
| 290 | if (mHighlights->getBoolValue()) { |
| 291 | lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR); |
| 292 | } else { |
| 293 | lightModel->setColorControl(osg::LightModel::SINGLE_COLOR); |
| 294 | } |
| 295 | } |
| 296 | private: |
| 297 | SGPropertyNode_ptr mHighlights; |
| 298 | }; |
| 299 | |
| 300 | class FGFogEnableUpdateCallback : public osg::StateSet::Callback { |
| 301 | public: |
| 302 | FGFogEnableUpdateCallback() : |
| 303 | mFogEnabled(fgGetNode("/sim/rendering/fog", true)) |
| 304 | { } |
| 305 | virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*) |
| 306 | { |
| 307 | if (mFogEnabled->getStringValue() == "disabled") { |
| 308 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 309 | } else { |
| 310 | stateSet->setMode(GL_FOG, osg::StateAttribute::ON); |
| 311 | } |
| 312 | } |
| 313 | private: |
| 314 | SGPropertyNode_ptr mFogEnabled; |
| 315 | }; |
| 316 | |
| 317 | class FGFogUpdateCallback : public osg::StateAttribute::Callback { |
| 318 | public: |
| 319 | virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv) |
| 320 | { |
| 321 | assert(dynamic_cast<SGUpdateVisitor*>(nv)); |
| 322 | assert(dynamic_cast<osg::Fog*>(sa)); |
| 323 | SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv); |
| 324 | osg::Fog* fog = static_cast<osg::Fog*>(sa); |
| 325 | fog->setMode(osg::Fog::EXP2); |
| 326 | fog->setColor(toOsg(updateVisitor->getFogColor())); |
| 327 | fog->setDensity(updateVisitor->getFogExp2Density()); |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | // update callback for the switch node guarding that splash |
| 332 | class FGScenerySwitchCallback : public osg::NodeCallback { |
| 333 | public: |
| 334 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
| 335 | { |
| 336 | assert(dynamic_cast<osg::Switch*>(node)); |
| 337 | osg::Switch* sw = static_cast<osg::Switch*>(node); |
| 338 | |
| 339 | bool enabled = scenery_enabled; |
| 340 | sw->setValue(0, enabled); |
| 341 | if (!enabled) |
| 342 | return; |
| 343 | traverse(node, nv); |
| 344 | } |
| 345 | |
| 346 | static bool scenery_enabled; |
| 347 | }; |
| 348 | |
| 349 | bool FGScenerySwitchCallback::scenery_enabled = false; |
| 350 | |
| 351 | FGRenderer::FGRenderer() : |
| 352 | _sky(NULL), |
| 353 | MaximumTextureSize(0), |
| 354 | _splash(nullptr) |
| 355 | { |
| 356 | _root = new osg::Group; |
| 357 | _root->setName("fakeRoot"); |
| 358 | _updateVisitor = new SGUpdateVisitor; |
| 359 | } |
| 360 | |
| 361 | FGRenderer::FGRenderer(osg::ref_ptr<osgViewer::CompositeViewer> composite_viewer) : |
| 362 | composite_viewer(composite_viewer), |
| 363 | _sky(NULL), |
| 364 | MaximumTextureSize(0) |
| 365 | { |
| 366 | _root = new osg::Group; |
| 367 | _root->setName("fakeRoot"); |
| 368 | _updateVisitor = new SGUpdateVisitor; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | FGRenderer::~FGRenderer() |
| 373 | { |
| 374 | SGPropertyChangeListenerVec::iterator i = _listeners.begin(); |
| 375 | for (; i != _listeners.end(); ++i) { |
| 376 | delete *i; |
| 377 | } |
| 378 | |
| 379 | // replace the viewer's scene completely |
| 380 | if (getView()) { |
| 381 | getView()->setSceneData(new osg::Group); |
| 382 | } |
| 383 | |
| 384 | delete _sky; |
| 385 | } |
| 386 | |
| 387 | class PointSpriteListener : public SGPropertyChangeListener { |
| 388 | public: |
| 389 | virtual void valueChanged(SGPropertyNode* node) { |
| 390 | static SGSceneFeatures* sceneFeatures = SGSceneFeatures::instance(); |
| 391 | sceneFeatures->setEnablePointSpriteLights(node->getIntValue()); |
| 392 | } |
| 393 | }; |
| 394 | |
| 395 | class DistanceAttenuationListener : public SGPropertyChangeListener { |
| 396 | public: |
| 397 | virtual void valueChanged(SGPropertyNode* node) { |
| 398 | static SGSceneFeatures* sceneFeatures = SGSceneFeatures::instance(); |
| 399 | sceneFeatures->setEnableDistanceAttenuationLights(node->getIntValue()); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | class DirectionalLightsListener : public SGPropertyChangeListener { |
| 404 | public: |
| 405 | virtual void valueChanged(SGPropertyNode* node) { |
| 406 | static SGSceneFeatures* sceneFeatures = SGSceneFeatures::instance(); |
| 407 | sceneFeatures->setEnableTriangleDirectionalLights(node->getIntValue()); |
| 408 | } |
| 409 | }; |
| 410 | |
| 411 | void |
| 412 | FGRenderer::addChangeListener(SGPropertyChangeListener* l, const char* path) |
| 413 | { |
| 414 | _listeners.push_back(l); |
| 415 | fgAddChangeListener(l, path); |
| 416 | } |
| 417 | |
| 418 | // Initialize various GL/view parameters |
| 419 | // |
| 420 | // Note that this appears to be called *after* FGRenderer::init(). |
| 421 | // |
| 422 | void |
| 423 | FGRenderer::preinit( void ) |
| 424 | { |
| 425 | // important that we reset the viewer sceneData here, to ensure the reference |
| 426 | // time for everything is in sync; otherwise on reset the Viewer and |
| 427 | // GraphicsWindow clocks are out of sync. |
| 428 | osgViewer::View* view = getView(); |
| 429 | view->setName("osgViewer"); |
| 430 | _viewerSceneRoot = new osg::Group; |
| 431 | _viewerSceneRoot->setName("viewerSceneRoot"); |
| 432 | view->setSceneData(_viewerSceneRoot); |
| 433 | view->setDatabasePager(FGScenery::getPagerSingleton()); |
| 434 | |
| 435 | _quickDrawable = nullptr; |
| 436 | getSplash(); |
| 437 | |
| 438 | if (composite_viewer) { |
| 439 | // Nothing to do - composite_viewer->addView() will tell view to use |
| 440 | // composite_viewer's FrameStamp. |
| 441 | } |
| 442 | else { |
| 443 | _frameStamp = new osg::FrameStamp; |
| 444 | view->setFrameStamp(_frameStamp.get()); |
| 445 | } |
| 446 | |
| 447 | // Scene doesn't seem to pass the frame stamp to the update |
| 448 | // visitor automatically. |
| 449 | _updateVisitor->setFrameStamp(getFrameStamp()); |
| 450 | getViewerBase()->setUpdateVisitor(_updateVisitor.get()); |
| 451 | fgSetDouble("/sim/startup/splash-alpha", 1.0); |
| 452 | |
| 453 | // hide the menubar if it overlaps the window, so the splash screen |
| 454 | // is completely visible. We reset this value when the splash screen |
| 455 | // is fading out. |
| 456 | fgSetBool("/sim/menubar/overlap-hide", true); |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | FGRenderer::init( void ) |
| 461 | { |
| 462 | if (!eventHandler) |
| 463 | eventHandler = new FGEventHandler(); |
| 464 | |
| 465 | sgUserDataInit( globals->get_props() ); |
| 466 | |
| 467 | SGPropertyNode* composite_viewer_enabled_prop = fgGetNode("/sim/rendering/composite-viewer-enabled", true); |
| 468 | // After we've read composite_viewer_enabled_prop here, changing its value |
| 469 | // will have no affect, so mark it as read-only for clarity. |
| 470 | composite_viewer_enabled_prop->setAttributes(SGPropertyNode::READ); |
| 471 | if (composite_viewer_enabled_prop->getBoolValue()) { |
| 472 | const char* osg_version = osgGetVersion(); |
| 473 | if (simgear::strutils::starts_with(osg_version, "3.4")) { |
| 474 | SG_LOG( SG_GENERAL, SG_POPUP, |
| 475 | "CompositeViewer is enabled and requires OpenSceneGraph-3.6, but\n" |
| 476 | " Flightgear has been built with OpenSceneGraph-" << osg_version << ".\n" |
| 477 | " There may be problems when opening/closing extra view windows.\n" |
| 478 | ); |
| 479 | } |
| 480 | composite_viewer_enabled = 1; |
| 481 | SG_LOG(SG_VIEW, SG_ALERT, "Creating osgViewer::CompositeViewer"); |
| 482 | if (composite_viewer) { |
| 483 | // reinit. |
| 484 | } |
| 485 | else { |
| 486 | composite_viewer = new osgViewer::CompositeViewer; |
| 487 | std::string affinity = fgGetString("/sim/thread-cpu-affinity"); |
| 488 | SG_LOG(SG_GENERAL, SG_ALERT, "affinity=" << affinity); |
| 489 | bool osg_affinity_flag = true; |
| 490 | if (affinity == "") {} |
| 491 | else if (affinity == "none") { |
| 492 | osg_affinity_flag = false; |
| 493 | } |
| 494 | else if (affinity == "osg") { |
| 495 | /* This is handled elsewhere. */ |
| 496 | } |
| 497 | else { |
| 498 | SG_LOG(SG_VIEW, SG_ALERT, "Unrecognised value for /sim/thread-cpu-affinity: " << affinity); |
| 499 | } |
| 500 | SG_LOG(SG_VIEW, SG_ALERT, "Calling composite_viewer->setUseConfigureAffinity() with flag=" << osg_affinity_flag); |
| 501 | composite_viewer->setUseConfigureAffinity(osg_affinity_flag); |
| 502 | } |
| 503 | |
| 504 | // https://stackoverflow.com/questions/15207076/openscenegraph-and-multiple-viewers |
| 505 | composite_viewer->setReleaseContextAtEndOfFrameHint(false); |
| 506 | composite_viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
| 507 | } |
| 508 | else { |
| 509 | composite_viewer_enabled = 0; |
| 510 | SG_LOG(SG_VIEW, SG_ALERT, "Not creating osgViewer::CompositeViewer"); |
| 511 | } |
| 512 | _scenery_loaded = fgGetNode("/sim/sceneryloaded", true); |
| 513 | _position_finalized = fgGetNode("/sim/position-finalized", true); |
| 514 | |
| 515 | _panel_hotspots = fgGetNode("/sim/panel-hotspots", true); |
| 516 | _virtual_cockpit = fgGetNode("/sim/virtual-cockpit", true); |
| 517 | |
| 518 | _sim_delta_sec = fgGetNode("/sim/time/delta-sec", true); |
| 519 | |
| 520 | _xsize = fgGetNode("/sim/startup/xsize", true); |
| 521 | _ysize = fgGetNode("/sim/startup/ysize", true); |
| 522 | _xpos = fgGetNode("/sim/startup/xpos", true); |
| 523 | _ypos = fgGetNode("/sim/startup/ypos", true); |
| 524 | _splash_alpha = fgGetNode("/sim/startup/splash-alpha", true); |
| 525 | _splashHiddenSignal = fgGetNode("/sim/signals/splash-hidden", true); |
| 526 | |
| 527 | _horizon_effect = fgGetNode("/sim/rendering/horizon-effect", true); |
| 528 | |
| 529 | _altitude_ft = fgGetNode("/position/altitude-ft", true); |
| 530 | |
| 531 | _cloud_status = fgGetNode("/environment/clouds/status", true); |
| 532 | _visibility_m = fgGetNode("/environment/visibility-m", true); |
| 533 | |
| 534 | // configure the lighting related parameters and add change listeners. |
| 535 | bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites", true); |
| 536 | bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation", false); |
| 537 | bool triangles = fgGetBool("/sim/rendering/triangle-directional-lights", true); |
| 538 | SGConfigureDirectionalLights(use_point_sprites, distance_attenuation, triangles); |
| 539 | |
| 540 | addChangeListener(new PointSpriteListener, "/sim/rendering/point-sprites"); |
| 541 | addChangeListener(new DistanceAttenuationListener, "/sim/rendering/distance-attenuation"); |
| 542 | addChangeListener(new DirectionalLightsListener, "/sim/rendering/triangle-directional-lights"); |
| 543 | |
| 544 | std::string tc = fgGetString("/sim/rendering/texture-compression"); |
| 545 | if (!tc.empty()) { |
| 546 | if (tc == "false" || tc == "off" || |
| 547 | tc == "0" || tc == "no" || |
| 548 | tc == "none" |
| 549 | ) { |
| 550 | SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression); |
| 551 | } else if (tc == "arb") { |
| 552 | SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::UseARBCompression); |
| 553 | } else if (tc == "dxt1") { |
| 554 | SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::UseDXT1Compression); |
| 555 | } else if (tc == "dxt3") { |
| 556 | SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::UseDXT3Compression); |
| 557 | } else if (tc == "dxt5") { |
| 558 | SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::UseDXT5Compression); |
| 559 | } else { |
| 560 | SG_LOG(SG_VIEW, SG_WARN, "Unknown texture compression setting!"); |
| 561 | } |
| 562 | } |
| 563 | SGSceneFeatures::instance()->setTextureCompressionPath(globals->get_texture_cache_dir()); |
| 564 | // create sky, but can't build until setupView, since we depend |
| 565 | // on other subsystems to be inited, eg Ephemeris |
| 566 | _sky = new SGSky; |
| 567 | |
| 568 | const SGPath texture_path = globals->get_fg_root() / "Textures" / "Sky"; |
| 569 | for (int i = 0; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) { |
| 570 | SGCloudLayer * layer = new SGCloudLayer(texture_path); |
| 571 | _sky->add_cloud_layer(layer); |
| 572 | } |
| 573 | |
| 574 | _sky->set_texture_path( texture_path ); |
| 575 | |
| 576 | // XXX: Should always be true |
| 577 | eventHandler->setChangeStatsCameraRenderOrder( true ); |
| 578 | } |
| 579 | |
| 580 | void FGRenderer::setupRoot() |
| 581 | { |
| 582 | osg::StateSet* stateSet = _root->getOrCreateStateSet(); |
| 583 | |
| 584 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
| 585 | |
| 586 | stateSet->setAttribute(new osg::Depth(osg::Depth::LESS)); |
| 587 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
| 588 | |
| 589 | stateSet->setAttribute(new osg::BlendFunc); |
| 590 | stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF); |
| 591 | |
| 592 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 593 | |
| 594 | // this will be set below |
| 595 | stateSet->setMode(GL_NORMALIZE, osg::StateAttribute::OFF); |
| 596 | |
| 597 | osg::Material* material = new osg::Material; |
| 598 | stateSet->setAttribute(material); |
| 599 | |
| 600 | stateSet->setTextureAttribute(0, new osg::TexEnv); |
| 601 | stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF); |
| 602 | |
| 603 | osg::Hint* hint = new osg::Hint(GL_FOG_HINT, GL_DONT_CARE); |
| 604 | hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/fog")); |
| 605 | stateSet->setAttribute(hint); |
| 606 | hint = new osg::Hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE); |
| 607 | hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/polygon-smooth")); |
| 608 | stateSet->setAttribute(hint); |
| 609 | hint = new osg::Hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); |
| 610 | hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/line-smooth")); |
| 611 | stateSet->setAttribute(hint); |
| 612 | hint = new osg::Hint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE); |
| 613 | hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/point-smooth")); |
| 614 | stateSet->setAttribute(hint); |
| 615 | hint = new osg::Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE); |
| 616 | hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/perspective-correction")); |
| 617 | stateSet->setAttribute(hint); |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | FGRenderer::setupView( void ) |
| 622 | { |
| 623 | osgViewer::View* view = globals->get_renderer()->getView(); |
| 624 | osg::initNotifyLevel(); |
| 625 | |
| 626 | // The number of polygon-offset "units" to place between layers. In |
| 627 | // principle, one is supposed to be enough. In practice, I find that |
| 628 | // my hardware/driver requires many more. |
| 629 | osg::PolygonOffset::setUnitsMultiplier(1); |
| 630 | osg::PolygonOffset::setFactorMultiplier(1); |
| 631 | |
| 632 | setupRoot(); |
| 633 | |
| 634 | // build the sky |
| 635 | auto ephemerisSub = globals->get_subsystem<Ephemeris>(); |
| 636 | |
| 637 | |
| 638 | // The sun and moon radius are scaled down numbers of the actual |
| 639 | // diameters. This is needed to fit both the sun and the moon |
| 640 | // within the distance to the far clip plane. |
| 641 | // |
| 642 | // Their distance to the observer is specified in FGRendered::updateSky() and |
| 643 | // set to 40000 for the Moon semi-mayor axis, 50000 for the mean |
| 644 | // Earth-Sun distance (1AU). The location of the stars is |
| 645 | // specified below in the call to sky->build() (80000). |
| 646 | // |
| 647 | // Mean Moon radius: 1,737.4 kilometers |
| 648 | // Moon Semi-mayor axis: 384,399 km |
| 649 | // => Rendered Moon radius = 1,737.4 / 384,399 * 40000 = 232.5 |
| 650 | // |
| 651 | // Photosphere Sun radius: 695,700 kilometers |
| 652 | // 1UA = 149,597,870.700 km |
| 653 | // => Rendered Sun radius = 695,700/149,597,870.700 * 50000 = 180.8 |
| 654 | // |
| 655 | |
| 656 | osg::ref_ptr<simgear::SGReaderWriterOptions> opt; |
| 657 | opt = simgear::SGReaderWriterOptions::fromPath(globals->get_fg_root()); |
| 658 | opt->setPropertyNode(globals->get_props()); |
| 659 | _sky->build( 80000.0, 80000.0, |
| 660 | 232.5, 180.8, |
| 661 | *ephemerisSub->data(), |
| 662 | fgGetNode("/environment", true), |
| 663 | opt.get()); |
| 664 | |
| 665 | view->getCamera() |
| 666 | ->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 667 | |
| 668 | |
| 669 | // need to update the light on every frame |
| 670 | // OSG LightSource objects are rather confusing. OSG only supports |
| 671 | // the 10 lights specified by OpenGL itself; if more than one |
| 672 | // LightSource in the scene graph have the same light number, it's |
| 673 | // indeterminate which values will be used to render geometry that |
| 674 | // has that light number enabled. Also, adding children to a |
| 675 | // LightSource is just a shortcut for setting up a state set that |
| 676 | // has the corresponding OpenGL light enabled: a LightSource will |
| 677 | // affect geometry anywhere in the scene graph that has its light |
| 678 | // number enabled in a state set. |
| 679 | osg::ref_ptr<LightSource> lightSource = new LightSource; |
| 680 | lightSource->setName("FGLightSource"); |
| 681 | lightSource->getLight()->setDataVariance(Object::DYNAMIC); |
| 682 | // relative because of CameraView being just a clever transform node |
| 683 | lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF); |
| 684 | lightSource->setLocalStateSetModes(osg::StateAttribute::ON); |
| 685 | lightSource->setUpdateCallback(new FGLightSourceUpdateCallback); |
| 686 | _viewerSceneRoot->addChild(lightSource); |
| 687 | |
| 688 | // we need a white diffuse light for the phase of the moon |
| 689 | osg::ref_ptr<LightSource> sunLight = new osg::LightSource; |
| 690 | sunLight->setName("sunLightSource"); |
| 691 | sunLight->getLight()->setDataVariance(Object::DYNAMIC); |
| 692 | sunLight->getLight()->setLightNum(1); |
| 693 | sunLight->setUpdateCallback(new FGLightSourceUpdateCallback(true)); |
| 694 | sunLight->setReferenceFrame(osg::LightSource::RELATIVE_RF); |
| 695 | sunLight->setLocalStateSetModes(osg::StateAttribute::ON); |
| 696 | |
| 697 | // Hang a StateSet above the sky subgraph in order to turn off |
| 698 | // light 0 |
| 699 | Group* skyGroup = _sky->getPreRoot(); |
| 700 | StateSet* skySS = skyGroup->getOrCreateStateSet(); |
| 701 | skySS->setMode(GL_LIGHT0, StateAttribute::OFF); |
| 702 | sunLight->addChild(skyGroup); |
| 703 | |
| 704 | _root->addChild(sunLight); |
| 705 | |
| 706 | osg::Group* sceneGroup = globals->get_scenery()->get_scene_graph(); |
| 707 | sceneGroup->setName("rendererScene"); |
| 708 | sceneGroup->setNodeMask(~simgear::BACKGROUND_BIT); |
| 709 | _root->addChild(sceneGroup); |
| 710 | |
| 711 | // setup state-set for main scenery (including models and aircraft) |
| 712 | osg::StateSet* stateSet = sceneGroup->getOrCreateStateSet(); |
| 713 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); |
| 714 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); |
| 715 | |
| 716 | // enable disable specular highlights. |
| 717 | // is the place where we might plug in an other fragment shader ... |
| 718 | osg::LightModel* lightModel = new osg::LightModel; |
| 719 | lightModel->setUpdateCallback(new FGLightModelUpdateCallback); |
| 720 | stateSet->setAttribute(lightModel); |
| 721 | |
| 722 | // switch to enable wireframe |
| 723 | osg::PolygonMode* polygonMode = new osg::PolygonMode; |
| 724 | polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback); |
| 725 | stateSet->setAttributeAndModes(polygonMode); |
| 726 | |
| 727 | // scene fog handling |
| 728 | osg::Fog* fog = new osg::Fog; |
| 729 | fog->setUpdateCallback(new FGFogUpdateCallback); |
| 730 | stateSet->setAttributeAndModes(fog); |
| 731 | stateSet->setUpdateCallback(new FGFogEnableUpdateCallback); |
| 732 | |
| 733 | osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 734 | if (guiCamera) { |
| 735 | #ifdef ENABLE_HUD |
| 736 | osg::Geode* hudGeode = new osg::Geode; |
| 737 | hudGeode->addDrawable(new SGHUDDrawable); |
| 738 | guiCamera->addChild(hudGeode); |
| 739 | #endif |
| 740 | |
| 741 | #if defined(HAVE_PUI) |
| 742 | _puiCamera = new flightgear::PUICamera; |
| 743 | _puiCamera->init(guiCamera, view); |
| 744 | #endif |
| 745 | |
| 746 | #if defined(ENABLE_QQ_UI) |
| 747 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(view); |
| 748 | if (viewer) { |
| 749 | std::string rootQMLPath = fgGetString("/sim/gui/qml-root-path"); |
| 750 | auto graphicsWindow = dynamic_cast<osgViewer::GraphicsWindow*>(guiCamera->getGraphicsContext()); |
| 751 | |
| 752 | if (!rootQMLPath.empty()) { |
| 753 | _quickDrawable = new QQuickDrawable; |
| 754 | _quickDrawable->setup(graphicsWindow, viewer); |
| 755 | _quickDrawable->setSourcePath(rootQMLPath); |
| 756 | |
| 757 | osg::Geode* qqGeode = new osg::Geode; |
| 758 | qqGeode->addDrawable(_quickDrawable); |
| 759 | guiCamera->addChild(qqGeode); |
| 760 | } |
| 761 | } |
| 762 | #endif |
| 763 | guiCamera->insertChild(0, FGPanelNode::create2DPanelNode()); |
| 764 | } |
| 765 | |
| 766 | osg::Switch* sw = new osg::Switch; |
| 767 | sw->setName("scenerySwitch"); |
| 768 | sw->setUpdateCallback(new FGScenerySwitchCallback); |
| 769 | sw->addChild(_root.get()); |
| 770 | _viewerSceneRoot->addChild(sw); |
| 771 | // The clouds are attached directly to the scene graph root |
| 772 | // because, in theory, they don't want the same default state set |
| 773 | // as the rest of the scene. This may not be true in practice. |
| 774 | _viewerSceneRoot->addChild(_sky->getCloudRoot()); |
| 775 | _viewerSceneRoot->addChild(FGCreateRedoutNode()); |
| 776 | |
| 777 | // Attach empty program to the scene root so that shader programs |
| 778 | // don't leak into state sets (effects) that shouldn't have one. |
| 779 | stateSet = _viewerSceneRoot->getOrCreateStateSet(); |
| 780 | stateSet->setAttributeAndModes(new osg::Program, osg::StateAttribute::ON); |
| 781 | } |
| 782 | |
| 783 | // Update all Visuals (redraws anything graphics related) |
| 784 | // Called every frame. |
| 785 | void |
| 786 | FGRenderer::update( ) { |
| 787 | if (!_position_finalized || !_scenery_loaded->getBoolValue()) |
| 788 | { |
| 789 | _splash_alpha->setDoubleValue(1.0); |
| 790 | if (_splashHiddenSignal->getBoolValue()) { |
| 791 | _splashHiddenSignal->setBoolValue(false); |
| 792 | } |
| 793 | |
| 794 | if (!MaximumTextureSize) { |
| 795 | osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 796 | if (guiCamera) { |
| 797 | GraphicsContext *gc = guiCamera->getGraphicsContext(); |
| 798 | osg::GLExtensions* gl2ext = gc->getState()->get<osg::GLExtensions>(); |
| 799 | if (gl2ext) { |
| 800 | MaximumTextureSize = gl2ext->maxTextureSize; |
| 801 | SGSceneFeatures::instance()->setMaxTextureSize(MaximumTextureSize); |
| 802 | SG_LOG(SG_VIEW, SG_INFO, "FGRenderer:: Maximum texture size " << MaximumTextureSize); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | if (_splash_alpha->getDoubleValue()>0.0) |
| 810 | { |
| 811 | // Fade out the splash screen |
| 812 | const double fade_time = 0.5; |
| 813 | const double fade_steps_per_sec = 10; |
| 814 | double delay_time = SGMiscd::min(fade_time/fade_steps_per_sec, |
| 815 | (SGTimeStamp::now() - _splash_time).toSecs()); |
| 816 | _splash_time = SGTimeStamp::now(); |
| 817 | double sAlpha = _splash_alpha->getDoubleValue(); |
| 818 | sAlpha -= SGMiscd::max(0.0,delay_time/fade_time); |
| 819 | FGScenerySwitchCallback::scenery_enabled = (sAlpha<1.0); |
| 820 | |
| 821 | if (sAlpha <= 0.0) { |
| 822 | _splashHiddenSignal->setBoolValue(true); |
| 823 | flightgear::addSentryBreadcrumb("splash-screen fade out complete", "info"); |
| 824 | } |
| 825 | |
| 826 | _splash_alpha->setDoubleValue((sAlpha < 0) ? 0.0 : sAlpha); |
| 827 | |
| 828 | syncPausePopupState(); |
| 829 | fgSetBool("/sim/menubar/overlap-hide", false); |
| 830 | } |
| 831 | |
| 832 | auto l = globals->get_subsystem<FGLight>(); |
| 833 | |
| 834 | // update fog params |
| 835 | double actual_visibility; |
| 836 | if (_cloud_status->getBoolValue()) { |
| 837 | actual_visibility = _sky->get_visibility(); |
| 838 | } else { |
| 839 | actual_visibility = _visibility_m->getDoubleValue(); |
| 840 | } |
| 841 | |
| 842 | // idle_state is now 1000 meaning we've finished all our |
| 843 | // initializations and are running the main loop, so this will |
| 844 | // now work without seg faulting the system. |
| 845 | |
| 846 | flightgear::View *current__view = globals->get_current_view(); |
| 847 | // Force update of center dependent values ... |
| 848 | current__view->set_dirty(); |
| 849 | |
| 850 | assert(composite_viewer_enabled != -1); |
| 851 | std::vector<osg::Camera*> cameras; |
| 852 | if (composite_viewer) { |
| 853 | assert(!viewer); |
| 854 | unsigned n = composite_viewer->getNumViews(); |
| 855 | for (unsigned i=0; i<n; ++i) { |
| 856 | osgViewer::View* view = composite_viewer->getView(i); |
| 857 | osg::Camera* camera = view->getCamera(); |
| 858 | cameras.push_back(camera); |
| 859 | } |
| 860 | } |
| 861 | else { |
| 862 | cameras.push_back(viewer->getCamera()); |
| 863 | } |
| 864 | for (osg::Camera* camera: cameras) { |
| 865 | osg::Vec4 clear_color = _altitude_ft->getDoubleValue() < 250000 |
| 866 | ? toOsg(l->adj_fog_color()) |
| 867 | // skydome ends at ~262000ft (default rendering) |
| 868 | // ~328000 ft (ALS) and would produce a strange |
| 869 | // looking greyish space -> black looks much |
| 870 | // better :-) |
| 871 | : osg::Vec4(0, 0, 0, 1); |
| 872 | camera->setClearColor(clear_color); |
| 873 | |
| 874 | updateSky(); |
| 875 | |
| 876 | // need to call the update visitor once |
| 877 | getFrameStamp()->setCalendarTime(*globals->get_time_params()->getGmt()); |
| 878 | _updateVisitor->setViewData(current__view->getViewPosition(), |
| 879 | current__view->getViewOrientation()); |
| 880 | //_updateVisitor->setViewData(eye2, center3); |
| 881 | SGVec3f sundirection(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]); |
| 882 | SGVec3f moondirection(l->moon_vec()[0], l->moon_vec()[1], l->moon_vec()[2]); |
| 883 | |
| 884 | _updateVisitor->setLight(sundirection,moondirection, l->scene_ambient(), |
| 885 | l->scene_diffuse(), l->scene_specular(), |
| 886 | l->adj_fog_color(), |
| 887 | l->get_sun_angle()*SGD_RADIANS_TO_DEGREES); |
| 888 | _updateVisitor->setVisibility(actual_visibility); |
| 889 | simgear::GroundLightManager::instance()->update(_updateVisitor.get()); |
| 890 | } |
| 891 | |
| 892 | osg::Node::NodeMask cullMask = ~simgear::LIGHTS_BITS & ~simgear::PICK_BIT; |
| 893 | cullMask |= simgear::GroundLightManager::instance() |
| 894 | ->getLightNodeMask(_updateVisitor.get()); |
| 895 | if (_panel_hotspots->getBoolValue()) |
| 896 | cullMask |= simgear::PICK_BIT; |
| 897 | CameraGroup::getDefault()->setCameraCullMasks(cullMask); |
| 898 | } |
| 899 | |
| 900 | void |
| 901 | FGRenderer::updateSky() |
| 902 | { |
| 903 | // update fog params if visibility has changed |
| 904 | double visibility_meters = _visibility_m->getDoubleValue(); |
| 905 | _sky->set_visibility(visibility_meters); |
| 906 | |
| 907 | double altitude_m = _altitude_ft->getDoubleValue() * SG_FEET_TO_METER; |
| 908 | _sky->modify_vis( altitude_m, 0.0 /* time factor, now unused */); |
| 909 | |
| 910 | auto l = globals->get_subsystem<FGLight>(); |
| 911 | |
| 912 | // The sun and moon distances are scaled down versions of the |
| 913 | // actual distance. See FGRenderer::setupView() for more details. |
| 914 | |
| 915 | SGSkyState sstate; |
| 916 | sstate.pos = globals->get_current_view()->getViewPosition(); |
| 917 | sstate.pos_geod = globals->get_current_view()->getPosition(); |
| 918 | sstate.ori = globals->get_current_view()->getViewOrientation(); |
| 919 | sstate.spin = l->get_sun_rotation(); |
| 920 | sstate.gst = globals->get_time_params()->getGst(); |
| 921 | sstate.sun_dist = 50000.0; |
| 922 | sstate.moon_dist_bare = 40000.0; |
| 923 | sstate.moon_dist_factor = 1.0; |
| 924 | sstate.sun_angle = l->get_sun_angle(); |
| 925 | |
| 926 | SGSkyColor scolor; |
| 927 | scolor.sky_color = SGVec3f(l->sky_color().data()); |
| 928 | scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data()); |
| 929 | scolor.fog_color = SGVec3f(l->adj_fog_color().data()); |
| 930 | scolor.cloud_color = SGVec3f(l->cloud_color().data()); |
| 931 | scolor.sun_angle = l->get_sun_angle(); |
| 932 | scolor.moon_angle = l->get_moon_angle(); |
| 933 | scolor.altitude_m = altitude_m; |
| 934 | |
| 935 | auto ephemerisSub = globals->get_subsystem<Ephemeris>(); |
| 936 | double delta_time_sec = _sim_delta_sec->getDoubleValue(); |
| 937 | _sky->reposition( sstate, *ephemerisSub->data(), delta_time_sec ); |
| 938 | _sky->repaint( scolor, *ephemerisSub->data() ); |
| 939 | } |
| 940 | |
| 941 | void |
| 942 | FGRenderer::resize( int width, int height, int x, int y ) |
| 943 | { |
| 944 | SG_LOG(SG_VIEW, SG_DEBUG, "FGRenderer::resize: new size " << width << " x " << height); |
| 945 | // must guard setting these, or PLIB-PUI fails with too many live interfaces |
| 946 | if (width != _xsize->getIntValue()) _xsize->setIntValue(width); |
| 947 | if (height != _ysize->getIntValue()) _ysize->setIntValue(height); |
| 948 | if (x != _xpos->getIntValue()) _xpos->setIntValue(x); |
| 949 | if (y != _ypos->getIntValue()) _ypos->setIntValue(y); |
| 950 | |
| 951 | // update splash node if present |
| 952 | _splash->resize(width, height); |
| 953 | #if defined(ENABLE_QQ_UI) |
| 954 | if (_quickDrawable) { |
| 955 | _quickDrawable->resize(width, height); |
| 956 | } |
| 957 | #endif |
| 958 | } |
| 959 | |
| 960 | void |
| 961 | FGRenderer::resize( int width, int height ) |
| 962 | { |
| 963 | resize(width, height, _xpos->getIntValue(), _ypos->getIntValue()); |
| 964 | } |
| 965 | |
| 966 | typedef osgUtil::LineSegmentIntersector::Intersection Intersection; |
| 967 | SGVec2d uvFromIntersection(const Intersection& hit) |
| 968 | { |
| 969 | // Taken from http://trac.openscenegraph.org/projects/osg/browser/OpenSceneGraph/trunk/examples/osgmovie/osgmovie.cpp |
| 970 | |
| 971 | osg::Drawable* drawable = hit.drawable.get(); |
| 972 | osg::Geometry* geometry = drawable ? drawable->asGeometry() : 0; |
| 973 | osg::Vec3Array* vertices = |
| 974 | geometry ? dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()) : 0; |
| 975 | |
| 976 | if( !vertices ) |
| 977 | { |
| 978 | SG_LOG(SG_INPUT, SG_WARN, "Unable to get vertices for intersection."); |
| 979 | return SGVec2d(-9999,-9999); |
| 980 | } |
| 981 | |
| 982 | // get the vertex indices. |
| 983 | const Intersection::IndexList& indices = hit.indexList; |
| 984 | const Intersection::RatioList& ratios = hit.ratioList; |
| 985 | |
| 986 | if( indices.size() != 3 || ratios.size() != 3 ) |
| 987 | { |
| 988 | SG_LOG( SG_INPUT, |
| 989 | SG_WARN, |
| 990 | "Intersection has insufficient indices to work with." ); |
| 991 | return SGVec2d(-9999,-9999); |
| 992 | } |
| 993 | |
| 994 | unsigned int i1 = indices[0]; |
| 995 | unsigned int i2 = indices[1]; |
| 996 | unsigned int i3 = indices[2]; |
| 997 | |
| 998 | float r1 = ratios[0]; |
| 999 | float r2 = ratios[1]; |
| 1000 | float r3 = ratios[2]; |
| 1001 | |
| 1002 | osg::Array* texcoords = |
| 1003 | (geometry->getNumTexCoordArrays() > 0) ? geometry->getTexCoordArray(0) : 0; |
| 1004 | osg::Vec2Array* texcoords_Vec2Array = |
| 1005 | dynamic_cast<osg::Vec2Array*>(texcoords); |
| 1006 | |
| 1007 | if( !texcoords_Vec2Array ) |
| 1008 | { |
| 1009 | SG_LOG(SG_INPUT, SG_WARN, "Unable to get texcoords for intersection."); |
| 1010 | return SGVec2d(-9999,-9999); |
| 1011 | } |
| 1012 | |
| 1013 | // we have tex coord array so now we can compute the final tex coord at the |
| 1014 | // point of intersection. |
| 1015 | osg::Vec2 tc1 = (*texcoords_Vec2Array)[i1]; |
| 1016 | osg::Vec2 tc2 = (*texcoords_Vec2Array)[i2]; |
| 1017 | osg::Vec2 tc3 = (*texcoords_Vec2Array)[i3]; |
| 1018 | |
| 1019 | return toSG( osg::Vec2d(tc1 * r1 + tc2 * r2 + tc3 * r3) ); |
| 1020 | } |
| 1021 | |
| 1022 | PickList FGRenderer::pick(const osg::Vec2& windowPos) |
| 1023 | { |
| 1024 | PickList result; |
| 1025 | osgUtil::LineSegmentIntersector::Intersections intersections; |
| 1026 | |
| 1027 | if (!computeIntersections(CameraGroup::getDefault(), windowPos, intersections)) |
| 1028 | return result; // return empty list |
| 1029 | |
| 1030 | // We attempt to highlight nodes until Highlight::highlight_nodes() |
| 1031 | // succeeds and returns +ve, or highlighting is disabled and it returns -1. |
| 1032 | auto highlight = globals->get_subsystem<Highlight>(); |
| 1033 | int highlight_num_props = 0; |
| 1034 | |
| 1035 | for (const auto& hit : intersections) { |
| 1036 | const osg::NodePath& np = hit.nodePath; |
| 1037 | osg::NodePath::const_reverse_iterator npi; |
| 1038 | |
| 1039 | for (npi = np.rbegin(); npi != np.rend(); ++npi) { |
| 1040 | if (!highlight_num_props && highlight) { |
| 1041 | highlight_num_props = highlight->highlightNodes(*npi); |
| 1042 | } |
| 1043 | SGSceneUserData* ud = SGSceneUserData::getSceneUserData(*npi); |
| 1044 | if (!ud || (ud->getNumPickCallbacks() == 0)) |
| 1045 | continue; |
| 1046 | |
| 1047 | for (unsigned i = 0; i < ud->getNumPickCallbacks(); ++i) { |
| 1048 | SGPickCallback* pickCallback = ud->getPickCallback(i); |
| 1049 | if (!pickCallback) |
| 1050 | continue; |
| 1051 | SGSceneryPick sceneryPick; |
| 1052 | sceneryPick.info.local = toSG(hit.getLocalIntersectPoint()); |
| 1053 | sceneryPick.info.wgs84 = toSG(hit.getWorldIntersectPoint()); |
| 1054 | |
| 1055 | if( pickCallback->needsUV() ) |
| 1056 | sceneryPick.info.uv = uvFromIntersection(hit); |
| 1057 | |
| 1058 | sceneryPick.callback = pickCallback; |
| 1059 | result.push_back(sceneryPick); |
| 1060 | } // of installed pick callbacks iteration |
| 1061 | } // of reverse node path walk |
| 1062 | } |
| 1063 | |
| 1064 | return result; |
| 1065 | } |
| 1066 | |
| 1067 | osgViewer::ViewerBase* FGRenderer::getViewerBase() |
| 1068 | { |
| 1069 | if (composite_viewer) { |
| 1070 | return composite_viewer.get(); |
| 1071 | } |
| 1072 | else { |
| 1073 | return viewer.get(); |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | osg::ref_ptr<osgViewer::CompositeViewer> FGRenderer::getCompositeViewer() |
| 1078 | { |
| 1079 | return composite_viewer; |
| 1080 | } |
| 1081 | |
| 1082 | osgViewer::View* FGRenderer::getView() |
| 1083 | { |
| 1084 | /* Would like to assert that FGRenderer::init() has always been called |
| 1085 | before we are called, with: |
| 1086 | assert(composite_viewer_enabled != -1); |
| 1087 | But this fails if user specifies -h, when we are called by |
| 1088 | FGGlobals::~FGGlobals(). |
| 1089 | */ |
| 1090 | if (composite_viewer && composite_viewer->getNumViews() > 0) { |
| 1091 | assert(composite_viewer->getNumViews()); |
| 1092 | return composite_viewer->getView(0); |
| 1093 | } |
| 1094 | else { |
| 1095 | return viewer.get(); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | const osgViewer::View* FGRenderer::getView() const |
| 1100 | { |
| 1101 | FGRenderer* this_ = const_cast<FGRenderer*>(this); |
| 1102 | return this_->getView(); |
| 1103 | } |
| 1104 | |
| 1105 | void |
| 1106 | FGRenderer::setView(osgViewer::View* view) |
| 1107 | { |
| 1108 | assert(composite_viewer_enabled != -1); |
| 1109 | if (composite_viewer) { |
| 1110 | if (composite_viewer->getNumViews() == 0) { |
| 1111 | SG_LOG(SG_VIEW, SG_DEBUG, "adding view to composite_viewer."); |
| 1112 | composite_viewer->stopThreading(); |
| 1113 | composite_viewer->addView(view); |
| 1114 | composite_viewer->startThreading(); |
| 1115 | } |
| 1116 | } |
| 1117 | else { |
| 1118 | osgViewer::Viewer* viewer_ = dynamic_cast<osgViewer::Viewer*>(view); |
| 1119 | assert(viewer_); |
| 1120 | viewer = viewer_; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | osg::FrameStamp* |
| 1125 | FGRenderer::getFrameStamp() |
| 1126 | { |
| 1127 | assert(composite_viewer_enabled != -1); |
| 1128 | if (composite_viewer) { |
| 1129 | assert(!viewer); |
| 1130 | return composite_viewer->getFrameStamp(); |
| 1131 | } |
| 1132 | else { |
| 1133 | assert(viewer); |
| 1134 | return viewer->getFrameStamp(); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | void |
| 1139 | FGRenderer::setEventHandler(FGEventHandler* eventHandler_) |
| 1140 | { |
| 1141 | eventHandler = eventHandler_; |
| 1142 | } |
| 1143 | |
| 1144 | void |
| 1145 | FGRenderer::addCamera(osg::Camera* camera, bool useSceneData) |
| 1146 | { |
| 1147 | bool should_restart_threading = getViewerBase()->areThreadsRunning(); |
| 1148 | if (should_restart_threading) { |
| 1149 | getViewerBase()->stopThreading(); |
| 1150 | } |
| 1151 | osg::Camera *guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 1152 | osg::GraphicsContext *gc = guiCamera->getGraphicsContext(); |
| 1153 | camera->setGraphicsContext(gc); |
| 1154 | if (composite_viewer) { |
| 1155 | composite_viewer->getView(0)->addSlave(camera, false); |
| 1156 | } else { |
| 1157 | viewer->addSlave(camera, false); |
| 1158 | } |
| 1159 | simgear::installEffectCullVisitor(camera); |
| 1160 | // Disable stats |
| 1161 | camera->setStats(0); |
| 1162 | if (should_restart_threading) { |
| 1163 | getViewerBase()->startThreading(); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | void |
| 1168 | FGRenderer::removeCamera(osg::Camera* camera) |
| 1169 | { |
| 1170 | bool should_restart_threading = getViewerBase()->areThreadsRunning(); |
| 1171 | if (should_restart_threading) { |
| 1172 | getViewerBase()->stopThreading(); |
| 1173 | } |
| 1174 | // Remove all children before removing the slave to prevent the graphics |
| 1175 | // window from automatically cleaning up all associated OpenGL objects. |
| 1176 | camera->removeChildren(0, camera->getNumChildren()); |
| 1177 | if (composite_viewer) { |
| 1178 | unsigned int index = composite_viewer->getView(0) |
| 1179 | ->findSlaveIndexForCamera(camera); |
| 1180 | composite_viewer->getView(0)->removeSlave(index); |
| 1181 | } else { |
| 1182 | unsigned int index = viewer->findSlaveIndexForCamera(camera); |
| 1183 | viewer->removeSlave(index); |
| 1184 | } |
| 1185 | if (should_restart_threading) { |
| 1186 | getViewerBase()->startThreading(); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | void |
| 1191 | FGRenderer::setPlanes( double zNear, double zFar ) |
| 1192 | { |
| 1193 | // _planes->set( osg::Vec3f( - zFar, - zFar * zNear, zFar - zNear ) ); |
| 1194 | } |
| 1195 | |
| 1196 | SplashScreen* |
| 1197 | FGRenderer::getSplash() |
| 1198 | { |
| 1199 | if (!_splash) |
| 1200 | _splash = new SplashScreen; |
| 1201 | return _splash; |
| 1202 | } |
| 1203 | |
| 1204 | bool |
| 1205 | fgDumpSceneGraphToFile(const char* filename) |
| 1206 | { |
| 1207 | osgViewer::View* view = globals->get_renderer()->getView(); |
| 1208 | return osgDB::writeNodeFile(*view->getSceneData(), filename); |
| 1209 | } |
| 1210 | |
| 1211 | bool |
| 1212 | fgDumpTerrainBranchToFile(const char* filename) |
| 1213 | { |
| 1214 | return osgDB::writeNodeFile( *globals->get_scenery()->get_terrain_branch(), |
| 1215 | filename ); |
| 1216 | } |
| 1217 | |
| 1218 | // For debugging |
| 1219 | bool |
| 1220 | fgDumpNodeToFile(osg::Node* node, const char* filename) |
| 1221 | { |
| 1222 | return osgDB::writeNodeFile(*node, filename); |
| 1223 | } |
| 1224 | |
| 1225 | namespace flightgear |
| 1226 | { |
| 1227 | using namespace osg; |
| 1228 | |
| 1229 | class VisibleSceneInfoVistor : public NodeVisitor, CullStack |
| 1230 | { |
| 1231 | public: |
| 1232 | VisibleSceneInfoVistor() |
| 1233 | : NodeVisitor(CULL_VISITOR, TRAVERSE_ACTIVE_CHILDREN) |
| 1234 | { |
| 1235 | setCullingMode(CullSettings::SMALL_FEATURE_CULLING |
| 1236 | | CullSettings::VIEW_FRUSTUM_CULLING); |
| 1237 | setComputeNearFarMode(CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 1238 | } |
| 1239 | |
| 1240 | VisibleSceneInfoVistor(const VisibleSceneInfoVistor& rhs) |
| 1241 | : osg::Object(rhs), NodeVisitor(rhs), CullStack(rhs) |
| 1242 | { |
| 1243 | } |
| 1244 | |
| 1245 | META_NodeVisitor("flightgear","VisibleSceneInfoVistor") |
| 1246 | |
| 1247 | typedef std::map<const std::string,int> InfoMap; |
| 1248 | |
| 1249 | void getNodeInfo(Node* node) |
| 1250 | { |
| 1251 | const char* typeName = typeid(*node).name(); |
| 1252 | classInfo[typeName]++; |
| 1253 | const std::string& nodeName = node->getName(); |
| 1254 | if (!nodeName.empty()) |
| 1255 | nodeInfo[nodeName]++; |
| 1256 | } |
| 1257 | |
| 1258 | void dumpInfo() |
| 1259 | { |
| 1260 | using namespace std; |
| 1261 | typedef vector<InfoMap::iterator> FreqVector; |
| 1262 | cout << "class info:\n"; |
| 1263 | FreqVector classes; |
| 1264 | for (InfoMap::iterator itr = classInfo.begin(), end = classInfo.end(); |
| 1265 | itr != end; |
| 1266 | ++itr) |
| 1267 | classes.push_back(itr); |
| 1268 | sort(classes.begin(), classes.end(), freqComp); |
| 1269 | for (FreqVector::iterator itr = classes.begin(), end = classes.end(); |
| 1270 | itr != end; |
| 1271 | ++itr) { |
| 1272 | cout << (*itr)->first << " " << (*itr)->second << "\n"; |
| 1273 | } |
| 1274 | cout << "\nnode info:\n"; |
| 1275 | FreqVector nodes; |
| 1276 | for (InfoMap::iterator itr = nodeInfo.begin(), end = nodeInfo.end(); |
| 1277 | itr != end; |
| 1278 | ++itr) |
| 1279 | nodes.push_back(itr); |
| 1280 | |
| 1281 | sort (nodes.begin(), nodes.end(), freqComp); |
| 1282 | for (FreqVector::iterator itr = nodes.begin(), end = nodes.end(); |
| 1283 | itr != end; |
| 1284 | ++itr) { |
| 1285 | cout << (*itr)->first << " " << (*itr)->second << "\n"; |
| 1286 | } |
| 1287 | cout << endl; |
| 1288 | } |
| 1289 | |
| 1290 | void doTraversal(Camera* camera, Node* root, Viewport* viewport) |
| 1291 | { |
| 1292 | ref_ptr<RefMatrix> projection |
| 1293 | = createOrReuseMatrix(camera->getProjectionMatrix()); |
| 1294 | ref_ptr<RefMatrix> mv = createOrReuseMatrix(camera->getViewMatrix()); |
| 1295 | if (!viewport) |
| 1296 | viewport = camera->getViewport(); |
| 1297 | if (viewport) |
| 1298 | pushViewport(viewport); |
| 1299 | pushProjectionMatrix(projection.get()); |
| 1300 | pushModelViewMatrix(mv.get(), Transform::ABSOLUTE_RF); |
| 1301 | root->accept(*this); |
| 1302 | popModelViewMatrix(); |
| 1303 | popProjectionMatrix(); |
| 1304 | if (viewport) |
| 1305 | popViewport(); |
| 1306 | dumpInfo(); |
| 1307 | } |
| 1308 | |
| 1309 | void apply(Node& node) |
| 1310 | { |
| 1311 | if (isCulled(node)) |
| 1312 | return; |
| 1313 | pushCurrentMask(); |
| 1314 | getNodeInfo(&node); |
| 1315 | traverse(node); |
| 1316 | popCurrentMask(); |
| 1317 | } |
| 1318 | void apply(Group& node) |
| 1319 | { |
| 1320 | if (isCulled(node)) |
| 1321 | return; |
| 1322 | pushCurrentMask(); |
| 1323 | getNodeInfo(&node); |
| 1324 | traverse(node); |
| 1325 | popCurrentMask(); |
| 1326 | } |
| 1327 | |
| 1328 | void apply(Transform& node) |
| 1329 | { |
| 1330 | if (isCulled(node)) |
| 1331 | return; |
| 1332 | pushCurrentMask(); |
| 1333 | ref_ptr<RefMatrix> matrix = createOrReuseMatrix(*getModelViewMatrix()); |
| 1334 | node.computeLocalToWorldMatrix(*matrix,this); |
| 1335 | pushModelViewMatrix(matrix.get(), node.getReferenceFrame()); |
| 1336 | getNodeInfo(&node); |
| 1337 | traverse(node); |
| 1338 | popModelViewMatrix(); |
| 1339 | popCurrentMask(); |
| 1340 | } |
| 1341 | |
| 1342 | void apply(Camera& camera) |
| 1343 | { |
| 1344 | // Save current cull settings |
| 1345 | CullSettings saved_cull_settings(*this); |
| 1346 | |
| 1347 | // set cull settings from this Camera |
| 1348 | setCullSettings(camera); |
| 1349 | // inherit the settings from above |
| 1350 | inheritCullSettings(saved_cull_settings, camera.getInheritanceMask()); |
| 1351 | |
| 1352 | // set the cull mask. |
| 1353 | unsigned int savedTraversalMask = getTraversalMask(); |
| 1354 | bool mustSetCullMask = (camera.getInheritanceMask() |
| 1355 | & osg::CullSettings::CULL_MASK) == 0; |
| 1356 | if (mustSetCullMask) |
| 1357 | setTraversalMask(camera.getCullMask()); |
| 1358 | |
| 1359 | osg::RefMatrix* projection = 0; |
| 1360 | osg::RefMatrix* modelview = 0; |
| 1361 | |
| 1362 | if (camera.getReferenceFrame()==osg::Transform::RELATIVE_RF) { |
| 1363 | if (camera.getTransformOrder()==osg::Camera::POST_MULTIPLY) { |
| 1364 | projection = createOrReuseMatrix(*getProjectionMatrix() |
| 1365 | *camera.getProjectionMatrix()); |
| 1366 | modelview = createOrReuseMatrix(*getModelViewMatrix() |
| 1367 | * camera.getViewMatrix()); |
| 1368 | } |
| 1369 | else { // pre multiply |
| 1370 | projection = createOrReuseMatrix(camera.getProjectionMatrix() |
| 1371 | * (*getProjectionMatrix())); |
| 1372 | modelview = createOrReuseMatrix(camera.getViewMatrix() |
| 1373 | * (*getModelViewMatrix())); |
| 1374 | } |
| 1375 | } else { |
| 1376 | // an absolute reference frame |
| 1377 | projection = createOrReuseMatrix(camera.getProjectionMatrix()); |
| 1378 | modelview = createOrReuseMatrix(camera.getViewMatrix()); |
| 1379 | } |
| 1380 | if (camera.getViewport()) |
| 1381 | pushViewport(camera.getViewport()); |
| 1382 | |
| 1383 | pushProjectionMatrix(projection); |
| 1384 | pushModelViewMatrix(modelview, camera.getReferenceFrame()); |
| 1385 | |
| 1386 | traverse(camera); |
| 1387 | |
| 1388 | // restore the previous model view matrix. |
| 1389 | popModelViewMatrix(); |
| 1390 | |
| 1391 | // restore the previous model view matrix. |
| 1392 | popProjectionMatrix(); |
| 1393 | |
| 1394 | if (camera.getViewport()) popViewport(); |
| 1395 | |
| 1396 | // restore the previous traversal mask settings |
| 1397 | if (mustSetCullMask) |
| 1398 | setTraversalMask(savedTraversalMask); |
| 1399 | |
| 1400 | // restore the previous cull settings |
| 1401 | setCullSettings(saved_cull_settings); |
| 1402 | } |
| 1403 | |
| 1404 | protected: |
| 1405 | // sort in reverse |
| 1406 | static bool freqComp(const InfoMap::iterator& lhs, const InfoMap::iterator& rhs) |
| 1407 | { |
| 1408 | return lhs->second > rhs->second; |
| 1409 | } |
| 1410 | InfoMap classInfo; |
| 1411 | InfoMap nodeInfo; |
| 1412 | }; |
| 1413 | |
| 1414 | bool printVisibleSceneInfo(FGRenderer* renderer) |
| 1415 | { |
| 1416 | osgViewer::View* view = renderer->getView(); |
| 1417 | VisibleSceneInfoVistor vsv; |
| 1418 | Viewport* vp = 0; |
| 1419 | if (!view->getCamera()->getViewport() && view->getNumSlaves() > 0) { |
| 1420 | const osg::View::Slave& slave = view->getSlave(0); |
| 1421 | vp = slave._camera->getViewport(); |
| 1422 | } |
| 1423 | vsv.doTraversal(view->getCamera(), view->getSceneData(), vp); |
| 1424 | return true; |
| 1425 | } |
| 1426 | |
| 1427 | } |
| 1428 | // end of renderer.cxx |