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