]> git.mxchange.org Git - flightgear.git/blob - Simulator/Time/light.cxx
Clean ups and reorganizations:
[flightgear.git] / Simulator / 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 <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include "Include/compiler.h"
37
38 #ifdef FG_MATH_EXCEPTION_CLASH
39 #  define exception c_exception
40 #endif
41
42 #ifdef FG_HAVE_STD_INCLUDES
43 #  include <cmath>
44 #else
45 #  include <math.h>
46 #endif
47
48 #include <string>
49 FG_USING_STD(string);
50
51 #include <Aircraft/aircraft.hxx>
52 #include <Debug/logstream.hxx>
53 #include <Include/fg_constants.h>
54 #include <Main/options.hxx>
55 #include <Main/views.hxx>
56 #include <Math/fg_geodesy.hxx>
57 #include <Math/interpolater.hxx>
58 #include <Math/mat3.h>
59 #include <Math/polar3d.hxx>
60
61 #include "fg_time.hxx"
62 #include "light.hxx"
63 #include "sunpos.hxx"
64
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     FG_LOG( FG_EVENT, FG_INFO, 
77             "Initializing Lighting interpolation tables." );
78
79     // build the path name to the ambient lookup table
80     string path = current_options.get_fg_root();
81     string ambient = path + "/Lighting/ambient";
82     string diffuse = path + "/Lighting/diffuse";
83     string sky     = path + "/Lighting/sky";
84
85     // initialize ambient table
86     ambient_tbl = new fgINTERPTABLE( ambient );
87
88     // initialize diffuse table
89     diffuse_tbl = new fgINTERPTABLE( diffuse );
90     
91     // initialize sky table
92     sky_tbl = new fgINTERPTABLE( sky );
93 }
94
95
96 // update lighting parameters based on current sun position
97 void fgLIGHT::Update( void ) {
98     FGInterface *f;
99     fgTIME *t;
100     // if the 4th field is 0.0, this specifies a direction ...
101     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
102     // base sky color
103     GLfloat base_sky_color[4] =        {0.60, 0.60, 0.90, 1.0};
104     // base fog color
105     GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
106     double deg, ambient, diffuse, sky_brightness;
107
108     f = current_aircraft.fdm_state;
109     t = &cur_time_params;
110
111     FG_LOG( FG_EVENT, FG_INFO, "Updating light parameters." );
112
113     // calculate lighting parameters based on sun's relative angle to
114     // local up
115
116     deg = sun_angle * 180.0 / FG_PI;
117     FG_LOG( FG_EVENT, FG_INFO, "  Sun angle = " << deg );
118
119     ambient = ambient_tbl->interpolate( deg );
120     diffuse = diffuse_tbl->interpolate( deg );
121     sky_brightness = sky_tbl->interpolate( deg );
122
123     FG_LOG( FG_EVENT, FG_INFO, 
124             "  ambient = " << ambient << "  diffuse = " << diffuse 
125             << "  sky = " << sky_brightness );
126
127     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
128
129     // if ( ambient < 0.02 ) { ambient = 0.02; }
130     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
131     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
132
133     scene_ambient[0] = white[0] * ambient;
134     scene_ambient[1] = white[1] * ambient;
135     scene_ambient[2] = white[2] * ambient;
136
137     scene_diffuse[0] = white[0] * diffuse;
138     scene_diffuse[1] = white[1] * diffuse;
139     scene_diffuse[2] = white[2] * diffuse;
140
141     // set sky color
142     sky_color[0] = base_sky_color[0] * sky_brightness;
143     sky_color[1] = base_sky_color[1] * sky_brightness;
144     sky_color[2] = base_sky_color[2] * sky_brightness;
145     sky_color[3] = base_sky_color[3];
146
147     // set fog color
148     fog_color[0] = base_fog_color[0] * sky_brightness;
149     fog_color[1] = base_fog_color[1] * sky_brightness;
150     fog_color[2] = base_fog_color[2] * sky_brightness;
151     fog_color[3] = base_fog_color[3];
152 }
153
154
155 // calculate fog color adjusted for sunrise/sunset effects
156 void fgLIGHT::UpdateAdjFog( void ) {
157     FGInterface *f;
158     double sun_angle_deg, rotation, param1[3], param2[3];
159
160     f = current_aircraft.fdm_state;
161
162     FG_LOG( FG_EVENT, FG_DEBUG, "Updating adjusted fog parameters." );
163
164     // set fog color (we'll try to match the sunset color in the
165     // direction we are looking
166
167     // first determine the difference between our view angle and local
168     // direction to the sun
169     rotation = -(sun_rotation + FG_PI) 
170         - (f->get_Psi() - current_view.get_view_offset());
171     while ( rotation < 0 ) {
172         rotation += FG_2PI;
173     }
174     while ( rotation > FG_2PI ) {
175         rotation -= FG_2PI;
176     }
177     rotation *= RAD_TO_DEG;
178     // fgPrintf( FG_EVENT, FG_INFO, 
179     //           "  View to sun difference in degrees = %.2f\n", rotation);
180
181     // next check if we are in a sunset/sunrise situation
182     sun_angle_deg = sun_angle * RAD_TO_DEG;
183     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
184         /* 0.0 - 0.6 */
185         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
186         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
187         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
188         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
189     } else {
190         param1[0] = param1[1] = param1[2] = 0.0;
191     }
192
193     if ( rotation - 180.0 <= 0.0 ) {
194         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
195         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
196         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
197         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
198     } else {
199         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
200         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
201         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
202         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
203     }
204
205     adj_fog_color[0] = fog_color[0] + param2[0];
206     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
207
208     adj_fog_color[1] = fog_color[1] + param2[1];
209     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
210
211     adj_fog_color[2] = fog_color[2] + param2[2];
212     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
213
214     adj_fog_color[3] = fog_color[3];
215 }
216
217
218 // Destructor
219 fgLIGHT::~fgLIGHT( void ) {
220 }
221
222
223 // $Log$
224 // Revision 1.1  1999/04/05 21:32:47  curt
225 // Initial revision
226 //
227 // Revision 1.27  1999/04/05 02:14:40  curt
228 // Fixed a fog coloring bug.
229 //
230 // Revision 1.26  1999/02/05 21:29:20  curt
231 // Modifications to incorporate Jon S. Berndts flight model code.
232 //
233 // Revision 1.25  1999/01/07 20:25:36  curt
234 // Portability changes and updates from Bernie Bright.
235 //
236 // Revision 1.24  1998/12/09 18:50:35  curt
237 // Converted "class fgVIEW" to "class FGView" and updated to make data
238 // members private and make required accessor functions.
239 //
240 // Revision 1.23  1998/12/05 15:54:30  curt
241 // Renamed class fgFLIGHT to class FGState as per request by JSB.
242 //
243 // Revision 1.22  1998/12/03 01:18:42  curt
244 // Converted fgFLIGHT to a class.
245 //
246 // Revision 1.21  1998/11/23 21:49:09  curt
247 // Borland portability tweaks.
248 //
249 // Revision 1.20  1998/11/06 21:18:27  curt
250 // Converted to new logstream debugging facility.  This allows release
251 // builds with no messages at all (and no performance impact) by using
252 // the -DFG_NDEBUG flag.
253 //
254 // Revision 1.19  1998/10/20 18:41:53  curt
255 // Tweaked sunrise/sunset colors.
256 //
257 // Revision 1.18  1998/10/17 01:34:30  curt
258 // C++ ifying ...
259 //
260 // Revision 1.17  1998/08/29 13:11:33  curt
261 // Bernie Bright writes:
262 //   I've created some new classes to enable pointers-to-functions and
263 //   pointers-to-class-methods to be treated like objects.  These objects
264 //   can be registered with fgEVENT_MGR.
265 //
266 //   File "Include/fg_callback.hxx" contains the callback class defns.
267 //
268 //   Modified fgEVENT and fgEVENT_MGR to use the callback classes.  Also
269 //   some minor tweaks to STL usage.
270 //
271 //   Added file "Include/fg_stl_config.h" to deal with STL portability
272 //   issues.  I've added an initial config for egcs (and probably gcc-2.8.x).
273 //   I don't have access to Visual C++ so I've left that for someone else.
274 //   This file is influenced by the stl_config.h file delivered with egcs.
275 //
276 //   Added "Include/auto_ptr.hxx" which contains an implementation of the
277 //   STL auto_ptr class which is not provided in all STL implementations
278 //   and is needed to use the callback classes.
279 //
280 //   Deleted fgLightUpdate() which was just a wrapper to call
281 //   fgLIGHT::Update().
282 //
283 //   Modified fg_init.cxx to register two method callbacks in place of the
284 //   old wrapper functions.
285 //
286 // Revision 1.16  1998/08/27 17:02:11  curt
287 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
288 // - use strings for fg_root and airport_id and added methods to return
289 //   them as strings,
290 // - inlined all access methods,
291 // - made the parsing functions private methods,
292 // - deleted some unused functions.
293 // - propogated some of these changes out a bit further.
294 //
295 // Revision 1.15  1998/08/25 20:53:33  curt
296 // Shuffled $FG_ROOT file layout.
297 //
298 // Revision 1.14  1998/08/06 12:47:22  curt
299 // Adjusted dusk/dawn lighting ...
300 //
301 // Revision 1.13  1998/07/24 21:42:26  curt
302 // Output message tweaks.
303 //
304 // Revision 1.12  1998/07/22 21:45:38  curt
305 // fg_time.cxx: Removed call to ctime() in a printf() which should be harmless
306 //   but seems to be triggering a bug.
307 // light.cxx: Added code to adjust fog color based on sunrise/sunset effects
308 //   and view orientation.  This is an attempt to match the fog color to the
309 //   sky color in the center of the screen.  You see discrepancies at the
310 //   edges, but what else can be done?
311 // sunpos.cxx: Caculate local direction to sun here.  (what compass direction
312 //   do we need to face to point directly at sun)
313 //
314 // Revision 1.11  1998/07/13 21:02:08  curt
315 // Wrote access functions for current fgOPTIONS.
316 //
317 // Revision 1.10  1998/07/08 14:48:38  curt
318 // polar3d.h renamed to polar3d.hxx
319 //
320 // Revision 1.9  1998/05/29 20:37:51  curt
321 // Renamed <Table>.table to be <Table> so we can add a .gz under DOS.
322 //
323 // Revision 1.8  1998/05/20 20:54:16  curt
324 // Converted fgLIGHT to a C++ class.
325 //
326 // Revision 1.7  1998/05/13 18:26:50  curt
327 // Root path info moved to fgOPTIONS.
328 //
329 // Revision 1.6  1998/05/11 18:18:51  curt
330 // Made fog color slightly bluish.
331 //
332 // Revision 1.5  1998/05/03 00:48:38  curt
333 // polar.h -> polar3d.h
334 //
335 // Revision 1.4  1998/04/28 01:22:18  curt
336 // Type-ified fgTIME and fgVIEW.
337 //
338 // Revision 1.3  1998/04/26 05:10:04  curt
339 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
340 //
341 // Revision 1.2  1998/04/24 00:52:30  curt
342 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
343 // Fog color fixes.
344 // Separated out lighting calcs into their own file.
345 //
346 // Revision 1.1  1998/04/22 13:24:06  curt
347 // C++ - ifiing the code a bit.
348 // Starting to reorginize some of the lighting calcs to use a table lookup.
349 //