]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
Add gamma correction to the sky color functions
[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 #include <simgear/screen/colors.hxx>
56 #include <simgear/sky/sky.hxx>
57
58 #include <Aircraft/aircraft.hxx>
59 #include <Main/globals.hxx>
60 #include <Main/viewer.hxx>
61
62 #include "light.hxx"
63 #include "sunpos.hxx"
64
65 extern SGSky *thesky;           // FIXME: from main.cxx
66
67 fgLIGHT cur_light_params;
68
69
70 // Constructor
71 fgLIGHT::fgLIGHT( void ) {
72 }
73
74
75 // initialize lighting tables
76 void fgLIGHT::Init( void ) {
77     SG_LOG( SG_EVENT, SG_INFO, 
78             "Initializing Lighting interpolation tables." );
79
80     // build the path name to the ambient lookup table
81     SGPath path( globals->get_fg_root() );
82     SGPath ambient = path;
83     ambient.append( "Lighting/ambient" );
84     SGPath diffuse = path;
85     diffuse.append( "Lighting/diffuse" );
86     SGPath specular = path;
87     specular.append( "Lighting/specular" );
88     SGPath sky = path;
89     sky.append( "Lighting/sky" );
90
91     // initialize ambient table
92     ambient_tbl = new SGInterpTable( ambient.str() );
93
94     // initialize diffuse table
95     diffuse_tbl = new SGInterpTable( diffuse.str() );
96     
97     // initialize diffuse table
98     specular_tbl = new SGInterpTable( specular.str() );
99     
100     // initialize sky table
101     sky_tbl = new SGInterpTable( sky.str() );
102 }
103
104
105 // update lighting parameters based on current sun position
106 void fgLIGHT::Update( void ) {
107     FGInterface *f;
108     // if the 4th field is 0.0, this specifies a direction ...
109     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
110     // base sky color
111     GLfloat base_sky_color[4] = { 0.392, 0.539, 0.752, 1.0 };
112     // base fog color
113     GLfloat base_fog_color[4] = { 0.90, 0.93, 1.0, 1.0 };
114     double deg, ambient, diffuse, specular, sky_brightness;
115
116     f = current_aircraft.fdm_state;
117
118     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
119
120     // first, correct the colors for system specific gamma settings
121     // gamma_correct( (float *)&base_sky_color );
122     // gamma_correct( (float *)&base_fog_color );
123
124     // calculate lighting parameters based on sun's relative angle to
125     // local up
126
127     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
128     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
129
130     ambient = ambient_tbl->interpolate( deg );
131     diffuse = diffuse_tbl->interpolate( deg );
132     specular = specular_tbl->interpolate( deg );
133     sky_brightness = sky_tbl->interpolate( deg );
134
135     SG_LOG( SG_EVENT, SG_INFO, 
136             "  ambient = " << ambient << "  diffuse = " << diffuse 
137             << "  specular = " << specular << "  sky = " << sky_brightness );
138
139     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
140
141     // if ( ambient < 0.02 ) { ambient = 0.02; }
142     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
143     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
144
145     scene_ambient[0] = white[0] * ambient;
146     scene_ambient[1] = white[1] * ambient;
147     scene_ambient[2] = white[2] * ambient;
148     scene_ambient[3] = 1.0;
149     gamma_correct( (float *)&scene_ambient );
150
151     scene_diffuse[0] = white[0] * diffuse;
152     scene_diffuse[1] = white[1] * diffuse;
153     scene_diffuse[2] = white[2] * diffuse;
154     scene_diffuse[3] = 1.0;
155     gamma_correct( (float *)&scene_diffuse );
156
157     scene_specular[0] = white[0] * specular;
158     scene_specular[1] = white[1] * specular;
159     scene_specular[2] = white[2] * specular;
160     scene_specular[3] = 1.0;
161     gamma_correct( (float *)&scene_specular );
162
163     // set sky color
164     sky_color[0] = base_sky_color[0] * sky_brightness;
165     sky_color[1] = base_sky_color[1] * sky_brightness;
166     sky_color[2] = base_sky_color[2] * sky_brightness;
167     sky_color[3] = base_sky_color[3];
168     gamma_correct( (float *)&sky_color );
169
170     // set cloud and fog color
171     cloud_color[0] = fog_color[0] = base_fog_color[0] * sky_brightness;
172     cloud_color[1] = fog_color[1] = base_fog_color[1] * sky_brightness;
173     cloud_color[2] = fog_color[2] = base_fog_color[2] * sky_brightness;
174     cloud_color[3] = fog_color[3] = base_fog_color[3];
175     gamma_correct( (float *)&cloud_color );
176
177     // update the cloud colors for sunrise/sunset effects (darken them)
178     if (sun_angle > 1.0) {
179        float sun2 = pow(sun_angle, 1.4);
180        cloud_color[0] /= sun2;
181        cloud_color[1] /= sun2;
182        cloud_color[2] /= sun2;
183     }
184     gamma_correct( (float *)&cloud_color );
185 }
186
187
188 // calculate fog color adjusted for sunrise/sunset effects
189 void fgLIGHT::UpdateAdjFog( void ) {
190     FGInterface *f;
191
192     f = current_aircraft.fdm_state;
193
194     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
195
196     // set fog color (we'll try to match the sunset color in the
197     // direction we are looking
198
199     // Do some sanity checking ...
200     if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
201         SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
202         exit(-1);
203     }
204     if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
205         SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
206         exit(-1);
207     }
208     if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
209          globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
210         SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = " 
211                 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
212         exit(-1);
213     }
214
215     double rotation;
216
217     // first determine the difference between our view angle and local
218     // direction to the sun
219     rotation = -(sun_rotation + SGD_PI) 
220         - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
221     while ( rotation < 0 ) {
222         rotation += SGD_2PI;
223     }
224     while ( rotation > SGD_2PI ) {
225         rotation -= SGD_2PI;
226     }
227
228 #ifdef USE_OLD_SUNSET_CODE
229
230     double sun_angle_deg, param1[3], param2[3];
231
232     rotation *= SGD_RADIANS_TO_DEGREES;
233     // fgPrintf( SG_EVENT, SG_INFO, 
234     //           "  View to sun difference in degrees = %.2f\n", rotation);
235
236     // next check if we are in a sunset/sunrise situation
237     sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
238     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
239         /* 0.0 - 0.6 */
240         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
241         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
242         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
243         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
244     } else {
245         param1[0] = param1[1] = param1[2] = 0.0;
246     }
247
248     if ( rotation - 180.0 <= 0.0 ) {
249         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
250         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
251         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
252         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
253     } else {
254         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
255         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
256         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
257         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
258     }
259
260     adj_fog_color[0] = fog_color[0] + param2[0];
261     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
262
263     adj_fog_color[1] = fog_color[1] + param2[1];
264     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
265
266     adj_fog_color[2] = fog_color[2] + param2[2];
267     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
268
269     adj_fog_color[3] = fog_color[3];
270
271 #else
272
273     float rf1 = 0.1 + fabs((rotation - SG_PI) / SG_PI) * 0.8;   // 0.1 .. 0.9
274     float rf2 = rf1 * rf1;
275     float rf3 = 1.0 - rf1;
276
277     float *sun_color = thesky->get_sun_color();
278     float s_red =   fog_color[0] * (1.26 - sun_color[0]/4.0);   // 100% red
279     float s_green = fog_color[1] * (0.45 + sun_color[1]/2.0);   //  40% green
280     float s_blue =  fog_color[2] * sun_color[2];                //   0% blue
281
282     float f_brightness = (sun_angle > 1.0) ? sun_angle : 1.0;
283     float f_red =   fog_color[0] / f_brightness;
284     float f_green = fog_color[1] / f_brightness;
285     float f_blue =  (fog_color[2] / f_brightness) * pow(sun_color[2], 1/3);
286
287     adj_fog_color[0] = rf3 * f_red   + rf2 * s_red;
288     adj_fog_color[1] = rf3 * f_green + rf2 * s_green;
289     adj_fog_color[2] = rf3 * f_blue  + rf2 * s_blue;
290
291     gamma_correct( (float *)&adj_fog_color );
292 #endif
293 }
294
295
296 // Destructor
297 fgLIGHT::~fgLIGHT( void ) {
298 }
299
300
301
302