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>
45 #include <osg/NodeCallback>
47 #include <osg/PolygonMode>
48 #include <osg/PolygonOffset>
49 #include <osg/Version>
52 #include <osgUtil/LineSegmentIntersector>
54 #include <osg/io_utils>
55 #include <osgDB/WriteFile>
57 #include <simgear/math/SGMath.hxx>
58 #include <simgear/screen/extensions.hxx>
59 #include <simgear/scene/material/matlib.hxx>
60 #include <simgear/scene/model/animation.hxx>
61 #include <simgear/scene/model/placement.hxx>
62 #include <simgear/scene/sky/sky.hxx>
63 #include <simgear/scene/util/SGUpdateVisitor.hxx>
64 #include <simgear/scene/util/RenderConstants.hxx>
65 #include <simgear/scene/tgdb/GroundLightManager.hxx>
66 #include <simgear/scene/tgdb/pt_lights.hxx>
67 #include <simgear/props/props.hxx>
68 #include <simgear/timing/sg_time.hxx>
69 #include <simgear/ephemeris/ephemeris.hxx>
70 #include <simgear/math/sg_random.h>
72 #include <simgear/screen/jpgfactory.hxx>
75 #include <simgear/environment/visual_enviro.hxx>
77 #include <Time/light.hxx>
78 #include <Time/light.hxx>
79 #include <Aircraft/aircraft.hxx>
80 #include <Cockpit/panel.hxx>
81 #include <Cockpit/cockpit.hxx>
82 #include <Cockpit/hud.hxx>
83 #include <Model/panelnode.hxx>
84 #include <Model/modelmgr.hxx>
85 #include <Model/acmodel.hxx>
86 #include <Scenery/scenery.hxx>
87 #include <Scenery/redout.hxx>
88 #include <Scenery/tilemgr.hxx>
89 #include <GUI/new_gui.hxx>
90 #include <Instrumentation/instrument_mgr.hxx>
91 #include <Instrumentation/HUD/HUD.hxx>
92 #include <Environment/precipitation_mgr.hxx>
94 #include <Include/general.hxx>
96 #include "renderer.hxx"
98 #include "CameraGroup.hxx"
99 #include "ViewPartitionNode.hxx"
101 // XXX Make this go away when OSG 2.2 is released.
102 #if (FG_OSG_VERSION >= 21004)
103 #define UPDATE_VISITOR_IN_VIEWER 1
107 using namespace simgear;
108 using namespace flightgear;
110 class FGHintUpdateCallback : public osg::StateAttribute::Callback {
112 FGHintUpdateCallback(const char* configNode) :
113 mConfigNode(fgGetNode(configNode, true))
115 virtual void operator()(osg::StateAttribute* stateAttribute,
118 assert(dynamic_cast<osg::Hint*>(stateAttribute));
119 osg::Hint* hint = static_cast<osg::Hint*>(stateAttribute);
121 const char* value = mConfigNode->getStringValue();
123 hint->setMode(GL_DONT_CARE);
124 else if (0 == strcmp(value, "nicest"))
125 hint->setMode(GL_NICEST);
126 else if (0 == strcmp(value, "fastest"))
127 hint->setMode(GL_FASTEST);
129 hint->setMode(GL_DONT_CARE);
132 SGSharedPtr<SGPropertyNode> mConfigNode;
136 class SGPuDrawable : public osg::Drawable {
140 // Dynamic stuff, do not store geometry
141 setUseDisplayList(false);
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);
188 osg::StateSet* stateSet = getOrCreateStateSet();
189 stateSet->setRenderBinDetails(1000, "RenderBin");
191 // speed optimization?
192 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
193 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
194 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
195 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
196 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
198 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
200 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
201 { drawImplementation(*renderInfo.getState()); }
202 void drawImplementation(osg::State& state) const
204 state.setActiveTextureUnit(0);
205 state.setClientActiveTextureUnit(0);
206 state.disableAllVertexArrays();
208 glPushAttrib(GL_ALL_ATTRIB_BITS);
209 glPushClientAttrib(~0u);
211 fgCockpitUpdate(&state);
213 FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
214 HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
217 // update the panel subsystem
218 if ( globals->get_current_panel() != NULL )
219 globals->get_current_panel()->update(state);
220 // We don't need a state here - can be safely removed when we can pick
229 virtual osg::Object* cloneType() const { return new SGHUDAndPanelDrawable; }
230 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGHUDAndPanelDrawable; }
235 class FGLightSourceUpdateCallback : public osg::NodeCallback {
237 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
239 assert(dynamic_cast<osg::LightSource*>(node));
240 osg::LightSource* lightSource = static_cast<osg::LightSource*>(node);
241 osg::Light* light = lightSource->getLight();
243 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
244 light->setAmbient(l->scene_ambient().osg());
245 light->setDiffuse(l->scene_diffuse().osg());
246 light->setSpecular(l->scene_specular().osg());
247 SGVec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0);
248 light->setPosition(position.osg());
249 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
250 light->setDirection(direction.osg());
251 light->setSpotExponent(0);
252 light->setSpotCutoff(180);
253 light->setConstantAttenuation(1);
254 light->setLinearAttenuation(0);
255 light->setQuadraticAttenuation(0);
261 class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback {
263 FGWireFrameModeUpdateCallback() :
264 mWireframe(fgGetNode("/sim/rendering/wireframe"))
266 virtual void operator()(osg::StateAttribute* stateAttribute,
269 assert(dynamic_cast<osg::PolygonMode*>(stateAttribute));
270 osg::PolygonMode* polygonMode;
271 polygonMode = static_cast<osg::PolygonMode*>(stateAttribute);
273 if (mWireframe->getBoolValue())
274 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
275 osg::PolygonMode::LINE);
277 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
278 osg::PolygonMode::FILL);
281 SGSharedPtr<SGPropertyNode> mWireframe;
284 class FGLightModelUpdateCallback : public osg::StateAttribute::Callback {
286 FGLightModelUpdateCallback() :
287 mHighlights(fgGetNode("/sim/rendering/specular-highlight"))
289 virtual void operator()(osg::StateAttribute* stateAttribute,
292 assert(dynamic_cast<osg::LightModel*>(stateAttribute));
293 osg::LightModel* lightModel;
294 lightModel = static_cast<osg::LightModel*>(stateAttribute);
297 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
298 lightModel->setAmbientIntensity(l->scene_ambient().osg());
300 lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1));
302 lightModel->setTwoSided(true);
303 lightModel->setLocalViewer(false);
305 if (mHighlights->getBoolValue()) {
306 lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
308 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
312 SGSharedPtr<SGPropertyNode> mHighlights;
315 class FGFogEnableUpdateCallback : public osg::StateSet::Callback {
317 FGFogEnableUpdateCallback() :
318 mFogEnabled(fgGetNode("/sim/rendering/fog"))
320 virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*)
322 if (strcmp(mFogEnabled->getStringValue(), "disabled") == 0) {
323 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
325 stateSet->setMode(GL_FOG, osg::StateAttribute::ON);
329 SGSharedPtr<SGPropertyNode> mFogEnabled;
332 class FGFogUpdateCallback : public osg::StateAttribute::Callback {
334 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
336 assert(dynamic_cast<SGUpdateVisitor*>(nv));
337 assert(dynamic_cast<osg::Fog*>(sa));
338 SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
339 osg::Fog* fog = static_cast<osg::Fog*>(sa);
340 fog->setMode(osg::Fog::EXP2);
341 fog->setColor(updateVisitor->getFogColor().osg());
342 fog->setDensity(updateVisitor->getFogExp2Density());
346 // update callback for the switch node guarding that splash
347 class FGScenerySwitchCallback : public osg::NodeCallback {
349 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
351 assert(dynamic_cast<osg::Switch*>(node));
352 osg::Switch* sw = static_cast<osg::Switch*>(node);
354 double t = globals->get_sim_time_sec();
355 bool enabled = 0 < t;
356 sw->setValue(0, enabled);
366 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
367 static osg::ref_ptr<SGUpdateVisitor> mUpdateVisitor= new SGUpdateVisitor;
369 static osg::ref_ptr<osg::Group> mRealRoot = new osg::Group;
371 static osg::ref_ptr<osg::Group> mRoot = new osg::Group;
373 static osg::ref_ptr<ViewPartitionNode> viewPartition = new ViewPartitionNode;
375 FGRenderer::FGRenderer()
377 #ifdef FG_JPEG_SERVER
378 jpgRenderFrame = FGRenderer::update;
380 manipulator = new FGManipulator;
383 FGRenderer::~FGRenderer()
385 #ifdef FG_JPEG_SERVER
386 jpgRenderFrame = NULL;
390 // Initialize various GL/view parameters
391 // XXX This should be called "preinit" or something, as it initializes
392 // critical parts of the scene graph in addition to the splash screen.
394 FGRenderer::splashinit( void ) {
395 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
396 mRealRoot = dynamic_cast<osg::Group*>(viewer->getSceneData());
397 mRealRoot->addChild(fgCreateSplashNode());
398 mFrameStamp = viewer->getFrameStamp();
399 // Scene doesn't seem to pass the frame stamp to the update
400 // visitor automatically.
401 mUpdateVisitor->setFrameStamp(mFrameStamp.get());
402 #ifdef UPDATE_VISITOR_IN_VIEWER
403 viewer->setUpdateVisitor(mUpdateVisitor.get());
405 osgViewer::Scene* scene = viewer->getScene();
406 scene->setUpdateVisitor(mUpdateVisitor.get());
411 FGRenderer::init( void )
413 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
414 osg::initNotifyLevel();
416 // The number of polygon-offset "units" to place between layers. In
417 // principle, one is supposed to be enough. In practice, I find that
418 // my hardware/driver requires many more.
419 osg::PolygonOffset::setUnitsMultiplier(1);
420 osg::PolygonOffset::setFactorMultiplier(1);
422 // Go full screen if requested ...
423 if ( fgGetBool("/sim/startup/fullscreen") )
427 ->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
429 osg::StateSet* stateSet = mRoot->getOrCreateStateSet();
431 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
433 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS));
434 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
436 stateSet->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01));
437 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
438 stateSet->setAttribute(new osg::BlendFunc);
439 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
441 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
443 // this will be set below
444 stateSet->setMode(GL_NORMALIZE, osg::StateAttribute::OFF);
446 osg::Material* material = new osg::Material;
447 stateSet->setAttribute(material);
449 stateSet->setTextureAttribute(0, new osg::TexEnv);
450 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
452 osg::Hint* hint = new osg::Hint(GL_FOG_HINT, GL_DONT_CARE);
453 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/fog"));
454 stateSet->setAttribute(hint);
455 hint = new osg::Hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
456 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/polygon-smooth"));
457 stateSet->setAttribute(hint);
458 hint = new osg::Hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
459 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/line-smooth"));
460 stateSet->setAttribute(hint);
461 hint = new osg::Hint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
462 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/point-smooth"));
463 stateSet->setAttribute(hint);
464 hint = new osg::Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
465 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/perspective-correction"));
466 stateSet->setAttribute(hint);
468 osg::Group* sceneGroup = new osg::Group;
469 sceneGroup->addChild(globals->get_scenery()->get_scene_graph());
470 sceneGroup->setNodeMask(~simgear::BACKGROUND_BIT);
472 //sceneGroup->addChild(thesky->getCloudRoot());
474 stateSet = sceneGroup->getOrCreateStateSet();
475 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
477 // need to update the light on every frame
478 osg::LightSource* lightSource = new osg::LightSource;
479 lightSource->setUpdateCallback(new FGLightSourceUpdateCallback);
480 // relative because of CameraView being just a clever transform node
481 lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF);
482 lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
484 lightSource->addChild(sceneGroup);
485 lightSource->addChild(thesky->getPreRoot());
486 mRoot->addChild(lightSource);
488 stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet();
489 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
490 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
491 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
493 // enable disable specular highlights.
494 // is the place where we might plug in an other fragment shader ...
495 osg::LightModel* lightModel = new osg::LightModel;
496 lightModel->setUpdateCallback(new FGLightModelUpdateCallback);
497 stateSet->setAttribute(lightModel);
499 // switch to enable wireframe
500 osg::PolygonMode* polygonMode = new osg::PolygonMode;
501 polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback);
502 stateSet->setAttributeAndModes(polygonMode);
504 // scene fog handling
505 osg::Fog* fog = new osg::Fog;
506 fog->setUpdateCallback(new FGFogUpdateCallback);
507 stateSet->setAttributeAndModes(fog);
508 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
511 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
513 osg::Geode* geode = new osg::Geode;
514 geode->addDrawable(new SGPuDrawable);
515 geode->addDrawable(new SGHUDAndPanelDrawable);
516 guiCamera->addChild(geode);
518 osg::Switch* sw = new osg::Switch;
519 sw->setUpdateCallback(new FGScenerySwitchCallback);
520 sw->addChild(mRoot.get());
521 viewPartition->addChild(sw);
522 viewPartition->addChild(thesky->getCloudRoot());
524 mRealRoot->addChild(viewPartition.get());
525 mRealRoot->addChild(FGCreateRedoutNode());
529 // Update all Visuals (redraws anything graphics related)
531 FGRenderer::update( bool refresh_camera_settings ) {
532 bool scenery_loaded = fgGetBool("sim/sceneryloaded")
533 || fgGetBool("sim/sceneryloaded-override");
534 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
535 if ( idle_state < 1000 || !scenery_loaded ) {
536 fgSetDouble("/sim/startup/splash-alpha", 1.0);
538 // Keep resetting sim time while the sim is initializing
539 // the splash screen is now in the scenegraph
540 globals->set_sim_time_sec( 0.0 );
544 // Fade out the splash screen over the first three seconds.
545 double sAlpha = SGMiscd::max(0, (2.5 - globals->get_sim_time_sec()) / 2.5);
546 fgSetDouble("/sim/startup/splash-alpha", sAlpha);
548 bool skyblend = fgGetBool("/sim/rendering/skyblend");
549 bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites");
550 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
551 bool distance_attenuation
552 = fgGetBool("/sim/rendering/distance-attenuation");
554 SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
555 distance_attenuation );
557 static const SGPropertyNode *groundlevel_nearplane
558 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
560 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
563 double actual_visibility;
564 if (fgGetBool("/environment/clouds/status")) {
565 actual_visibility = thesky->get_visibility();
567 actual_visibility = fgGetDouble("/environment/visibility-m");
570 // idle_state is now 1000 meaning we've finished all our
571 // initializations and are running the main loop, so this will
572 // now work without seg faulting the system.
574 FGViewer *current__view = globals->get_current_view();
575 // Force update of center dependent values ...
576 current__view->set_dirty();
578 if ( refresh_camera_settings ) {
580 resize( fgGetInt("/sim/startup/xsize"),
581 fgGetInt("/sim/startup/ysize") );
583 osg::Camera *camera = viewer->getCamera();
587 if ( fgGetBool("/sim/rendering/textures") ) {
588 SGVec4f clearColor(l->adj_fog_color());
589 camera->setClearColor(clearColor.osg());
592 SGVec4f clearColor(l->sky_color());
593 camera->setClearColor(clearColor.osg());
596 // update fog params if visibility has changed
597 double visibility_meters = fgGetDouble("/environment/visibility-m");
598 thesky->set_visibility(visibility_meters);
600 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
601 ( global_multi_loop * fgGetInt("/sim/speed-up") )
602 / (double)fgGetInt("/sim/model-hz") );
604 // update the sky dome
607 // The sun and moon distances are scaled down versions
608 // of the actual distance to get both the moon and the sun
609 // within the range of the far clip plane.
610 // Moon distance: 384,467 kilometers
611 // Sun distance: 150,000,000 kilometers
613 double sun_horiz_eff, moon_horiz_eff;
614 if (fgGetBool("/sim/rendering/horizon-effect")) {
615 sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
616 moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
618 sun_horiz_eff = moon_horiz_eff = 1.0;
621 static SGSkyState sstate;
623 sstate.view_pos = toVec3f(current__view->get_view_pos());
624 sstate.zero_elev = toVec3f(current__view->get_zero_elev());
625 sstate.view_up = current__view->get_world_up();
626 sstate.lon = current__view->getLongitude_deg()
627 * SGD_DEGREES_TO_RADIANS;
628 sstate.lat = current__view->getLatitude_deg()
629 * SGD_DEGREES_TO_RADIANS;
630 sstate.alt = current__view->getAltitudeASL_ft()
632 sstate.spin = l->get_sun_rotation();
633 sstate.gst = globals->get_time_params()->getGst();
634 sstate.sun_ra = globals->get_ephem()->getSunRightAscension();
635 sstate.sun_dec = globals->get_ephem()->getSunDeclination();
636 sstate.sun_dist = 50000.0 * sun_horiz_eff;
637 sstate.moon_ra = globals->get_ephem()->getMoonRightAscension();
638 sstate.moon_dec = globals->get_ephem()->getMoonDeclination();
639 sstate.moon_dist = 40000.0 * moon_horiz_eff;
640 sstate.sun_angle = l->get_sun_angle();
644 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
645 << l->sky_color()[0] << " "
646 << l->sky_color()[1] << " "
647 << l->sky_color()[2] << " "
648 << l->sky_color()[3] );
649 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
650 << l->fog_color()[0] << " "
651 << l->fog_color()[1] << " "
652 << l->fog_color()[2] << " "
653 << l->fog_color()[3] );
654 SG_LOG( SG_GENERAL, SG_BULK,
655 " sun_angle = " << l->sun_angle
656 << " moon_angle = " << l->moon_angle );
659 static SGSkyColor scolor;
661 scolor.sky_color = SGVec3f(l->sky_color().data());
662 scolor.fog_color = SGVec3f(l->adj_fog_color().data());
663 scolor.cloud_color = SGVec3f(l->cloud_color().data());
664 scolor.sun_angle = l->get_sun_angle();
665 scolor.moon_angle = l->get_moon_angle();
666 scolor.nplanets = globals->get_ephem()->getNumPlanets();
667 scolor.nstars = globals->get_ephem()->getNumStars();
668 scolor.planet_data = globals->get_ephem()->getPlanets();
669 scolor.star_data = globals->get_ephem()->getStars();
671 thesky->reposition( sstate, delta_time_sec );
672 thesky->repaint( scolor );
675 SG_LOG( SG_GENERAL, SG_BULK,
676 "thesky->reposition( view_pos = " << view_pos[0] << " "
677 << view_pos[1] << " " << view_pos[2] );
678 SG_LOG( SG_GENERAL, SG_BULK,
679 " zero_elev = " << zero_elev[0] << " "
680 << zero_elev[1] << " " << zero_elev[2]
681 << " lon = " << cur_fdm_state->get_Longitude()
682 << " lat = " << cur_fdm_state->get_Latitude() );
683 SG_LOG( SG_GENERAL, SG_BULK,
684 " sun_rot = " << l->get_sun_rotation
685 << " gst = " << SGTime::cur_time_params->getGst() );
686 SG_LOG( SG_GENERAL, SG_BULK,
687 " sun ra = " << globals->get_ephem()->getSunRightAscension()
688 << " sun dec = " << globals->get_ephem()->getSunDeclination()
689 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
690 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
694 // shadows->setupShadows(
695 // current__view->getLongitude_deg(),
696 // current__view->getLatitude_deg(),
697 // globals->get_time_params()->getGst(),
698 // globals->get_ephem()->getSunRightAscension(),
699 // globals->get_ephem()->getSunDeclination(),
700 // l->get_sun_angle());
704 // sgEnviro.setLight(l->adj_fog_color());
705 // sgEnviro.startOfFrame(current__view->get_view_pos(),
706 // current__view->get_world_up(),
707 // current__view->getLongitude_deg(),
708 // current__view->getLatitude_deg(),
709 // current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
713 // sgEnviro.drawLightning();
715 // double current_view_origin_airspeed_horiz_kt =
716 // fgGetDouble("/velocities/airspeed-kt", 0.0)
717 // * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
718 // * SGD_DEGREES_TO_RADIANS);
722 // shadows->endOfFrame();
724 // need to call the update visitor once
725 mFrameStamp->setCalendarTime(*globals->get_time_params()->getGmt());
726 mUpdateVisitor->setViewData(current__view->getViewPosition(),
727 current__view->getViewOrientation());
728 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
729 mUpdateVisitor->setLight(direction, l->scene_ambient(),
730 l->scene_diffuse(), l->scene_specular(),
732 l->get_sun_angle()*SGD_RADIANS_TO_DEGREES);
733 mUpdateVisitor->setVisibility(actual_visibility);
734 viewPartition->setVisibility(actual_visibility);
735 simgear::GroundLightManager::instance()->update(mUpdateVisitor.get());
736 bool hotspots = fgGetBool("/sim/panel-hotspots");
737 osg::Node::NodeMask cullMask = ~simgear::LIGHTS_BITS & ~simgear::PICK_BIT;
738 cullMask |= simgear::GroundLightManager::instance()
739 ->getLightNodeMask(mUpdateVisitor.get());
741 cullMask |= simgear::PICK_BIT;
742 camera->setCullMask(cullMask);
744 for (int i = 0; i < viewer->getNumSlaves(); ++i)
745 viewer->getSlave(i)._camera->setCullMask(cullMask);
750 // options.cxx needs to see this for toggle_panel()
751 // Handle new window size or exposure
753 FGRenderer::resize( int width, int height ) {
756 if ( (!fgGetBool("/sim/virtual-cockpit"))
757 && fgPanelVisible() && idle_state == 1000 ) {
758 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
759 globals->get_current_panel()->getYOffset()) / 768.0);
764 static int lastwidth = 0;
765 static int lastheight = 0;
766 if (width != lastwidth)
767 fgSetInt("/sim/startup/xsize", lastwidth = width);
768 if (height != lastheight)
769 fgSetInt("/sim/startup/ysize", lastheight = height);
772 FGViewMgr *viewmgr = globals->get_viewmgr();
774 for ( int i = 0; i < viewmgr->size(); ++i ) {
775 viewmgr->get_view(i)->
776 set_aspect_ratio((float)view_h / (float)width);
781 void FGRenderer::setCameraParameters(float vfov, float aspectRatio,
782 float zNear, float zFar)
785 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
786 viewer->getCamera()->setProjectionMatrixAsPerspective(vfov,
792 FGRenderer::pick(std::vector<SGSceneryPick>& pickList,
793 const osgGA::GUIEventAdapter* ea)
795 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
796 // wipe out the return ...
798 typedef osgUtil::LineSegmentIntersector::Intersections Intersections;
799 Intersections intersections;
801 if (!computeIntersections(CameraGroup::getDefault(), ea, intersections))
803 for (Intersections::iterator hit = intersections.begin(),
804 e = intersections.end();
807 const osg::NodePath& np = hit->nodePath;
808 osg::NodePath::const_reverse_iterator npi;
809 for (npi = np.rbegin(); npi != np.rend(); ++npi) {
810 SGSceneUserData* ud = SGSceneUserData::getSceneUserData(*npi);
813 for (unsigned i = 0; i < ud->getNumPickCallbacks(); ++i) {
814 SGPickCallback* pickCallback = ud->getPickCallback(i);
817 SGSceneryPick sceneryPick;
818 sceneryPick.info.local = SGVec3d(hit->getLocalIntersectPoint());
819 sceneryPick.info.wgs84 = SGVec3d(hit->getWorldIntersectPoint());
820 sceneryPick.callback = pickCallback;
821 pickList.push_back(sceneryPick);
825 return !pickList.empty();
829 FGRenderer::addCamera(osg::Camera* camera, bool useSceneData)
831 mRealRoot->addChild(camera);
835 fgDumpSceneGraphToFile(const char* filename)
837 return osgDB::writeNodeFile(*mRealRoot.get(), filename);
841 fgDumpTerrainBranchToFile(const char* filename)
843 return osgDB::writeNodeFile( *globals->get_scenery()->get_terrain_branch(),
849 fgDumpNodeToFile(osg::Node* node, const char* filename)
851 return osgDB::writeNodeFile(*node, filename);
853 // end of renderer.cxx