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.
29 #include <simgear/compiler.h>
35 #include <osg/ref_ptr>
36 #include <osg/AlphaFunc>
37 #include <osg/BlendFunc>
38 #include <osg/CameraNode>
39 #include <osg/CameraView>
40 #include <osg/CullFace>
44 #include <osg/LightModel>
45 #include <osg/NodeCallback>
47 #include <osg/MatrixTransform>
48 #include <osg/Multisample>
50 #include <osg/PolygonMode>
51 #include <osg/ShadeModel>
55 #include <osg/ColorMatrix>
57 #include <osgUtil/SceneView>
58 #include <osgUtil/UpdateVisitor>
60 #include <osg/io_utils>
61 #include <osgDB/WriteFile>
62 #include <osgDB/ReadFile>
65 #include <simgear/math/SGMath.hxx>
66 #include <simgear/screen/extensions.hxx>
67 #include <simgear/scene/material/matlib.hxx>
68 #include <simgear/scene/model/animation.hxx>
69 #include <simgear/scene/model/model.hxx>
70 #include <simgear/scene/model/modellib.hxx>
71 #include <simgear/scene/model/placement.hxx>
72 #include <simgear/scene/util/SGUpdateVisitor.hxx>
73 #include <simgear/scene/tgdb/pt_lights.hxx>
74 #include <simgear/props/props.hxx>
75 #include <simgear/timing/sg_time.hxx>
76 #include <simgear/ephemeris/ephemeris.hxx>
77 #include <simgear/math/sg_random.h>
79 #include <simgear/screen/jpgfactory.hxx>
82 #include <simgear/environment/visual_enviro.hxx>
84 #include <Scenery/tileentry.hxx>
85 #include <Time/light.hxx>
86 #include <Time/light.hxx>
87 #include <Aircraft/aircraft.hxx>
88 #include <Cockpit/panel.hxx>
89 #include <Cockpit/cockpit.hxx>
90 #include <Cockpit/hud.hxx>
91 #include <Model/panelnode.hxx>
92 #include <Model/modelmgr.hxx>
93 #include <Model/acmodel.hxx>
94 #include <Scenery/scenery.hxx>
95 #include <Scenery/tilemgr.hxx>
96 #include <ATC/ATCdisplay.hxx>
97 #include <GUI/new_gui.hxx>
98 #include <Instrumentation/instrument_mgr.hxx>
99 #include <Instrumentation/HUD/HUD.hxx>
101 #include "splash.hxx"
102 #include "renderer.hxx"
106 class FGSunLightUpdateCallback : public osg::StateAttribute::Callback {
108 virtual void operator()(osg::StateAttribute* stateAttribute,
111 assert(dynamic_cast<osg::Light*>(stateAttribute));
112 osg::Light* light = static_cast<osg::Light*>(stateAttribute);
114 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
115 SGVec4f ambient(l->scene_ambient());
116 light->setAmbient(ambient.osg());
117 SGVec4f diffuse(l->scene_diffuse());
118 light->setDiffuse(diffuse.osg());
119 SGVec4f specular(l->scene_specular());
120 light->setSpecular(specular.osg());
121 SGVec4f position(l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2], 0);
122 light->setPosition(position.osg());
124 light->setDirection(osg::Vec3(0, 0, -1));
125 light->setSpotExponent(0);
126 light->setSpotCutoff(180);
127 light->setConstantAttenuation(1);
128 light->setLinearAttenuation(0);
129 light->setQuadraticAttenuation(0);
133 class FGWireFrameModeUpdateCallback : public osg::StateAttribute::Callback {
135 FGWireFrameModeUpdateCallback() :
136 mWireframe(fgGetNode("/sim/rendering/wireframe"))
138 virtual void operator()(osg::StateAttribute* stateAttribute,
141 assert(dynamic_cast<osg::PolygonMode*>(stateAttribute));
142 osg::PolygonMode* polygonMode;
143 polygonMode = static_cast<osg::PolygonMode*>(stateAttribute);
145 if (mWireframe->getBoolValue())
146 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
147 osg::PolygonMode::LINE);
149 polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK,
150 osg::PolygonMode::FILL);
153 SGSharedPtr<SGPropertyNode> mWireframe;
156 class FGLightModelUpdateCallback : public osg::StateAttribute::Callback {
158 FGLightModelUpdateCallback() :
159 mHighlights(fgGetNode("/sim/rendering/specular-highlight"))
161 virtual void operator()(osg::StateAttribute* stateAttribute,
164 assert(dynamic_cast<osg::LightModel*>(stateAttribute));
165 osg::LightModel* lightModel;
166 lightModel = static_cast<osg::LightModel*>(stateAttribute);
169 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
170 SGVec4f ambient(l->scene_ambient());
171 lightModel->setAmbientIntensity(ambient.osg());
173 lightModel->setAmbientIntensity(osg::Vec4(0, 0, 0, 1));
175 lightModel->setTwoSided(true);
177 if (mHighlights->getBoolValue()) {
178 lightModel->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR);
179 lightModel->setLocalViewer(true);
181 lightModel->setColorControl(osg::LightModel::SINGLE_COLOR);
182 lightModel->setLocalViewer(false);
186 SGSharedPtr<SGPropertyNode> mHighlights;
189 class FGFogEnableUpdateCallback : public osg::StateSet::Callback {
191 FGFogEnableUpdateCallback() :
192 mFogEnabled(fgGetNode("/sim/rendering/fog"))
194 virtual void operator()(osg::StateSet* stateSet, osg::NodeVisitor*)
196 if (strcmp(mFogEnabled->getStringValue(), "disabled") == 0) {
197 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
199 stateSet->setMode(GL_FOG, osg::StateAttribute::ON);
203 SGSharedPtr<SGPropertyNode> mFogEnabled;
206 // fog constants. I'm a little nervous about putting actual code out
207 // here but it seems to work (?)
208 static const double m_log01 = -log( 0.01 );
209 static const double sqrt_m_log01 = sqrt( m_log01 );
210 static GLfloat fog_exp_density;
211 static GLfloat fog_exp2_density;
212 static GLfloat rwy_exp2_punch_through;
213 static GLfloat taxi_exp2_punch_through;
214 static GLfloat ground_exp2_punch_through;
219 static osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
220 static osg::ref_ptr<osg::FrameStamp> mFrameStamp = new osg::FrameStamp;
222 static osg::ref_ptr<osg::Group> mRoot = new osg::Group;
224 static osg::ref_ptr<osg::CameraView> mCameraView = new osg::CameraView;
225 static osg::ref_ptr<osg::CameraNode> mBackGroundCamera = new osg::CameraNode;
226 static osg::ref_ptr<osg::CameraNode> mSceneCamera = new osg::CameraNode;
228 static osg::ref_ptr<osg::Fog> mFog = new osg::Fog;
229 static osg::ref_ptr<osg::Fog> mRunwayLightingFog = new osg::Fog;
230 static osg::ref_ptr<osg::Fog> mTaxiLightingFog = new osg::Fog;
231 static osg::ref_ptr<osg::Fog> mGroundLightingFog = new osg::Fog;
233 FGRenderer::FGRenderer()
235 #ifdef FG_JPEG_SERVER
236 jpgRenderFrame = FGRenderer::update;
240 FGRenderer::~FGRenderer()
242 #ifdef FG_JPEG_SERVER
243 jpgRenderFrame = NULL;
247 // Initialize various GL/view parameters
249 FGRenderer::init( void ) {
251 osg::initNotifyLevel();
253 // Go full screen if requested ...
254 if ( fgGetBool("/sim/startup/fullscreen") )
257 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
258 (!fgGetBool("/sim/rendering/shading"))) {
259 // if fastest fog requested, or if flat shading force fastest
260 glHint ( GL_FOG_HINT, GL_FASTEST );
261 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
262 glHint ( GL_FOG_HINT, GL_DONT_CARE );
265 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
266 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
267 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
269 sceneView->setDefaults(osgUtil::SceneView::COMPILE_GLOBJECTS_AT_INIT);
271 mFog->setMode(osg::Fog::EXP2);
272 mRunwayLightingFog->setMode(osg::Fog::EXP2);
273 mTaxiLightingFog->setMode(osg::Fog::EXP2);
274 mGroundLightingFog->setMode(osg::Fog::EXP2);
276 sceneView->setFrameStamp(mFrameStamp.get());
278 sceneView->setUpdateVisitor(new SGUpdateVisitor);
280 sceneView->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
281 sceneView->getCamera()->setClearMask(0);
283 osg::StateSet* stateSet = mRoot->getOrCreateStateSet();
285 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
287 stateSet->setAttribute(new osg::Depth(osg::Depth::LEQUAL));
288 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
290 stateSet->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01));
291 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
292 stateSet->setAttribute(new osg::BlendFunc);
293 stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
295 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
297 // osg::Material* material = new osg::Material;
298 // stateSet->setAttribute(material);
300 // stateSet->setAttribute(new osg::CullFace(osg::CullFace::BACK));
301 // stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
304 // need to update the light on every frame
305 osg::Light* sunLight = new osg::Light;
306 sunLight->setLightNum(0);
307 sunLight->setUpdateCallback(new FGSunLightUpdateCallback);
308 stateSet->setAttributeAndModes(sunLight, osg::StateAttribute::ON);
309 osg::LightModel* lightModel = new osg::LightModel;
310 lightModel->setUpdateCallback(new FGLightModelUpdateCallback);
311 stateSet->setAttributeAndModes(lightModel, osg::StateAttribute::ON);
313 // this is the topmost scenegraph node for osg
314 mBackGroundCamera->addChild(thesky->getPreRoot());
315 mBackGroundCamera->setClearMask(GL_COLOR_BUFFER_BIT);
317 GLbitfield inheritanceMask = osg::CullSettings::ALL_VARIABLES;
318 inheritanceMask &= ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE;
319 inheritanceMask &= ~osg::CullSettings::NEAR_FAR_RATIO;
320 inheritanceMask &= ~osg::CullSettings::CULLING_MODE;
321 mBackGroundCamera->setInheritanceMask(inheritanceMask);
322 mBackGroundCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
323 mBackGroundCamera->setCullingMode(osg::CullSettings::NO_CULLING);
325 mBackGroundCamera->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
327 mRoot->addChild(mBackGroundCamera.get());
330 sceneView->getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
332 mSceneCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
333 inheritanceMask = osg::CullSettings::ALL_VARIABLES;
334 inheritanceMask &= ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE;
335 inheritanceMask &= ~osg::CullSettings::CULLING_MODE;
336 mSceneCamera->setInheritanceMask(inheritanceMask);
337 mSceneCamera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
338 mSceneCamera->setCullingMode(osg::CullSettings::DEFAULT_CULLING);
341 stateSet = globals->get_scenery()->get_scene_graph()->getOrCreateStateSet();
342 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
343 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
344 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
345 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
347 // switch to enable wireframe
348 osg::PolygonMode* polygonMode = new osg::PolygonMode;
349 polygonMode->setUpdateCallback(new FGWireFrameModeUpdateCallback);
350 stateSet->setAttributeAndModes(polygonMode);
352 // scene fog handling
353 stateSet->setAttributeAndModes(mFog.get());
354 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
356 mRoot->addChild(mSceneCamera.get());
358 mSceneCamera->addChild(globals->get_scenery()->get_scene_graph());
360 stateSet = mSceneCamera->getOrCreateStateSet();
361 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
362 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
364 // this one contains all lights, here we set the light states we did
365 // in the plib case with plain OpenGL
366 osg::Group* lightGroup = new osg::Group;
367 mSceneCamera->addChild(lightGroup);
368 lightGroup->addChild(globals->get_scenery()->get_gnd_lights_root());
369 lightGroup->addChild(globals->get_scenery()->get_vasi_lights_root());
370 lightGroup->addChild(globals->get_scenery()->get_rwy_lights_root());
371 lightGroup->addChild(globals->get_scenery()->get_taxi_lights_root());
373 stateSet = globals->get_scenery()->get_gnd_lights_root()->getOrCreateStateSet();
374 stateSet->setAttributeAndModes(mFog.get());
375 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
376 stateSet = globals->get_scenery()->get_vasi_lights_root()->getOrCreateStateSet();
377 stateSet->setAttributeAndModes(mRunwayLightingFog.get());
378 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
379 stateSet = globals->get_scenery()->get_rwy_lights_root()->getOrCreateStateSet();
380 stateSet->setAttributeAndModes(mRunwayLightingFog.get());
381 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
382 stateSet = globals->get_scenery()->get_taxi_lights_root()->getOrCreateStateSet();
383 stateSet->setAttributeAndModes(mTaxiLightingFog.get());
384 stateSet->setUpdateCallback(new FGFogEnableUpdateCallback);
386 mCameraView->addChild(mRoot.get());
387 sceneView->setSceneData(mCameraView.get());
389 mSceneCamera->addChild(thesky->getCloudRoot());
391 // sceneView->getState()->setCheckForGLErrors(osg::State::ONCE_PER_ATTRIBUTE);
395 // Update all Visuals (redraws anything graphics related)
397 FGRenderer::update( bool refresh_camera_settings ) {
398 bool scenery_loaded = fgGetBool("sim/sceneryloaded")
399 || fgGetBool("sim/sceneryloaded-override");
401 if ( idle_state < 1000 || !scenery_loaded ) {
402 // still initializing, draw the splash screen
405 // Keep resetting sim time while the sim is initializing
406 globals->set_sim_time_sec( 0.0 );
407 SGAnimation::set_sim_time_sec( 0.0 );
411 bool skyblend = fgGetBool("/sim/rendering/skyblend");
412 bool use_point_sprites = fgGetBool("/sim/rendering/point-sprites");
413 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
414 bool distance_attenuation
415 = fgGetBool("/sim/rendering/distance-attenuation");
416 SGConfigureDirectionalLights( use_point_sprites, enhanced_lighting,
417 distance_attenuation );
419 static const SGPropertyNode *groundlevel_nearplane
420 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
422 FGLight *l = static_cast<FGLight*>(globals->get_subsystem("lighting"));
425 double actual_visibility;
426 if (fgGetBool("/environment/clouds/status")) {
427 actual_visibility = thesky->get_visibility();
429 actual_visibility = fgGetDouble("/environment/visibility-m");
432 static double last_visibility = -9999;
433 if ( actual_visibility != last_visibility ) {
434 last_visibility = actual_visibility;
436 fog_exp_density = m_log01 / actual_visibility;
437 fog_exp2_density = sqrt_m_log01 / actual_visibility;
438 ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
439 if ( actual_visibility < 8000 ) {
440 rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
441 taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
443 rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
444 taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
446 mFog->setDensity(fog_exp2_density);
447 mRunwayLightingFog->setDensity(rwy_exp2_punch_through);
448 mTaxiLightingFog->setDensity(taxi_exp2_punch_through);
449 mGroundLightingFog->setDensity(ground_exp2_punch_through);
452 // idle_state is now 1000 meaning we've finished all our
453 // initializations and are running the main loop, so this will
454 // now work without seg faulting the system.
456 FGViewer *current__view = globals->get_current_view();
457 // Force update of center dependent values ...
458 current__view->set_dirty();
460 if ( refresh_camera_settings ) {
462 resize( fgGetInt("/sim/startup/xsize"),
463 fgGetInt("/sim/startup/ysize") );
465 // OSGFXME: compute the view directly without indirection through ssg
467 sgTransposeNegateMat4(viewmat, (sgVec4 *)current__view->get_VIEW());
468 sgMat4 cameraMatrix = {
469 { 1.0f, 0.0f, 0.0f, 0.0f },
470 { 0.0f, 0.0f, -1.0f, 0.0f },
471 { 0.0f, 1.0f, 0.0f, 0.0f },
472 { 0.0f, 0.0f, 0.0f, 1.0f }
474 sgPreMultMat4(cameraMatrix, viewmat);
477 for (unsigned i = 0; i < 4; ++i)
478 for (unsigned j = 0; j < 4; ++j)
479 m(i, j) = cameraMatrix[i][j];
483 mCameraView->setPosition(osg::Vec3d(m(3,0), m(3,1), m(3,2)));
484 mCameraView->setAttitude(attitude);
488 if ( fgGetBool("/sim/rendering/textures") ) {
489 SGVec4f clearColor(l->adj_fog_color());
490 mBackGroundCamera->setClearColor(clearColor.osg());
493 SGVec4f clearColor(l->sky_color());
494 mBackGroundCamera->setClearColor(clearColor.osg());
497 // update fog params if visibility has changed
498 double visibility_meters = fgGetDouble("/environment/visibility-m");
499 thesky->set_visibility(visibility_meters);
501 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
502 ( global_multi_loop * fgGetInt("/sim/speed-up") )
503 / (double)fgGetInt("/sim/model-hz") );
505 // update the sky dome
508 // The sun and moon distances are scaled down versions
509 // of the actual distance to get both the moon and the sun
510 // within the range of the far clip plane.
511 // Moon distance: 384,467 kilometers
512 // Sun distance: 150,000,000 kilometers
514 double sun_horiz_eff, moon_horiz_eff;
515 if (fgGetBool("/sim/rendering/horizon-effect")) {
516 sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
517 moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
519 sun_horiz_eff = moon_horiz_eff = 1.0;
522 static SGSkyState sstate;
524 sstate.view_pos = SGVec3f(current__view->get_view_pos());
525 sstate.zero_elev = SGVec3f(current__view->get_zero_elev());
526 sstate.view_up = SGVec3f(current__view->get_world_up());
527 sstate.lon = current__view->getLongitude_deg()
528 * SGD_DEGREES_TO_RADIANS;
529 sstate.lat = current__view->getLatitude_deg()
530 * SGD_DEGREES_TO_RADIANS;
531 sstate.alt = current__view->getAltitudeASL_ft()
533 sstate.spin = l->get_sun_rotation();
534 sstate.gst = globals->get_time_params()->getGst();
535 sstate.sun_ra = globals->get_ephem()->getSunRightAscension();
536 sstate.sun_dec = globals->get_ephem()->getSunDeclination();
537 sstate.sun_dist = 50000.0 * sun_horiz_eff;
538 sstate.moon_ra = globals->get_ephem()->getMoonRightAscension();
539 sstate.moon_dec = globals->get_ephem()->getMoonDeclination();
540 sstate.moon_dist = 40000.0 * moon_horiz_eff;
541 sstate.sun_angle = l->get_sun_angle();
545 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
546 << l->sky_color()[0] << " "
547 << l->sky_color()[1] << " "
548 << l->sky_color()[2] << " "
549 << l->sky_color()[3] );
550 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
551 << l->fog_color()[0] << " "
552 << l->fog_color()[1] << " "
553 << l->fog_color()[2] << " "
554 << l->fog_color()[3] );
555 SG_LOG( SG_GENERAL, SG_BULK,
556 " sun_angle = " << l->sun_angle
557 << " moon_angle = " << l->moon_angle );
560 static SGSkyColor scolor;
562 scolor.sky_color = SGVec3f(l->sky_color());
563 scolor.fog_color = SGVec3f(l->adj_fog_color());
564 scolor.cloud_color = SGVec3f(l->cloud_color());
565 scolor.sun_angle = l->get_sun_angle();
566 scolor.moon_angle = l->get_moon_angle();
567 scolor.nplanets = globals->get_ephem()->getNumPlanets();
568 scolor.nstars = globals->get_ephem()->getNumStars();
569 scolor.planet_data = globals->get_ephem()->getPlanets();
570 scolor.star_data = globals->get_ephem()->getStars();
572 thesky->reposition( sstate, delta_time_sec );
573 thesky->repaint( scolor );
576 SG_LOG( SG_GENERAL, SG_BULK,
577 "thesky->reposition( view_pos = " << view_pos[0] << " "
578 << view_pos[1] << " " << view_pos[2] );
579 SG_LOG( SG_GENERAL, SG_BULK,
580 " zero_elev = " << zero_elev[0] << " "
581 << zero_elev[1] << " " << zero_elev[2]
582 << " lon = " << cur_fdm_state->get_Longitude()
583 << " lat = " << cur_fdm_state->get_Latitude() );
584 SG_LOG( SG_GENERAL, SG_BULK,
585 " sun_rot = " << l->get_sun_rotation
586 << " gst = " << SGTime::cur_time_params->getGst() );
587 SG_LOG( SG_GENERAL, SG_BULK,
588 " sun ra = " << globals->get_ephem()->getSunRightAscension()
589 << " sun dec = " << globals->get_ephem()->getSunDeclination()
590 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
591 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
595 // shadows->setupShadows(
596 // current__view->getLongitude_deg(),
597 // current__view->getLatitude_deg(),
598 // globals->get_time_params()->getGst(),
599 // globals->get_ephem()->getSunRightAscension(),
600 // globals->get_ephem()->getSunDeclination(),
601 // l->get_sun_angle());
605 if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
606 SGVec4f color(l->adj_fog_color());
607 mFog->setColor(color.osg());
608 mRunwayLightingFog->setColor(color.osg());
609 mTaxiLightingFog->setColor(color.osg());
610 mGroundLightingFog->setColor(color.osg());
614 // sgEnviro.setLight(l->adj_fog_color());
616 // texture parameters
617 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
619 double agl = current__view->getAltitudeASL_ft()*SG_FEET_TO_METER
620 - current__view->getSGLocation()->get_cur_elev_m();
622 float scene_nearplane, scene_farplane;
624 scene_nearplane = 10.0f;
625 scene_farplane = 120000.0f;
627 scene_nearplane = groundlevel_nearplane->getDoubleValue();
628 scene_farplane = 120000.0f;
631 setNearFar( scene_nearplane, scene_farplane );
633 // sgEnviro.startOfFrame(current__view->get_view_pos(),
634 // current__view->get_world_up(),
635 // current__view->getLongitude_deg(),
636 // current__view->getLatitude_deg(),
637 // current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
641 // sgEnviro.drawLightning();
643 // double current_view_origin_airspeed_horiz_kt =
644 // fgGetDouble("/velocities/airspeed-kt", 0.0)
645 // * cos( fgGetDouble("/orientation/pitch-deg", 0.0)
646 // * SGD_DEGREES_TO_RADIANS);
647 // TODO:find the real view speed, not the AC one
648 // sgEnviro.drawPrecipitation(
649 // fgGetDouble("/environment/metar/rain-norm", 0.0),
650 // fgGetDouble("/environment/metar/snow-norm", 0.0),
651 // fgGetDouble("/environment/metar/hail-norm", 0.0),
652 // current__view->getPitch_deg() + current__view->getPitchOffset_deg(),
653 // current__view->getRoll_deg() + current__view->getRollOffset_deg(),
654 // - current__view->getHeadingOffset_deg(),
655 // current_view_origin_airspeed_horiz_kt
660 // shadows->endOfFrame();
662 // need to call the update visitor once
663 globals->get_aircraft_model()->select( true );
664 FGTileMgr::set_tile_filter( true );
665 mFrameStamp->setReferenceTime(globals->get_sim_time_sec());
666 mFrameStamp->setFrameNumber(1+mFrameStamp->getFrameNumber());
667 mFrameStamp->setCalendarTime(*globals->get_time_params()->getGmt());
672 glPushAttrib(GL_ALL_ATTRIB_BITS);
673 glPushClientAttrib(~0u);
675 // display HUD && Panel
677 glDisable( GL_DEPTH_TEST );
679 fgCockpitUpdate(sceneView->getState());
681 FGInstrumentMgr *instr = static_cast<FGInstrumentMgr*>(globals->get_subsystem("instrumentation"));
682 HUD *hud = static_cast<HUD*>(instr->get_subsystem("hud"));
683 hud->draw(*sceneView->getState());
685 // update the panel subsystem
686 if ( globals->get_current_panel() != NULL )
687 globals->get_current_panel()->update(*sceneView->getState());
688 // We don't need a state here - can be safely removed when we can pick
692 if((fgGetBool("/sim/atc/enabled"))
693 || (fgGetBool("/sim/ai-traffic/enabled")))
694 globals->get_ATC_display()->update(delta_time_sec,
695 *sceneView->getState());
697 // We can do translucent menus, so why not. :-)
698 glDisable( GL_TEXTURE_2D ) ;
699 glDisable( GL_CULL_FACE ) ;
700 glEnable( GL_BLEND ) ;
701 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
704 // Fade out the splash screen over the first three seconds.
705 double t = globals->get_sim_time_sec();
707 fgSplashUpdate((2.5 - t) / 2.5);
715 // options.cxx needs to see this for toggle_panel()
716 // Handle new window size or exposure
718 FGRenderer::resize( int width, int height ) {
721 if ( (!fgGetBool("/sim/virtual-cockpit"))
722 && fgPanelVisible() && idle_state == 1000 ) {
723 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
724 globals->get_current_panel()->getYOffset()) / 768.0);
729 sceneView->getViewport()->setViewport(0, height - view_h, width, view_h);
731 static int lastwidth = 0;
732 static int lastheight = 0;
733 if (width != lastwidth)
734 fgSetInt("/sim/startup/xsize", lastwidth = width);
735 if (height != lastheight)
736 fgSetInt("/sim/startup/ysize", lastheight = height);
738 guiInitMouse(width, height);
741 FGViewMgr *viewmgr = globals->get_viewmgr();
743 for ( int i = 0; i < viewmgr->size(); ++i ) {
744 viewmgr->get_view(i)->
745 set_aspect_ratio((float)view_h / (float)width);
748 setFOV( viewmgr->get_current_view()->get_h_fov(),
749 viewmgr->get_current_view()->get_v_fov() );
750 // cout << "setFOV(" << viewmgr->get_current_view()->get_h_fov()
751 // << ", " << viewmgr->get_current_view()->get_v_fov() << ")"
757 // These are wrapper functions around ssgSetNearFar() and ssgSetFOV()
758 // which will post process and rewrite the resulting frustum if we
759 // want to do asymmetric view frustums.
761 static void fgHackFrustum() {
763 // specify a percent of the configured view frustum to actually
764 // display. This is a bit of a hack to achieve asymmetric view
765 // frustums. For instance, if you want to display two monitors
766 // side by side, you could specify each with a double fov, a 0.5
767 // aspect ratio multiplier, and then the left side monitor would
768 // have a left_pct = 0.0, a right_pct = 0.5, a bottom_pct = 0.0,
769 // and a top_pct = 1.0. The right side monitor would have a
770 // left_pct = 0.5 and a right_pct = 1.0.
772 static SGPropertyNode *left_pct
773 = fgGetNode("/sim/current-view/frustum-left-pct");
774 static SGPropertyNode *right_pct
775 = fgGetNode("/sim/current-view/frustum-right-pct");
776 static SGPropertyNode *bottom_pct
777 = fgGetNode("/sim/current-view/frustum-bottom-pct");
778 static SGPropertyNode *top_pct
779 = fgGetNode("/sim/current-view/frustum-top-pct");
784 sceneView->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar);
785 // cout << " l = " << f->getLeft()
786 // << " r = " << f->getRight()
787 // << " b = " << f->getBot()
788 // << " t = " << f->getTop()
789 // << " n = " << f->getNear()
790 // << " f = " << f->getFar()
793 double width = right - left;
794 double height = top - bottom;
798 if ( left_pct != NULL ) {
799 l = left + width * left_pct->getDoubleValue();
804 if ( right_pct != NULL ) {
805 r = left + width * right_pct->getDoubleValue();
810 if ( bottom_pct != NULL ) {
811 b = bottom + height * bottom_pct->getDoubleValue();
816 if ( top_pct != NULL ) {
817 t = bottom + height * top_pct->getDoubleValue();
822 sceneView->setProjectionMatrixAsFrustum(l, r, b, t, zNear, zFar);
826 // we need some static storage space for these values. However, we
827 // can't store it in a renderer class object because the functions
828 // that manipulate these are static. They are static so they can
829 // interface to the display callback system. There's probably a
830 // better way, there has to be a better way, but I'm not seeing it
832 static float fov_width = 55.0;
833 static float fov_height = 42.0;
834 static float fov_near = 1.0;
835 static float fov_far = 1000.0;
838 /** FlightGear code should use this routine to set the FOV rather than
839 * calling the ssg routine directly
841 void FGRenderer::setFOV( float w, float h ) {
845 sceneView->setProjectionMatrixAsPerspective(fov_height,
846 fov_width/fov_height,
848 // fully specify the view frustum before hacking it (so we don't
849 // accumulate hacked effects
851 // sgEnviro.setFOV( w, h );
855 /** FlightGear code should use this routine to set the Near/Far clip
856 * planes rather than calling the ssg routine directly
858 void FGRenderer::setNearFar( float n, float f ) {
859 // OSGFIXME: we have currently too much z-buffer fights
864 sceneView->setProjectionMatrixAsPerspective(fov_height,
865 fov_width/fov_height,
868 sceneView->getCamera()->setNearFarRatio(fov_near/fov_far);
869 mSceneCamera->setNearFarRatio(fov_near/fov_far);
871 // fully specify the view frustum before hacking it (so we don't
872 // accumulate hacked effects
876 bool FGRenderer::getPickInfo( SGVec3d& pt, SGVec3d& dir,
877 unsigned x, unsigned y )
879 // Get the matrices involved in the transform from global to screen
881 osg::Matrix pm = sceneView->getCamera()->getProjectionMatrix();
884 osg::NodePathList paths;
885 paths = globals->get_scenery()->get_scene_graph()->getParentalNodePaths();
886 if (!paths.empty()) {
887 // Ok, we know that this should not have multiple parents ...
888 // FIXME: is this allways true?
889 mv = osg::computeLocalToEye(sceneView->getCamera()->getViewMatrix(),
890 paths.front(), false);
893 // Compose and invert
894 osg::Matrix m = osg::Matrix::inverse(mv*pm);
896 // Get the width and height of the display to be able to normalize the
898 float width = fgGetInt("/sim/startup/xsize");
899 float height = fgGetInt("/sim/startup/ysize");
901 // Compute some coordinates of in the line from the eyepoint to the
902 // mouse click coodinates.
903 // First build the normalized projection coordinates
904 osg::Vec4 normPt((2*x - width)/width, -(2*y - height)/height, 1, 1);
905 // Transform them into the real world
906 osg::Vec4 worldPt4 = m.preMult(normPt);
907 if (fabs(worldPt4[3]) < SGLimitsf::min())
909 SGVec3f worldPt(worldPt4[0]/worldPt4[3],
910 worldPt4[1]/worldPt4[3],
911 worldPt4[2]/worldPt4[3]);
913 // Now build a direction from the point
914 FGViewer* view = globals->get_current_view();
915 dir = normalize(toVec3d(worldPt - SGVec3f(view->get_view_pos())));
917 // Copy the start point
918 pt = SGVec3d(view->get_absolute_view_pos());
920 // OSGFIXME: ist this sufficient??? especially the precision problems here??
921 // bool mSceneView->projectWindowXYIntoObject(int x,int y,osg::Vec3& near_point,osg::Vec3& far_point) const;
927 // end of renderer.cxx