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;
94 bool glPointSpriteIsSupported = false;
97 // fog constants. I'm a little nervous about putting actual code out
98 // here but it seems to work (?)
99 static const double m_log01 = -log( 0.01 );
100 static const double sqrt_m_log01 = sqrt( m_log01 );
101 static GLfloat fog_exp_density;
102 static GLfloat fog_exp2_density;
103 static GLfloat rwy_exp2_punch_through;
104 static GLfloat taxi_exp2_punch_through;
105 static GLfloat ground_exp2_punch_through;
111 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
113 { 1.0f, 0.0f, 0.0f, 0.0f },
114 { 0.0f, 0.0f, -1.0f, 0.0f },
115 { 0.0f, 1.0f, 0.0f, 0.0f },
116 { 0.0f, 0.0f, 0.0f, 1.0f }
119 ssgSimpleState *cloud3d_imposter_state;
120 ssgSimpleState *default_state;
121 ssgSimpleState *hud_and_panel;
122 ssgSimpleState *menus;
124 SGShadowVolume *shadows;
126 FGRenderer::FGRenderer()
128 #ifdef FG_JPEG_SERVER
129 jpgRenderFrame = FGRenderer::update;
133 FGRenderer::~FGRenderer()
135 #ifdef FG_JPEG_SERVER
136 jpgRenderFrame = NULL;
142 FGRenderer::build_states( void ) {
143 default_state = new ssgSimpleState;
144 default_state->ref();
145 default_state->disable( GL_TEXTURE_2D );
146 default_state->enable( GL_CULL_FACE );
147 default_state->enable( GL_COLOR_MATERIAL );
148 default_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
149 default_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
150 default_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
151 default_state->disable( GL_BLEND );
152 default_state->disable( GL_ALPHA_TEST );
153 default_state->disable( GL_LIGHTING );
155 cloud3d_imposter_state = new ssgSimpleState;
156 cloud3d_imposter_state->ref();
157 cloud3d_imposter_state->enable( GL_TEXTURE_2D );
158 cloud3d_imposter_state->enable( GL_CULL_FACE );
159 cloud3d_imposter_state->enable( GL_COLOR_MATERIAL );
160 cloud3d_imposter_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
161 cloud3d_imposter_state->setMaterial( GL_DIFFUSE, 1, 1, 1, 1 );
162 cloud3d_imposter_state->setMaterial( GL_AMBIENT, 1, 1, 1, 1 );
163 cloud3d_imposter_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
164 cloud3d_imposter_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
165 cloud3d_imposter_state->enable( GL_BLEND );
166 cloud3d_imposter_state->enable( GL_ALPHA_TEST );
167 cloud3d_imposter_state->disable( GL_LIGHTING );
169 hud_and_panel = new ssgSimpleState;
170 hud_and_panel->ref();
171 hud_and_panel->disable( GL_CULL_FACE );
172 hud_and_panel->disable( GL_TEXTURE_2D );
173 hud_and_panel->disable( GL_LIGHTING );
174 hud_and_panel->enable( GL_BLEND );
176 menus = new ssgSimpleState;
178 menus->disable( GL_CULL_FACE );
179 menus->disable( GL_TEXTURE_2D );
180 menus->enable( GL_BLEND );
182 shadows = new SGShadowVolume( globals->get_scenery()->get_scene_graph() );
183 shadows->init( fgGetNode("/sim/rendering", true) );
184 shadows->addOccluder( globals->get_scenery()->get_aircraft_branch(), SGShadowVolume::occluderTypeAircraft );
189 // Initialize various GL/view parameters
191 FGRenderer::init( void ) {
193 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
195 // Go full screen if requested ...
196 if ( fgGetBool("/sim/startup/fullscreen") ) {
200 // If enabled, normal vectors specified with glNormal are scaled
201 // to unit length after transformation. Enabling this has
202 // performance implications. See the docs for glNormal.
203 // glEnable( GL_NORMALIZE );
205 glEnable( GL_LIGHTING );
206 glEnable( GL_LIGHT0 );
207 // glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec ); // done later with ssg
210 sgSetVec3( sunpos, l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2] );
211 ssgGetLight( 0 ) -> setPosition( sunpos );
213 glFogi (GL_FOG_MODE, GL_EXP2);
214 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
215 (!fgGetBool("/sim/rendering/shading"))) {
216 // if fastest fog requested, or if flat shading force fastest
217 glHint ( GL_FOG_HINT, GL_FASTEST );
218 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
219 glHint ( GL_FOG_HINT, GL_DONT_CARE );
221 if ( fgGetBool("/sim/rendering/wireframe") ) {
223 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
226 // This is the default anyways, but it can't hurt
227 glFrontFace ( GL_CCW );
230 if ( SGIsOpenGLExtensionSupported("GL_ARB_point_sprite") ||
231 SGIsOpenGLExtensionSupported("GL_NV_point_sprite") )
233 glEnable(GL_POINT_SPRITE);
234 glEnable(GL_POINT_SMOOTH);
235 glPointSpriteIsSupported = true;
237 glEnable(GL_LINE_SMOOTH);
238 // glEnable(GL_POLYGON_SMOOTH);
239 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
240 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
241 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
246 // Update all Visuals (redraws anything graphics related)
248 FGRenderer::update( bool refresh_camera_settings ) {
249 bool scenery_loaded = fgGetBool("sim/sceneryloaded") \
250 || fgGetBool("sim/sceneryloaded-override");
252 if ( idle_state < 1000 || !scenery_loaded ) {
253 // still initializing, draw the splash screen
256 // Keep resetting sim time while the sim is initializing
257 globals->set_sim_time_sec( 0.0 );
258 SGAnimation::set_sim_time_sec( 0.0 );
262 bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
263 bool skyblend = fgGetBool("/sim/rendering/skyblend");
264 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
265 bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
266 bool volumetric_clouds = sgEnviro.get_clouds_enable_state();
267 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
268 bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") &&
269 !volumetric_clouds &&
270 !SGCloudLayer::enable_bump_mapping; // ugly artefact now
272 bool multi_pass_clouds = false;
274 bool draw_clouds = fgGetBool("/environment/clouds/status");
276 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
277 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
279 // static const SGPropertyNode *longitude
280 // = fgGetNode("/position/longitude-deg");
281 // static const SGPropertyNode *latitude
282 // = fgGetNode("/position/latitude-deg");
283 // static const SGPropertyNode *altitude
284 // = fgGetNode("/position/altitude-ft");
285 static const SGPropertyNode *groundlevel_nearplane
286 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
288 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
289 static double last_visibility = -9999;
292 double actual_visibility;
293 if (fgGetBool("/environment/clouds/status")) {
294 actual_visibility = thesky->get_visibility();
296 actual_visibility = fgGetDouble("/environment/visibility-m");
299 // TODO:TEST only, don't commit that !!
300 // sgFXperFrameInit();
302 sgShaderFrameInit(delta_time_sec);
304 if ( actual_visibility != last_visibility ) {
305 last_visibility = actual_visibility;
307 fog_exp_density = m_log01 / actual_visibility;
308 fog_exp2_density = sqrt_m_log01 / actual_visibility;
309 ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
310 if ( actual_visibility < 8000 ) {
311 rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
312 taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
314 rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
315 taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
320 // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
321 // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
322 // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
323 // GLfloat mat_shininess[] = { 10.0 };
324 GLbitfield clear_mask;
326 // idle_state is now 1000 meaning we've finished all our
327 // initializations and are running the main loop, so this will
328 // now work without seg faulting the system.
330 FGViewer *current__view = globals->get_current_view();
332 // calculate our current position in cartesian space
333 Point3D cntr = globals->get_scenery()->get_next_center();
334 globals->get_scenery()->set_center(cntr);
335 // Force update of center dependent values ...
336 current__view->set_dirty();
338 if ( refresh_camera_settings ) {
340 resize( fgGetInt("/sim/startup/xsize"),
341 fgGetInt("/sim/startup/ysize") );
343 // Tell GL we are switching to model view parameters
344 glMatrixMode(GL_MODELVIEW);
346 ssgSetCamera( (sgVec4 *)current__view->get_VIEW() );
349 clear_mask = GL_DEPTH_BUFFER_BIT;
350 if ( fgGetBool("/sim/rendering/wireframe") ) {
351 clear_mask |= GL_COLOR_BUFFER_BIT;
355 if ( fgGetBool("/sim/rendering/textures") ) {
356 // glClearColor(black[0], black[1], black[2], black[3]);
357 glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
358 l->adj_fog_color()[2], l->adj_fog_color()[3]);
359 clear_mask |= GL_COLOR_BUFFER_BIT;
362 glClearColor(l->sky_color()[0], l->sky_color()[1],
363 l->sky_color()[2], l->sky_color()[3]);
364 clear_mask |= GL_COLOR_BUFFER_BIT;
366 if ( multi_pass_clouds && draw_clouds ) {
368 clear_mask |= GL_STENCIL_BUFFER_BIT;
370 glClear( clear_mask );
372 // set the opengl state to known default values
373 default_state->force();
375 // update fog params if visibility has changed
376 double visibility_meters = fgGetDouble("/environment/visibility-m");
377 thesky->set_visibility(visibility_meters);
379 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
380 ( global_multi_loop * fgGetInt("/sim/speed-up") )
381 / (double)fgGetInt("/sim/model-hz") );
383 // Set correct opengl fog density
384 glFogf (GL_FOG_DENSITY, fog_exp2_density);
386 // update the sky dome
389 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
390 << l->sky_color()[0] << " "
391 << l->sky_color()[1] << " "
392 << l->sky_color()[2] << " "
393 << l->sky_color()[3] );
394 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
395 << l->fog_color()[0] << " "
396 << l->fog_color()[1] << " "
397 << l->fog_color()[2] << " "
398 << l->fog_color()[3] );
399 SG_LOG( SG_GENERAL, SG_BULK,
400 " sun_angle = " << l->sun_angle
401 << " moon_angle = " << l->moon_angle );
404 static SGSkyColor scolor;
405 // FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
407 scolor.sky_color = l->sky_color();
408 scolor.fog_color = l->adj_fog_color();
409 scolor.cloud_color = l->cloud_color();
410 scolor.sun_angle = l->get_sun_angle();
411 scolor.moon_angle = l->get_moon_angle();
412 scolor.nplanets = globals->get_ephem()->getNumPlanets();
413 scolor.nstars = globals->get_ephem()->getNumStars();
414 scolor.planet_data = globals->get_ephem()->getPlanets();
415 scolor.star_data = globals->get_ephem()->getStars();
417 thesky->repaint( scolor );
420 SG_LOG( SG_GENERAL, SG_BULK,
421 "thesky->reposition( view_pos = " << view_pos[0] << " "
422 << view_pos[1] << " " << view_pos[2] );
423 SG_LOG( SG_GENERAL, SG_BULK,
424 " zero_elev = " << zero_elev[0] << " "
425 << zero_elev[1] << " " << zero_elev[2]
426 << " lon = " << cur_fdm_state->get_Longitude()
427 << " lat = " << cur_fdm_state->get_Latitude() );
428 SG_LOG( SG_GENERAL, SG_BULK,
429 " sun_rot = " << l->get_sun_rotation
430 << " gst = " << SGTime::cur_time_params->getGst() );
431 SG_LOG( SG_GENERAL, SG_BULK,
432 " sun ra = " << globals->get_ephem()->getSunRightAscension()
433 << " sun dec = " << globals->get_ephem()->getSunDeclination()
434 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
435 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
438 // The sun and moon distances are scaled down versions
439 // of the actual distance to get both the moon and the sun
440 // within the range of the far clip plane.
441 // Moon distance: 384,467 kilometers
442 // Sun distance: 150,000,000 kilometers
443 double sun_horiz_eff, moon_horiz_eff;
444 if (fgGetBool("/sim/rendering/horizon-effect")) {
445 sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
446 moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
448 sun_horiz_eff = moon_horiz_eff = 1.0;
451 static SGSkyState sstate;
453 sstate.view_pos = current__view->get_view_pos();
454 sstate.zero_elev = current__view->get_zero_elev();
455 sstate.view_up = current__view->get_world_up();
456 sstate.lon = current__view->getLongitude_deg()
457 * SGD_DEGREES_TO_RADIANS;
458 sstate.lat = current__view->getLatitude_deg()
459 * SGD_DEGREES_TO_RADIANS;
460 sstate.alt = current__view->getAltitudeASL_ft()
462 sstate.spin = l->get_sun_rotation();
463 sstate.gst = globals->get_time_params()->getGst();
464 sstate.sun_ra = globals->get_ephem()->getSunRightAscension();
465 sstate.sun_dec = globals->get_ephem()->getSunDeclination();
466 sstate.sun_dist = 50000.0 * sun_horiz_eff;
467 sstate.moon_ra = globals->get_ephem()->getMoonRightAscension();
468 sstate.moon_dec = globals->get_ephem()->getMoonDeclination();
469 sstate.moon_dist = 40000.0 * moon_horiz_eff;
471 thesky->reposition( sstate, delta_time_sec );
473 shadows->setupShadows(
474 current__view->getLongitude_deg(),
475 current__view->getLatitude_deg(),
476 globals->get_time_params()->getGst(),
477 globals->get_ephem()->getSunRightAscension(),
478 globals->get_ephem()->getSunDeclination(),
482 glEnable( GL_DEPTH_TEST );
483 if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
485 glFogi( GL_FOG_MODE, GL_EXP2 );
486 glFogfv( GL_FOG_COLOR, l->adj_fog_color() );
490 // set sun/lighting parameters
491 ssgGetLight( 0 ) -> setPosition( l->sun_vec() );
493 // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
494 // we only update GL_AMBIENT for our lights we will never get
495 // a completely dark scene. So, we set GL_LIGHT_MODEL_AMBIENT
496 // explicitely to black.
497 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
499 ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
500 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
501 ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_specular() );
503 sgEnviro.setLight(l->adj_fog_color());
505 // texture parameters
506 // glEnable( GL_TEXTURE_2D );
507 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
508 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
510 double agl = current__view->getAltitudeASL_ft()*SG_FEET_TO_METER
511 - current__view->getSGLocation()->get_cur_elev_m();
514 scene_nearplane = 10.0f;
515 scene_farplane = 120000.0f;
517 scene_nearplane = groundlevel_nearplane->getDoubleValue();
518 scene_farplane = 120000.0f;
521 setNearFar( scene_nearplane, scene_farplane );
523 sgEnviro.startOfFrame(current__view->get_view_pos(),
524 current__view->get_world_up(),
525 current__view->getLongitude_deg(),
526 current__view->getLatitude_deg(),
527 current__view->getAltitudeASL_ft() * SG_FEET_TO_METER,
530 if ( draw_otw && skyblend ) {
531 // draw the sky backdrop
533 // we need a white diffuse light for the phase of the moon
534 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
535 thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
537 // return to the desired diffuse color
538 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
541 // draw the ssg scene
542 glEnable( GL_DEPTH_TEST );
544 if ( fgGetBool("/sim/rendering/wireframe") ) {
546 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
552 FGTileMgr::set_tile_filter( true );
553 sgSetModelFilter( false );
554 globals->get_aircraft_model()->select( false );
555 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
557 // Disable depth buffer update, draw the clouds
558 glDepthMask( GL_FALSE );
559 if( !volumetric_clouds )
560 thesky->drawUpperClouds();
561 if ( multi_pass_clouds ) {
562 thesky->drawLowerClouds();
564 glDepthMask( GL_TRUE );
566 if ( multi_pass_clouds ) {
567 // Draw the objects except the aircraft
568 // and update the stencil buffer with 1
569 glEnable( GL_STENCIL_TEST );
570 glStencilFunc( GL_ALWAYS, 1, 1 );
571 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
573 FGTileMgr::set_tile_filter( false );
574 sgSetModelFilter( true );
575 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
577 FGTileMgr::set_tile_filter( true );
578 sgSetModelFilter( true );
579 globals->get_aircraft_model()->select( false );
580 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
584 // This is a bit kludgy. Every 200 frames, do an extra
585 // traversal of the scene graph without drawing anything, but
586 // with the field-of-view set to 360x360 degrees. This
587 // ensures that out-of-range random objects that are not in
588 // the current view frustum will still be freed properly.
589 static int counter = 0;
591 if (counter >= 200) {
594 // No need to put the near plane too close;
595 // this way, at least the aircraft can be
597 f.setNearFar(1000, 1000000);
599 ssgGetModelviewMatrix(m);
600 FGTileMgr::set_tile_filter( true );
601 sgSetModelFilter( true );
602 globals->get_scenery()->get_scene_graph()->cull(&f, m, true);
606 // change state for lighting here
608 // draw runway lighting
609 glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
611 // CLO - 02/25/2005 - DO WE NEED THIS extra fgSetNearFar()?
612 // fgSetNearFar( scene_nearplane, scene_farplane );
614 if ( enhanced_lighting ) {
616 // Enable states for drawing points with GL_extension
617 glEnable(GL_POINT_SMOOTH);
619 if ( distance_attenuation && glPointParameterIsSupported )
621 // Enable states for drawing points with GL_extension
622 glEnable(GL_POINT_SMOOTH);
624 float quadratic[3] = {1.0, 0.001, 0.0000001};
625 // makes the points fade as they move away
626 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
627 glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0);
632 // blending function for runway lights
633 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
636 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
637 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
638 glEnable(GL_TEXTURE_GEN_S);
639 glEnable(GL_TEXTURE_GEN_T);
640 glPolygonMode(GL_FRONT, GL_POINT);
642 // draw runway lighting
644 ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
645 ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
648 // change punch through and then draw taxi lighting
649 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
651 // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
652 // glFogfv ( GL_FOG_COLOR, taxi_fog );
654 ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
658 glPolygonMode(GL_FRONT, GL_FILL);
659 glDisable(GL_TEXTURE_GEN_S);
660 glDisable(GL_TEXTURE_GEN_T);
662 //static int _frame_count = 0;
663 //if (_frame_count % 30 == 0) {
664 // printf("SSG: %s\n", ssgShowStats());
672 if ( enhanced_lighting ) {
673 if ( distance_attenuation && glPointParameterIsSupported ) {
674 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
675 default_attenuation);
679 glDisable(GL_POINT_SMOOTH);
682 // draw ground lighting
683 glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
685 ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
688 sgEnviro.drawLightning();
690 if ( draw_otw && draw_clouds ) {
691 if ( multi_pass_clouds ) {
692 // Disable depth buffer update, draw the clouds where the
693 // objects overwrite the already drawn clouds, by testing
694 // the stencil buffer against 1
695 glDepthMask( GL_FALSE );
696 glStencilFunc( GL_EQUAL, 1, 1 );
697 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
698 thesky->drawUpperClouds();
699 thesky->drawLowerClouds();
700 glDepthMask( GL_TRUE );
701 glDisable( GL_STENCIL_TEST );
703 glDepthMask( GL_FALSE );
704 if( volumetric_clouds )
705 thesky->drawUpperClouds();
706 thesky->drawLowerClouds();
707 glDepthMask( GL_TRUE );
710 sgEnviro.drawPrecipitation(
711 fgGetDouble("/environment/metar/rain-norm", 0.0),
712 fgGetDouble("/environment/metar/snow-norm", 0.0),
713 fgGetDouble("/environment/metar/hail-norm", 0.0),
714 current__view->getPitch_deg() - current__view->getPitchOffset_deg(),
715 current__view->getRoll_deg() + current__view->getRollOffset_deg(),
716 - current__view->getHeadingOffset_deg(),
717 fgGetDouble("/velocities/airspeed-kt", 0.0));
719 // compute shadows and project them on screen
720 bool is_internal = globals->get_current_view()->getInternal();
721 // draw before ac because ac internal rendering clear the depth buffer
723 globals->get_aircraft_model()->select( true );
725 shadows->endOfFrame();
728 FGTileMgr::set_tile_filter( false );
729 sgSetModelFilter( false );
730 globals->get_aircraft_model()->select( true );
731 globals->get_model_mgr()->draw();
732 globals->get_aircraft_model()->draw();
734 FGTileMgr::set_tile_filter( true );
735 sgSetModelFilter( true );
736 globals->get_aircraft_model()->select( true );
738 // in 'external' view the ac can be culled, so shadows have not been draw in the
739 // posttrav callback, this would be a rare case if the getInternal was acting
740 // as expected (ie in internal view, getExternal returns false)
742 shadows->endOfFrame();
744 // display HUD && Panel
746 glDisable( GL_DEPTH_TEST );
747 // glDisable( GL_CULL_FACE );
748 // glDisable( GL_TEXTURE_2D );
750 // update the controls subsystem
751 globals->get_controls()->update(delta_time_sec);
753 hud_and_panel->apply();
756 // Use the hud_and_panel ssgSimpleState for rendering the ATC output
757 // This only works properly if called before the panel call
758 if((fgGetBool("/sim/atc/enabled")) || (fgGetBool("/sim/ai-traffic/enabled")))
759 globals->get_ATC_display()->update(delta_time_sec);
761 // Update any messages from the Nasal System
762 globals->get_Nasal_display()->update(delta_time_sec);
764 // update the panel subsystem
765 if ( globals->get_current_panel() != NULL ) {
766 globals->get_current_panel()->update(delta_time_sec);
770 // We can do translucent menus, so why not. :-)
772 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
774 // glDisable ( GL_BLEND ) ;
776 glEnable( GL_DEPTH_TEST );
779 // Fade out the splash screen over the first three seconds.
780 double t = globals->get_sim_time_sec();
782 fgSplashUpdate((2.5 - t) / 2.5);
787 // options.cxx needs to see this for toggle_panel()
788 // Handle new window size or exposure
790 FGRenderer::resize( int width, int height ) {
793 if ( (!fgGetBool("/sim/virtual-cockpit"))
794 && fgPanelVisible() && idle_state == 1000 ) {
795 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
796 globals->get_current_panel()->getYOffset()) / 768.0);
801 glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) );
803 static int lastwidth = width;
804 static int lastheight = height;
805 if (width != lastwidth)
806 fgSetInt("/sim/startup/xsize", lastwidth = width);
807 if (height != lastheight)
808 fgSetInt("/sim/startup/ysize", lastheight = height);
810 guiInitMouse(width, height);
813 FGViewMgr *viewmgr = globals->get_viewmgr();
815 for ( int i = 0; i < viewmgr->size(); ++i ) {
816 viewmgr->get_view(i)->
817 set_aspect_ratio((float)view_h / (float)width);
820 setFOV( viewmgr->get_current_view()->get_h_fov(),
821 viewmgr->get_current_view()->get_v_fov() );
822 // cout << "setFOV(" << viewmgr->get_current_view()->get_h_fov()
823 // << ", " << viewmgr->get_current_view()->get_v_fov() << ")"
833 // These are wrapper functions around ssgSetNearFar() and ssgSetFOV()
834 // which will post process and rewrite the resulting frustum if we
835 // want to do asymmetric view frustums.
837 static void fgHackFrustum() {
839 // specify a percent of the configured view frustum to actually
840 // display. This is a bit of a hack to achieve asymmetric view
841 // frustums. For instance, if you want to display two monitors
842 // side by side, you could specify each with a double fov, a 0.5
843 // aspect ratio multiplier, and then the left side monitor would
844 // have a left_pct = 0.0, a right_pct = 0.5, a bottom_pct = 0.0,
845 // and a top_pct = 1.0. The right side monitor would have a
846 // left_pct = 0.5 and a right_pct = 1.0.
848 static SGPropertyNode *left_pct
849 = fgGetNode("/sim/current-view/frustum-left-pct");
850 static SGPropertyNode *right_pct
851 = fgGetNode("/sim/current-view/frustum-right-pct");
852 static SGPropertyNode *bottom_pct
853 = fgGetNode("/sim/current-view/frustum-bottom-pct");
854 static SGPropertyNode *top_pct
855 = fgGetNode("/sim/current-view/frustum-top-pct");
857 sgFrustum *f = ssgGetFrustum();
859 // cout << " l = " << f->getLeft()
860 // << " r = " << f->getRight()
861 // << " b = " << f->getBot()
862 // << " t = " << f->getTop()
863 // << " n = " << f->getNear()
864 // << " f = " << f->getFar()
867 double width = f->getRight() - f->getLeft();
868 double height = f->getTop() - f->getBot();
872 if ( left_pct != NULL ) {
873 l = f->getLeft() + width * left_pct->getDoubleValue();
878 if ( right_pct != NULL ) {
879 r = f->getLeft() + width * right_pct->getDoubleValue();
884 if ( bottom_pct != NULL ) {
885 b = f->getBot() + height * bottom_pct->getDoubleValue();
890 if ( top_pct != NULL ) {
891 t = f->getBot() + height * top_pct->getDoubleValue();
896 ssgSetFrustum(l, r, b, t, f->getNear(), f->getFar());
900 // we need some static storage space for these values. However, we
901 // can't store it in a renderer class object because the functions
902 // that manipulate these are static. They are static so they can
903 // interface to the display callback system. There's probably a
904 // better way, there has to be a better way, but I'm not seeing it
906 static float fov_width = 55.0;
907 static float fov_height = 42.0;
908 static float fov_near = 1.0;
909 static float fov_far = 1000.0;
912 /** FlightGear code should use this routine to set the FOV rather than
913 * calling the ssg routine directly
915 void FGRenderer::setFOV( float w, float h ) {
919 // fully specify the view frustum before hacking it (so we don't
920 // accumulate hacked effects
922 ssgSetNearFar( fov_near, fov_far );
924 sgEnviro.setFOV( w, h );
928 /** FlightGear code should use this routine to set the Near/Far clip
929 * planes rather than calling the ssg routine directly
931 void FGRenderer::setNearFar( float n, float f ) {
935 // fully specify the view frustum before hacking it (so we don't
936 // accumulate hacked effects
937 ssgSetNearFar( n, f );
938 ssgSetFOV( fov_width, fov_height );
943 bool FGRenderer::getPickInfo( sgdVec3 pt, sgdVec3 dir, unsigned x, unsigned y )
945 // Get the matrices involved in the transform from global to screen
948 ssgGetProjectionMatrix(pm);
950 ssgGetModelviewMatrix(mv);
954 sgMultMat4(m, pm, mv);
958 // Get the width and height of the display to be able to normalize the
960 float width = fgGetInt("/sim/startup/xsize");
961 float height = fgGetInt("/sim/startup/ysize");
963 // Compute some coordinates of in the line from the eyepoint to the
964 // mouse click coodinates.
965 // First build the normalized projection coordinates
967 sgSetVec4(normPt, (2*x - width)/width, -(2*y - height)/height, 1, 1);
968 // Transform them into the real world
970 sgXformPnt4(worldPt, normPt, m);
973 sgScaleVec3(worldPt, 1/worldPt[3]);
975 // Now build a direction from the point
976 FGViewer* view = globals->get_current_view();
978 sgSubVec3(fDir, worldPt, view->get_view_pos());
979 sgdSetVec3(dir, fDir);
980 sgdNormalizeVec3(dir);
982 // Copy the start point
983 sgdCopyVec3(pt, view->get_absolute_view_pos());
988 // end of renderer.cxx