]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
Update the fog color based on the color of the sun. This is a first attempt and proba...
[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/sky/sky.hxx>
56
57 #include <Aircraft/aircraft.hxx>
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
66 fgLIGHT cur_light_params;
67
68
69 // Constructor
70 fgLIGHT::fgLIGHT( void ) {
71 }
72
73
74 // initialize lighting tables
75 void fgLIGHT::Init( void ) {
76     SG_LOG( SG_EVENT, SG_INFO, 
77             "Initializing Lighting interpolation tables." );
78
79     // build the path name to the ambient lookup table
80     SGPath path( globals->get_fg_root() );
81     SGPath ambient = path;
82     ambient.append( "Lighting/ambient" );
83     SGPath diffuse = path;
84     diffuse.append( "Lighting/diffuse" );
85     SGPath specular = path;
86     specular.append( "Lighting/specular" );
87     SGPath sky = path;
88     sky.append( "Lighting/sky" );
89
90     // initialize ambient table
91     ambient_tbl = new SGInterpTable( ambient.str() );
92
93     // initialize diffuse table
94     diffuse_tbl = new SGInterpTable( diffuse.str() );
95     
96     // initialize diffuse table
97     specular_tbl = new SGInterpTable( specular.str() );
98     
99     // initialize sky table
100     sky_tbl = new SGInterpTable( sky.str() );
101 }
102
103
104 // update lighting parameters based on current sun position
105 void fgLIGHT::Update( void ) {
106     FGInterface *f;
107     // if the 4th field is 0.0, this specifies a direction ...
108     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
109     // base sky color
110 #if defined (sgi) || defined( macintosh )
111     GLfloat base_sky_color[4] = { 0.252, 0.403, 0.657, 1.0 };
112 #else // default
113     GLfloat base_sky_color[4] = { 0.336, 0.406, 0.574, 1.0 };
114 #endif
115     GLfloat base_fog_color[4] = { 0.80, 0.83, 1.0, 1.0 };
116     double deg, ambient, diffuse, specular, sky_brightness;
117
118     f = current_aircraft.fdm_state;
119
120     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
121
122     // calculate lighting parameters based on sun's relative angle to
123     // local up
124
125     // base fog color
126     float *sun_color = thesky->get_sun_color();
127     base_fog_color[0] *= (0.75 + sun_color[0]/4);
128     base_fog_color[1] *= (0.5 + sun_color[1]/2);
129     base_fog_color[2] *= sun_color[2];
130
131
132     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
133     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
134
135     ambient = ambient_tbl->interpolate( deg );
136     diffuse = diffuse_tbl->interpolate( deg );
137     specular = specular_tbl->interpolate( deg );
138     sky_brightness = sky_tbl->interpolate( deg );
139
140     SG_LOG( SG_EVENT, SG_INFO, 
141             "  ambient = " << ambient << "  diffuse = " << diffuse 
142             << "  specular = " << specular << "  sky = " << sky_brightness );
143
144     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
145
146     // if ( ambient < 0.02 ) { ambient = 0.02; }
147     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
148     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
149
150     scene_ambient[0] = white[0] * ambient;
151     scene_ambient[1] = white[1] * ambient;
152     scene_ambient[2] = white[2] * ambient;
153     scene_ambient[3] = 1.0;
154
155     scene_diffuse[0] = white[0] * diffuse;
156     scene_diffuse[1] = white[1] * diffuse;
157     scene_diffuse[2] = white[2] * diffuse;
158     scene_diffuse[3] = 1.0;
159
160     scene_specular[0] = white[0] * specular;
161     scene_specular[1] = white[1] * specular;
162     scene_specular[2] = white[2] * specular;
163     scene_specular[3] = 1.0;
164
165     // set sky color
166     sky_color[0] = base_sky_color[0] * sky_brightness;
167     sky_color[1] = base_sky_color[1] * sky_brightness;
168     sky_color[2] = base_sky_color[2] * sky_brightness;
169     sky_color[3] = base_sky_color[3];
170
171     // set fog color
172     fog_color[0] = base_fog_color[0] * sky_brightness;
173     fog_color[1] = base_fog_color[1] * sky_brightness;
174     fog_color[2] = base_fog_color[2] * sky_brightness;
175     fog_color[3] = base_fog_color[3];
176 }
177
178
179 // calculate fog color adjusted for sunrise/sunset effects
180 void fgLIGHT::UpdateAdjFog( void ) {
181     FGInterface *f;
182     double sun_angle_deg, rotation, param1[3], param2[3];
183
184     f = current_aircraft.fdm_state;
185
186     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
187
188     // set fog color (we'll try to match the sunset color in the
189     // direction we are looking
190
191     // Do some sanity checking ...
192     if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
193         SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
194         exit(-1);
195     }
196     if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
197         SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
198         exit(-1);
199     }
200     if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
201          globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
202         SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = " 
203                 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
204         exit(-1);
205     }
206
207     // first determine the difference between our view angle and local
208     // direction to the sun
209     rotation = -(sun_rotation + SGD_PI) 
210         - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
211     while ( rotation < 0 ) {
212         rotation += SGD_2PI;
213     }
214     while ( rotation > SGD_2PI ) {
215         rotation -= SGD_2PI;
216     }
217     rotation *= SGD_RADIANS_TO_DEGREES;
218     // fgPrintf( SG_EVENT, SG_INFO, 
219     //           "  View to sun difference in degrees = %.2f\n", rotation);
220
221     // next check if we are in a sunset/sunrise situation
222     sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
223     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
224         /* 0.0 - 0.6 */
225         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
226         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
227         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
228         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
229     } else {
230         param1[0] = param1[1] = param1[2] = 0.0;
231     }
232
233     if ( rotation - 180.0 <= 0.0 ) {
234         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
235         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
236         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
237         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
238     } else {
239         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
240         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
241         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
242         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
243     }
244
245     adj_fog_color[0] = fog_color[0] + param2[0];
246     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
247
248     adj_fog_color[1] = fog_color[1] + param2[1];
249     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
250
251     adj_fog_color[2] = fog_color[2] + param2[2];
252     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
253
254     adj_fog_color[3] = fog_color[3];
255 }
256
257
258 // Destructor
259 fgLIGHT::~fgLIGHT( void ) {
260 }
261
262
263
264