2 // light.hxx -- lighting routines
4 // Written by Curtis Olson, started April 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
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>
48 #include "fg_time.hxx"
53 fgLIGHT cur_light_params;
57 fgLIGHT::fgLIGHT( void ) {
61 // initialize lighting tables
62 void fgLIGHT::Init( void ) {
65 fgPrintf( FG_EVENT, FG_INFO,
66 "Initializing Lighting interpolation tables.\n" );
68 // build the path name to the ambient lookup table
69 current_options.get_fg_root(path);
70 strcat(path, "/Scenery/");
71 strcat(path, "Ambient");
72 // initialize ambient table
73 ambient_tbl = new fgINTERPTABLE(path);
75 // build the path name to the diffuse lookup table
76 current_options.get_fg_root(path);
77 strcat(path, "/Scenery/");
78 strcat(path, "Diffuse");
79 // initialize diffuse table
80 diffuse_tbl = new fgINTERPTABLE(path);
82 // build the path name to the sky lookup table
83 current_options.get_fg_root(path);
84 strcat(path, "/Scenery/");
86 // initialize sky table
87 sky_tbl = new fgINTERPTABLE(path);
91 // update lighting parameters based on current sun position
92 void fgLIGHT::Update( void ) {
96 // if the 4th field is 0.0, this specifies a direction ...
97 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
99 GLfloat base_sky_color[4] = {0.60, 0.60, 0.90, 1.0};
101 GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
102 double deg, ambient, diffuse, sky_brightness;
104 f = current_aircraft.flight;
105 t = &cur_time_params;
108 fgPrintf( FG_EVENT, FG_INFO, "Updating light parameters.\n" );
110 // calculate lighting parameters based on sun's relative angle to
113 deg = sun_angle * 180.0 / FG_PI;
114 fgPrintf( FG_EVENT, FG_INFO, " Sun angle = %.2f.\n", deg );
116 ambient = ambient_tbl->interpolate( deg );
117 diffuse = diffuse_tbl->interpolate( deg );
118 sky_brightness = sky_tbl->interpolate( deg );
120 fgPrintf( FG_EVENT, FG_INFO,
121 " ambient = %.2f diffuse = %.2f sky = %.2f\n",
122 ambient, diffuse, sky_brightness );
124 // sky_brightness = 0.15; // used to force a dark sky (when testing)
126 // if ( ambient < 0.02 ) { ambient = 0.02; }
127 // if ( diffuse < 0.0 ) { diffuse = 0.0; }
128 // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
130 scene_ambient[0] = white[0] * ambient;
131 scene_ambient[1] = white[1] * ambient;
132 scene_ambient[2] = white[2] * ambient;
134 scene_diffuse[0] = white[0] * diffuse;
135 scene_diffuse[1] = white[1] * diffuse;
136 scene_diffuse[2] = white[2] * diffuse;
139 sky_color[0] = base_sky_color[0] * sky_brightness;
140 sky_color[1] = base_sky_color[1] * sky_brightness;
141 sky_color[2] = base_sky_color[2] * sky_brightness;
142 sky_color[3] = base_sky_color[3];
145 fog_color[0] = base_fog_color[0] * sky_brightness;
146 fog_color[1] = base_fog_color[1] * sky_brightness;
147 fog_color[2] = base_fog_color[2] * sky_brightness;
148 fog_color[3] = base_fog_color[3];
152 // calculate fog color adjusted for sunrise/sunset effects
153 void fgLIGHT::UpdateAdjFog( void ) {
156 double sun_angle_deg, rotation, param1[3], param2[3];
158 f = current_aircraft.flight;
161 fgPrintf( FG_EVENT, FG_DEBUG, "Updating adjusted fog parameters.\n" );
163 // set fog color (we'll try to match the sunset color in the
164 // direction we are looking
166 // first determine the difference between our view angle and local
167 // direction to the sun
168 rotation = -(sun_rotation + FG_PI) - (FG_Psi - v->view_offset) ;
169 while ( rotation < 0 ) {
172 while ( rotation > FG_2PI ) {
175 rotation *= RAD_TO_DEG;
176 // fgPrintf( FG_EVENT, FG_INFO,
177 // " View to sun difference in degrees = %.2f\n", rotation);
179 // next check if we are in a sunset/sunrise situation
180 sun_angle_deg = sun_angle * RAD_TO_DEG;
181 if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
183 param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 10.0;
184 param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 50.0;
187 param1[0] = param1[1] = param1[2] = 0.0;
190 if ( rotation - 180.0 <= 0.0 ) {
191 param2[0] = param1[0] * (180.0 - rotation) / 180.0;
192 param2[1] = param1[1] * (180.0 - rotation) / 180.0;
193 param2[2] = param1[2] * (180.0 - rotation) / 180.0;
194 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
196 param2[0] = param1[0] * (rotation - 180.0) / 180.0;
197 param2[1] = param1[1] * (rotation - 180.0) / 180.0;
198 param2[2] = param1[2] * (rotation - 180.0) / 180.0;
199 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
202 adj_fog_color[0] = fog_color[0] + param2[0];
203 if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
205 adj_fog_color[1] = fog_color[1] + param2[1];
206 if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
208 adj_fog_color[2] = fog_color[2] + param2[2];
209 if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
211 adj_fog_color[3] = fog_color[3];
216 fgLIGHT::~fgLIGHT( void ) {
220 // wrapper function for updating light parameters via the event scheduler
221 void fgLightUpdate ( void ) {
223 l = &cur_light_params;
230 // Revision 1.14 1998/08/06 12:47:22 curt
231 // Adjusted dusk/dawn lighting ...
233 // Revision 1.13 1998/07/24 21:42:26 curt
234 // Output message tweaks.
236 // Revision 1.12 1998/07/22 21:45:38 curt
237 // fg_time.cxx: Removed call to ctime() in a printf() which should be harmless
238 // but seems to be triggering a bug.
239 // light.cxx: Added code to adjust fog color based on sunrise/sunset effects
240 // and view orientation. This is an attempt to match the fog color to the
241 // sky color in the center of the screen. You see discrepancies at the
242 // edges, but what else can be done?
243 // sunpos.cxx: Caculate local direction to sun here. (what compass direction
244 // do we need to face to point directly at sun)
246 // Revision 1.11 1998/07/13 21:02:08 curt
247 // Wrote access functions for current fgOPTIONS.
249 // Revision 1.10 1998/07/08 14:48:38 curt
250 // polar3d.h renamed to polar3d.hxx
252 // Revision 1.9 1998/05/29 20:37:51 curt
253 // Renamed <Table>.table to be <Table> so we can add a .gz under DOS.
255 // Revision 1.8 1998/05/20 20:54:16 curt
256 // Converted fgLIGHT to a C++ class.
258 // Revision 1.7 1998/05/13 18:26:50 curt
259 // Root path info moved to fgOPTIONS.
261 // Revision 1.6 1998/05/11 18:18:51 curt
262 // Made fog color slightly bluish.
264 // Revision 1.5 1998/05/03 00:48:38 curt
265 // polar.h -> polar3d.h
267 // Revision 1.4 1998/04/28 01:22:18 curt
268 // Type-ified fgTIME and fgVIEW.
270 // Revision 1.3 1998/04/26 05:10:04 curt
271 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
273 // Revision 1.2 1998/04/24 00:52:30 curt
274 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
276 // Separated out lighting calcs into their own file.
278 // Revision 1.1 1998/04/22 13:24:06 curt
279 // C++ - ifiing the code a bit.
280 // Starting to reorginize some of the lighting calcs to use a table lookup.