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/tgdb/GroundLightManager.hxx>
67 #include <simgear/scene/tgdb/pt_lights.hxx>
68 #include <simgear/props/props.hxx>
69 #include <simgear/timing/sg_time.hxx>
70 #include <simgear/ephemeris/ephemeris.hxx>
71 #include <simgear/math/sg_random.h>
73 #include <simgear/screen/jpgfactory.hxx>
76 #include <simgear/environment/visual_enviro.hxx>
78 #include <Time/light.hxx>
79 #include <Time/light.hxx>
80 #include <Aircraft/aircraft.hxx>
81 #include <Cockpit/panel.hxx>
82 #include <Cockpit/cockpit.hxx>
83 #include <Cockpit/hud.hxx>
84 #include <Model/panelnode.hxx>
85 #include <Model/modelmgr.hxx>
86 #include <Model/acmodel.hxx>
87 #include <Scenery/scenery.hxx>
88 #include <Scenery/redout.hxx>
89 #include <Scenery/tilemgr.hxx>
90 #include <GUI/new_gui.hxx>
91 #include <Instrumentation/instrument_mgr.hxx>
92 #include <Instrumentation/HUD/HUD.hxx>
93 #include <Environment/precipitation_mgr.hxx>
95 #include <Include/general.hxx>
97 #include "renderer.hxx"
99 #include "CameraGroup.hxx"
100 #include "FGEventHandler.hxx"
103 using namespace simgear;
104 using namespace flightgear;
106 class FGHintUpdateCallback : public osg::StateAttribute::Callback {
108 FGHintUpdateCallback(const char* configNode) :
109 mConfigNode(fgGetNode(configNode, true))
111 virtual void operator()(osg::StateAttribute* stateAttribute,
114 assert(dynamic_cast<osg::Hint*>(stateAttribute));
115 osg::Hint* hint = static_cast<osg::Hint*>(stateAttribute);
117 const char* value = mConfigNode->getStringValue();
119 hint->setMode(GL_DONT_CARE);
120 else if (0 == strcmp(value, "nicest"))
121 hint->setMode(GL_NICEST);
122 else if (0 == strcmp(value, "fastest"))
123 hint->setMode(GL_FASTEST);
125 hint->setMode(GL_DONT_CARE);
128 SGSharedPtr<SGPropertyNode> mConfigNode;
132 class SGPuDrawable : public osg::Drawable {
136 // Dynamic stuff, do not store geometry
137 setUseDisplayList(false);
139 osg::StateSet* stateSet = getOrCreateStateSet();
140 stateSet->setRenderBinDetails(1001, "RenderBin");
141 // speed optimization?
142 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
143 // We can do translucent menus, so why not. :-)
144 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
145 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
146 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
148 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
150 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
151 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
153 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
154 { drawImplementation(*renderInfo.getState()); }
155 void drawImplementation(osg::State& state) const
157 state.setActiveTextureUnit(0);
158 state.setClientActiveTextureUnit(0);
160 state.disableAllVertexArrays();
162 glPushAttrib(GL_ALL_ATTRIB_BITS);
163 glPushClientAttrib(~0u);
171 virtual osg::Object* cloneType() const { return new SGPuDrawable; }
172 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGPuDrawable; }
177 class SGHUDAndPanelDrawable : public osg::Drawable {
179 SGHUDAndPanelDrawable()
181 // Dynamic stuff, do not store geometry
182 setUseDisplayList(false);
184 osg::StateSet* stateSet = getOrCreateStateSet();
185 stateSet->setRenderBinDetails(1000, "RenderBin");
187 // speed optimization?
188 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
189 stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA));
190 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
191 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
192 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
194 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
196 virtual void drawImplementation(osg::RenderInfo& renderInfo) const
197 { drawImplementation(*renderInfo.getState()); }
198 void drawImplementation(osg::State& state) const
200 state.setActiveTextureUnit(0);
201 state.setClientActiveTextureUnit(0);
202 state.disableAllVertexArrays();
204 glPushAttrib(GL_ALL_ATTRIB_BITS);
205 glPushClientAttrib(~0u);
207 fgCockpitUpdate(&state);
209 FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
210 HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
213 // update the panel subsystem
214 if ( globals->get_current_panel() != NULL )
215 globals->get_current_panel()->update(state);
216 // We don't need a state here - can be safely removed when we can pick
225 virtual osg::Object* cloneType() const { return new SGHUDAndPanelDrawable; }
226 virtual osg::Object* clone(const osg::CopyOp&) const { return new SGHUDAndPanelDrawable; }
231 class FGLightSourceUpdateCallback : public osg::NodeCallback {
233 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
235 assert(dynamic_cast<osg::LightSource*>(node));
236 osg::LightSource* lightSource = static_cast<osg::LightSource*>(node);
237 osg::Light* light = lightSource->getLight();
239 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
240 light->setAmbient(l->scene_ambient().osg());
241 light->setDiffuse(l->scene_diffuse().osg());
242 light->setSpecular(l->scene_specular().osg());
243 SGVec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0);
244 light->setPosition(position.osg());
245 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
246 light->setDirection(direction.osg());
247 light->setSpotExponent(0);
248 light->setSpotCutoff(180);
249 light->setConstantAttenuation(1);
250 light->setLinearAttenuation(0);
251 light->setQuadraticAttenuation(0);
257 class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback {
259 FGWireFrameModeUpdateCallback() :
260 mWireframe(fgGetNode("/sim/rendering/wireframe"))
262 virtual void operator()(osg::StateAttribute* stateAttribute,
265 assert(dynamic_cast<osg::PolygonMode*>(stateAttribute));
266 osg::PolygonMode* polygonMode;
267 polygonMode = static_cast<osg::PolygonMode*>(stateAttribute);
269 if (mWireframe->getBoolValue())
270 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
271 osg::PolygonMode::LINE);
273 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
274 osg::PolygonMode::FILL);
277 SGSharedPtr<SGPropertyNode> mWireframe;
280 class FGLightModelUpdateCallback : public osg::StateAttribute::Callback {
282 FGLightModelUpdateCallback() :
283 mHighlights(fgGetNode("/sim/rendering/specular-highlight"))
285 virtual void operator()(osg::StateAttribute* stateAttribute,
288 assert(dynamic_cast<osg::LightModel*>(stateAttribute));
289 osg::LightModel* lightModel;
290 lightModel = static_cast<osg::LightModel*>(stateAttribute);
293 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
294 lightModel->setAmbientIntensity(l->scene_ambient().osg());
296 lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1));
298 lightModel->setTwoSided(true);
299 lightModel->setLocalViewer(false);
301 if (mHighlights->getBoolValue()) {
302 lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
304 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
308 SGSharedPtr<SGPropertyNode> mHighlights;
311 class FGFogEnableUpdateCallback : public osg::StateSet::Callback {
313 FGFogEnableUpdateCallback() :
314 mFogEnabled(fgGetNode("/sim/rendering/fog"))
316 virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*)
318 if (strcmp(mFogEnabled->getStringValue(), "disabled") == 0) {
319 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
321 stateSet->setMode(GL_FOG, osg::StateAttribute::ON);
325 SGSharedPtr<SGPropertyNode> mFogEnabled;
328 class FGFogUpdateCallback : public osg::StateAttribute::Callback {
330 virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
332 assert(dynamic_cast<SGUpdateVisitor*>(nv));
333 assert(dynamic_cast<osg::Fog*>(sa));
334 SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
335 osg::Fog* fog = static_cast<osg::Fog*>(sa);
336 fog->setMode(osg::Fog::EXP2);
337 fog->setColor(updateVisitor->getFogColor().osg());
338 fog->setDensity(updateVisitor->getFogExp2Density());
342 // update callback for the switch node guarding that splash
343 class FGScenerySwitchCallback : public osg::NodeCallback {
345 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
347 assert(dynamic_cast<osg::Switch*>(node));
348 osg::Switch* sw = static_cast<osg::Switch*>(node);
350 double t = globals->get_sim_time_sec();
351 bool enabled = 0 < t;
352 sw->setValue(0, enabled);
362 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
363 static osg::ref_ptr<SGUpdateVisitor> mUpdateVisitor= new SGUpdateVisitor;
365 static osg::ref_ptr<osg::Group> mRealRoot = new osg::Group;
367 static osg::ref_ptr<osg::Group> mRoot = new osg::Group;
369 FGRenderer::FGRenderer()
371 #ifdef FG_JPEG_SERVER
372 jpgRenderFrame = FGRenderer::update;
374 eventHandler = new FGEventHandler;
377 FGRenderer::~FGRenderer()
379 #ifdef FG_JPEG_SERVER
380 jpgRenderFrame = NULL;
384 // Initialize various GL/view parameters
385 // XXX This should be called "preinit" or something, as it initializes
386 // critical parts of the scene graph in addition to the splash screen.
388 FGRenderer::splashinit( void ) {
389 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
390 mRealRoot = dynamic_cast<osg::Group*>(viewer->getSceneData());
391 mRealRoot->addChild(fgCreateSplashNode());
392 mFrameStamp = viewer->getFrameStamp();
393 // Scene doesn't seem to pass the frame stamp to the update
394 // visitor automatically.
395 mUpdateVisitor->setFrameStamp(mFrameStamp.get());
396 viewer->setUpdateVisitor(mUpdateVisitor.get());
400 FGRenderer::init( void )
402 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
403 osg::initNotifyLevel();
405 // The number of polygon-offset "units" to place between layers. In
406 // principle, one is supposed to be enough. In practice, I find that
407 // my hardware/driver requires many more.
408 osg::PolygonOffset::setUnitsMultiplier(1);
409 osg::PolygonOffset::setFactorMultiplier(1);
411 // Go full screen if requested ...
412 if ( fgGetBool("/sim/startup/fullscreen") )
416 ->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
418 osg::StateSet* stateSet = mRoot->getOrCreateStateSet();
420 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
422 stateSet->setAttribute(new osg::Depth(osg::Depth::LESS));
423 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
425 stateSet->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01));
426 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
427 stateSet->setAttribute(new osg::BlendFunc);
428 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
430 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
432 // this will be set below
433 stateSet->setMode(GL_NORMALIZE, osg::StateAttribute::OFF);
435 osg::Material* material = new osg::Material;
436 stateSet->setAttribute(material);
438 stateSet->setTextureAttribute(0, new osg::TexEnv);
439 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
441 osg::Hint* hint = new osg::Hint(GL_FOG_HINT, GL_DONT_CARE);
442 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/fog"));
443 stateSet->setAttribute(hint);
444 hint = new osg::Hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
445 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/polygon-smooth"));
446 stateSet->setAttribute(hint);
447 hint = new osg::Hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
448 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/line-smooth"));
449 stateSet->setAttribute(hint);
450 hint = new osg::Hint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
451 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/point-smooth"));
452 stateSet->setAttribute(hint);
453 hint = new osg::Hint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
454 hint->setUpdateCallback(new FGHintUpdateCallback("/sim/rendering/perspective-correction"));
455 stateSet->setAttribute(hint);
457 osg::Group* sceneGroup = new osg::Group;
458 sceneGroup->addChild(globals->get_scenery()->get_scene_graph());
459 sceneGroup->setNodeMask(~simgear::BACKGROUND_BIT);
461 //sceneGroup->addChild(thesky->getCloudRoot());
463 stateSet = sceneGroup->getOrCreateStateSet();
464 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
466 // need to update the light on every frame
467 osg::LightSource* lightSource = new osg::LightSource;
468 lightSource->setUpdateCallback(new FGLightSourceUpdateCallback);
469 // relative because of CameraView being just a clever transform node
470 lightSource->setReferenceFrame(osg::LightSource::RELATIVE_RF);
471 lightSource->setLocalStateSetModes(osg::StateAttribute::ON);
473 lightSource->addChild(sceneGroup);
474 lightSource->addChild(thesky->getPreRoot());
475 mRoot->addChild(lightSource);
477 stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet();
478 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
479 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
480 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
482 // enable disable specular highlights.
483 // is the place where we might plug in an other fragment shader ...
484 osg::LightModel* lightModel = new osg::LightModel;
485 lightModel->setUpdateCallback(new FGLightModelUpdateCallback);
486 stateSet->setAttribute(lightModel);
488 // switch to enable wireframe
489 osg::PolygonMode* polygonMode = new osg::PolygonMode;
490 polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback);
491 stateSet->setAttributeAndModes(polygonMode);
493 // scene fog handling
494 osg::Fog* fog = new osg::Fog;
495 fog->setUpdateCallback(new FGFogUpdateCallback);
496 stateSet->setAttributeAndModes(fog);
497 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
500 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
502 osg::Geode* geode = new osg::Geode;
503 geode->addDrawable(new SGPuDrawable);
504 geode->addDrawable(new SGHUDAndPanelDrawable);
505 guiCamera->addChild(geode);
507 osg::Switch* sw = new osg::Switch;
508 sw->setUpdateCallback(new FGScenerySwitchCallback);
509 sw->addChild(mRoot.get());
510 mRealRoot->addChild(sw);
511 mRealRoot->addChild(thesky->getCloudRoot());
512 mRealRoot->addChild(FGCreateRedoutNode());
516 // Update all Visuals (redraws anything graphics related)
518 FGRenderer::update( bool refresh_camera_settings ) {
519 bool scenery_loaded = fgGetBool("sim/sceneryloaded")
520 || fgGetBool("sim/sceneryloaded-override");
521 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
522 if ( idle_state < 1000 || !scenery_loaded ) {
523 fgSetDouble("/sim/startup/splash-alpha", 1.0);
525 // Keep resetting sim time while the sim is initializing
526 // the splash screen is now in the scenegraph
527 globals->set_sim_time_sec( 0.0 );
531 // Fade out the splash screen over the first three seconds.
532 double sAlpha = SGMiscd::max(0, (2.5 - globals->get_sim_time_sec()) / 2.5);
533 fgSetDouble("/sim/startup/splash-alpha", sAlpha);
535 bool skyblend = fgGetBool("/sim/rendering/skyblend");
536 bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites");
537 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
538 bool distance_attenuation
539 = fgGetBool("/sim/rendering/distance-attenuation");
541 SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
542 distance_attenuation );
544 static const SGPropertyNode *groundlevel_nearplane
545 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
547 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
550 double actual_visibility;
551 if (fgGetBool("/environment/clouds/status")) {
552 actual_visibility = thesky->get_visibility();
554 actual_visibility = fgGetDouble("/environment/visibility-m");
557 // idle_state is now 1000 meaning we've finished all our
558 // initializations and are running the main loop, so this will
559 // now work without seg faulting the system.
561 FGViewer *current__view = globals->get_current_view();
562 // Force update of center dependent values ...
563 current__view->set_dirty();
565 if ( refresh_camera_settings ) {
567 resize( fgGetInt("/sim/startup/xsize"),
568 fgGetInt("/sim/startup/ysize") );
570 osg::Camera *camera = viewer->getCamera();
574 if ( fgGetBool("/sim/rendering/textures") ) {
575 SGVec4f clearColor(l->adj_fog_color());
576 camera->setClearColor(clearColor.osg());
579 SGVec4f clearColor(l->sky_color());
580 camera->setClearColor(clearColor.osg());
583 // update fog params if visibility has changed
584 double visibility_meters = fgGetDouble("/environment/visibility-m");
585 thesky->set_visibility(visibility_meters);
587 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
588 ( global_multi_loop * fgGetInt("/sim/speed-up") )
589 / (double)fgGetInt("/sim/model-hz") );
591 // update the sky dome
594 // The sun and moon distances are scaled down versions
595 // of the actual distance to get both the moon and the sun
596 // within the range of the far clip plane.
597 // Moon distance: 384,467 kilometers
598 // Sun distance: 150,000,000 kilometers
600 double sun_horiz_eff, moon_horiz_eff;
601 if (fgGetBool("/sim/rendering/horizon-effect")) {
603 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_sun_angle()),
607 = 0.67 + pow(osg::clampAbove(0.5 + cos(l->get_moon_angle()),
611 sun_horiz_eff = moon_horiz_eff = 1.0;
616 SGVec3d viewPos = current__view->getViewPosition();
617 sstate.view_pos = toVec3f(viewPos);
618 SGGeod geodViewPos = SGGeod::fromCart(viewPos);
619 SGGeod geodZeroViewPos = SGGeod::fromGeodM(geodViewPos, 0);
620 sstate.zero_elev = toVec3f(SGVec3d::fromGeod(geodZeroViewPos));
621 SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos);
622 sstate.view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
623 sstate.lon = geodViewPos.getLongitudeRad();
624 sstate.lat = geodViewPos.getLatitudeRad();
625 sstate.alt = geodViewPos.getElevationM();
626 sstate.spin = l->get_sun_rotation();
627 sstate.gst = globals->get_time_params()->getGst();
628 sstate.sun_dist = 50000.0 * sun_horiz_eff;
629 sstate.moon_dist = 40000.0 * moon_horiz_eff;
630 sstate.sun_angle = l->get_sun_angle();
634 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
635 << l->sky_color()[0] << " "
636 << l->sky_color()[1] << " "
637 << l->sky_color()[2] << " "
638 << l->sky_color()[3] );
639 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
640 << l->fog_color()[0] << " "
641 << l->fog_color()[1] << " "
642 << l->fog_color()[2] << " "
643 << l->fog_color()[3] );
644 SG_LOG( SG_GENERAL, SG_BULK,
645 " sun_angle = " << l->sun_angle
646 << " moon_angle = " << l->moon_angle );
651 scolor.sky_color = SGVec3f(l->sky_color().data());
652 scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data());
653 scolor.fog_color = SGVec3f(l->adj_fog_color().data());
654 scolor.cloud_color = SGVec3f(l->cloud_color().data());
655 scolor.sun_angle = l->get_sun_angle();
656 scolor.moon_angle = l->get_moon_angle();
658 thesky->reposition( sstate, *globals->get_ephem(), delta_time_sec );
659 thesky->repaint( scolor, *globals->get_ephem() );
662 SG_LOG( SG_GENERAL, SG_BULK,
663 "thesky->reposition( view_pos = " << view_pos[0] << " "
664 << view_pos[1] << " " << view_pos[2] );
665 SG_LOG( SG_GENERAL, SG_BULK,
666 " zero_elev = " << zero_elev[0] << " "
667 << zero_elev[1] << " " << zero_elev[2]
668 << " lon = " << cur_fdm_state->get_Longitude()
669 << " lat = " << cur_fdm_state->get_Latitude() );
670 SG_LOG( SG_GENERAL, SG_BULK,
671 " sun_rot = " << l->get_sun_rotation
672 << " gst = " << SGTime::cur_time_params->getGst() );
673 SG_LOG( SG_GENERAL, SG_BULK,
674 " sun ra = " << globals->get_ephem()->getSunRightAscension()
675 << " sun dec = " << globals->get_ephem()->getSunDeclination()
676 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
677 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
681 // shadows->setupShadows(
682 // current__view->getLongitude_deg(),
683 // current__view->getLatitude_deg(),
684 // globals->get_time_params()->getGst(),
685 // globals->get_ephem()->getSunRightAscension(),
686 // globals->get_ephem()->getSunDeclination(),
687 // l->get_sun_angle());
691 // sgEnviro.setLight(l->adj_fog_color());
692 // sgEnviro.startOfFrame(current__view->get_view_pos(),
693 // current__view->get_world_up(),
694 // current__view->getLongitude_deg(),
695 // current__view->getLatitude_deg(),
696 // current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
700 // sgEnviro.drawLightning();
702 // double current_view_origin_airspeed_horiz_kt =
703 // fgGetDouble("/velocities/airspeed-kt", 0.0)
704 // * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
705 // * SGD_DEGREES_TO_RADIANS);
709 // shadows->endOfFrame();
711 // need to call the update visitor once
712 mFrameStamp->setCalendarTime(*globals->get_time_params()->getGmt());
713 mUpdateVisitor->setViewData(current__view->getViewPosition(),
714 current__view->getViewOrientation());
715 SGVec3f direction(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2]);
716 mUpdateVisitor->setLight(direction, l->scene_ambient(),
717 l->scene_diffuse(), l->scene_specular(),
719 l->get_sun_angle()*SGD_RADIANS_TO_DEGREES);
720 mUpdateVisitor->setVisibility(actual_visibility);
721 simgear::GroundLightManager::instance()->update(mUpdateVisitor.get());
722 bool hotspots = fgGetBool("/sim/panel-hotspots");
723 osg::Node::NodeMask cullMask = ~simgear::LIGHTS_BITS & ~simgear::PICK_BIT;
724 cullMask |= simgear::GroundLightManager::instance()
725 ->getLightNodeMask(mUpdateVisitor.get());
727 cullMask |= simgear::PICK_BIT;
728 CameraGroup::getDefault()->setCameraCullMasks(cullMask);
733 // options.cxx needs to see this for toggle_panel()
734 // Handle new window size or exposure
736 FGRenderer::resize( int width, int height ) {
739 if ( (!fgGetBool("/sim/virtual-cockpit"))
740 && fgPanelVisible() && idle_state == 1000 ) {
741 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
742 globals->get_current_panel()->getYOffset()) / 768.0);
747 static int lastwidth = 0;
748 static int lastheight = 0;
749 if (width != lastwidth)
750 fgSetInt("/sim/startup/xsize", lastwidth = width);
751 if (height != lastheight)
752 fgSetInt("/sim/startup/ysize", lastheight = height);
755 FGViewMgr *viewmgr = globals->get_viewmgr();
757 for ( int i = 0; i < viewmgr->size(); ++i ) {
758 viewmgr->get_view(i)->
759 set_aspect_ratio((float)view_h / (float)width);
765 FGRenderer::pick(std::vector<SGSceneryPick>& pickList,
766 const osgGA::GUIEventAdapter* ea)
768 osgViewer::Viewer* viewer = globals->get_renderer()->getViewer();
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 = SGVec3d(hit->getLocalIntersectPoint());
792 sceneryPick.info.wgs84 = SGVec3d(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