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