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.
35 #include <simgear/compiler.h>
37 #ifdef SG_MATH_EXCEPTION_CLASH
38 # define exception c_exception
41 #ifdef SG_HAVE_STD_INCLUDES
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/screen/colors.hxx>
56 #include <simgear/sky/sky.hxx>
58 #include <Aircraft/aircraft.hxx>
59 #include <Main/globals.hxx>
60 #include <Main/viewer.hxx>
65 extern SGSky *thesky; // FIXME: from main.cxx
67 fgLIGHT cur_light_params;
71 fgLIGHT::fgLIGHT( void ) {
75 // initialize lighting tables
76 void fgLIGHT::Init( void ) {
77 SG_LOG( SG_EVENT, SG_INFO,
78 "Initializing Lighting interpolation tables." );
80 // build the path name to the ambient lookup table
81 SGPath path( globals->get_fg_root() );
82 SGPath ambient = path;
83 ambient.append( "Lighting/ambient" );
84 SGPath diffuse = path;
85 diffuse.append( "Lighting/diffuse" );
86 SGPath specular = path;
87 specular.append( "Lighting/specular" );
89 sky.append( "Lighting/sky" );
91 // initialize ambient table
92 ambient_tbl = new SGInterpTable( ambient.str() );
94 // initialize diffuse table
95 diffuse_tbl = new SGInterpTable( diffuse.str() );
97 // initialize diffuse table
98 specular_tbl = new SGInterpTable( specular.str() );
100 // initialize sky table
101 sky_tbl = new SGInterpTable( sky.str() );
105 // update lighting parameters based on current sun position
106 void fgLIGHT::Update( void ) {
108 // if the 4th field is 0.0, this specifies a direction ...
109 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
111 GLfloat base_sky_color[4] = { 0.392, 0.539, 0.752, 1.0 };
113 GLfloat base_fog_color[4] = { 0.90, 0.93, 1.0, 1.0 };
114 double deg, ambient, diffuse, specular, sky_brightness;
116 f = current_aircraft.fdm_state;
118 SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
120 // first, correct the colors for system specific gamma settings
121 // gamma_correct( (float *)&base_sky_color );
122 // gamma_correct( (float *)&base_fog_color );
124 // calculate lighting parameters based on sun's relative angle to
127 deg = sun_angle * SGD_RADIANS_TO_DEGREES;
128 SG_LOG( SG_EVENT, SG_INFO, " Sun angle = " << deg );
130 ambient = ambient_tbl->interpolate( deg );
131 diffuse = diffuse_tbl->interpolate( deg );
132 specular = specular_tbl->interpolate( deg );
133 sky_brightness = sky_tbl->interpolate( deg );
135 SG_LOG( SG_EVENT, SG_INFO,
136 " ambient = " << ambient << " diffuse = " << diffuse
137 << " specular = " << specular << " sky = " << sky_brightness );
139 // sky_brightness = 0.15; // used to force a dark sky (when testing)
141 // if ( ambient < 0.02 ) { ambient = 0.02; }
142 // if ( diffuse < 0.0 ) { diffuse = 0.0; }
143 // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
145 scene_ambient[0] = white[0] * ambient;
146 scene_ambient[1] = white[1] * ambient;
147 scene_ambient[2] = white[2] * ambient;
148 scene_ambient[3] = 1.0;
149 gamma_correct( (float *)&scene_ambient );
151 scene_diffuse[0] = white[0] * diffuse;
152 scene_diffuse[1] = white[1] * diffuse;
153 scene_diffuse[2] = white[2] * diffuse;
154 scene_diffuse[3] = 1.0;
155 gamma_correct( (float *)&scene_diffuse );
157 scene_specular[0] = white[0] * specular;
158 scene_specular[1] = white[1] * specular;
159 scene_specular[2] = white[2] * specular;
160 scene_specular[3] = 1.0;
161 gamma_correct( (float *)&scene_specular );
164 sky_color[0] = base_sky_color[0] * sky_brightness;
165 sky_color[1] = base_sky_color[1] * sky_brightness;
166 sky_color[2] = base_sky_color[2] * sky_brightness;
167 sky_color[3] = base_sky_color[3];
168 gamma_correct( (float *)&sky_color );
170 // set cloud and fog color
171 cloud_color[0] = fog_color[0] = base_fog_color[0] * sky_brightness;
172 cloud_color[1] = fog_color[1] = base_fog_color[1] * sky_brightness;
173 cloud_color[2] = fog_color[2] = base_fog_color[2] * sky_brightness;
174 cloud_color[3] = fog_color[3] = base_fog_color[3];
175 gamma_correct( (float *)&cloud_color );
177 // update the cloud colors for sunrise/sunset effects (darken them)
178 if (sun_angle > 1.0) {
179 float sun2 = pow(sun_angle, 1.4);
180 cloud_color[0] /= sun2;
181 cloud_color[1] /= sun2;
182 cloud_color[2] /= sun2;
184 gamma_correct( (float *)&cloud_color );
188 // calculate fog color adjusted for sunrise/sunset effects
189 void fgLIGHT::UpdateAdjFog( void ) {
192 f = current_aircraft.fdm_state;
194 SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
196 // set fog color (we'll try to match the sunset color in the
197 // direction we are looking
199 // Do some sanity checking ...
200 if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
201 SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
204 if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
205 SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
208 if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
209 globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
210 SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = "
211 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
217 // first determine the difference between our view angle and local
218 // direction to the sun
219 rotation = -(sun_rotation + SGD_PI)
220 - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
221 while ( rotation < 0 ) {
224 while ( rotation > SGD_2PI ) {
228 #ifdef USE_OLD_SUNSET_CODE
230 double sun_angle_deg, param1[3], param2[3];
232 rotation *= SGD_RADIANS_TO_DEGREES;
233 // fgPrintf( SG_EVENT, SG_INFO,
234 // " View to sun difference in degrees = %.2f\n", rotation);
236 // next check if we are in a sunset/sunrise situation
237 sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
238 if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
240 param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
241 param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
242 param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
243 // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
245 param1[0] = param1[1] = param1[2] = 0.0;
248 if ( rotation - 180.0 <= 0.0 ) {
249 param2[0] = param1[0] * (180.0 - rotation) / 180.0;
250 param2[1] = param1[1] * (180.0 - rotation) / 180.0;
251 param2[2] = param1[2] * (180.0 - rotation) / 180.0;
252 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
254 param2[0] = param1[0] * (rotation - 180.0) / 180.0;
255 param2[1] = param1[1] * (rotation - 180.0) / 180.0;
256 param2[2] = param1[2] * (rotation - 180.0) / 180.0;
257 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
260 adj_fog_color[0] = fog_color[0] + param2[0];
261 if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
263 adj_fog_color[1] = fog_color[1] + param2[1];
264 if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
266 adj_fog_color[2] = fog_color[2] + param2[2];
267 if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
269 adj_fog_color[3] = fog_color[3];
273 float rf1 = 0.1 + fabs((rotation - SG_PI) / SG_PI) * 0.8; // 0.1 .. 0.9
274 float rf2 = rf1 * rf1;
275 float rf3 = 1.0 - rf1;
277 float *sun_color = thesky->get_sun_color();
278 float s_red = fog_color[0] * (1.26 - sun_color[0]/4.0); // 100% red
279 float s_green = fog_color[1] * (0.45 + sun_color[1]/2.0); // 40% green
280 float s_blue = fog_color[2] * sun_color[2]; // 0% blue
282 float f_brightness = (sun_angle > 1.0) ? sun_angle : 1.0;
283 float f_red = fog_color[0] / f_brightness;
284 float f_green = fog_color[1] / f_brightness;
285 float f_blue = (fog_color[2] / f_brightness) * pow(sun_color[2], 1/3);
287 adj_fog_color[0] = rf3 * f_red + rf2 * s_red;
288 adj_fog_color[1] = rf3 * f_green + rf2 * s_green;
289 adj_fog_color[2] = rf3 * f_blue + rf2 * s_blue;
291 gamma_correct( (float *)&adj_fog_color );
297 fgLIGHT::~fgLIGHT( void ) {