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