]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
Make the sky colour brighter, change the fog colour accordingly and add support for...
[flightgear.git] / src / Time / light.cxx
1 //
2 // light.hxx -- lighting routines
3 //
4 // Written by Curtis Olson, started April 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include GLUT_H
34
35 #include <simgear/compiler.h>
36
37 #ifdef SG_MATH_EXCEPTION_CLASH
38 #  define exception c_exception
39 #endif
40
41 #ifdef SG_HAVE_STD_INCLUDES
42 #  include <cmath>
43 #else
44 #  include <math.h>
45 #endif
46
47 #include <string>
48 SG_USING_STD(string);
49
50 #include <simgear/constants.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/math/interpolater.hxx>
53 #include <simgear/math/polar3d.hxx>
54 #include <simgear/misc/sg_path.hxx>
55
56 #include <Aircraft/aircraft.hxx>
57 #include <Main/globals.hxx>
58 #include <Main/viewer.hxx>
59
60 #include "light.hxx"
61 #include "sunpos.hxx"
62
63
64 fgLIGHT cur_light_params;
65
66
67 // Constructor
68 fgLIGHT::fgLIGHT( void ) {
69 }
70
71
72 // initialize lighting tables
73 void fgLIGHT::Init( void ) {
74     SG_LOG( SG_EVENT, SG_INFO, 
75             "Initializing Lighting interpolation tables." );
76
77     // build the path name to the ambient lookup table
78     SGPath path( globals->get_fg_root() );
79     SGPath ambient = path;
80     ambient.append( "Lighting/ambient" );
81     SGPath diffuse = path;
82     diffuse.append( "Lighting/diffuse" );
83     SGPath specular = path;
84     specular.append( "Lighting/specular" );
85     SGPath sky = path;
86     sky.append( "Lighting/sky" );
87
88     // initialize ambient table
89     ambient_tbl = new SGInterpTable( ambient.str() );
90
91     // initialize diffuse table
92     diffuse_tbl = new SGInterpTable( diffuse.str() );
93     
94     // initialize diffuse table
95     specular_tbl = new SGInterpTable( specular.str() );
96     
97     // initialize sky table
98     sky_tbl = new SGInterpTable( sky.str() );
99 }
100
101
102 // update lighting parameters based on current sun position
103 void fgLIGHT::Update( void ) {
104     FGInterface *f;
105     // if the 4th field is 0.0, this specifies a direction ...
106     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
107     // base sky color
108 #if defined (sgi) || defined( macintosh )
109     GLfloat base_sky_color[4] = { 0.252, 0.403, 0.657, 1.0 };
110 #else // default
111     GLfloat base_sky_color[4] = { 0.336, 0.406, 0.574, 1.0 };
112 #endif
113     // base fog color
114     GLfloat base_fog_color[4] = { 0.90, 0.90, 1.00, 1.0 };
115     double deg, ambient, diffuse, specular, sky_brightness;
116
117     f = current_aircraft.fdm_state;
118
119     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
120
121     // calculate lighting parameters based on sun's relative angle to
122     // local up
123
124     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
125     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
126
127     ambient = ambient_tbl->interpolate( deg );
128     diffuse = diffuse_tbl->interpolate( deg );
129     specular = specular_tbl->interpolate( deg );
130     sky_brightness = sky_tbl->interpolate( deg );
131
132     SG_LOG( SG_EVENT, SG_INFO, 
133             "  ambient = " << ambient << "  diffuse = " << diffuse 
134             << "  specular = " << specular << "  sky = " << sky_brightness );
135
136     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
137
138     // if ( ambient < 0.02 ) { ambient = 0.02; }
139     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
140     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
141
142     scene_ambient[0] = white[0] * ambient;
143     scene_ambient[1] = white[1] * ambient;
144     scene_ambient[2] = white[2] * ambient;
145     scene_ambient[3] = 1.0;
146
147     scene_diffuse[0] = white[0] * diffuse;
148     scene_diffuse[1] = white[1] * diffuse;
149     scene_diffuse[2] = white[2] * diffuse;
150     scene_diffuse[3] = 1.0;
151
152     scene_specular[0] = white[0] * specular;
153     scene_specular[1] = white[1] * specular;
154     scene_specular[2] = white[2] * specular;
155     scene_specular[3] = 1.0;
156
157     // set sky color
158     sky_color[0] = base_sky_color[0] * sky_brightness;
159     sky_color[1] = base_sky_color[1] * sky_brightness;
160     sky_color[2] = base_sky_color[2] * sky_brightness;
161     sky_color[3] = base_sky_color[3];
162
163     // set fog color
164     fog_color[0] = base_fog_color[0] * sky_brightness;
165     fog_color[1] = base_fog_color[1] * sky_brightness;
166     fog_color[2] = base_fog_color[2] * sky_brightness;
167     fog_color[3] = base_fog_color[3];
168 }
169
170
171 // calculate fog color adjusted for sunrise/sunset effects
172 void fgLIGHT::UpdateAdjFog( void ) {
173     FGInterface *f;
174     double sun_angle_deg, rotation, param1[3], param2[3];
175
176     f = current_aircraft.fdm_state;
177
178     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
179
180     // set fog color (we'll try to match the sunset color in the
181     // direction we are looking
182
183     // Do some sanity checking ...
184     if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
185         SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
186         exit(-1);
187     }
188     if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
189         SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
190         exit(-1);
191     }
192     if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
193          globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
194         SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = " 
195                 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
196         exit(-1);
197     }
198
199     // first determine the difference between our view angle and local
200     // direction to the sun
201     rotation = -(sun_rotation + SGD_PI) 
202         - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
203     while ( rotation < 0 ) {
204         rotation += SGD_2PI;
205     }
206     while ( rotation > SGD_2PI ) {
207         rotation -= SGD_2PI;
208     }
209     rotation *= SGD_RADIANS_TO_DEGREES;
210     // fgPrintf( SG_EVENT, SG_INFO, 
211     //           "  View to sun difference in degrees = %.2f\n", rotation);
212
213     // next check if we are in a sunset/sunrise situation
214     sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
215     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
216         /* 0.0 - 0.6 */
217         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
218         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
219         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
220         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
221     } else {
222         param1[0] = param1[1] = param1[2] = 0.0;
223     }
224
225     if ( rotation - 180.0 <= 0.0 ) {
226         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
227         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
228         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
229         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
230     } else {
231         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
232         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
233         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
234         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
235     }
236
237     adj_fog_color[0] = fog_color[0] + param2[0];
238     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
239
240     adj_fog_color[1] = fog_color[1] + param2[1];
241     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
242
243     adj_fog_color[2] = fog_color[2] + param2[2];
244     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
245
246     adj_fog_color[3] = fog_color[3];
247 }
248
249
250 // Destructor
251 fgLIGHT::~fgLIGHT( void ) {
252 }
253
254
255
256