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