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/Version>
53 #include <osgUtil/LineSegmentIntersector>
55 #include <osg/io_utils>
56 #include <osgDB/WriteFile>
58 #include <simgear/math/SGMath.hxx>
59 #include <simgear/screen/extensions.hxx>
60 #include <simgear/scene/material/matlib.hxx>
61 #include <simgear/scene/model/animation.hxx>
62 #include <simgear/scene/model/placement.hxx>
63 #include <simgear/scene/sky/sky.hxx>
64 #include <simgear/scene/util/SGUpdateVisitor.hxx>
65 #include <simgear/scene/util/RenderConstants.hxx>
66 #include <simgear/scene/util/SGSceneUserData.hxx>
67 #include <simgear/scene/tgdb/GroundLightManager.hxx>
68 #include <simgear/scene/tgdb/pt_lights.hxx>
69 #include <simgear/props/props.hxx>
70 #include <simgear/timing/sg_time.hxx>
71 #include <simgear/ephemeris/ephemeris.hxx>
72 #include <simgear/math/sg_random.h>
74 #include <simgear/screen/jpgfactory.hxx>
77 #include <simgear/environment/visual_enviro.hxx>
79 #include <Time/light.hxx>
80 #include <Time/light.hxx>
81 #include <Aircraft/aircraft.hxx>
82 #include <Cockpit/panel.hxx>
83 #include <Cockpit/cockpit.hxx>
84 #include <Cockpit/hud.hxx>
85 #include <Model/panelnode.hxx>
86 #include <Model/modelmgr.hxx>
87 #include <Model/acmodel.hxx>
88 #include <Scenery/scenery.hxx>
89 #include <Scenery/redout.hxx>
90 #include <Scenery/tilemgr.hxx>
91 #include <GUI/new_gui.hxx>
92 #include <Instrumentation/instrument_mgr.hxx>
93 #include <Instrumentation/HUD/HUD.hxx>
94 #include <Environment/precipitation_mgr.hxx>
96 #include <Include/general.hxx>
98 #include "renderer.hxx"
100 #include "CameraGroup.hxx"
101 #include "FGEventHandler.hxx"
102 #include <Main/viewer.hxx>
105 using namespace simgear;
106 using namespace flightgear;
108 class FGHintUpdateCallback : public osg::StateAttribute::Callback {
110 FGHintUpdateCallback(const char* configNode) :
111 mConfigNode(fgGetNode(configNode, true))
113 virtual void operator()(osg::StateAttribute* stateAttribute,
116 assert(dynamic_cast<osg::Hint*>(stateAttribute));
117 osg::Hint* hint = static_cast<osg::Hint*>(stateAttribute);
119 const char* value = mConfigNode->getStringValue();
121 hint->setMode(GL_DONT_CARE);
122 else if (0 == strcmp(value, "nicest"))
123 hint->setMode(GL_NICEST);
124 else if (0 == strcmp(value, "fastest"))
125 hint->setMode(GL_FASTEST);
127 hint->setMode(GL_DONT_CARE);
130 SGSharedPtr<SGPropertyNode> mConfigNode;
134 class SGPuDrawable : public osg::Drawable {
138 // Dynamic stuff, do not store geometry
139 setUseDisplayList(false);
141 osg::StateSet* stateSet = getOrCreateStateSet();
142 stateSet->setRenderBinDetails(1001, "RenderBin");
143 // speed optimization?
144 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
145 // We can do translucent menus, so why not. :-)
146 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
147 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
148 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
150 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
152 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
153 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
155 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
156 { drawImplementation(*renderInfo.getState()); }
157 void drawImplementation(osg::State& state) const
159 state.setActiveTextureUnit(0);
160 state.setClientActiveTextureUnit(0);
162 state.disableAllVertexArrays();
164 glPushAttrib(GL_ALL_ATTRIB_BITS);
165 glPushClientAttrib(~0u);
173 virtual osg::Object* cloneType() const { return new SGPuDrawable; }
174 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGPuDrawable; }
179 class SGHUDAndPanelDrawable : public osg::Drawable {
181 SGHUDAndPanelDrawable()
183 // Dynamic stuff, do not store geometry
184 setUseDisplayList(false);
186 osg::StateSet* stateSet = getOrCreateStateSet();
187 stateSet->setRenderBinDetails(1000, "RenderBin");
189 // speed optimization?
190 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
191 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
192 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
193 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
194 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
196 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
198 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
199 { drawImplementation(*renderInfo.getState()); }
200 void drawImplementation(osg::State& state) const
202 state.setActiveTextureUnit(0);
203 state.setClientActiveTextureUnit(0);
204 state.disableAllVertexArrays();
206 glPushAttrib(GL_ALL_ATTRIB_BITS);
207 glPushClientAttrib(~0u);
209 fgCockpitUpdate(&state);
211 FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
212 HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
215 // update the panel subsystem
216 if ( globals->get_current_panel() != NULL )
217 globals->get_current_panel()->update(state);
218 // We don't need a state here - can be safely removed when we can pick
227 virtual osg::Object* cloneType() const { return new SGHUDAndPanelDrawable; }
228 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGHUDAndPanelDrawable; }
233 class FGLightSourceUpdateCallback : public osg::NodeCallback {
235 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
237 assert(dynamic_cast<osg::LightSource*>(node));
238 osg::LightSource* lightSource = static_cast<osg::LightSource*>(node);
239 osg::Light* light = lightSource->getLight();
241 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
242 light->setAmbient(toOsg(l->scene_ambient()));
243 light->setDiffuse(toOsg(l->scene_diffuse()));
244 light->setSpecular(toOsg(l->scene_specular()));
245 osg::Vec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0);
246 light->setPosition(position);
247 osg::Vec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
248 light->setDirection(direction);
249 light->setSpotExponent(0);
250 light->setSpotCutoff(180);
251 light->setConstantAttenuation(1);
252 light->setLinearAttenuation(0);
253 light->setQuadraticAttenuation(0);
259 class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback {
261 FGWireFrameModeUpdateCallback() :
262 mWireframe(fgGetNode("/sim/rendering/wireframe"))
264 virtual void operator()(osg::StateAttribute* stateAttribute,
267 assert(dynamic_cast<osg::PolygonMode*>(stateAttribute));
268 osg::PolygonMode* polygonMode;
269 polygonMode = static_cast<osg::PolygonMode*>(stateAttribute);
271 if (mWireframe->getBoolValue())
272 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
273 osg::PolygonMode::LINE);
275 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
276 osg::PolygonMode::FILL);
279 SGSharedPtr<SGPropertyNode> mWireframe;
282 class FGLightModelUpdateCallback : public osg::StateAttribute::Callback {
284 FGLightModelUpdateCallback() :
285 mHighlights(fgGetNode("/sim/rendering/specular-highlight"))
287 virtual void operator()(osg::StateAttribute* stateAttribute,
290 assert(dynamic_cast<osg::LightModel*>(stateAttribute));
291 osg::LightModel* lightModel;
292 lightModel = static_cast<osg::LightModel*>(stateAttribute);
295 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
296 lightModel->setAmbientIntensity(toOsg(l->scene_ambient());
298 lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1));
300 lightModel->setTwoSided(true);
301 lightModel->setLocalViewer(false);
303 if (mHighlights->getBoolValue()) {
304 lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
306 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
310 SGSharedPtr<SGPropertyNode> mHighlights;
313 class FGFogEnableUpdateCallback : public osg::StateSet::Callback {
315 FGFogEnableUpdateCallback() :
316 mFogEnabled(fgGetNode("/sim/rendering/fog"))
318 virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*)
320 if (strcmp(mFogEnabled->getStringValue(), "disabled") == 0) {
321 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
323 stateSet->setMode(GL_FOG, osg::StateAttribute::ON);
327 SGSharedPtr<SGPropertyNode> mFogEnabled;
330 class FGFogUpdateCallback : public osg::StateAttribute::Callback {
332 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
334 assert(dynamic_cast<SGUpdateVisitor*>(nv));
335 assert(dynamic_cast<osg::Fog*>(sa));
336 SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
337 osg::Fog* fog = static_cast<osg::Fog*>(sa);
338 fog->setMode(osg::Fog::EXP2);
339 fog->setColor(toOsg(updateVisitor->getFogColor()));
340 fog->setDensity(updateVisitor->getFogExp2Density());
344 // update callback for the switch node guarding that splash
345 class FGScenerySwitchCallback : public osg::NodeCallback {
347 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
349 assert(dynamic_cast<osg::Switch*>(node));
350 osg::Switch* sw = static_cast<osg::Switch*>(node);
352 double t = globals->get_sim_time_sec();
353 bool enabled = 0 < t;
354 sw->setValue(0, enabled);
364 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
365 static osg::ref_ptr<SGUpdateVisitor> mUpdateVisitor= new SGUpdateVisitor;
367 static osg::ref_ptr<osg::Group> mRealRoot = new osg::Group;
369 static osg::ref_ptr<osg::Group> mRoot = new osg::Group;
371 FGRenderer::FGRenderer()
373 #ifdef FG_JPEG_SERVER
374 jpgRenderFrame = FGRenderer::update;
376 eventHandler = new FGEventHandler;
379 FGRenderer::~FGRenderer()
381 #ifdef FG_JPEG_SERVER
382 jpgRenderFrame = NULL;
386 // Initialize various GL/view parameters
387 // XXX This should be called "preinit" or something, as it initializes
388 // critical parts of the scene graph in addition to the splash screen.
390 FGRenderer::splashinit( void ) {
391 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
392 mRealRoot = dynamic_cast<osg::Group*>(viewer->getSceneData());
393 mRealRoot->addChild(fgCreateSplashNode());
394 mFrameStamp = viewer->getFrameStamp();
395 // Scene doesn't seem to pass the frame stamp to the update
396 // visitor automatically.
397 mUpdateVisitor->setFrameStamp(mFrameStamp.get());
398 viewer->setUpdateVisitor(mUpdateVisitor.get());
402 FGRenderer::init( void )
404 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
405 osg::initNotifyLevel();
407 // The number of polygon-offset "units" to place between layers. In
408 // principle, one is supposed to be enough. In practice, I find that
409 // my hardware/driver requires many more.
410 osg::PolygonOffset::setUnitsMultiplier(1);
411 osg::PolygonOffset::setFactorMultiplier(1);
413 // Go full screen if requested ...
414 if ( fgGetBool("/sim/startup/fullscreen") )
418 ->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
420 osg::StateSet* stateSet = mRoot->getOrCreateStateSet();
422 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
424 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS));
425 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
427 stateSet->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01));
428 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
429 stateSet->setAttribute(new osg::BlendFunc);
430 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
432 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
434 // this will be set below
435 stateSet->setMode(GL_NORMALIZE, osg::StateAttribute::OFF);
437 osg::Material* material = new osg::Material;
438 stateSet->setAttribute(material);
440 stateSet->setTextureAttribute(0, new osg::TexEnv);
441 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
443 osg::Hint* hint = new osg::Hint(GL_FOG_HINT, GL_DONT_CARE);
444 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/fog"));
445 stateSet->setAttribute(hint);
446 hint = new osg::Hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
447 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/polygon-smooth"));
448 stateSet->setAttribute(hint);
449 hint = new osg::Hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
450 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/line-smooth"));
451 stateSet->setAttribute(hint);
452 hint = new osg::Hint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
453 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/point-smooth"));
454 stateSet->setAttribute(hint);
455 hint = new osg::Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
456 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/perspective-correction"));
457 stateSet->setAttribute(hint);
459 osg::Group* sceneGroup = new osg::Group;
460 sceneGroup->addChild(globals->get_scenery()->get_scene_graph());
461 sceneGroup->setNodeMask(~simgear::BACKGROUND_BIT);
463 //sceneGroup->addChild(thesky->getCloudRoot());
465 stateSet = sceneGroup->getOrCreateStateSet();
466 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
468 // need to update the light on every frame
469 osg::LightSource* lightSource = new osg::LightSource;
470 lightSource->setUpdateCallback(new FGLightSourceUpdateCallback);
471 // relative because of CameraView being just a clever transform node
472 lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF);
473 lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
475 lightSource->addChild(sceneGroup);
476 lightSource->addChild(thesky->getPreRoot());
477 mRoot->addChild(lightSource);
479 stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet();
480 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
481 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
482 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
484 // enable disable specular highlights.
485 // is the place where we might plug in an other fragment shader ...
486 osg::LightModel* lightModel = new osg::LightModel;
487 lightModel->setUpdateCallback(new FGLightModelUpdateCallback);
488 stateSet->setAttribute(lightModel);
490 // switch to enable wireframe
491 osg::PolygonMode* polygonMode = new osg::PolygonMode;
492 polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback);
493 stateSet->setAttributeAndModes(polygonMode);
495 // scene fog handling
496 osg::Fog* fog = new osg::Fog;
497 fog->setUpdateCallback(new FGFogUpdateCallback);
498 stateSet->setAttributeAndModes(fog);
499 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
502 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
504 osg::Geode* geode = new osg::Geode;
505 geode->addDrawable(new SGPuDrawable);
506 geode->addDrawable(new SGHUDAndPanelDrawable);
507 guiCamera->addChild(geode);
509 osg::Switch* sw = new osg::Switch;
510 sw->setUpdateCallback(new FGScenerySwitchCallback);
511 sw->addChild(mRoot.get());
512 mRealRoot->addChild(sw);
513 mRealRoot->addChild(thesky->getCloudRoot());
514 mRealRoot->addChild(FGCreateRedoutNode());
518 // Update all Visuals (redraws anything graphics related)
520 FGRenderer::update( bool refresh_camera_settings ) {
521 bool scenery_loaded = fgGetBool("sim/sceneryloaded")
522 || fgGetBool("sim/sceneryloaded-override");
523 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
524 if ( idle_state < 1000 || !scenery_loaded ) {
525 fgSetDouble("/sim/startup/splash-alpha", 1.0);
527 // Keep resetting sim time while the sim is initializing
528 // the splash screen is now in the scenegraph
529 globals->set_sim_time_sec( 0.0 );
533 // Fade out the splash screen over the first three seconds.
534 double sAlpha = SGMiscd::max(0, (2.5 - globals->get_sim_time_sec()) / 2.5);
535 fgSetDouble("/sim/startup/splash-alpha", sAlpha);
537 bool skyblend = fgGetBool("/sim/rendering/skyblend");
538 bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites");
539 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
540 bool distance_attenuation
541 = fgGetBool("/sim/rendering/distance-attenuation");
543 SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
544 distance_attenuation );
546 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
549 double actual_visibility;
550 if (fgGetBool("/environment/clouds/status")) {
551 actual_visibility = thesky->get_visibility();
553 actual_visibility = fgGetDouble("/environment/visibility-m");
556 // idle_state is now 1000 meaning we've finished all our
557 // initializations and are running the main loop, so this will
558 // now work without seg faulting the system.
560 FGViewer *current__view = globals->get_current_view();
561 // Force update of center dependent values ...
562 current__view->set_dirty();
564 if ( refresh_camera_settings ) {
566 resize( fgGetInt("/sim/startup/xsize"),
567 fgGetInt("/sim/startup/ysize") );
569 osg::Camera *camera = viewer->getCamera();
573 if ( fgGetBool("/sim/rendering/textures") ) {
574 SGVec4f clearColor(l->adj_fog_color());
575 camera->setClearColor(toOsg(clearColor));
578 SGVec4f clearColor(l->sky_color());
579 camera->setClearColor(toOsg(clearColor));
582 // update fog params if visibility has changed
583 double visibility_meters = fgGetDouble("/environment/visibility-m");
584 thesky->set_visibility(visibility_meters);
586 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
587 ( global_multi_loop * fgGetInt("/sim/speed-up") )
588 / (double)fgGetInt("/sim/model-hz") );
590 // update the sky dome
593 // The sun and moon distances are scaled down versions
594 // of the actual distance to get both the moon and the sun
595 // within the range of the far clip plane.
596 // Moon distance: 384,467 kilometers
597 // Sun distance: 150,000,000 kilometers
599 double sun_horiz_eff, moon_horiz_eff;
600 if (fgGetBool("/sim/rendering/horizon-effect")) {
602 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_sun_angle()),
606 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_moon_angle()),
610 sun_horiz_eff = moon_horiz_eff = 1.0;
615 SGVec3d viewPos = current__view->getViewPosition();
616 sstate.view_pos = toVec3f(viewPos);
617 SGGeod geodViewPos = SGGeod::fromCart(viewPos);
618 SGGeod geodZeroViewPos = SGGeod::fromGeodM(geodViewPos, 0);
619 sstate.zero_elev = toVec3f(SGVec3d::fromGeod(geodZeroViewPos));
620 SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos);
621 sstate.view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
622 sstate.lon = geodViewPos.getLongitudeRad();
623 sstate.lat = geodViewPos.getLatitudeRad();
624 sstate.alt = geodViewPos.getElevationM();
625 sstate.spin = l->get_sun_rotation();
626 sstate.gst = globals->get_time_params()->getGst();
627 sstate.sun_dist = 50000.0 * sun_horiz_eff;
628 sstate.moon_dist = 40000.0 * moon_horiz_eff;
629 sstate.sun_angle = l->get_sun_angle();
633 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
634 << l->sky_color()[0] << " "
635 << l->sky_color()[1] << " "
636 << l->sky_color()[2] << " "
637 << l->sky_color()[3] );
638 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
639 << l->fog_color()[0] << " "
640 << l->fog_color()[1] << " "
641 << l->fog_color()[2] << " "
642 << l->fog_color()[3] );
643 SG_LOG( SG_GENERAL, SG_BULK,
644 " sun_angle = " << l->sun_angle
645 << " moon_angle = " << l->moon_angle );
650 scolor.sky_color = SGVec3f(l->sky_color().data());
651 scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data());
652 scolor.fog_color = SGVec3f(l->adj_fog_color().data());
653 scolor.cloud_color = SGVec3f(l->cloud_color().data());
654 scolor.sun_angle = l->get_sun_angle();
655 scolor.moon_angle = l->get_moon_angle();
657 thesky->reposition( sstate, *globals->get_ephem(), delta_time_sec );
658 thesky->repaint( scolor, *globals->get_ephem() );
661 SG_LOG( SG_GENERAL, SG_BULK,
662 "thesky->reposition( view_pos = " << view_pos[0] << " "
663 << view_pos[1] << " " << view_pos[2] );
664 SG_LOG( SG_GENERAL, SG_BULK,
665 " zero_elev = " << zero_elev[0] << " "
666 << zero_elev[1] << " " << zero_elev[2]
667 << " lon = " << cur_fdm_state->get_Longitude()
668 << " lat = " << cur_fdm_state->get_Latitude() );
669 SG_LOG( SG_GENERAL, SG_BULK,
670 " sun_rot = " << l->get_sun_rotation
671 << " gst = " << SGTime::cur_time_params->getGst() );
672 SG_LOG( SG_GENERAL, SG_BULK,
673 " sun ra = " << globals->get_ephem()->getSunRightAscension()
674 << " sun dec = " << globals->get_ephem()->getSunDeclination()
675 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
676 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
680 // shadows->setupShadows(
681 // current__view->getLongitude_deg(),
682 // current__view->getLatitude_deg(),
683 // globals->get_time_params()->getGst(),
684 // globals->get_ephem()->getSunRightAscension(),
685 // globals->get_ephem()->getSunDeclination(),
686 // l->get_sun_angle());
690 // sgEnviro.setLight(l->adj_fog_color());
691 // sgEnviro.startOfFrame(current__view->get_view_pos(),
692 // current__view->get_world_up(),
693 // current__view->getLongitude_deg(),
694 // current__view->getLatitude_deg(),
695 // current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
699 // sgEnviro.drawLightning();
701 // double current_view_origin_airspeed_horiz_kt =
702 // fgGetDouble("/velocities/airspeed-kt", 0.0)
703 // * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
704 // * SGD_DEGREES_TO_RADIANS);
708 // shadows->endOfFrame();
710 // need to call the update visitor once
711 mFrameStamp->setCalendarTime(*globals->get_time_params()->getGmt());
712 mUpdateVisitor->setViewData(current__view->getViewPosition(),
713 current__view->getViewOrientation());
714 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
715 mUpdateVisitor->setLight(direction, l->scene_ambient(),
716 l->scene_diffuse(), l->scene_specular(),
718 l->get_sun_angle()*SGD_RADIANS_TO_DEGREES);
719 mUpdateVisitor->setVisibility(actual_visibility);
720 simgear::GroundLightManager::instance()->update(mUpdateVisitor.get());
721 bool hotspots = fgGetBool("/sim/panel-hotspots");
722 osg::Node::NodeMask cullMask = ~simgear::LIGHTS_BITS & ~simgear::PICK_BIT;
723 cullMask |= simgear::GroundLightManager::instance()
724 ->getLightNodeMask(mUpdateVisitor.get());
726 cullMask |= simgear::PICK_BIT;
727 CameraGroup::getDefault()->setCameraCullMasks(cullMask);
732 // options.cxx needs to see this for toggle_panel()
733 // Handle new window size or exposure
735 FGRenderer::resize( int width, int height ) {
738 if ( (!fgGetBool("/sim/virtual-cockpit"))
739 && fgPanelVisible() && idle_state == 1000 ) {
740 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
741 globals->get_current_panel()->getYOffset()) / 768.0);
746 static int lastwidth = 0;
747 static int lastheight = 0;
748 if (width != lastwidth)
749 fgSetInt("/sim/startup/xsize", lastwidth = width);
750 if (height != lastheight)
751 fgSetInt("/sim/startup/ysize", lastheight = height);
754 FGViewMgr *viewmgr = globals->get_viewmgr();
756 for ( int i = 0; i < viewmgr->size(); ++i ) {
757 viewmgr->get_view(i)->
758 set_aspect_ratio((float)view_h / (float)width);
764 FGRenderer::pick(std::vector<SGSceneryPick>& pickList,
765 const osgGA::GUIEventAdapter* ea)
767 // wipe out the return ...
769 typedef osgUtil::LineSegmentIntersector::Intersections Intersections;
770 Intersections intersections;
772 if (!computeIntersections(CameraGroup::getDefault(), ea, intersections))
774 for (Intersections::iterator hit = intersections.begin(),
775 e = intersections.end();
778 const osg::NodePath& np = hit->nodePath;
779 osg::NodePath::const_reverse_iterator npi;
780 for (npi = np.rbegin(); npi != np.rend(); ++npi) {
781 SGSceneUserData* ud = SGSceneUserData::getSceneUserData(*npi);
784 for (unsigned i = 0; i < ud->getNumPickCallbacks(); ++i) {
785 SGPickCallback* pickCallback = ud->getPickCallback(i);
788 SGSceneryPick sceneryPick;
789 sceneryPick.info.local = toSG(hit->getLocalIntersectPoint());
790 sceneryPick.info.wgs84 = toSG(hit->getWorldIntersectPoint());
791 sceneryPick.callback = pickCallback;
792 pickList.push_back(sceneryPick);
796 return !pickList.empty();
800 FGRenderer::setViewer(osgViewer::Viewer* viewer_)
806 FGRenderer::setEventHandler(FGEventHandler* eventHandler_)
808 eventHandler = eventHandler_;
812 FGRenderer::addCamera(osg::Camera* camera, bool useSceneData)
814 mRealRoot->addChild(camera);
818 fgDumpSceneGraphToFile(const char* filename)
820 return osgDB::writeNodeFile(*mRealRoot.get(), filename);
824 fgDumpTerrainBranchToFile(const char* filename)
826 return osgDB::writeNodeFile( *globals->get_scenery()->get_terrain_branch(),
832 fgDumpNodeToFile(osg::Node* node, const char* filename)
834 return osgDB::writeNodeFile(*node, filename);
836 // end of renderer.cxx