]> git.mxchange.org Git - flightgear.git/blob - src/Main/renderer.cxx
- implement progress information (enabled by default; can be turned off via
[flightgear.git] / src / Main / renderer.cxx
1 // renderer.cxx -- top level sim routines
2 //
3 // Written by Curtis Olson, started May 1997.
4 // This file contains parts of main.cxx prior to october 2004
5 //
6 // Copyright (C) 1997 - 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>
33 #endif
34
35 #include <plib/ssg.h>
36 #include <plib/netSocket.h>
37
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>
48 #ifdef FG_JPEG_SERVER
49 #include <simgear/screen/jpgfactory.hxx>
50 #endif
51
52 #ifdef FG_USE_CLOUDS_3D
53 #  include <simgear/scene/sky/clouds3d/SkySceneLoader.hpp>
54 #  include <simgear/scene/sky/clouds3d/SkyUtil.hpp>
55 #endif
56
57 #include <Scenery/tileentry.hxx>
58 #include <Time/light.hxx>
59 #include <Time/light.hxx>
60 #include <Aircraft/aircraft.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 <ATC/ATCdisplay.hxx>
70 #include <Replay/replay.hxx>
71 #include <GUI/new_gui.hxx>
72
73 #include "splash.hxx"
74 #include "renderer.hxx"
75 #include "main.hxx"
76
77
78 float default_attenuation[3] = {1.0, 0.0, 0.0};
79
80 ssgSelector *lightpoints_brightness = new ssgSelector;
81 ssgTransform *lightpoints_transform = new ssgTransform;
82 FGTileEntry *dummy_tile;
83 sgVec3 rway_ols;
84
85 // Clip plane settings...
86 float scene_nearplane = 0.5f;
87 float scene_farplane = 120000.0f;
88
89 glPointParameterfProc glPointParameterfPtr = 0;
90 glPointParameterfvProc glPointParameterfvPtr = 0;
91 bool glPointParameterIsSupported = false;
92
93
94 // fog constants.  I'm a little nervous about putting actual code out
95 // here but it seems to work (?)
96 static const double m_log01 = -log( 0.01 );
97 static const double sqrt_m_log01 = sqrt( m_log01 );
98 static GLfloat fog_exp_density;
99 static GLfloat fog_exp2_density;
100 static GLfloat rwy_exp2_punch_through;
101 static GLfloat taxi_exp2_punch_through;
102 static GLfloat ground_exp2_punch_through;
103
104 // Sky structures
105 SGSky *thesky;
106
107 #ifdef FG_USE_CLOUDS_3D
108   SkySceneLoader *sgClouds3d;
109   bool _bcloud_orig = true;
110 #endif
111
112 // hack
113 sgMat4 copy_of_ssgOpenGLAxisSwapMatrix =
114 {
115   {  1.0f,  0.0f,  0.0f,  0.0f },
116   {  0.0f,  0.0f, -1.0f,  0.0f },
117   {  0.0f,  1.0f,  0.0f,  0.0f },
118   {  0.0f,  0.0f,  0.0f,  1.0f }
119 };
120
121 ssgSimpleState *cloud3d_imposter_state;
122 ssgSimpleState *default_state;
123 ssgSimpleState *hud_and_panel;
124 ssgSimpleState *menus;
125
126 FGRenderer::FGRenderer()
127 {
128 #ifdef FG_JPEG_SERVER
129    jpgRenderFrame = FGRenderer::update;
130 #endif
131 }
132
133 FGRenderer::~FGRenderer()
134 {
135 #ifdef FG_JPEG_SERVER
136    jpgRenderFrame = NULL;
137 #endif
138 }
139
140
141 void
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 );
154
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 );
168
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 );
175
176     menus = new ssgSimpleState;
177     menus->ref();
178     menus->disable( GL_CULL_FACE );
179     menus->disable( GL_TEXTURE_2D );
180     menus->enable( GL_BLEND );
181 }
182
183
184 // Initialize various GL/view parameters
185 void
186 FGRenderer::init( void ) {
187
188     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
189
190     // Go full screen if requested ...
191     if ( fgGetBool("/sim/startup/fullscreen") ) {
192         fgOSFullScreen();
193     }
194
195     // If enabled, normal vectors specified with glNormal are scaled
196     // to unit length after transformation.  Enabling this has
197     // performance implications.  See the docs for glNormal.
198     // glEnable( GL_NORMALIZE );
199
200     glEnable( GL_LIGHTING );
201     glEnable( GL_LIGHT0 );
202     // glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );  // done later with ssg
203
204     sgVec3 sunpos;
205     sgSetVec3( sunpos, l->sun_vec()[0], l->sun_vec()[1], l->sun_vec()[2] );
206     ssgGetLight( 0 ) -> setPosition( sunpos );
207
208     glFogi (GL_FOG_MODE, GL_EXP2);
209     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) || 
210          (!fgGetBool("/sim/rendering/shading"))) {
211         // if fastest fog requested, or if flat shading force fastest
212         glHint ( GL_FOG_HINT, GL_FASTEST );
213     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
214         glHint ( GL_FOG_HINT, GL_NICEST );
215     }
216     if ( fgGetBool("/sim/rendering/wireframe") ) {
217         // draw wire frame
218         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
219     }
220
221     // This is the default anyways, but it can't hurt
222     glFrontFace ( GL_CCW );
223
224     // Just testing ...
225     // glEnable(GL_POINT_SMOOTH);
226     // glEnable(GL_LINE_SMOOTH);
227     // glEnable(GL_POLYGON_SMOOTH);      
228 }
229
230
231
232 // Update all Visuals (redraws anything graphics related)
233 void
234 FGRenderer::update( bool refresh_camera_settings ) {
235     bool scenery_loaded = fgGetBool("sim/sceneryloaded") || fgGetBool("sim/sceneryloaded-override");
236
237     if ( idle_state < 1000 || !scenery_loaded ) {
238         // still initializing, draw the splash screen
239         fgSplashUpdate(1.0);
240
241         // Keep resetting sim time while the sim is initializing
242         globals->set_sim_time_sec( 0.0 );
243         SGAnimation::set_sim_time_sec( 0.0 );
244         return;
245     }
246
247
248     bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
249     bool skyblend = fgGetBool("/sim/rendering/skyblend");
250     bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
251     bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
252 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
253     bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") && 
254                                 !SGCloudLayer::enable_bump_mapping;  // ugly artefact now
255 #else
256     bool multi_pass_clouds = false;
257 #endif
258     bool draw_clouds = fgGetBool("/environment/clouds/status");
259
260     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
261     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
262
263     // static const SGPropertyNode *longitude
264     //     = fgGetNode("/position/longitude-deg");
265     // static const SGPropertyNode *latitude
266     //     = fgGetNode("/position/latitude-deg");
267     // static const SGPropertyNode *altitude
268     //     = fgGetNode("/position/altitude-ft");
269     static const SGPropertyNode *groundlevel_nearplane
270         = fgGetNode("/sim/current-view/ground-level-nearplane-m");
271
272     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
273     static double last_visibility = -9999;
274
275     // update fog params
276     double actual_visibility;
277     if (fgGetBool("/environment/clouds/status")) {
278         actual_visibility = thesky->get_visibility();
279     } else {
280         actual_visibility = fgGetDouble("/environment/visibility-m");
281     }
282
283     if ( actual_visibility != last_visibility ) {
284         last_visibility = actual_visibility;
285
286         fog_exp_density = m_log01 / actual_visibility;
287         fog_exp2_density = sqrt_m_log01 / actual_visibility;
288         ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
289         if ( actual_visibility < 8000 ) {
290             rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
291             taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
292         } else {
293             rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
294             taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
295         }
296     }
297
298     // double angle;
299     // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
300     // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
301     // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
302     // GLfloat mat_shininess[] = { 10.0 };
303     GLbitfield clear_mask;
304
305     // idle_state is now 1000 meaning we've finished all our
306     // initializations and are running the main loop, so this will
307     // now work without seg faulting the system.
308
309     FGViewer *current__view = globals->get_current_view();
310
311     // calculate our current position in cartesian space
312     Point3D cntr = globals->get_scenery()->get_next_center();
313     globals->get_scenery()->set_center(cntr);
314     current__view->set_scenery_center(cntr);
315
316     if ( refresh_camera_settings ) {
317         // update view port
318         resize( fgGetInt("/sim/startup/xsize"),
319                 fgGetInt("/sim/startup/ysize") );
320
321         // Tell GL we are switching to model view parameters
322         glMatrixMode(GL_MODELVIEW);
323         glLoadIdentity();
324         ssgSetCamera( (sgVec4 *)current__view->get_VIEW() );
325     }
326
327     if ( fgGetBool("/sim/rendering/clouds3d") ) {
328         glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
329         cloud3d_imposter_state->force();
330         glDisable( GL_FOG );
331         glColor4f( 1.0, 1.0, 1.0, 1.0 );
332         glEnable(GL_DEPTH_TEST);
333         glEnable(GL_BLEND);
334         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
335
336 #ifdef FG_USE_CLOUDS_3D
337         if ( _bcloud_orig ) {
338             Point3D c = globals->get_scenery()->get_center();
339             sgClouds3d->Set_Cloud_Orig( &c );
340             _bcloud_orig = false;
341         }
342         sgClouds3d->Update( current__view->get_absolute_view_pos() );
343 #endif
344         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
345         glDisable(GL_DEPTH_TEST);
346     }
347
348     clear_mask = GL_DEPTH_BUFFER_BIT;
349     if ( fgGetBool("/sim/rendering/wireframe") ) {
350         clear_mask |= GL_COLOR_BUFFER_BIT;
351     }
352
353     if ( skyblend ) {
354         if ( fgGetBool("/sim/rendering/textures") ) {
355         // glClearColor(black[0], black[1], black[2], black[3]);
356         glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
357                      l->adj_fog_color()[2], l->adj_fog_color()[3]);
358         clear_mask |= GL_COLOR_BUFFER_BIT;
359         }
360     } else {
361         glClearColor(l->sky_color()[0], l->sky_color()[1],
362                      l->sky_color()[2], l->sky_color()[3]);
363         clear_mask |= GL_COLOR_BUFFER_BIT;
364     }
365     if ( multi_pass_clouds && draw_clouds ) {
366         glClearStencil( 0 );
367         clear_mask |= GL_STENCIL_BUFFER_BIT;
368     }
369     glClear( clear_mask );
370
371     // set the opengl state to known default values
372     default_state->force();
373
374     // update fog params if visibility has changed
375     double visibility_meters = fgGetDouble("/environment/visibility-m");
376     thesky->set_visibility(visibility_meters);
377
378     thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
379                         ( global_multi_loop * fgGetInt("/sim/speed-up") )
380                         / (double)fgGetInt("/sim/model-hz") );
381
382     // Set correct opengl fog density
383     glFogf (GL_FOG_DENSITY, fog_exp2_density);
384
385     // update the sky dome
386     if ( skyblend ) {
387         /*
388          SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
389          << l->sky_color()[0] << " "
390          << l->sky_color()[1] << " "
391          << l->sky_color()[2] << " "
392          << l->sky_color()[3] );
393         SG_LOG( SG_GENERAL, SG_BULK, "    fog = "
394          << l->fog_color()[0] << " "
395          << l->fog_color()[1] << " "
396          << l->fog_color()[2] << " "
397          << l->fog_color()[3] ); 
398         SG_LOG( SG_GENERAL, SG_BULK,
399                 "    sun_angle = " << l->sun_angle
400          << "    moon_angle = " << l->moon_angle );
401         */
402
403         static SGSkyColor scolor;
404         FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
405
406         scolor.sky_color   = l->sky_color();
407         scolor.fog_color   = l->adj_fog_color();
408         scolor.cloud_color = l->cloud_color();
409         scolor.sun_angle   = l->get_sun_angle();
410         scolor.moon_angle  = l->get_moon_angle();
411         scolor.nplanets    = globals->get_ephem()->getNumPlanets();
412         scolor.nstars      = globals->get_ephem()->getNumStars();
413         scolor.planet_data = globals->get_ephem()->getPlanets();
414         scolor.star_data   = globals->get_ephem()->getStars();
415
416         thesky->repaint( scolor );
417
418         /*
419         SG_LOG( SG_GENERAL, SG_BULK,
420                 "thesky->reposition( view_pos = " << view_pos[0] << " "
421          << view_pos[1] << " " << view_pos[2] );
422         SG_LOG( SG_GENERAL, SG_BULK,
423                 "    zero_elev = " << zero_elev[0] << " "
424          << zero_elev[1] << " " << zero_elev[2]
425          << " lon = " << cur_fdm_state->get_Longitude()
426          << " lat = " << cur_fdm_state->get_Latitude() );
427         SG_LOG( SG_GENERAL, SG_BULK,
428                 "    sun_rot = " << l->get_sun_rotation
429          << " gst = " << SGTime::cur_time_params->getGst() );
430         SG_LOG( SG_GENERAL, SG_BULK,
431              "    sun ra = " << globals->get_ephem()->getSunRightAscension()
432           << " sun dec = " << globals->get_ephem()->getSunDeclination()
433           << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
434           << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
435         */
436
437         // The sun and moon distances are scaled down versions
438         // of the actual distance to get both the moon and the sun
439         // within the range of the far clip plane.
440         // Moon distance:    384,467 kilometers
441         // Sun distance: 150,000,000 kilometers
442         double sun_horiz_eff, moon_horiz_eff;
443         if (fgGetBool("/sim/rendering/horizon-effect")) {
444         sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
445         moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
446         } else {
447            sun_horiz_eff = moon_horiz_eff = 1.0;
448         }
449
450         static SGSkyState sstate;
451
452         sstate.view_pos  = current__view->get_view_pos();
453         sstate.zero_elev = current__view->get_zero_elev();
454         sstate.view_up   = current__view->get_world_up();
455         sstate.lon       = current__view->getLongitude_deg()
456                             * SGD_DEGREES_TO_RADIANS;
457         sstate.lat       = current__view->getLatitude_deg()
458                             * SGD_DEGREES_TO_RADIANS;
459         sstate.alt       = current__view->getAltitudeASL_ft()
460                             * SG_FEET_TO_METER;
461         sstate.spin      = l->get_sun_rotation();
462         sstate.gst       = globals->get_time_params()->getGst();
463         sstate.sun_ra    = globals->get_ephem()->getSunRightAscension();
464         sstate.sun_dec   = globals->get_ephem()->getSunDeclination();
465         sstate.sun_dist  = 50000.0 * sun_horiz_eff;
466         sstate.moon_ra   = globals->get_ephem()->getMoonRightAscension();
467         sstate.moon_dec  = globals->get_ephem()->getMoonDeclination();
468         sstate.moon_dist = 40000.0 * moon_horiz_eff;
469
470         thesky->reposition( sstate, delta_time_sec );
471     }
472
473     glEnable( GL_DEPTH_TEST );
474     if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
475         glEnable( GL_FOG );
476         glFogi( GL_FOG_MODE, GL_EXP2 );
477         glFogfv( GL_FOG_COLOR, l->adj_fog_color() );
478     }
479
480     // set sun/lighting parameters
481     ssgGetLight( 0 ) -> setPosition( l->sun_vec() );
482
483     // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
484     // we only update GL_AMBIENT for our lights we will never get
485     // a completely dark scene.  So, we set GL_LIGHT_MODEL_AMBIENT
486     // explicitely to black.
487     glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
488
489     ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
490     ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
491     ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_specular() );
492
493     // texture parameters
494     // glEnable( GL_TEXTURE_2D );
495     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
496     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
497
498     double agl =
499         current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
500         - globals->get_scenery()->get_cur_elev();
501
502     if ( agl > 50.0 ) {
503         scene_nearplane = 10.0f;
504         scene_farplane = 120000.0f;
505     } else {
506         scene_nearplane = groundlevel_nearplane->getDoubleValue();
507         scene_farplane = 120000.0f;
508     }
509
510     setNearFar( scene_nearplane, scene_farplane );
511
512     if ( draw_otw && skyblend ) {
513         // draw the sky backdrop
514
515         // we need a white diffuse light for the phase of the moon
516         ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
517         thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
518                          fog_exp2_density );
519         // return to the desired diffuse color
520         ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
521     }
522
523     // draw the ssg scene
524     glEnable( GL_DEPTH_TEST );
525
526     if ( fgGetBool("/sim/rendering/wireframe") ) {
527         // draw wire frame
528         glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
529     }
530     if ( draw_otw ) {
531         if ( draw_clouds ) {
532
533             // Draw the terrain
534             FGTileMgr::set_tile_filter( true );
535             sgSetModelFilter( false );
536             globals->get_aircraft_model()->select( false );
537             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
538
539             // Disable depth buffer update, draw the clouds
540             glDepthMask( GL_FALSE );
541             thesky->drawUpperClouds();
542             if ( multi_pass_clouds ) {
543                 thesky->drawLowerClouds();
544             }
545             glDepthMask( GL_TRUE );
546
547             if ( multi_pass_clouds ) {
548                 // Draw the objects except the aircraft
549                 //  and update the stencil buffer with 1
550                 glEnable( GL_STENCIL_TEST );
551                 glStencilFunc( GL_ALWAYS, 1, 1 );
552                 glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
553             }
554             FGTileMgr::set_tile_filter( false );
555             sgSetModelFilter( true );
556             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
557         } else {
558             FGTileMgr::set_tile_filter( true );
559             sgSetModelFilter( true );
560             globals->get_aircraft_model()->select( false );
561             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
562         }
563     }
564
565     // This is a bit kludgy.  Every 200 frames, do an extra
566     // traversal of the scene graph without drawing anything, but
567     // with the field-of-view set to 360x360 degrees.  This
568     // ensures that out-of-range random objects that are not in
569     // the current view frustum will still be freed properly.
570     static int counter = 0;
571     counter++;
572     if (counter >= 200) {
573         sgFrustum f;
574         f.setFOV(360, 360);
575                 // No need to put the near plane too close;
576                 // this way, at least the aircraft can be
577                 // culled.
578         f.setNearFar(1000, 1000000);
579         sgMat4 m;
580         ssgGetModelviewMatrix(m);
581         FGTileMgr::set_tile_filter( true );
582         sgSetModelFilter( true );
583         globals->get_scenery()->get_scene_graph()->cull(&f, m, true);
584         counter = 0;
585     }
586
587     // change state for lighting here
588
589     // draw runway lighting
590     glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
591
592     // CLO - 02/25/2005 - DO WE NEED THIS extra fgSetNearFar()?
593     // fgSetNearFar( scene_nearplane, scene_farplane );
594
595     if ( enhanced_lighting ) {
596
597         // Enable states for drawing points with GL_extension
598         glEnable(GL_POINT_SMOOTH);
599
600         if ( distance_attenuation && glPointParameterIsSupported )
601         {
602             // Enable states for drawing points with GL_extension
603             glEnable(GL_POINT_SMOOTH);
604
605             float quadratic[3] = {1.0, 0.001, 0.0000001};
606             // makes the points fade as they move away
607             glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
608             glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0); 
609         }
610
611         glPointSize(4.0);
612
613         // blending function for runway lights
614         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
615     }
616
617     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
618     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
619     glEnable(GL_TEXTURE_GEN_S);
620     glEnable(GL_TEXTURE_GEN_T);
621     glPolygonMode(GL_FRONT, GL_POINT);
622
623     // draw runway lighting
624     if ( draw_otw ) {
625         ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
626         ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
627     }
628
629     // change punch through and then draw taxi lighting
630     glFogf ( GL_FOG_DENSITY, fog_exp2_density );
631     // sgVec3 taxi_fog;
632     // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
633     // glFogfv ( GL_FOG_COLOR, taxi_fog );
634     if ( draw_otw ) {
635         ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
636     }
637
638     // clean up lighting
639     glPolygonMode(GL_FRONT, GL_FILL);
640     glDisable(GL_TEXTURE_GEN_S);
641     glDisable(GL_TEXTURE_GEN_T);
642
643     //static int _frame_count = 0;
644     //if (_frame_count % 30 == 0) {
645     //  printf("SSG: %s\n", ssgShowStats());
646     //}
647     //else {
648     //  ssgShowStats();
649     //}
650     //_frame_count++;
651
652
653     if ( enhanced_lighting ) {
654         if ( distance_attenuation && glPointParameterIsSupported ) {
655             glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
656                                   default_attenuation);
657         }
658
659         glPointSize(1.0);
660         glDisable(GL_POINT_SMOOTH);
661     }
662
663     // draw ground lighting
664     glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
665     if ( draw_otw ) {
666         ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
667     }
668
669     if ( draw_otw && fgGetBool("/sim/rendering/clouds3d") ) {
670         glDisable( GL_FOG );
671         glDisable( GL_LIGHTING );
672         // cout << "drawing new clouds" << endl;
673
674         glEnable(GL_DEPTH_TEST);
675         glEnable(GL_BLEND);
676         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
677
678         /*
679         glEnable( GL_TEXTURE_2D );
680         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
681         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
682         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
683         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
684         */
685
686 #ifdef FG_USE_CLOUDS_3D
687         sgClouds3d->Draw((sgVec4 *)current__view->get_VIEW());
688 #endif
689         glEnable( GL_FOG );
690         glEnable( GL_LIGHTING );
691         glEnable( GL_DEPTH_TEST );
692         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
693     }
694
695     if ( draw_otw && draw_clouds ) {
696         if ( multi_pass_clouds ) {
697             // Disable depth buffer update, draw the clouds where the
698             //  objects overwrite the already drawn clouds, by testing
699             //  the stencil buffer against 1
700             glDepthMask( GL_FALSE );
701             glStencilFunc( GL_EQUAL, 1, 1 );
702             glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
703             thesky->drawUpperClouds();
704             thesky->drawLowerClouds();
705             glDepthMask( GL_TRUE );
706             glDisable( GL_STENCIL_TEST );
707         } else {
708             glDepthMask( GL_FALSE );
709             thesky->drawLowerClouds();
710             glDepthMask( GL_TRUE );
711         }
712     }
713
714     if ( draw_otw ) {
715         FGTileMgr::set_tile_filter( false );
716         sgSetModelFilter( false );
717         globals->get_aircraft_model()->select( true );
718         globals->get_model_mgr()->draw();
719         globals->get_aircraft_model()->draw();
720         // If the view is internal, the previous line draw the 
721         //  cockpit with modified near/far clip planes and deselect
722         //  the aircraft in the global scenegraph
723         // Otherwise, it just enables the aircraft: The scenegraph
724         //  must be drawn again to see the plane.
725         ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
726         FGTileMgr::set_tile_filter( true );
727         sgSetModelFilter( true );
728         globals->get_aircraft_model()->select( true );
729     }
730
731     // display HUD && Panel
732     glDisable( GL_FOG );
733     glDisable( GL_DEPTH_TEST );
734     // glDisable( GL_CULL_FACE );
735     // glDisable( GL_TEXTURE_2D );
736
737     // update the controls subsystem
738     globals->get_controls()->update(delta_time_sec);
739
740     hud_and_panel->apply();
741     fgCockpitUpdate();
742
743     // Use the hud_and_panel ssgSimpleState for rendering the ATC output
744     // This only works properly if called before the panel call
745     if((fgGetBool("/sim/ATC/enabled")) || (fgGetBool("/sim/ai-traffic/enabled")))
746         globals->get_ATC_display()->update(delta_time_sec);
747
748     // update the panel subsystem
749     if ( globals->get_current_panel() != NULL ) {
750         globals->get_current_panel()->update(delta_time_sec);
751     }
752     fgUpdate3DPanels();
753
754     // We can do translucent menus, so why not. :-)
755     menus->apply();
756     glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
757     puDisplay();
758     // glDisable ( GL_BLEND ) ;
759
760     glEnable( GL_DEPTH_TEST );
761     glEnable( GL_FOG );
762
763     // Fade out the splash screen over the first three seconds.
764     double t = globals->get_sim_time_sec();
765     if (t <= 2.5)
766         fgSplashUpdate((2.5 - t) / 2.5);
767 }
768
769
770
771 // options.cxx needs to see this for toggle_panel()
772 // Handle new window size or exposure
773 void
774 FGRenderer::resize( int width, int height ) {
775     int view_h;
776
777     if ( (!fgGetBool("/sim/virtual-cockpit"))
778          && fgPanelVisible() && idle_state == 1000 ) {
779         view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
780                              globals->get_current_panel()->getYOffset()) / 768.0);
781     } else {
782         view_h = height;
783     }
784
785     glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) );
786
787     fgSetInt("/sim/startup/xsize", width);
788     fgSetInt("/sim/startup/ysize", height);
789     guiInitMouse(width, height);
790
791     // for all views
792     FGViewMgr *viewmgr = globals->get_viewmgr();
793     if (viewmgr) {
794       for ( int i = 0; i < viewmgr->size(); ++i ) {
795         viewmgr->get_view(i)->
796           set_aspect_ratio((float)view_h / (float)width);
797       }
798
799       setFOV( viewmgr->get_current_view()->get_h_fov(),
800               viewmgr->get_current_view()->get_v_fov() );
801       // cout << "setFOV(" << viewmgr->get_current_view()->get_h_fov()
802       //      << ", " << viewmgr->get_current_view()->get_v_fov() << ")"
803       //      << endl;
804
805 #ifdef FG_USE_CLOUDS_3D
806       sgClouds3d->Resize( viewmgr->get_current_view()->get_h_fov(),
807                           viewmgr->get_current_view()->get_v_fov() );
808 #endif
809     }
810
811     fgHUDReshape();
812
813 }
814
815
816 // These are wrapper functions around ssgSetNearFar() and ssgSetFOV()
817 // which will post process and rewrite the resulting frustum if we
818 // want to do asymmetric view frustums.
819
820 static void fgHackFrustum() {
821
822     // specify a percent of the configured view frustum to actually
823     // display.  This is a bit of a hack to achieve asymmetric view
824     // frustums.  For instance, if you want to display two monitors
825     // side by side, you could specify each with a double fov, a 0.5
826     // aspect ratio multiplier, and then the left side monitor would
827     // have a left_pct = 0.0, a right_pct = 0.5, a bottom_pct = 0.0,
828     // and a top_pct = 1.0.  The right side monitor would have a
829     // left_pct = 0.5 and a right_pct = 1.0.
830
831     static SGPropertyNode *left_pct
832         = fgGetNode("/sim/current-view/frustum-left-pct");
833     static SGPropertyNode *right_pct
834         = fgGetNode("/sim/current-view/frustum-right-pct");
835     static SGPropertyNode *bottom_pct
836         = fgGetNode("/sim/current-view/frustum-bottom-pct");
837     static SGPropertyNode *top_pct
838         = fgGetNode("/sim/current-view/frustum-top-pct");
839
840     sgFrustum *f = ssgGetFrustum();
841
842     // cout << " l = " << f->getLeft()
843     //      << " r = " << f->getRight()
844     //      << " b = " << f->getBot()
845     //      << " t = " << f->getTop()
846     //      << " n = " << f->getNear()
847     //      << " f = " << f->getFar()
848     //      << endl;
849
850     double width = f->getRight() - f->getLeft();
851     double height = f->getTop() - f->getBot();
852
853     double l, r, t, b;
854
855     if ( left_pct != NULL ) {
856         l = f->getLeft() + width * left_pct->getDoubleValue();
857     } else {
858         l = f->getLeft();
859     }
860
861     if ( right_pct != NULL ) {
862         r = f->getLeft() + width * right_pct->getDoubleValue();
863     } else {
864         r = f->getRight();
865     }
866
867     if ( bottom_pct != NULL ) {
868         b = f->getBot() + height * bottom_pct->getDoubleValue();
869     } else {
870         b = f->getBot();
871     }
872
873     if ( top_pct != NULL ) {
874         t = f->getBot() + height * top_pct->getDoubleValue();
875     } else {
876         t = f->getTop();
877     }
878
879     ssgSetFrustum(l, r, b, t, f->getNear(), f->getFar());
880 }
881
882
883 // we need some static storage space for these values.  However, we
884 // can't store it in a renderer class object because the functions
885 // that manipulate these are static.  They are static so they can
886 // interface to the display callback system.  There's probably a
887 // better way, there has to be a better way, but I'm not seeing it
888 // right now.
889 static float fov_width = 55.0;
890 static float fov_height = 42.0;
891 static float fov_near = 1.0;
892 static float fov_far = 1000.0;
893
894
895 /** FlightGear code should use this routine to set the FOV rather than
896  *  calling the ssg routine directly
897  */
898 void FGRenderer::setFOV( float w, float h ) {
899     fov_width = w;
900     fov_height = h;
901
902     // fully specify the view frustum before hacking it (so we don't
903     // accumulate hacked effects
904     ssgSetFOV( w, h );
905     ssgSetNearFar( fov_near, fov_far );
906     fgHackFrustum();
907 }
908
909
910 /** FlightGear code should use this routine to set the Near/Far clip
911  *  planes rather than calling the ssg routine directly
912  */
913 void FGRenderer::setNearFar( float n, float f ) {
914     fov_near = n;
915     fov_far = f;
916
917     // fully specify the view frustum before hacking it (so we don't
918     // accumulate hacked effects
919     ssgSetNearFar( n, f );
920     ssgSetFOV( fov_width, fov_height );
921
922     fgHackFrustum();
923 }
924
925 // end of renderer.cxx