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