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 - curt@flightgear.org
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 #ifdef FG_USE_CLOUDS_3D
50 # include <simgear/scene/sky/clouds3d/SkySceneLoader.hpp>
51 # include <simgear/scene/sky/clouds3d/SkyUtil.hpp>
54 #include <Scenery/tileentry.hxx>
55 #include <Time/light.hxx>
56 #include <Time/light.hxx>
57 #include <Aircraft/aircraft.hxx>
58 #include <Cockpit/panel.hxx>
59 #include <Cockpit/cockpit.hxx>
60 #include <Cockpit/radiostack.hxx>
61 #include <Cockpit/hud.hxx>
62 #include <Model/panelnode.hxx>
63 #include <Model/modelmgr.hxx>
64 #include <Model/acmodel.hxx>
65 #include <Scenery/scenery.hxx>
66 #include <Scenery/tilemgr.hxx>
67 #include <ATC/ATCdisplay.hxx>
68 #include <Replay/replay.hxx>
69 #include <GUI/new_gui.hxx>
72 #include <MultiPlayer/multiplaytxmgr.hxx>
73 #include <MultiPlayer/multiplayrxmgr.hxx>
77 #include "renderer.hxx"
81 float default_attenuation[3] = {1.0, 0.0, 0.0};
83 ssgSelector *lightpoints_brightness = new ssgSelector;
84 ssgTransform *lightpoints_transform = new ssgTransform;
85 FGTileEntry *dummy_tile;
88 // Clip plane settings...
89 float scene_nearplane = 0.5f;
90 float scene_farplane = 120000.0f;
92 glPointParameterfProc glPointParameterfPtr = 0;
93 glPointParameterfvProc glPointParameterfvPtr = 0;
94 bool glPointParameterIsSupported = 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;
110 #ifdef FG_USE_CLOUDS_3D
111 SkySceneLoader *sgClouds3d;
112 bool _bcloud_orig = true;
116 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
118 { 1.0f, 0.0f, 0.0f, 0.0f },
119 { 0.0f, 0.0f, -1.0f, 0.0f },
120 { 0.0f, 1.0f, 0.0f, 0.0f },
121 { 0.0f, 0.0f, 0.0f, 1.0f }
124 ssgSimpleState *cloud3d_imposter_state;
125 ssgSimpleState *default_state;
126 ssgSimpleState *hud_and_panel;
127 ssgSimpleState *menus;
130 void fgRenderFrame() {
132 // Update any multiplayer models
133 globals->get_multiplayer_rx_mgr()->Update();
135 globals->get_renderer()->update(delta_time_sec);
138 void fgReshape(int width, int height) {
139 globals->get_renderer()->resize(width,height);
143 FGRenderer::build_states( void ) {
144 default_state = new ssgSimpleState;
145 default_state->ref();
146 default_state->disable( GL_TEXTURE_2D );
147 default_state->enable( GL_CULL_FACE );
148 default_state->enable( GL_COLOR_MATERIAL );
149 default_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
150 default_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
151 default_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
152 default_state->disable( GL_BLEND );
153 default_state->disable( GL_ALPHA_TEST );
154 default_state->disable( GL_LIGHTING );
156 cloud3d_imposter_state = new ssgSimpleState;
157 cloud3d_imposter_state->ref();
158 cloud3d_imposter_state->enable( GL_TEXTURE_2D );
159 cloud3d_imposter_state->enable( GL_CULL_FACE );
160 cloud3d_imposter_state->enable( GL_COLOR_MATERIAL );
161 cloud3d_imposter_state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
162 cloud3d_imposter_state->setMaterial( GL_DIFFUSE, 1, 1, 1, 1 );
163 cloud3d_imposter_state->setMaterial( GL_AMBIENT, 1, 1, 1, 1 );
164 cloud3d_imposter_state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
165 cloud3d_imposter_state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
166 cloud3d_imposter_state->enable( GL_BLEND );
167 cloud3d_imposter_state->enable( GL_ALPHA_TEST );
168 cloud3d_imposter_state->disable( GL_LIGHTING );
170 hud_and_panel = new ssgSimpleState;
171 hud_and_panel->ref();
172 hud_and_panel->disable( GL_CULL_FACE );
173 hud_and_panel->disable( GL_TEXTURE_2D );
174 hud_and_panel->disable( GL_LIGHTING );
175 hud_and_panel->enable( GL_BLEND );
177 menus = new ssgSimpleState;
179 menus->disable( GL_CULL_FACE );
180 menus->disable( GL_TEXTURE_2D );
181 menus->enable( GL_BLEND );
185 // Initialize various GL/view parameters
187 FGRenderer::init( void ) {
189 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
191 // Go full screen if requested ...
192 if ( fgGetBool("/sim/startup/fullscreen") ) {
196 // If enabled, normal vectors specified with glNormal are scaled
197 // to unit length after transformation. Enabling this has
198 // performance implications. See the docs for glNormal.
199 // glEnable( GL_NORMALIZE );
201 glEnable( GL_LIGHTING );
202 glEnable( GL_LIGHT0 );
203 // glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec ); // done later with ssg
206 sgSetVec3( sunpos, l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2] );
207 ssgGetLight( 0 ) -> setPosition( sunpos );
209 glFogi (GL_FOG_MODE, GL_EXP2);
210 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
211 (!fgGetBool("/sim/rendering/shading"))) {
212 // if fastest fog requested, or if flat shading force fastest
213 glHint ( GL_FOG_HINT, GL_FASTEST );
214 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
215 glHint ( GL_FOG_HINT, GL_NICEST );
217 if ( fgGetBool("/sim/rendering/wireframe") ) {
219 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
222 // This is the default anyways, but it can't hurt
223 glFrontFace ( GL_CCW );
226 // glEnable(GL_POINT_SMOOTH);
227 // glEnable(GL_LINE_SMOOTH);
228 // glEnable(GL_POLYGON_SMOOTH);
232 // For HiRes screen Dumps using Brian Pauls TR Library
234 FGRenderer::screendump( void ) {
235 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
236 bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") &&
237 !SGCloudLayer::enable_bump_mapping; // ugly artefact now
239 bool multi_pass_clouds = false;
241 bool draw_clouds = fgGetBool("/environment/clouds/status");
243 if ( fgPanelVisible() ) {
244 GLfloat height = fgGetInt("/sim/startup/ysize");
246 (globals->get_current_panel()->getViewHeight() - globals->get_current_panel()->getYOffset())
247 * (height / 768.0) + 1;
248 glTranslatef( 0.0, view_h, 0.0 );
251 static GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
252 static GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
254 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
256 glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
257 l->adj_fog_color()[2], l->adj_fog_color()[3]);
259 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
261 // set the opengl state to known default values
262 default_state->force();
265 glFogf ( GL_FOG_DENSITY, fog_exp2_density);
266 glFogi ( GL_FOG_MODE, GL_EXP2 );
267 glFogfv ( GL_FOG_COLOR, l->adj_fog_color() );
269 // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
270 // we only update GL_AMBIENT for our lights we will never get
271 // a completely dark scene. So, we set GL_LIGHT_MODEL_AMBIENT
272 // explicitely to black.
273 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
274 glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE );
276 ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
278 // texture parameters
279 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
280 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
282 // we need a white diffuse light for the phase of the moon
283 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
284 thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
287 // draw the ssg scene
288 // return to the desired diffuse color
289 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
290 glEnable( GL_DEPTH_TEST );
291 ssgSetNearFar( scene_nearplane, scene_farplane );
294 FGTileMgr::set_tile_filter( true );
295 sgSetModelFilter( false );
296 globals->get_aircraft_model()->select( false );
297 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
298 // Disable depth buffer update, draw the clouds
299 glDepthMask( GL_FALSE );
300 thesky->drawUpperClouds();
301 if ( multi_pass_clouds ) {
302 thesky->drawLowerClouds();
304 glDepthMask( GL_TRUE );
305 if ( multi_pass_clouds ) {
306 // Draw the objects except the aircraft
307 // and update the stencil buffer with 1
308 glEnable( GL_STENCIL_TEST );
309 glStencilFunc( GL_ALWAYS, 1, 1 );
310 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
312 FGTileMgr::set_tile_filter( false );
313 sgSetModelFilter( true );
314 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
316 FGTileMgr::set_tile_filter( true );
317 sgSetModelFilter( true );
318 globals->get_aircraft_model()->select( false );
319 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
323 glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
324 ssgSetNearFar( scene_nearplane, scene_farplane );
325 ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
326 ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
328 ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
331 if ( multi_pass_clouds ) {
332 // Disable depth buffer update, draw the clouds where the
333 // objects overwrite the already drawn clouds, by testing
334 // the stencil buffer against 1
335 glDepthMask( GL_FALSE );
336 glStencilFunc( GL_EQUAL, 1, 1 );
337 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
338 thesky->drawUpperClouds();
339 thesky->drawLowerClouds();
340 glDepthMask( GL_TRUE );
341 glDisable( GL_STENCIL_TEST );
343 glDepthMask( GL_FALSE );
344 thesky->drawLowerClouds();
345 glDepthMask( GL_TRUE );
349 globals->get_aircraft_model()->select( true );
350 globals->get_model_mgr()->draw();
351 globals->get_aircraft_model()->draw();
355 // Update all Visuals (redraws anything graphics related)
357 FGRenderer::update(double dt) {
358 bool scenery_loaded = fgGetBool("sim/sceneryloaded") || fgGetBool("sim/sceneryloaded-override");
359 bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
360 bool skyblend = fgGetBool("/sim/rendering/skyblend");
361 bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
362 bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
363 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
364 bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") &&
365 !SGCloudLayer::enable_bump_mapping; // ugly artefact now
367 bool multi_pass_clouds = false;
369 bool draw_clouds = fgGetBool("/environment/clouds/status");
371 GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
372 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
374 // static const SGPropertyNode *longitude
375 // = fgGetNode("/position/longitude-deg");
376 // static const SGPropertyNode *latitude
377 // = fgGetNode("/position/latitude-deg");
378 // static const SGPropertyNode *altitude
379 // = fgGetNode("/position/altitude-ft");
380 static const SGPropertyNode *groundlevel_nearplane
381 = fgGetNode("/sim/current-view/ground-level-nearplane-m");
383 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
384 static double last_visibility = -9999;
387 double actual_visibility;
388 if (fgGetBool("/environment/clouds/status"))
389 actual_visibility = thesky->get_visibility();
391 actual_visibility = fgGetDouble("/environment/visibility-m");
392 if ( actual_visibility != last_visibility ) {
393 last_visibility = actual_visibility;
395 fog_exp_density = m_log01 / actual_visibility;
396 fog_exp2_density = sqrt_m_log01 / actual_visibility;
397 ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
398 if ( actual_visibility < 8000 ) {
399 rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
400 taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
402 rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
403 taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
408 // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
409 // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
410 // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
411 // GLfloat mat_shininess[] = { 10.0 };
412 GLbitfield clear_mask;
414 if ( idle_state != 1000 || !scenery_loaded ) {
415 // still initializing, draw the splash screen
416 if ( fgGetBool("/sim/startup/splash-screen") ) {
417 fgSplashUpdate(0.0, 1.0);
419 // Keep resetting sim time while the sim is initializing
420 globals->set_sim_time_sec( 0.0 );
421 SGAnimation::set_sim_time_sec( 0.0 );
423 // idle_state is now 1000 meaning we've finished all our
424 // initializations and are running the main loop, so this will
425 // now work without seg faulting the system.
427 FGViewer *current__view = globals->get_current_view();
429 // calculate our current position in cartesian space
430 globals->get_scenery()->set_center( globals->get_scenery()->get_next_center() );
433 fgReshape( fgGetInt("/sim/startup/xsize"),
434 fgGetInt("/sim/startup/ysize") );
436 if ( fgGetBool("/sim/rendering/clouds3d") ) {
437 glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
438 cloud3d_imposter_state->force();
440 glColor4f( 1.0, 1.0, 1.0, 1.0 );
441 glEnable(GL_DEPTH_TEST);
443 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
445 #ifdef FG_USE_CLOUDS_3D
446 if ( _bcloud_orig ) {
447 Point3D c = globals->get_scenery()->get_center();
448 sgClouds3d->Set_Cloud_Orig( &c );
449 _bcloud_orig = false;
451 sgClouds3d->Update( current__view->get_absolute_view_pos() );
453 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
454 glDisable(GL_DEPTH_TEST);
457 clear_mask = GL_DEPTH_BUFFER_BIT;
458 if ( fgGetBool("/sim/rendering/wireframe") ) {
459 clear_mask |= GL_COLOR_BUFFER_BIT;
463 if ( fgGetBool("/sim/rendering/textures") ) {
464 // glClearColor(black[0], black[1], black[2], black[3]);
465 glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
466 l->adj_fog_color()[2], l->adj_fog_color()[3]);
467 clear_mask |= GL_COLOR_BUFFER_BIT;
470 glClearColor(l->sky_color()[0], l->sky_color()[1],
471 l->sky_color()[2], l->sky_color()[3]);
472 clear_mask |= GL_COLOR_BUFFER_BIT;
474 if ( multi_pass_clouds && draw_clouds ) {
476 clear_mask |= GL_STENCIL_BUFFER_BIT;
478 glClear( clear_mask );
480 // Tell GL we are switching to model view parameters
482 // I really should create a derived ssg node or use a call
483 // back or something so that I can draw the sky within the
484 // ssgCullAndDraw() function, but for now I just mimic what
485 // ssg does to set up the model view matrix
486 glMatrixMode(GL_MODELVIEW);
488 ssgSetCamera( (sgVec4 *)current__view->get_VIEW() );
490 // set the opengl state to known default values
491 default_state->force();
493 // update fog params if visibility has changed
494 double visibility_meters = fgGetDouble("/environment/visibility-m");
495 thesky->set_visibility(visibility_meters);
497 thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
498 ( global_multi_loop * fgGetInt("/sim/speed-up") )
499 / (double)fgGetInt("/sim/model-hz") );
501 // Set correct opengl fog density
502 glFogf (GL_FOG_DENSITY, fog_exp2_density);
504 // update the sky dome
507 SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
508 << l->sky_color()[0] << " "
509 << l->sky_color()[1] << " "
510 << l->sky_color()[2] << " "
511 << l->sky_color()[3] );
512 SG_LOG( SG_GENERAL, SG_BULK, " fog = "
513 << l->fog_color()[0] << " "
514 << l->fog_color()[1] << " "
515 << l->fog_color()[2] << " "
516 << l->fog_color()[3] );
517 SG_LOG( SG_GENERAL, SG_BULK,
518 " sun_angle = " << l->sun_angle
519 << " moon_angle = " << l->moon_angle );
522 static SGSkyColor scolor;
523 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
525 scolor.sky_color = l->sky_color();
526 scolor.fog_color = l->adj_fog_color();
527 scolor.cloud_color = l->cloud_color();
528 scolor.sun_angle = l->get_sun_angle();
529 scolor.moon_angle = l->get_moon_angle();
530 scolor.nplanets = globals->get_ephem()->getNumPlanets();
531 scolor.nstars = globals->get_ephem()->getNumStars();
532 scolor.planet_data = globals->get_ephem()->getPlanets();
533 scolor.star_data = globals->get_ephem()->getStars();
535 thesky->repaint( scolor );
538 SG_LOG( SG_GENERAL, SG_BULK,
539 "thesky->reposition( view_pos = " << view_pos[0] << " "
540 << view_pos[1] << " " << view_pos[2] );
541 SG_LOG( SG_GENERAL, SG_BULK,
542 " zero_elev = " << zero_elev[0] << " "
543 << zero_elev[1] << " " << zero_elev[2]
544 << " lon = " << cur_fdm_state->get_Longitude()
545 << " lat = " << cur_fdm_state->get_Latitude() );
546 SG_LOG( SG_GENERAL, SG_BULK,
547 " sun_rot = " << l->get_sun_rotation
548 << " gst = " << SGTime::cur_time_params->getGst() );
549 SG_LOG( SG_GENERAL, SG_BULK,
550 " sun ra = " << globals->get_ephem()->getSunRightAscension()
551 << " sun dec = " << globals->get_ephem()->getSunDeclination()
552 << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
553 << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
556 // The sun and moon distances are scaled down versions
557 // of the actual distance to get both the moon and the sun
558 // within the range of the far clip plane.
559 // Moon distance: 384,467 kilometers
560 // Sun distance: 150,000,000 kilometers
561 double sun_horiz_eff, moon_horiz_eff;
562 if (fgGetBool("/sim/rendering/horizon-effect")) {
563 sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
564 moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
566 sun_horiz_eff = moon_horiz_eff = 1.0;
569 static SGSkyState sstate;
571 sstate.view_pos = current__view->get_view_pos();
572 sstate.zero_elev = current__view->get_zero_elev();
573 sstate.view_up = current__view->get_world_up();
574 sstate.lon = current__view->getLongitude_deg()
575 * SGD_DEGREES_TO_RADIANS;
576 sstate.lat = current__view->getLatitude_deg()
577 * SGD_DEGREES_TO_RADIANS;
578 sstate.alt = current__view->getAltitudeASL_ft()
580 sstate.spin = l->get_sun_rotation();
581 sstate.gst = globals->get_time_params()->getGst();
582 sstate.sun_ra = globals->get_ephem()->getSunRightAscension();
583 sstate.sun_dec = globals->get_ephem()->getSunDeclination();
584 sstate.sun_dist = 50000.0 * sun_horiz_eff;
585 sstate.moon_ra = globals->get_ephem()->getMoonRightAscension();
586 sstate.moon_dec = globals->get_ephem()->getMoonDeclination();
587 sstate.moon_dist = 40000.0 * moon_horiz_eff;
589 thesky->reposition( sstate, delta_time_sec );
592 glEnable( GL_DEPTH_TEST );
593 if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
595 glFogi( GL_FOG_MODE, GL_EXP2 );
596 glFogfv( GL_FOG_COLOR, l->adj_fog_color() );
599 // set sun/lighting parameters
600 ssgGetLight( 0 ) -> setPosition( l->sun_vec() );
602 // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
603 // we only update GL_AMBIENT for our lights we will never get
604 // a completely dark scene. So, we set GL_LIGHT_MODEL_AMBIENT
605 // explicitely to black.
606 glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
608 ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
609 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
610 ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_specular() );
612 // texture parameters
613 // glEnable( GL_TEXTURE_2D );
614 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
615 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
617 // glMatrixMode( GL_PROJECTION );
619 ssgSetFOV( current__view->get_h_fov(),
620 current__view->get_v_fov() );
623 current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
624 - globals->get_scenery()->get_cur_elev();
627 scene_nearplane = 10.0f;
628 scene_farplane = 120000.0f;
630 scene_nearplane = groundlevel_nearplane->getDoubleValue();
631 scene_farplane = 120000.0f;
634 ssgSetNearFar( scene_nearplane, scene_farplane );
636 if ( draw_otw && skyblend ) {
637 // draw the sky backdrop
639 // we need a white diffuse light for the phase of the moon
640 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
641 thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
643 // return to the desired diffuse color
644 ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
647 // draw the ssg scene
648 glEnable( GL_DEPTH_TEST );
650 ssgSetNearFar( scene_nearplane, scene_farplane );
652 if ( fgGetBool("/sim/rendering/wireframe") ) {
654 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
660 FGTileMgr::set_tile_filter( true );
661 sgSetModelFilter( false );
662 globals->get_aircraft_model()->select( false );
663 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
665 // Disable depth buffer update, draw the clouds
666 glDepthMask( GL_FALSE );
667 thesky->drawUpperClouds();
668 if ( multi_pass_clouds ) {
669 thesky->drawLowerClouds();
671 glDepthMask( GL_TRUE );
673 if ( multi_pass_clouds ) {
674 // Draw the objects except the aircraft
675 // and update the stencil buffer with 1
676 glEnable( GL_STENCIL_TEST );
677 glStencilFunc( GL_ALWAYS, 1, 1 );
678 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
680 FGTileMgr::set_tile_filter( false );
681 sgSetModelFilter( true );
682 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
684 FGTileMgr::set_tile_filter( true );
685 sgSetModelFilter( true );
686 globals->get_aircraft_model()->select( false );
687 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
691 // This is a bit kludgy. Every 200 frames, do an extra
692 // traversal of the scene graph without drawing anything, but
693 // with the field-of-view set to 360x360 degrees. This
694 // ensures that out-of-range random objects that are not in
695 // the current view frustum will still be freed properly.
696 static int counter = 0;
698 if (counter == 200) {
701 // No need to put the near plane too close;
702 // this way, at least the aircraft can be
704 f.setNearFar(1000, 1000000);
706 ssgGetModelviewMatrix(m);
707 FGTileMgr::set_tile_filter( true );
708 sgSetModelFilter( true );
709 globals->get_scenery()->get_scene_graph()->cull(&f, m, true);
713 // change state for lighting here
715 // draw runway lighting
716 glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
717 ssgSetNearFar( scene_nearplane, scene_farplane );
719 if ( enhanced_lighting ) {
721 // Enable states for drawing points with GL_extension
722 glEnable(GL_POINT_SMOOTH);
724 if ( distance_attenuation && glPointParameterIsSupported )
726 // Enable states for drawing points with GL_extension
727 glEnable(GL_POINT_SMOOTH);
729 float quadratic[3] = {1.0, 0.001, 0.0000001};
730 // makes the points fade as they move away
731 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
732 glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0);
737 // blending function for runway lights
738 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
741 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
742 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
743 glEnable(GL_TEXTURE_GEN_S);
744 glEnable(GL_TEXTURE_GEN_T);
745 glPolygonMode(GL_FRONT, GL_POINT);
747 // draw runway lighting
749 ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
750 ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
753 // change punch through and then draw taxi lighting
754 glFogf ( GL_FOG_DENSITY, fog_exp2_density );
756 // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
757 // glFogfv ( GL_FOG_COLOR, taxi_fog );
759 ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
763 glPolygonMode(GL_FRONT, GL_FILL);
764 glDisable(GL_TEXTURE_GEN_S);
765 glDisable(GL_TEXTURE_GEN_T);
767 //static int _frame_count = 0;
768 //if (_frame_count % 30 == 0) {
769 // printf("SSG: %s\n", ssgShowStats());
777 if ( enhanced_lighting ) {
778 if ( distance_attenuation && glPointParameterIsSupported ) {
779 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
780 default_attenuation);
784 glDisable(GL_POINT_SMOOTH);
787 // draw ground lighting
788 glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
790 ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
793 if ( draw_otw && fgGetBool("/sim/rendering/clouds3d") ) {
795 glDisable( GL_LIGHTING );
796 // cout << "drawing new clouds" << endl;
798 glEnable(GL_DEPTH_TEST);
800 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
803 glEnable( GL_TEXTURE_2D );
804 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
805 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
806 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
807 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
810 #ifdef FG_USE_CLOUDS_3D
811 sgClouds3d->Draw((sgVec4 *)current__view->get_VIEW());
814 glEnable( GL_LIGHTING );
815 glEnable( GL_DEPTH_TEST );
816 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
819 if ( draw_otw && draw_clouds ) {
820 if ( multi_pass_clouds ) {
821 // Disable depth buffer update, draw the clouds where the
822 // objects overwrite the already drawn clouds, by testing
823 // the stencil buffer against 1
824 glDepthMask( GL_FALSE );
825 glStencilFunc( GL_EQUAL, 1, 1 );
826 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
827 thesky->drawUpperClouds();
828 thesky->drawLowerClouds();
829 glDepthMask( GL_TRUE );
830 glDisable( GL_STENCIL_TEST );
832 glDepthMask( GL_FALSE );
833 thesky->drawLowerClouds();
834 glDepthMask( GL_TRUE );
839 FGTileMgr::set_tile_filter( false );
840 sgSetModelFilter( false );
841 globals->get_aircraft_model()->select( true );
842 globals->get_model_mgr()->draw();
843 globals->get_aircraft_model()->draw();
844 // If the view is internal, the previous line draw the
845 // cockpit with modified near/far clip planes and deselect
846 // the aircraft in the global scenegraph
847 // Otherwise, it just enables the aircraft: The scenegraph
848 // must be drawn again to see the plane.
849 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
850 FGTileMgr::set_tile_filter( true );
851 sgSetModelFilter( true );
852 globals->get_aircraft_model()->select( true );
855 // display HUD && Panel
857 glDisable( GL_DEPTH_TEST );
858 // glDisable( GL_CULL_FACE );
859 // glDisable( GL_TEXTURE_2D );
861 // update the controls subsystem
862 globals->get_controls()->update(delta_time_sec);
864 hud_and_panel->apply();
867 // Use the hud_and_panel ssgSimpleState for rendering the ATC output
868 // This only works properly if called before the panel call
869 if((fgGetBool("/sim/ATC/enabled")) || (fgGetBool("/sim/ai-traffic/enabled")))
870 globals->get_ATC_display()->update(delta_time_sec);
872 // update the panel subsystem
873 if ( globals->get_current_panel() != NULL ) {
874 globals->get_current_panel()->update(delta_time_sec);
878 // We can do translucent menus, so why not. :-)
880 glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
882 // glDisable ( GL_BLEND ) ;
884 glEnable( GL_DEPTH_TEST );
887 // Fade out the splash screen over the first three seconds.
888 double t = globals->get_sim_time_sec();
890 fgSplashUpdate(0.0, 1.0);
891 } else if ( t <= 3.0) {
892 fgSplashUpdate(0.0, (3.0 - t) / 2.0);
899 // options.cxx needs to see this for toggle_panel()
900 // Handle new window size or exposure
902 FGRenderer::resize( int width, int height ) {
905 if ( (!fgGetBool("/sim/virtual-cockpit"))
906 && fgPanelVisible() && idle_state == 1000 ) {
907 view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
908 globals->get_current_panel()->getYOffset()) / 768.0);
913 glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) );
915 fgSetInt("/sim/startup/xsize", width);
916 fgSetInt("/sim/startup/ysize", height);
917 guiInitMouse(width, height);
920 FGViewMgr *viewmgr = globals->get_viewmgr();
922 for ( int i = 0; i < viewmgr->size(); ++i ) {
923 viewmgr->get_view(i)->
924 set_aspect_ratio((float)view_h / (float)width);
927 ssgSetFOV( viewmgr->get_current_view()->get_h_fov(),
928 viewmgr->get_current_view()->get_v_fov() );
930 #ifdef FG_USE_CLOUDS_3D
931 sgClouds3d->Resize( viewmgr->get_current_view()->get_h_fov(),
932 viewmgr->get_current_view()->get_v_fov() );
941 // end of renderer.cxx