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