1 // renderer.cxx -- top level sim routines
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
6 // Copyright (C) 1997 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
30 #include <simgear/compiler.h>
32 #include <osg/ref_ptr>
33 #include <osg/AlphaFunc>
34 #include <osg/BlendFunc>
36 #include <osg/CullFace>
42 #include <osg/LightModel>
43 #include <osg/LightSource>
44 #include <osg/Material>
46 #include <osg/NodeCallback>
48 #include <osg/PolygonMode>
49 #include <osg/PolygonOffset>
50 #include <osg/Program>
51 #include <osg/Version>
54 #include <osgUtil/LineSegmentIntersector>
56 #include <osg/io_utils>
57 #include <osgDB/WriteFile>
59 #include <simgear/math/SGMath.hxx>
60 #include <simgear/screen/extensions.hxx>
61 #include <simgear/scene/material/matlib.hxx>
62 #include <simgear/scene/model/animation.hxx>
63 #include <simgear/scene/model/placement.hxx>
64 #include <simgear/scene/sky/sky.hxx>
65 #include <simgear/scene/util/SGUpdateVisitor.hxx>
66 #include <simgear/scene/util/RenderConstants.hxx>
67 #include <simgear/scene/util/SGSceneUserData.hxx>
68 #include <simgear/scene/tgdb/GroundLightManager.hxx>
69 #include <simgear/scene/tgdb/pt_lights.hxx>
70 #include <simgear/structure/OSGUtils.hxx>
71 #include <simgear/props/props.hxx>
72 #include <simgear/timing/sg_time.hxx>
73 #include <simgear/ephemeris/ephemeris.hxx>
74 #include <simgear/math/sg_random.h>
76 #include <simgear/screen/jpgfactory.hxx>
79 #include <simgear/environment/visual_enviro.hxx>
81 #include <Time/light.hxx>
82 #include <Time/light.hxx>
83 #include <Cockpit/panel.hxx>
84 #include <Cockpit/cockpit.hxx>
85 #include <Cockpit/hud.hxx>
86 #include <Model/panelnode.hxx>
87 #include <Model/modelmgr.hxx>
88 #include <Model/acmodel.hxx>
89 #include <Scenery/scenery.hxx>
90 #include <Scenery/redout.hxx>
91 #include <Scenery/tilemgr.hxx>
92 #include <GUI/new_gui.hxx>
93 #include <Instrumentation/instrument_mgr.hxx>
94 #include <Instrumentation/HUD/HUD.hxx>
95 #include <Environment/precipitation_mgr.hxx>
97 #include <Include/general.hxx>
99 #include "renderer.hxx"
101 #include "CameraGroup.hxx"
102 #include "FGEventHandler.hxx"
103 #include <Main/viewer.hxx>
106 using namespace simgear;
107 using namespace flightgear;
109 class FGHintUpdateCallback : public osg::StateAttribute::Callback {
111 FGHintUpdateCallback(const char* configNode) :
112 mConfigNode(fgGetNode(configNode, true))
114 virtual void operator()(osg::StateAttribute* stateAttribute,
117 assert(dynamic_cast<osg::Hint*>(stateAttribute));
118 osg::Hint* hint = static_cast<osg::Hint*>(stateAttribute);
120 const char* value = mConfigNode->getStringValue();
122 hint->setMode(GL_DONT_CARE);
123 else if (0 == strcmp(value, "nicest"))
124 hint->setMode(GL_NICEST);
125 else if (0 == strcmp(value, "fastest"))
126 hint->setMode(GL_FASTEST);
128 hint->setMode(GL_DONT_CARE);
131 SGSharedPtr<SGPropertyNode> mConfigNode;
135 class SGPuDrawable : public osg::Drawable {
139 // Dynamic stuff, do not store geometry
140 setUseDisplayList(false);
141 setDataVariance(Object::DYNAMIC);
143 osg::StateSet* stateSet = getOrCreateStateSet();
144 stateSet->setRenderBinDetails(1001, "RenderBin");
145 // speed optimization?
146 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
147 // We can do translucent menus, so why not. :-)
148 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
149 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
150 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
152 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
154 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
155 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
157 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
158 { drawImplementation(*renderInfo.getState()); }
159 void drawImplementation(osg::State& state) const
161 state.setActiveTextureUnit(0);
162 state.setClientActiveTextureUnit(0);
164 state.disableAllVertexArrays();
166 glPushAttrib(GL_ALL_ATTRIB_BITS);
167 glPushClientAttrib(~0u);
175 virtual osg::Object* cloneType() const { return new SGPuDrawable; }
176 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGPuDrawable; }
181 class SGHUDAndPanelDrawable : public osg::Drawable {
183 SGHUDAndPanelDrawable()
185 // Dynamic stuff, do not store geometry
186 setUseDisplayList(false);
187 setDataVariance(Object::DYNAMIC);
189 osg::StateSet* stateSet = getOrCreateStateSet();
190 stateSet->setRenderBinDetails(1000, "RenderBin");
192 // speed optimization?
193 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
194 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
195 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
196 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
197 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
199 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
201 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
202 { drawImplementation(*renderInfo.getState()); }
203 void drawImplementation(osg::State& state) const
205 state.setActiveTextureUnit(0);
206 state.setClientActiveTextureUnit(0);
207 state.disableAllVertexArrays();
209 glPushAttrib(GL_ALL_ATTRIB_BITS);
210 glPushClientAttrib(~0u);
212 fgCockpitUpdate(&state);
214 FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
215 HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
218 // update the panel subsystem
219 if ( globals->get_current_panel() != NULL )
220 globals->get_current_panel()->update(state);
221 // We don't need a state here - can be safely removed when we can pick
230 virtual osg::Object* cloneType() const { return new SGHUDAndPanelDrawable; }
231 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGHUDAndPanelDrawable; }
236 class FGLightSourceUpdateCallback : public osg::NodeCallback {
240 * @param isSun true if the light is the actual sun i.e., for
241 * illuminating the moon.
243 FGLightSourceUpdateCallback(bool isSun = false) : _isSun(isSun) {}
244 FGLightSourceUpdateCallback(const FGLightSourceUpdateCallback& nc,
246 : NodeCallback(nc, op), _isSun(nc._isSun)
248 META_Object(flightgear,FGLightSourceUpdateCallback);
250 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
252 assert(dynamic_cast<osg::LightSource*>(node));
253 osg::LightSource* lightSource = static_cast<osg::LightSource*>(node);
254 osg::Light* light = lightSource->getLight();
256 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
258 light->setAmbient(Vec4(0.0f, 0.0f, 0.0f, 0.0f));
259 light->setDiffuse(Vec4(1.0f, 1.0f, 1.0f, 1.0f));
260 light->setSpecular(Vec4(0.0f, 0.0f, 0.0f, 0.0f));
262 light->setAmbient(toOsg(l->scene_ambient()));
263 light->setDiffuse(toOsg(l->scene_diffuse()));
264 light->setSpecular(toOsg(l->scene_specular()));
266 osg::Vec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0);
267 light->setPosition(position);
275 class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback {
277 FGWireFrameModeUpdateCallback() :
278 mWireframe(fgGetNode("/sim/rendering/wireframe"))
280 virtual void operator()(osg::StateAttribute* stateAttribute,
283 assert(dynamic_cast<osg::PolygonMode*>(stateAttribute));
284 osg::PolygonMode* polygonMode;
285 polygonMode = static_cast<osg::PolygonMode*>(stateAttribute);
287 if (mWireframe->getBoolValue())
288 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
289 osg::PolygonMode::LINE);
291 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
292 osg::PolygonMode::FILL);
295 SGSharedPtr<SGPropertyNode> mWireframe;
298 class FGLightModelUpdateCallback : public osg::StateAttribute::Callback {
300 FGLightModelUpdateCallback() :
301 mHighlights(fgGetNode("/sim/rendering/specular-highlight"))
303 virtual void operator()(osg::StateAttribute* stateAttribute,
306 assert(dynamic_cast<osg::LightModel*>(stateAttribute));
307 osg::LightModel* lightModel;
308 lightModel = static_cast<osg::LightModel*>(stateAttribute);
311 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
312 lightModel->setAmbientIntensity(toOsg(l->scene_ambient());
314 lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1));
316 lightModel->setTwoSided(true);
317 lightModel->setLocalViewer(false);
319 if (mHighlights->getBoolValue()) {
320 lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
322 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
326 SGSharedPtr<SGPropertyNode> mHighlights;
329 class FGFogEnableUpdateCallback : public osg::StateSet::Callback {
331 FGFogEnableUpdateCallback() :
332 mFogEnabled(fgGetNode("/sim/rendering/fog"))
334 virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*)
336 if (strcmp(mFogEnabled->getStringValue(), "disabled") == 0) {
337 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
339 stateSet->setMode(GL_FOG, osg::StateAttribute::ON);
343 SGSharedPtr<SGPropertyNode> mFogEnabled;
346 class FGFogUpdateCallback : public osg::StateAttribute::Callback {
348 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
350 assert(dynamic_cast<SGUpdateVisitor*>(nv));
351 assert(dynamic_cast<osg::Fog*>(sa));
352 SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
353 osg::Fog* fog = static_cast<osg::Fog*>(sa);
354 fog->setMode(osg::Fog::EXP2);
355 fog->setColor(toOsg(updateVisitor->getFogColor()));
356 fog->setDensity(updateVisitor->getFogExp2Density());
360 // update callback for the switch node guarding that splash
361 class FGScenerySwitchCallback : public osg::NodeCallback {
363 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
365 assert(dynamic_cast<osg::Switch*>(node));
366 osg::Switch* sw = static_cast<osg::Switch*>(node);
368 double t = globals->get_sim_time_sec();
369 bool enabled = 0 < t;
370 sw->setValue(0, enabled);
380 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
381 static osg::ref_ptr<SGUpdateVisitor> mUpdateVisitor= new SGUpdateVisitor;
383 static osg::ref_ptr<osg::Group> mRealRoot = new osg::Group;
385 static osg::ref_ptr<osg::Group> mRoot = new osg::Group;
387 FGRenderer::FGRenderer()
389 #ifdef FG_JPEG_SERVER
390 jpgRenderFrame = FGRenderer::update;
392 eventHandler = new FGEventHandler;
395 FGRenderer::~FGRenderer()
397 #ifdef FG_JPEG_SERVER
398 jpgRenderFrame = NULL;
402 // Initialize various GL/view parameters
403 // XXX This should be called "preinit" or something, as it initializes
404 // critical parts of the scene graph in addition to the splash screen.
406 FGRenderer::splashinit( void ) {
407 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
408 mRealRoot = dynamic_cast<osg::Group*>(viewer->getSceneData());
409 mRealRoot->addChild(fgCreateSplashNode());
410 mFrameStamp = viewer->getFrameStamp();
411 // Scene doesn't seem to pass the frame stamp to the update
412 // visitor automatically.
413 mUpdateVisitor->setFrameStamp(mFrameStamp.get());
414 viewer->setUpdateVisitor(mUpdateVisitor.get());
418 FGRenderer::init( void )
420 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
421 osg::initNotifyLevel();
423 // The number of polygon-offset "units" to place between layers. In
424 // principle, one is supposed to be enough. In practice, I find that
425 // my hardware/driver requires many more.
426 osg::PolygonOffset::setUnitsMultiplier(1);
427 osg::PolygonOffset::setFactorMultiplier(1);
429 // Go full screen if requested ...
430 if ( fgGetBool("/sim/startup/fullscreen") )
434 ->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
436 osg::StateSet* stateSet = mRoot->getOrCreateStateSet();
438 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
440 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS));
441 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
443 stateSet->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01));
444 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
445 stateSet->setAttribute(new osg::BlendFunc);
446 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
448 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
450 // this will be set below
451 stateSet->setMode(GL_NORMALIZE, osg::StateAttribute::OFF);
453 osg::Material* material = new osg::Material;
454 stateSet->setAttribute(material);
456 stateSet->setTextureAttribute(0, new osg::TexEnv);
457 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
459 osg::Hint* hint = new osg::Hint(GL_FOG_HINT, GL_DONT_CARE);
460 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/fog"));
461 stateSet->setAttribute(hint);
462 hint = new osg::Hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
463 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/polygon-smooth"));
464 stateSet->setAttribute(hint);
465 hint = new osg::Hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
466 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/line-smooth"));
467 stateSet->setAttribute(hint);
468 hint = new osg::Hint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
469 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/point-smooth"));
470 stateSet->setAttribute(hint);
471 hint = new osg::Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
472 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/perspective-correction"));
473 stateSet->setAttribute(hint);
475 osg::Group* sceneGroup = new osg::Group;
476 sceneGroup->addChild(globals->get_scenery()->get_scene_graph());
477 sceneGroup->setNodeMask(~simgear::BACKGROUND_BIT);
479 //sceneGroup->addChild(thesky->getCloudRoot());
481 stateSet = sceneGroup->getOrCreateStateSet();
482 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
484 // need to update the light on every frame
485 // OSG LightSource objects are rather confusing. OSG only supports
486 // the 10 lights specified by OpenGL itself; if more than one
487 // LightSource in the scene graph have the same light number, it's
488 // indeterminate which values will be used to render geometry that
489 // has that light number enabled. Also, adding children to a
490 // LightSource is just a shortcut for setting up a state set that
491 // has the corresponding OpenGL light enabled: a LightSource will
492 // affect geometry anywhere in the scene graph that has its light
493 // number enabled in a state set.
494 LightSource* lightSource = new LightSource;
495 lightSource->getLight()->setDataVariance(Object::DYNAMIC);
496 // relative because of CameraView being just a clever transform node
497 lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF);
498 lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
499 lightSource->setUpdateCallback(new FGLightSourceUpdateCallback);
500 mRealRoot->addChild(lightSource);
501 // we need a white diffuse light for the phase of the moon
502 osg::LightSource* sunLight = new osg::LightSource;
503 sunLight->getLight()->setDataVariance(Object::DYNAMIC);
504 sunLight->getLight()->setLightNum(1);
505 sunLight->setUpdateCallback(new FGLightSourceUpdateCallback(true));
506 sunLight->setReferenceFrame(osg::LightSource::RELATIVE_RF);
507 sunLight->setLocalStateSetModes(osg::StateAttribute::ON);
508 // Hang a StateSet above the sky subgraph in order to turn off
510 Group* skyGroup = new Group;
511 StateSet* skySS = skyGroup->getOrCreateStateSet();
512 skySS->setMode(GL_LIGHT0, StateAttribute::OFF);
513 skyGroup->addChild(thesky->getPreRoot());
514 sunLight->addChild(skyGroup);
515 mRoot->addChild(sceneGroup);
516 mRoot->addChild(sunLight);
517 // Clouds are added to the scene graph later
518 stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet();
519 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
520 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
521 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
523 // enable disable specular highlights.
524 // is the place where we might plug in an other fragment shader ...
525 osg::LightModel* lightModel = new osg::LightModel;
526 lightModel->setUpdateCallback(new FGLightModelUpdateCallback);
527 stateSet->setAttribute(lightModel);
529 // switch to enable wireframe
530 osg::PolygonMode* polygonMode = new osg::PolygonMode;
531 polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback);
532 stateSet->setAttributeAndModes(polygonMode);
534 // scene fog handling
535 osg::Fog* fog = new osg::Fog;
536 fog->setUpdateCallback(new FGFogUpdateCallback);
537 stateSet->setAttributeAndModes(fog);
538 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
541 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
543 osg::Geode* geode = new osg::Geode;
544 geode->addDrawable(new SGPuDrawable);
545 geode->addDrawable(new SGHUDAndPanelDrawable);
546 guiCamera->addChild(geode);
548 osg::Switch* sw = new osg::Switch;
549 sw->setUpdateCallback(new FGScenerySwitchCallback);
550 sw->addChild(mRoot.get());
551 mRealRoot->addChild(sw);
552 // The clouds are attached directly to the scene graph root
553 // because, in theory, they don't want the same default state set
554 // as the rest of the scene. This may not be true in practice.
555 mRealRoot->addChild(thesky->getCloudRoot());
556 mRealRoot->addChild(FGCreateRedoutNode());
557 // Attach empty program to the scene root so that shader programs
558 // don't leak into state sets (effects) that shouldn't have one.
559 stateSet = mRealRoot->getOrCreateStateSet();
560 stateSet->setAttributeAndModes(new osg::Program, osg::StateAttribute::ON);
564 // Update all Visuals (redraws anything graphics related)
566 FGRenderer::update( bool refresh_camera_settings ) {
567 bool scenery_loaded = fgGetBool("sim/sceneryloaded")
568 || fgGetBool("sim/sceneryloaded-override");
569 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
570 if ( idle_state < 1000 || !scenery_loaded ) {
571 fgSetDouble("/sim/startup/splash-alpha", 1.0);
573 // Keep resetting sim time while the sim is initializing
574 // the splash screen is now in the scenegraph
575 globals->set_sim_time_sec( 0.0 );
579 // Fade out the splash screen over the first three seconds.
580 double sAlpha = SGMiscd::max(0, (2.5 - globals->get_sim_time_sec()) / 2.5);
581 fgSetDouble("/sim/startup/splash-alpha", sAlpha);
583 bool skyblend = fgGetBool("/sim/rendering/skyblend");
584 bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites");
585 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
586 bool distance_attenuation
587 = fgGetBool("/sim/rendering/distance-attenuation");
589 SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
590 distance_attenuation );
592 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
595 double actual_visibility;
596 if (fgGetBool("/environment/clouds/status")) {
597 actual_visibility = thesky->get_visibility();
599 actual_visibility = fgGetDouble("/environment/visibility-m");
602 // idle_state is now 1000 meaning we've finished all our
603 // initializations and are running the main loop, so this will
604 // now work without seg faulting the system.
606 FGViewer *current__view = globals->get_current_view();
607 // Force update of center dependent values ...
608 current__view->set_dirty();
610 if ( refresh_camera_settings ) {
612 resize( fgGetInt("/sim/startup/xsize"),
613 fgGetInt("/sim/startup/ysize") );
615 osg::Camera *camera = viewer->getCamera();
619 if ( fgGetBool("/sim/rendering/textures") ) {
620 SGVec4f clearColor(l->adj_fog_color());
621 camera->setClearColor(toOsg(clearColor));
624 SGVec4f clearColor(l->sky_color());
625 camera->setClearColor(toOsg(clearColor));
628 // update fog params if visibility has changed
629 double visibility_meters = fgGetDouble("/environment/visibility-m");
630 thesky->set_visibility(visibility_meters);
632 double altitude_m = fgGetDouble("/position/altitude-ft") * SG_FEET_TO_METER;
633 thesky->modify_vis( altitude_m,
634 ( global_multi_loop * fgGetInt("/sim/speed-up") )
635 / (double)fgGetInt("/sim/model-hz") );
637 // update the sky dome
640 // The sun and moon distances are scaled down versions
641 // of the actual distance to get both the moon and the sun
642 // within the range of the far clip plane.
643 // Moon distance: 384,467 kilometers
644 // Sun distance: 150,000,000 kilometers
646 double sun_horiz_eff, moon_horiz_eff;
647 if (fgGetBool("/sim/rendering/horizon-effect")) {
649 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_sun_angle()),
653 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_moon_angle()),
657 sun_horiz_eff = moon_horiz_eff = 1.0;
661 sstate.pos = current__view->getViewPosition();
662 sstate.pos_geod = current__view->getPosition();
663 sstate.ori = current__view->getViewOrientation();
664 sstate.spin = l->get_sun_rotation();
665 sstate.gst = globals->get_time_params()->getGst();
666 sstate.sun_dist = 50000.0 * sun_horiz_eff;
667 sstate.moon_dist = 40000.0 * moon_horiz_eff;
668 sstate.sun_angle = l->get_sun_angle();
671 scolor.sky_color = SGVec3f(l->sky_color().data());
672 scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data());
673 scolor.fog_color = SGVec3f(l->adj_fog_color().data());
674 scolor.cloud_color = SGVec3f(l->cloud_color().data());
675 scolor.sun_angle = l->get_sun_angle();
676 scolor.moon_angle = l->get_moon_angle();
678 thesky->reposition( sstate, *globals->get_ephem(), delta_time_sec );
679 thesky->repaint( scolor, *globals->get_ephem() );
682 // shadows->setupShadows(
683 // current__view->getLongitude_deg(),
684 // current__view->getLatitude_deg(),
685 // globals->get_time_params()->getGst(),
686 // globals->get_ephem()->getSunRightAscension(),
687 // globals->get_ephem()->getSunDeclination(),
688 // l->get_sun_angle());
692 // sgEnviro.setLight(l->adj_fog_color());
693 // sgEnviro.startOfFrame(current__view->get_view_pos(),
694 // current__view->get_world_up(),
695 // current__view->getLongitude_deg(),
696 // current__view->getLatitude_deg(),
697 // current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
701 // sgEnviro.drawLightning();
703 // double current_view_origin_airspeed_horiz_kt =
704 // fgGetDouble("/velocities/airspeed-kt", 0.0)
705 // * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
706 // * SGD_DEGREES_TO_RADIANS);
710 // shadows->endOfFrame();
712 // need to call the update visitor once
713 mFrameStamp->setCalendarTime(*globals->get_time_params()->getGmt());
714 mUpdateVisitor->setViewData(current__view->getViewPosition(),
715 current__view->getViewOrientation());
716 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
717 mUpdateVisitor->setLight(direction, l->scene_ambient(),
718 l->scene_diffuse(), l->scene_specular(),
720 l->get_sun_angle()*SGD_RADIANS_TO_DEGREES);
721 mUpdateVisitor->setVisibility(actual_visibility);
722 simgear::GroundLightManager::instance()->update(mUpdateVisitor.get());
723 bool hotspots = fgGetBool("/sim/panel-hotspots");
724 osg::Node::NodeMask cullMask = ~simgear::LIGHTS_BITS & ~simgear::PICK_BIT;
725 cullMask |= simgear::GroundLightManager::instance()
726 ->getLightNodeMask(mUpdateVisitor.get());
728 cullMask |= simgear::PICK_BIT;
729 CameraGroup::getDefault()->setCameraCullMasks(cullMask);
734 // options.cxx needs to see this for toggle_panel()
735 // Handle new window size or exposure
737 FGRenderer::resize( int width, int height ) {
740 if ( (!fgGetBool("/sim/virtual-cockpit"))
741 && fgPanelVisible() && idle_state == 1000 ) {
742 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
743 globals->get_current_panel()->getYOffset()) / 768.0);
748 static int lastwidth = 0;
749 static int lastheight = 0;
750 if (width != lastwidth)
751 fgSetInt("/sim/startup/xsize", lastwidth = width);
752 if (height != lastheight)
753 fgSetInt("/sim/startup/ysize", lastheight = height);
756 FGViewMgr *viewmgr = globals->get_viewmgr();
758 for ( int i = 0; i < viewmgr->size(); ++i ) {
759 viewmgr->get_view(i)->
760 set_aspect_ratio((float)view_h / (float)width);
766 FGRenderer::pick(std::vector<SGSceneryPick>& pickList,
767 const osgGA::GUIEventAdapter* ea)
769 // wipe out the return ...
771 typedef osgUtil::LineSegmentIntersector::Intersections Intersections;
772 Intersections intersections;
774 if (!computeIntersections(CameraGroup::getDefault(), ea, intersections))
776 for (Intersections::iterator hit = intersections.begin(),
777 e = intersections.end();
780 const osg::NodePath& np = hit->nodePath;
781 osg::NodePath::const_reverse_iterator npi;
782 for (npi = np.rbegin(); npi != np.rend(); ++npi) {
783 SGSceneUserData* ud = SGSceneUserData::getSceneUserData(*npi);
786 for (unsigned i = 0; i < ud->getNumPickCallbacks(); ++i) {
787 SGPickCallback* pickCallback = ud->getPickCallback(i);
790 SGSceneryPick sceneryPick;
791 sceneryPick.info.local = toSG(hit->getLocalIntersectPoint());
792 sceneryPick.info.wgs84 = toSG(hit->getWorldIntersectPoint());
793 sceneryPick.callback = pickCallback;
794 pickList.push_back(sceneryPick);
798 return !pickList.empty();
802 FGRenderer::setViewer(osgViewer::Viewer* viewer_)
808 FGRenderer::setEventHandler(FGEventHandler* eventHandler_)
810 eventHandler = eventHandler_;
814 FGRenderer::addCamera(osg::Camera* camera, bool useSceneData)
816 mRealRoot->addChild(camera);
820 fgDumpSceneGraphToFile(const char* filename)
822 return osgDB::writeNodeFile(*mRealRoot.get(), filename);
826 fgDumpTerrainBranchToFile(const char* filename)
828 return osgDB::writeNodeFile( *globals->get_scenery()->get_terrain_branch(),
834 fgDumpNodeToFile(osg::Node* node, const char* filename)
836 return osgDB::writeNodeFile(*node, filename);
838 // end of renderer.cxx