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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <simgear/compiler.h>
36 #include <plib/netSocket.h>
38 #include <simgear/screen/extensions.hxx>
39 #include <simgear/scene/material/matlib.hxx>
40 #include <simgear/props/props.hxx>
41 #include <simgear/timing/sg_time.hxx>
42 #include <simgear/scene/model/animation.hxx>
43 #include <simgear/ephemeris/ephemeris.hxx>
44 #include <simgear/scene/model/placement.hxx>
45 #include <simgear/math/sg_random.h>
46 #include <simgear/scene/model/modellib.hxx>
47 #include <simgear/scene/model/model.hxx>
49 #include <simgear/screen/jpgfactory.hxx>
52 #include <simgear/environment/visual_enviro.hxx>
54 #include <simgear/scene/model/shadowvolume.hxx>
56 #include <Scenery/tileentry.hxx>
57 #include <Time/light.hxx>
58 #include <Time/light.hxx>
59 #include <Aircraft/aircraft.hxx>
60 // #include <Aircraft/replay.hxx>
61 #include <Cockpit/panel.hxx>
62 #include <Cockpit/cockpit.hxx>
63 #include <Cockpit/hud.hxx>
64 #include <Model/panelnode.hxx>
65 #include <Model/modelmgr.hxx>
66 #include <Model/acmodel.hxx>
67 #include <Scenery/scenery.hxx>
68 #include <Scenery/tilemgr.hxx>
69 #include <Scripting/NasalDisplay.hxx>
70 #include <ATC/ATCdisplay.hxx>
71 #include <GUI/new_gui.hxx>
74 #include "renderer.hxx"
78 extern void sgShaderFrameInit(double delta_time_sec);
80 float default_attenuation[3] = {1.0, 0.0, 0.0};
82 ssgSelector *lightpoints_brightness = new ssgSelector;
83 ssgTransform *lightpoints_transform = new ssgTransform;
84 FGTileEntry *dummy_tile;
87 // Clip plane settings...
88 float scene_nearplane = 0.5f;
89 float scene_farplane = 120000.0f;
91 glPointParameterfProc glPointParameterfPtr = 0;
92 glPointParameterfvProc glPointParameterfvPtr = 0;
93 bool glPointParameterIsSupported = false;
96 // fog constants. I'm a little nervous about putting actual code out
97 // here but it seems to work (?)
98 static const double m_log01 = -log( 0.01 );
99 static const double sqrt_m_log01 = sqrt( m_log01 );
100 static GLfloat fog_exp_density;
101 static GLfloat fog_exp2_density;
102 static GLfloat rwy_exp2_punch_through;
103 static GLfloat taxi_exp2_punch_through;
104 static GLfloat ground_exp2_punch_through;
110 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
112 { 1.0f, 0.0f, 0.0f, 0.0f },
113 { 0.0f, 0.0f, -1.0f, 0.0f },
114 { 0.0f, 1.0f, 0.0f, 0.0f },
115 { 0.0f, 0.0f, 0.0f, 1.0f }
118 ssgSimpleState *cloud3d_imposter_state;
119 ssgSimpleState *default_state;
120 ssgSimpleState *hud_and_panel;
121 ssgSimpleState *menus;
123 SGShadowVolume *shadows;
125 FGRenderer::FGRenderer()
127 #ifdef FG_JPEG_SERVER
128 jpgRenderFrame = FGRenderer::update;
132 FGRenderer::~FGRenderer()
134 #ifdef FG_JPEG_SERVER
135 jpgRenderFrame = NULL;
141 FGRenderer::build_states( void ) {
142 default_state = new ssgSimpleState;
143 default_state->ref();
144 default_state->disable( GL_TEXTURE_2D );
145 default_state->enable( GL_CULL_FACE );
146 default_state->enable( GL_COLOR_MATERIAL );
147 default_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
148 default_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
149 default_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
150 default_state->disable( GL_BLEND );
151 default_state->disable( GL_ALPHA_TEST );
152 default_state->disable( GL_LIGHTING );
154 cloud3d_imposter_state = new ssgSimpleState;
155 cloud3d_imposter_state->ref();
156 cloud3d_imposter_state->enable( GL_TEXTURE_2D );
157 cloud3d_imposter_state->enable( GL_CULL_FACE );
158 cloud3d_imposter_state->enable( GL_COLOR_MATERIAL );
159 cloud3d_imposter_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
160 cloud3d_imposter_state->setMaterial( GL_DIFFUSE, 1, 1, 1, 1 );
161 cloud3d_imposter_state->setMaterial( GL_AMBIENT, 1, 1, 1, 1 );
162 cloud3d_imposter_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
163 cloud3d_imposter_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
164 cloud3d_imposter_state->enable( GL_BLEND );
165 cloud3d_imposter_state->enable( GL_ALPHA_TEST );
166 cloud3d_imposter_state->disable( GL_LIGHTING );
168 hud_and_panel = new ssgSimpleState;
169 hud_and_panel->ref();
170 hud_and_panel->disable( GL_CULL_FACE );
171 hud_and_panel->disable( GL_TEXTURE_2D );
172 hud_and_panel->disable( GL_LIGHTING );
173 hud_and_panel->enable( GL_BLEND );
175 menus = new ssgSimpleState;
177 menus->disable( GL_CULL_FACE );
178 menus->disable( GL_TEXTURE_2D );
179 menus->enable( GL_BLEND );
181 shadows = new SGShadowVolume( globals->get_scenery()->get_scene_graph() );
182 shadows->init( fgGetNode("/sim/rendering", true) );
183 shadows->addOccluder( globals->get_scenery()->get_aircraft_branch(), SGShadowVolume::occluderTypeAircraft );
188 // Initialize various GL/view parameters
190 FGRenderer::init( void ) {
192 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
194 // Go full screen if requested ...
195 if ( fgGetBool("/sim/startup/fullscreen") ) {
199 // If enabled, normal vectors specified with glNormal are scaled
200 // to unit length after transformation. Enabling this has
201 // performance implications. See the docs for glNormal.
202 // glEnable( GL_NORMALIZE );
204 glEnable( GL_LIGHTING );
205 glEnable( GL_LIGHT0 );
206 // glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec ); // done later with ssg
209 sgSetVec3( sunpos, l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2] );
210 ssgGetLight( 0 ) -> setPosition( sunpos );
212 glFogi (GL_FOG_MODE, GL_EXP2);
213 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
214 (!fgGetBool("/sim/rendering/shading"))) {
215 // if fastest fog requested, or if flat shading force fastest
216 glHint ( GL_FOG_HINT, GL_FASTEST );
217 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
218 glHint ( GL_FOG_HINT, GL_DONT_CARE );
220 if ( fgGetBool("/sim/rendering/wireframe") ) {
222 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
225 // This is the default anyways, but it can't hurt
226 glFrontFace ( GL_CCW );
229 glEnable(GL_POINT_SMOOTH);
230 glEnable(GL_LINE_SMOOTH);
231 // glEnable(GL_POLYGON_SMOOTH);
232 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
233 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
234 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
239 // Update all Visuals (redraws anything graphics related)
241 FGRenderer::update( bool refresh_camera_settings ) {
242 bool scenery_loaded = fgGetBool("sim/sceneryloaded") \
243 || fgGetBool("sim/sceneryloaded-override");
245 if ( idle_state < 1000 || !scenery_loaded ) {
246 // still initializing, draw the splash screen
249 // Keep resetting sim time while the sim is initializing
250 globals->set_sim_time_sec( 0.0 );
251 SGAnimation::set_sim_time_sec( 0.0 );
255 bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
256 bool skyblend = fgGetBool("/sim/rendering/skyblend");
257 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
258 bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
259 bool volumetric_clouds = sgEnviro.get_clouds_enable_state();
260 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
261 bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") &&
262 !volumetric_clouds &&
263 !SGCloudLayer::enable_bump_mapping; // ugly artefact now
265 bool multi_pass_clouds = false;
267 bool draw_clouds = fgGetBool("/environment/clouds/status");
269 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
270 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
272 // static const SGPropertyNode *longitude
273 // = fgGetNode("/position/longitude-deg");
274 // static const SGPropertyNode *latitude
275 // = fgGetNode("/position/latitude-deg");
276 // static const SGPropertyNode *altitude
277 // = fgGetNode("/position/altitude-ft");
278 static const SGPropertyNode *groundlevel_nearplane
279 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
281 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
282 static double last_visibility = -9999;
285 double actual_visibility;
286 if (fgGetBool("/environment/clouds/status")) {
287 actual_visibility = thesky->get_visibility();
289 actual_visibility = fgGetDouble("/environment/visibility-m");
292 // TODO:TEST only, don't commit that !!
293 // sgFXperFrameInit();
295 sgShaderFrameInit(delta_time_sec);
297 if ( actual_visibility != last_visibility ) {
298 last_visibility = actual_visibility;
300 fog_exp_density = m_log01 / actual_visibility;
301 fog_exp2_density = sqrt_m_log01 / actual_visibility;
302 ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
303 if ( actual_visibility < 8000 ) {
304 rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
305 taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
307 rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
308 taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
313 // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
314 // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
315 // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
316 // GLfloat mat_shininess[] = { 10.0 };
317 GLbitfield clear_mask;
319 // idle_state is now 1000 meaning we've finished all our
320 // initializations and are running the main loop, so this will
321 // now work without seg faulting the system.
323 FGViewer *current__view = globals->get_current_view();
325 // calculate our current position in cartesian space
326 Point3D cntr = globals->get_scenery()->get_next_center();
327 globals->get_scenery()->set_center(cntr);
328 // Force update of center dependent values ...
329 current__view->set_dirty();
331 if ( refresh_camera_settings ) {
333 resize( fgGetInt("/sim/startup/xsize"),
334 fgGetInt("/sim/startup/ysize") );
336 // Tell GL we are switching to model view parameters
337 glMatrixMode(GL_MODELVIEW);
339 ssgSetCamera( (sgVec4 *)current__view->get_VIEW() );
342 clear_mask = GL_DEPTH_BUFFER_BIT;
343 if ( fgGetBool("/sim/rendering/wireframe") ) {
344 clear_mask |= GL_COLOR_BUFFER_BIT;
348 if ( fgGetBool("/sim/rendering/textures") ) {
349 // glClearColor(black[0], black[1], black[2], black[3]);
350 glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
351 l->adj_fog_color()[2], l->adj_fog_color()[3]);
352 clear_mask |= GL_COLOR_BUFFER_BIT;
355 glClearColor(l->sky_color()[0], l->sky_color()[1],
356 l->sky_color()[2], l->sky_color()[3]);
357 clear_mask |= GL_COLOR_BUFFER_BIT;
359 if ( multi_pass_clouds && draw_clouds ) {
361 clear_mask |= GL_STENCIL_BUFFER_BIT;
363 glClear( clear_mask );
365 // set the opengl state to known default values
366 default_state->force();
368 // update fog params if visibility has changed
369 double visibility_meters = fgGetDouble("/environment/visibility-m");
370 thesky->set_visibility(visibility_meters);
372 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
373 ( global_multi_loop * fgGetInt("/sim/speed-up") )
374 / (double)fgGetInt("/sim/model-hz") );
376 // Set correct opengl fog density
377 glFogf (GL_FOG_DENSITY, fog_exp2_density);
379 // update the sky dome
382 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
383 << l->sky_color()[0] << " "
384 << l->sky_color()[1] << " "
385 << l->sky_color()[2] << " "
386 << l->sky_color()[3] );
387 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
388 << l->fog_color()[0] << " "
389 << l->fog_color()[1] << " "
390 << l->fog_color()[2] << " "
391 << l->fog_color()[3] );
392 SG_LOG( SG_GENERAL, SG_BULK,
393 " sun_angle = " << l->sun_angle
394 << " moon_angle = " << l->moon_angle );
397 static SGSkyColor scolor;
398 // FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
400 scolor.sky_color = l->sky_color();
401 scolor.fog_color = l->adj_fog_color();
402 scolor.cloud_color = l->cloud_color();
403 scolor.sun_angle = l->get_sun_angle();
404 scolor.moon_angle = l->get_moon_angle();
405 scolor.nplanets = globals->get_ephem()->getNumPlanets();
406 scolor.nstars = globals->get_ephem()->getNumStars();
407 scolor.planet_data = globals->get_ephem()->getPlanets();
408 scolor.star_data = globals->get_ephem()->getStars();
410 thesky->repaint( scolor );
413 SG_LOG( SG_GENERAL, SG_BULK,
414 "thesky->reposition( view_pos = " << view_pos[0] << " "
415 << view_pos[1] << " " << view_pos[2] );
416 SG_LOG( SG_GENERAL, SG_BULK,
417 " zero_elev = " << zero_elev[0] << " "
418 << zero_elev[1] << " " << zero_elev[2]
419 << " lon = " << cur_fdm_state->get_Longitude()
420 << " lat = " << cur_fdm_state->get_Latitude() );
421 SG_LOG( SG_GENERAL, SG_BULK,
422 " sun_rot = " << l->get_sun_rotation
423 << " gst = " << SGTime::cur_time_params->getGst() );
424 SG_LOG( SG_GENERAL, SG_BULK,
425 " sun ra = " << globals->get_ephem()->getSunRightAscension()
426 << " sun dec = " << globals->get_ephem()->getSunDeclination()
427 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
428 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
431 // The sun and moon distances are scaled down versions
432 // of the actual distance to get both the moon and the sun
433 // within the range of the far clip plane.
434 // Moon distance: 384,467 kilometers
435 // Sun distance: 150,000,000 kilometers
436 double sun_horiz_eff, moon_horiz_eff;
437 if (fgGetBool("/sim/rendering/horizon-effect")) {
438 sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
439 moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
441 sun_horiz_eff = moon_horiz_eff = 1.0;
444 static SGSkyState sstate;
446 sstate.view_pos = current__view->get_view_pos();
447 sstate.zero_elev = current__view->get_zero_elev();
448 sstate.view_up = current__view->get_world_up();
449 sstate.lon = current__view->getLongitude_deg()
450 * SGD_DEGREES_TO_RADIANS;
451 sstate.lat = current__view->getLatitude_deg()
452 * SGD_DEGREES_TO_RADIANS;
453 sstate.alt = current__view->getAltitudeASL_ft()
455 sstate.spin = l->get_sun_rotation();
456 sstate.gst = globals->get_time_params()->getGst();
457 sstate.sun_ra = globals->get_ephem()->getSunRightAscension();
458 sstate.sun_dec = globals->get_ephem()->getSunDeclination();
459 sstate.sun_dist = 50000.0 * sun_horiz_eff;
460 sstate.moon_ra = globals->get_ephem()->getMoonRightAscension();
461 sstate.moon_dec = globals->get_ephem()->getMoonDeclination();
462 sstate.moon_dist = 40000.0 * moon_horiz_eff;
464 thesky->reposition( sstate, delta_time_sec );
466 shadows->setupShadows(
467 current__view->getLongitude_deg(),
468 current__view->getLatitude_deg(),
469 globals->get_time_params()->getGst(),
470 globals->get_ephem()->getSunRightAscension(),
471 globals->get_ephem()->getSunDeclination(),
475 glEnable( GL_DEPTH_TEST );
476 if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
478 glFogi( GL_FOG_MODE, GL_EXP2 );
479 glFogfv( GL_FOG_COLOR, l->adj_fog_color() );
483 // set sun/lighting parameters
484 ssgGetLight( 0 ) -> setPosition( l->sun_vec() );
486 // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
487 // we only update GL_AMBIENT for our lights we will never get
488 // a completely dark scene. So, we set GL_LIGHT_MODEL_AMBIENT
489 // explicitely to black.
490 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
492 ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
493 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
494 ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_specular() );
496 sgEnviro.setLight(l->adj_fog_color());
498 // texture parameters
499 // glEnable( GL_TEXTURE_2D );
500 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
501 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
503 double agl = current__view->getAltitudeASL_ft()*SG_FEET_TO_METER
504 - current__view->getSGLocation()->get_cur_elev_m();
507 scene_nearplane = 10.0f;
508 scene_farplane = 120000.0f;
510 scene_nearplane = groundlevel_nearplane->getDoubleValue();
511 scene_farplane = 120000.0f;
514 setNearFar( scene_nearplane, scene_farplane );
516 sgEnviro.startOfFrame(current__view->get_view_pos(),
517 current__view->get_world_up(),
518 current__view->getLongitude_deg(),
519 current__view->getLatitude_deg(),
520 current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
523 if ( draw_otw && skyblend ) {
524 // draw the sky backdrop
526 // we need a white diffuse light for the phase of the moon
527 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
528 thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
530 // return to the desired diffuse color
531 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
534 // draw the ssg scene
535 glEnable( GL_DEPTH_TEST );
537 if ( fgGetBool("/sim/rendering/wireframe") ) {
539 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
545 FGTileMgr::set_tile_filter( true );
546 sgSetModelFilter( false );
547 globals->get_aircraft_model()->select( false );
548 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
550 // Disable depth buffer update, draw the clouds
551 glDepthMask( GL_FALSE );
552 if( !volumetric_clouds )
553 thesky->drawUpperClouds();
554 if ( multi_pass_clouds ) {
555 thesky->drawLowerClouds();
557 glDepthMask( GL_TRUE );
559 if ( multi_pass_clouds ) {
560 // Draw the objects except the aircraft
561 // and update the stencil buffer with 1
562 glEnable( GL_STENCIL_TEST );
563 glStencilFunc( GL_ALWAYS, 1, 1 );
564 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
566 FGTileMgr::set_tile_filter( false );
567 sgSetModelFilter( true );
568 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
570 FGTileMgr::set_tile_filter( true );
571 sgSetModelFilter( true );
572 globals->get_aircraft_model()->select( false );
573 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
577 // This is a bit kludgy. Every 200 frames, do an extra
578 // traversal of the scene graph without drawing anything, but
579 // with the field-of-view set to 360x360 degrees. This
580 // ensures that out-of-range random objects that are not in
581 // the current view frustum will still be freed properly.
582 static int counter = 0;
584 if (counter >= 200) {
587 // No need to put the near plane too close;
588 // this way, at least the aircraft can be
590 f.setNearFar(1000, 1000000);
592 ssgGetModelviewMatrix(m);
593 FGTileMgr::set_tile_filter( true );
594 sgSetModelFilter( true );
595 globals->get_scenery()->get_scene_graph()->cull(&f, m, true);
599 // change state for lighting here
601 // draw runway lighting
602 glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
604 // CLO - 02/25/2005 - DO WE NEED THIS extra fgSetNearFar()?
605 // fgSetNearFar( scene_nearplane, scene_farplane );
607 if ( enhanced_lighting ) {
609 // Enable states for drawing points with GL_extension
610 glEnable(GL_POINT_SMOOTH);
612 if ( distance_attenuation && glPointParameterIsSupported )
614 // Enable states for drawing points with GL_extension
615 glEnable(GL_POINT_SMOOTH);
617 float quadratic[3] = {1.0, 0.001, 0.0000001};
618 // makes the points fade as they move away
619 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
620 glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0);
625 // blending function for runway lights
626 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
629 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
630 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
631 glEnable(GL_TEXTURE_GEN_S);
632 glEnable(GL_TEXTURE_GEN_T);
633 glPolygonMode(GL_FRONT, GL_POINT);
635 // draw runway lighting
637 ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
638 ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
641 // change punch through and then draw taxi lighting
642 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
644 // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
645 // glFogfv ( GL_FOG_COLOR, taxi_fog );
647 ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
651 glPolygonMode(GL_FRONT, GL_FILL);
652 glDisable(GL_TEXTURE_GEN_S);
653 glDisable(GL_TEXTURE_GEN_T);
655 //static int _frame_count = 0;
656 //if (_frame_count % 30 == 0) {
657 // printf("SSG: %s\n", ssgShowStats());
665 if ( enhanced_lighting ) {
666 if ( distance_attenuation && glPointParameterIsSupported ) {
667 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
668 default_attenuation);
672 glDisable(GL_POINT_SMOOTH);
675 // draw ground lighting
676 glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
678 ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
681 sgEnviro.drawLightning();
683 if ( draw_otw && draw_clouds ) {
684 if ( multi_pass_clouds ) {
685 // Disable depth buffer update, draw the clouds where the
686 // objects overwrite the already drawn clouds, by testing
687 // the stencil buffer against 1
688 glDepthMask( GL_FALSE );
689 glStencilFunc( GL_EQUAL, 1, 1 );
690 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
691 thesky->drawUpperClouds();
692 thesky->drawLowerClouds();
693 glDepthMask( GL_TRUE );
694 glDisable( GL_STENCIL_TEST );
696 glDepthMask( GL_FALSE );
697 if( volumetric_clouds )
698 thesky->drawUpperClouds();
699 thesky->drawLowerClouds();
700 glDepthMask( GL_TRUE );
703 sgEnviro.drawPrecipitation(
704 fgGetDouble("/environment/metar/rain-norm", 0.0),
705 fgGetDouble("/environment/metar/snow-norm", 0.0),
706 fgGetDouble("/environment/metar/hail-norm", 0.0),
707 current__view->getPitch_deg() - current__view->getPitchOffset_deg(),
708 current__view->getRoll_deg() + current__view->getRollOffset_deg(),
709 - current__view->getHeadingOffset_deg(),
710 fgGetDouble("/velocities/airspeed-kt", 0.0));
712 // compute shadows and project them on screen
713 bool is_internal = globals->get_current_view()->getInternal();
714 // draw before ac because ac internal rendering clear the depth buffer
716 globals->get_aircraft_model()->select( true );
718 shadows->endOfFrame();
721 FGTileMgr::set_tile_filter( false );
722 sgSetModelFilter( false );
723 globals->get_aircraft_model()->select( true );
724 globals->get_model_mgr()->draw();
725 globals->get_aircraft_model()->draw();
727 FGTileMgr::set_tile_filter( true );
728 sgSetModelFilter( true );
729 globals->get_aircraft_model()->select( true );
731 // in 'external' view the ac can be culled, so shadows have not been draw in the
732 // posttrav callback, this would be a rare case if the getInternal was acting
733 // as expected (ie in internal view, getExternal returns false)
735 shadows->endOfFrame();
737 // display HUD && Panel
739 glDisable( GL_DEPTH_TEST );
740 // glDisable( GL_CULL_FACE );
741 // glDisable( GL_TEXTURE_2D );
743 // update the controls subsystem
744 globals->get_controls()->update(delta_time_sec);
746 hud_and_panel->apply();
749 // Use the hud_and_panel ssgSimpleState for rendering the ATC output
750 // This only works properly if called before the panel call
751 if((fgGetBool("/sim/atc/enabled")) || (fgGetBool("/sim/ai-traffic/enabled")))
752 globals->get_ATC_display()->update(delta_time_sec);
754 // Update any messages from the Nasal System
755 globals->get_Nasal_display()->update(delta_time_sec);
757 // update the panel subsystem
758 if ( globals->get_current_panel() != NULL ) {
759 globals->get_current_panel()->update(delta_time_sec);
763 // We can do translucent menus, so why not. :-)
765 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
767 // glDisable ( GL_BLEND ) ;
769 glEnable( GL_DEPTH_TEST );
772 // Fade out the splash screen over the first three seconds.
773 double t = globals->get_sim_time_sec();
775 fgSplashUpdate((2.5 - t) / 2.5);
780 // options.cxx needs to see this for toggle_panel()
781 // Handle new window size or exposure
783 FGRenderer::resize( int width, int height ) {
786 if ( (!fgGetBool("/sim/virtual-cockpit"))
787 && fgPanelVisible() && idle_state == 1000 ) {
788 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
789 globals->get_current_panel()->getYOffset()) / 768.0);
794 glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) );
796 fgSetInt("/sim/startup/xsize", width);
797 fgSetInt("/sim/startup/ysize", height);
798 guiInitMouse(width, height);
801 FGViewMgr *viewmgr = globals->get_viewmgr();
803 for ( int i = 0; i < viewmgr->size(); ++i ) {
804 viewmgr->get_view(i)->
805 set_aspect_ratio((float)view_h / (float)width);
808 setFOV( viewmgr->get_current_view()->get_h_fov(),
809 viewmgr->get_current_view()->get_v_fov() );
810 // cout << "setFOV(" << viewmgr->get_current_view()->get_h_fov()
811 // << ", " << viewmgr->get_current_view()->get_v_fov() << ")"
821 // These are wrapper functions around ssgSetNearFar() and ssgSetFOV()
822 // which will post process and rewrite the resulting frustum if we
823 // want to do asymmetric view frustums.
825 static void fgHackFrustum() {
827 // specify a percent of the configured view frustum to actually
828 // display. This is a bit of a hack to achieve asymmetric view
829 // frustums. For instance, if you want to display two monitors
830 // side by side, you could specify each with a double fov, a 0.5
831 // aspect ratio multiplier, and then the left side monitor would
832 // have a left_pct = 0.0, a right_pct = 0.5, a bottom_pct = 0.0,
833 // and a top_pct = 1.0. The right side monitor would have a
834 // left_pct = 0.5 and a right_pct = 1.0.
836 static SGPropertyNode *left_pct
837 = fgGetNode("/sim/current-view/frustum-left-pct");
838 static SGPropertyNode *right_pct
839 = fgGetNode("/sim/current-view/frustum-right-pct");
840 static SGPropertyNode *bottom_pct
841 = fgGetNode("/sim/current-view/frustum-bottom-pct");
842 static SGPropertyNode *top_pct
843 = fgGetNode("/sim/current-view/frustum-top-pct");
845 sgFrustum *f = ssgGetFrustum();
847 // cout << " l = " << f->getLeft()
848 // << " r = " << f->getRight()
849 // << " b = " << f->getBot()
850 // << " t = " << f->getTop()
851 // << " n = " << f->getNear()
852 // << " f = " << f->getFar()
855 double width = f->getRight() - f->getLeft();
856 double height = f->getTop() - f->getBot();
860 if ( left_pct != NULL ) {
861 l = f->getLeft() + width * left_pct->getDoubleValue();
866 if ( right_pct != NULL ) {
867 r = f->getLeft() + width * right_pct->getDoubleValue();
872 if ( bottom_pct != NULL ) {
873 b = f->getBot() + height * bottom_pct->getDoubleValue();
878 if ( top_pct != NULL ) {
879 t = f->getBot() + height * top_pct->getDoubleValue();
884 ssgSetFrustum(l, r, b, t, f->getNear(), f->getFar());
888 // we need some static storage space for these values. However, we
889 // can't store it in a renderer class object because the functions
890 // that manipulate these are static. They are static so they can
891 // interface to the display callback system. There's probably a
892 // better way, there has to be a better way, but I'm not seeing it
894 static float fov_width = 55.0;
895 static float fov_height = 42.0;
896 static float fov_near = 1.0;
897 static float fov_far = 1000.0;
900 /** FlightGear code should use this routine to set the FOV rather than
901 * calling the ssg routine directly
903 void FGRenderer::setFOV( float w, float h ) {
907 // fully specify the view frustum before hacking it (so we don't
908 // accumulate hacked effects
910 ssgSetNearFar( fov_near, fov_far );
912 sgEnviro.setFOV( w, h );
916 /** FlightGear code should use this routine to set the Near/Far clip
917 * planes rather than calling the ssg routine directly
919 void FGRenderer::setNearFar( float n, float f ) {
923 // fully specify the view frustum before hacking it (so we don't
924 // accumulate hacked effects
925 ssgSetNearFar( n, f );
926 ssgSetFOV( fov_width, fov_height );
931 bool FGRenderer::getPickInfo( sgdVec3 pt, sgdVec3 dir, unsigned x, unsigned y )
933 // Get the matrices involved in the transform from global to screen
936 ssgGetProjectionMatrix(pm);
938 ssgGetModelviewMatrix(mv);
942 sgMultMat4(m, pm, mv);
946 // Get the width and height of the display to be able to normalize the
948 float width = fgGetInt("/sim/startup/xsize");
949 float height = fgGetInt("/sim/startup/ysize");
951 // Compute some coordinates of in the line from the eyepoint to the
952 // mouse click coodinates.
953 // First build the normalized projection coordinates
955 sgSetVec4(normPt, (2*x - width)/width, -(2*y - height)/height, 1, 1);
956 // Transform them into the real world
958 sgXformPnt4(worldPt, normPt, m);
961 sgScaleVec3(worldPt, 1/worldPt[3]);
963 // Now build a direction from the point
964 FGViewer* view = globals->get_current_view();
966 sgSubVec3(fDir, worldPt, view->get_view_pos());
967 sgdSetVec3(dir, fDir);
968 sgdNormalizeVec3(dir);
970 // Copy the start point
971 sgdCopyVec3(pt, view->get_absolute_view_pos());
976 // end of renderer.cxx