]> git.mxchange.org Git - flightgear.git/blob - src/Main/renderer.cxx
Constant-speed props were seeking to engine speed, not prop speed.
[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 // Update all Visuals (redraws anything graphics related)
232 void
233 FGRenderer::update( bool refresh_camera_settings ) {
234     bool scenery_loaded = fgGetBool("sim/sceneryloaded") || fgGetBool("sim/sceneryloaded-override");
235     bool draw_otw = fgGetBool("/sim/rendering/draw-otw");
236     bool skyblend = fgGetBool("/sim/rendering/skyblend");
237     bool enhanced_lighting = fgGetBool("/sim/rendering/enhanced-lighting");
238     bool distance_attenuation = fgGetBool("/sim/rendering/distance-attenuation");
239 #ifdef FG_ENABLE_MULTIPASS_CLOUDS
240     bool multi_pass_clouds = fgGetBool("/sim/rendering/multi-pass-clouds") && 
241                                 !SGCloudLayer::enable_bump_mapping;  // ugly artefact now
242 #else
243     bool multi_pass_clouds = false;
244 #endif
245     bool draw_clouds = fgGetBool("/environment/clouds/status");
246
247     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
248     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
249
250     // static const SGPropertyNode *longitude
251     //     = fgGetNode("/position/longitude-deg");
252     // static const SGPropertyNode *latitude
253     //     = fgGetNode("/position/latitude-deg");
254     // static const SGPropertyNode *altitude
255     //     = fgGetNode("/position/altitude-ft");
256     static const SGPropertyNode *groundlevel_nearplane
257         = fgGetNode("/sim/current-view/ground-level-nearplane-m");
258
259     FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
260     static double last_visibility = -9999;
261
262     // update fog params
263     double actual_visibility;
264     if (fgGetBool("/environment/clouds/status")) {
265         actual_visibility = thesky->get_visibility();
266     } else {
267         actual_visibility = fgGetDouble("/environment/visibility-m");
268     }
269
270     if ( actual_visibility != last_visibility ) {
271         last_visibility = actual_visibility;
272
273         fog_exp_density = m_log01 / actual_visibility;
274         fog_exp2_density = sqrt_m_log01 / actual_visibility;
275         ground_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
276         if ( actual_visibility < 8000 ) {
277             rwy_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 2.5);
278             taxi_exp2_punch_through = sqrt_m_log01 / (actual_visibility * 1.5);
279         } else {
280             rwy_exp2_punch_through = sqrt_m_log01 / ( 8000 * 2.5 );
281             taxi_exp2_punch_through = sqrt_m_log01 / ( 8000 * 1.5 );
282         }
283     }
284
285     // double angle;
286     // GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
287     // GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
288     // GLfloat terrain_color[4] = { 0.54, 0.44, 0.29, 1.0 };
289     // GLfloat mat_shininess[] = { 10.0 };
290     GLbitfield clear_mask;
291
292     if ( idle_state != 1000 || !scenery_loaded ) {
293         // still initializing, draw the splash screen
294         if ( fgGetBool("/sim/startup/splash-screen") ) {
295             fgSplashUpdate(0.0, 1.0);
296         }
297         // Keep resetting sim time while the sim is initializing
298         globals->set_sim_time_sec( 0.0 );
299         SGAnimation::set_sim_time_sec( 0.0 );
300     } else {
301         // idle_state is now 1000 meaning we've finished all our
302         // initializations and are running the main loop, so this will
303         // now work without seg faulting the system.
304
305         FGViewer *current__view = globals->get_current_view();
306
307         // calculate our current position in cartesian space
308         globals->get_scenery()->set_center( globals->get_scenery()->get_next_center() );
309
310         if ( refresh_camera_settings ) {
311             // update view port
312             resize( fgGetInt("/sim/startup/xsize"),
313                     fgGetInt("/sim/startup/ysize") );
314
315             // Tell GL we are switching to model view parameters
316             glMatrixMode(GL_MODELVIEW);
317             glLoadIdentity();
318             ssgSetCamera( (sgVec4 *)current__view->get_VIEW() );
319         }
320
321         if ( fgGetBool("/sim/rendering/clouds3d") ) {
322             glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
323             cloud3d_imposter_state->force();
324             glDisable( GL_FOG );
325             glColor4f( 1.0, 1.0, 1.0, 1.0 );
326             glEnable(GL_DEPTH_TEST);
327             glEnable(GL_BLEND);
328             glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
329
330 #ifdef FG_USE_CLOUDS_3D
331             if ( _bcloud_orig ) {
332                 Point3D c = globals->get_scenery()->get_center();
333                 sgClouds3d->Set_Cloud_Orig( &c );
334                 _bcloud_orig = false;
335             }
336             sgClouds3d->Update( current__view->get_absolute_view_pos() );
337 #endif
338             glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
339             glDisable(GL_DEPTH_TEST);
340         }
341
342         clear_mask = GL_DEPTH_BUFFER_BIT;
343         if ( fgGetBool("/sim/rendering/wireframe") ) {
344             clear_mask |= GL_COLOR_BUFFER_BIT;
345         }
346
347         if ( skyblend ) {
348             if ( fgGetBool("/sim/rendering/textures") ) {
349             // glClearColor(black[0], black[1], black[2], black[3]);
350             glClearColor(l->adj_fog_color()[0], l->adj_fog_color()[1],
351                          l->adj_fog_color()[2], l->adj_fog_color()[3]);
352             clear_mask |= GL_COLOR_BUFFER_BIT;
353             }
354         } else {
355             glClearColor(l->sky_color()[0], l->sky_color()[1],
356                          l->sky_color()[2], l->sky_color()[3]);
357             clear_mask |= GL_COLOR_BUFFER_BIT;
358         }
359         if ( multi_pass_clouds && draw_clouds ) {
360             glClearStencil( 0 );
361             clear_mask |= GL_STENCIL_BUFFER_BIT;
362         }
363         glClear( clear_mask );
364
365         // set the opengl state to known default values
366         default_state->force();
367
368         // update fog params if visibility has changed
369         double visibility_meters = fgGetDouble("/environment/visibility-m");
370         thesky->set_visibility(visibility_meters);
371
372         thesky->modify_vis( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
373                             ( global_multi_loop * fgGetInt("/sim/speed-up") )
374                             / (double)fgGetInt("/sim/model-hz") );
375
376         // Set correct opengl fog density
377         glFogf (GL_FOG_DENSITY, fog_exp2_density);
378
379         // update the sky dome
380         if ( skyblend ) {
381             /*
382              SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = "
383              << l->sky_color()[0] << " "
384              << l->sky_color()[1] << " "
385              << l->sky_color()[2] << " "
386              << l->sky_color()[3] );
387             SG_LOG( SG_GENERAL, SG_BULK, "    fog = "
388              << l->fog_color()[0] << " "
389              << l->fog_color()[1] << " "
390              << l->fog_color()[2] << " "
391              << l->fog_color()[3] ); 
392             SG_LOG( SG_GENERAL, SG_BULK,
393                     "    sun_angle = " << l->sun_angle
394              << "    moon_angle = " << l->moon_angle );
395             */
396
397             static SGSkyColor scolor;
398             FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
399
400             scolor.sky_color   = l->sky_color();
401             scolor.fog_color   = l->adj_fog_color();
402             scolor.cloud_color = l->cloud_color();
403             scolor.sun_angle   = l->get_sun_angle();
404             scolor.moon_angle  = l->get_moon_angle();
405             scolor.nplanets    = globals->get_ephem()->getNumPlanets();
406             scolor.nstars      = globals->get_ephem()->getNumStars();
407             scolor.planet_data = globals->get_ephem()->getPlanets();
408             scolor.star_data   = globals->get_ephem()->getStars();
409
410             thesky->repaint( scolor );
411
412             /*
413             SG_LOG( SG_GENERAL, SG_BULK,
414                     "thesky->reposition( view_pos = " << view_pos[0] << " "
415              << view_pos[1] << " " << view_pos[2] );
416             SG_LOG( SG_GENERAL, SG_BULK,
417                     "    zero_elev = " << zero_elev[0] << " "
418              << zero_elev[1] << " " << zero_elev[2]
419              << " lon = " << cur_fdm_state->get_Longitude()
420              << " lat = " << cur_fdm_state->get_Latitude() );
421             SG_LOG( SG_GENERAL, SG_BULK,
422                     "    sun_rot = " << l->get_sun_rotation
423              << " gst = " << SGTime::cur_time_params->getGst() );
424             SG_LOG( SG_GENERAL, SG_BULK,
425                  "    sun ra = " << globals->get_ephem()->getSunRightAscension()
426               << " sun dec = " << globals->get_ephem()->getSunDeclination()
427               << " moon ra = " << globals->get_ephem()->getMoonRightAscension()
428               << " moon dec = " << globals->get_ephem()->getMoonDeclination() );
429             */
430
431             // The sun and moon distances are scaled down versions
432             // of the actual distance to get both the moon and the sun
433             // within the range of the far clip plane.
434             // Moon distance:    384,467 kilometers
435             // Sun distance: 150,000,000 kilometers
436             double sun_horiz_eff, moon_horiz_eff;
437             if (fgGetBool("/sim/rendering/horizon-effect")) {
438             sun_horiz_eff = 0.67+pow(0.5+cos(l->get_sun_angle())*2/2, 0.33)/3;
439             moon_horiz_eff = 0.67+pow(0.5+cos(l->get_moon_angle())*2/2, 0.33)/3;
440             } else {
441                sun_horiz_eff = moon_horiz_eff = 1.0;
442             }
443
444             static SGSkyState sstate;
445
446             sstate.view_pos  = current__view->get_view_pos();
447             sstate.zero_elev = current__view->get_zero_elev();
448             sstate.view_up   = current__view->get_world_up();
449             sstate.lon       = current__view->getLongitude_deg()
450                                 * SGD_DEGREES_TO_RADIANS;
451             sstate.lat       = current__view->getLatitude_deg()
452                                 * SGD_DEGREES_TO_RADIANS;
453             sstate.alt       = current__view->getAltitudeASL_ft()
454                                 * SG_FEET_TO_METER;
455             sstate.spin      = l->get_sun_rotation();
456             sstate.gst       = globals->get_time_params()->getGst();
457             sstate.sun_ra    = globals->get_ephem()->getSunRightAscension();
458             sstate.sun_dec   = globals->get_ephem()->getSunDeclination();
459             sstate.sun_dist  = 50000.0 * sun_horiz_eff;
460             sstate.moon_ra   = globals->get_ephem()->getMoonRightAscension();
461             sstate.moon_dec  = globals->get_ephem()->getMoonDeclination();
462             sstate.moon_dist = 40000.0 * moon_horiz_eff;
463
464             thesky->reposition( sstate, delta_time_sec );
465         }
466
467         glEnable( GL_DEPTH_TEST );
468         if ( strcmp(fgGetString("/sim/rendering/fog"), "disabled") ) {
469             glEnable( GL_FOG );
470             glFogi( GL_FOG_MODE, GL_EXP2 );
471             glFogfv( GL_FOG_COLOR, l->adj_fog_color() );
472         }
473
474         // set sun/lighting parameters
475         ssgGetLight( 0 ) -> setPosition( l->sun_vec() );
476
477         // GL_LIGHT_MODEL_AMBIENT has a default non-zero value so if
478         // we only update GL_AMBIENT for our lights we will never get
479         // a completely dark scene.  So, we set GL_LIGHT_MODEL_AMBIENT
480         // explicitely to black.
481         glLightModelfv( GL_LIGHT_MODEL_AMBIENT, black );
482
483         ssgGetLight( 0 ) -> setColour( GL_AMBIENT, l->scene_ambient() );
484         ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
485         ssgGetLight( 0 ) -> setColour( GL_SPECULAR, l->scene_specular() );
486
487         // texture parameters
488         // glEnable( GL_TEXTURE_2D );
489         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
490         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
491
492         double agl =
493             current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
494             - globals->get_scenery()->get_cur_elev();
495
496         if ( agl > 10.0 ) {
497             scene_nearplane = 10.0f;
498             scene_farplane = 120000.0f;
499         } else {
500             scene_nearplane = groundlevel_nearplane->getDoubleValue();
501             scene_farplane = 120000.0f;
502         }
503
504         ssgSetNearFar( scene_nearplane, scene_farplane );
505
506         if ( draw_otw && skyblend ) {
507             // draw the sky backdrop
508
509             // we need a white diffuse light for the phase of the moon
510             ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, white );
511             thesky->preDraw( cur_fdm_state->get_Altitude() * SG_FEET_TO_METER,
512                              fog_exp2_density );
513             // return to the desired diffuse color
514             ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse() );
515         }
516
517         // draw the ssg scene
518         glEnable( GL_DEPTH_TEST );
519
520         ssgSetNearFar( scene_nearplane, scene_farplane );
521
522         if ( fgGetBool("/sim/rendering/wireframe") ) {
523             // draw wire frame
524             glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
525         }
526         if ( draw_otw ) {
527             if ( draw_clouds ) {
528
529                 // Draw the terrain
530                 FGTileMgr::set_tile_filter( true );
531                 sgSetModelFilter( false );
532                 globals->get_aircraft_model()->select( false );
533                 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
534
535                 // Disable depth buffer update, draw the clouds
536                 glDepthMask( GL_FALSE );
537                 thesky->drawUpperClouds();
538                 if ( multi_pass_clouds ) {
539                     thesky->drawLowerClouds();
540                 }
541                 glDepthMask( GL_TRUE );
542
543                 if ( multi_pass_clouds ) {
544                     // Draw the objects except the aircraft
545                     //  and update the stencil buffer with 1
546                     glEnable( GL_STENCIL_TEST );
547                     glStencilFunc( GL_ALWAYS, 1, 1 );
548                     glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE );
549                 }
550                 FGTileMgr::set_tile_filter( false );
551                 sgSetModelFilter( true );
552                 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
553             } else {
554                 FGTileMgr::set_tile_filter( true );
555                 sgSetModelFilter( true );
556                 globals->get_aircraft_model()->select( false );
557                 ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
558             }
559         }
560
561         // This is a bit kludgy.  Every 200 frames, do an extra
562         // traversal of the scene graph without drawing anything, but
563         // with the field-of-view set to 360x360 degrees.  This
564         // ensures that out-of-range random objects that are not in
565         // the current view frustum will still be freed properly.
566         static int counter = 0;
567         counter++;
568         if (counter == 200) {
569             sgFrustum f;
570             f.setFOV(360, 360);
571                     // No need to put the near plane too close;
572                     // this way, at least the aircraft can be
573                     // culled.
574             f.setNearFar(1000, 1000000);
575             sgMat4 m;
576             ssgGetModelviewMatrix(m);
577             FGTileMgr::set_tile_filter( true );
578             sgSetModelFilter( true );
579             globals->get_scenery()->get_scene_graph()->cull(&f, m, true);
580             counter = 0;
581         }
582
583         // change state for lighting here
584
585         // draw runway lighting
586         glFogf (GL_FOG_DENSITY, rwy_exp2_punch_through);
587         ssgSetNearFar( scene_nearplane, scene_farplane );
588
589         if ( enhanced_lighting ) {
590
591             // Enable states for drawing points with GL_extension
592             glEnable(GL_POINT_SMOOTH);
593
594             if ( distance_attenuation && glPointParameterIsSupported )
595             {
596                 // Enable states for drawing points with GL_extension
597                 glEnable(GL_POINT_SMOOTH);
598
599                 float quadratic[3] = {1.0, 0.001, 0.0000001};
600                 // makes the points fade as they move away
601                 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT, quadratic);
602                 glPointParameterfPtr(GL_POINT_SIZE_MIN_EXT, 1.0); 
603             }
604
605             glPointSize(4.0);
606
607             // blending function for runway lights
608             glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ;
609         }
610
611         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
612         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
613         glEnable(GL_TEXTURE_GEN_S);
614         glEnable(GL_TEXTURE_GEN_T);
615         glPolygonMode(GL_FRONT, GL_POINT);
616
617         // draw runway lighting
618         if ( draw_otw ) {
619             ssgCullAndDraw( globals->get_scenery()->get_vasi_lights_root() );
620             ssgCullAndDraw( globals->get_scenery()->get_rwy_lights_root() );
621         }
622
623         // change punch through and then draw taxi lighting
624         glFogf ( GL_FOG_DENSITY, fog_exp2_density );
625         // sgVec3 taxi_fog;
626         // sgSetVec3( taxi_fog, 0.0, 0.0, 0.0 );
627         // glFogfv ( GL_FOG_COLOR, taxi_fog );
628         if ( draw_otw ) {
629             ssgCullAndDraw( globals->get_scenery()->get_taxi_lights_root() );
630         }
631
632         // clean up lighting
633         glPolygonMode(GL_FRONT, GL_FILL);
634         glDisable(GL_TEXTURE_GEN_S);
635         glDisable(GL_TEXTURE_GEN_T);
636
637         //static int _frame_count = 0;
638         //if (_frame_count % 30 == 0) {
639         //  printf("SSG: %s\n", ssgShowStats());
640         //}
641         //else {
642         //  ssgShowStats();
643         //}
644         //_frame_count++;
645
646
647         if ( enhanced_lighting ) {
648             if ( distance_attenuation && glPointParameterIsSupported ) {
649                 glPointParameterfvPtr(GL_DISTANCE_ATTENUATION_EXT,
650                                       default_attenuation);
651             }
652
653             glPointSize(1.0);
654             glDisable(GL_POINT_SMOOTH);
655         }
656
657         // draw ground lighting
658         glFogf (GL_FOG_DENSITY, ground_exp2_punch_through);
659         if ( draw_otw ) {
660             ssgCullAndDraw( globals->get_scenery()->get_gnd_lights_root() );
661         }
662
663         if ( draw_otw && fgGetBool("/sim/rendering/clouds3d") ) {
664             glDisable( GL_FOG );
665             glDisable( GL_LIGHTING );
666             // cout << "drawing new clouds" << endl;
667
668             glEnable(GL_DEPTH_TEST);
669             glEnable(GL_BLEND);
670             glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ) ;
671
672             /*
673             glEnable( GL_TEXTURE_2D );
674             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
675             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
676             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
677             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
678             */
679
680 #ifdef FG_USE_CLOUDS_3D
681             sgClouds3d->Draw((sgVec4 *)current__view->get_VIEW());
682 #endif
683             glEnable( GL_FOG );
684             glEnable( GL_LIGHTING );
685             glEnable( GL_DEPTH_TEST );
686             glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
687         }
688
689         if ( draw_otw && draw_clouds ) {
690             if ( multi_pass_clouds ) {
691                 // Disable depth buffer update, draw the clouds where the
692                 //  objects overwrite the already drawn clouds, by testing
693                 //  the stencil buffer against 1
694                 glDepthMask( GL_FALSE );
695                 glStencilFunc( GL_EQUAL, 1, 1 );
696                 glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
697                 thesky->drawUpperClouds();
698                 thesky->drawLowerClouds();
699                 glDepthMask( GL_TRUE );
700                 glDisable( GL_STENCIL_TEST );
701             } else {
702                 glDepthMask( GL_FALSE );
703                 thesky->drawLowerClouds();
704                 glDepthMask( GL_TRUE );
705             }
706         }
707
708         if ( draw_otw ) {
709             FGTileMgr::set_tile_filter( false );
710             sgSetModelFilter( false );
711             globals->get_aircraft_model()->select( true );
712             globals->get_model_mgr()->draw();
713             globals->get_aircraft_model()->draw();
714             // If the view is internal, the previous line draw the 
715             //  cockpit with modified near/far clip planes and deselect
716             //  the aircraft in the global scenegraph
717             // Otherwise, it just enables the aircraft: The scenegraph
718             //  must be drawn again to see the plane.
719             ssgCullAndDraw( globals->get_scenery()->get_scene_graph() );
720             FGTileMgr::set_tile_filter( true );
721             sgSetModelFilter( true );
722             globals->get_aircraft_model()->select( true );
723         }
724
725         // display HUD && Panel
726         glDisable( GL_FOG );
727         glDisable( GL_DEPTH_TEST );
728         // glDisable( GL_CULL_FACE );
729         // glDisable( GL_TEXTURE_2D );
730
731         // update the controls subsystem
732         globals->get_controls()->update(delta_time_sec);
733
734         hud_and_panel->apply();
735         fgCockpitUpdate();
736
737         // Use the hud_and_panel ssgSimpleState for rendering the ATC output
738         // This only works properly if called before the panel call
739         if((fgGetBool("/sim/ATC/enabled")) || (fgGetBool("/sim/ai-traffic/enabled")))
740             globals->get_ATC_display()->update(delta_time_sec);
741
742         // update the panel subsystem
743         if ( globals->get_current_panel() != NULL ) {
744             globals->get_current_panel()->update(delta_time_sec);
745         }
746         fgUpdate3DPanels();
747
748         // We can do translucent menus, so why not. :-)
749         menus->apply();
750         glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
751         puDisplay();
752         // glDisable ( GL_BLEND ) ;
753
754         glEnable( GL_DEPTH_TEST );
755         glEnable( GL_FOG );
756
757         // Fade out the splash screen over the first three seconds.
758         double t = globals->get_sim_time_sec();
759         if ( t <= 1.0 ) {
760             fgSplashUpdate(0.0, 1.0);
761         } else if ( t <= 3.0) {
762             fgSplashUpdate(0.0, (3.0 - t) / 2.0);
763         }
764     }
765 }
766
767
768
769 // options.cxx needs to see this for toggle_panel()
770 // Handle new window size or exposure
771 void
772 FGRenderer::resize( int width, int height ) {
773     int view_h;
774
775     if ( (!fgGetBool("/sim/virtual-cockpit"))
776          && fgPanelVisible() && idle_state == 1000 ) {
777         view_h = (int)(height * (globals->get_current_panel()->getViewHeight() -
778                              globals->get_current_panel()->getYOffset()) / 768.0);
779     } else {
780         view_h = height;
781     }
782
783     glViewport( 0, (GLint)(height - view_h), (GLint)(width), (GLint)(view_h) );
784
785     fgSetInt("/sim/startup/xsize", width);
786     fgSetInt("/sim/startup/ysize", height);
787     guiInitMouse(width, height);
788
789     // for all views
790     FGViewMgr *viewmgr = globals->get_viewmgr();
791     if (viewmgr) {
792       for ( int i = 0; i < viewmgr->size(); ++i ) {
793         viewmgr->get_view(i)->
794           set_aspect_ratio((float)view_h / (float)width);
795       }
796
797       ssgSetFOV( viewmgr->get_current_view()->get_h_fov(),
798                  viewmgr->get_current_view()->get_v_fov() );
799
800 #ifdef FG_USE_CLOUDS_3D
801       sgClouds3d->Resize( viewmgr->get_current_view()->get_h_fov(),
802                           viewmgr->get_current_view()->get_v_fov() );
803 #endif
804     }
805
806     fgHUDReshape();
807
808 }
809
810
811 // end of renderer.cxx