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.
36 #include <simgear/compiler.h>
38 #ifdef SG_MATH_EXCEPTION_CLASH
39 # define exception c_exception
42 #ifdef SG_HAVE_STD_INCLUDES
51 #include <simgear/constants.h>
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/math/interpolater.hxx>
54 #include <simgear/math/polar3d.hxx>
55 #include <simgear/misc/sg_path.hxx>
57 #include <Aircraft/aircraft.hxx>
58 #include <Main/globals.hxx>
59 #include <Main/viewer.hxx>
65 fgLIGHT cur_light_params;
69 fgLIGHT::fgLIGHT( void ) {
73 // initialize lighting tables
74 void fgLIGHT::Init( void ) {
75 SG_LOG( SG_EVENT, SG_INFO,
76 "Initializing Lighting interpolation tables." );
78 // build the path name to the ambient lookup table
79 SGPath path( globals->get_fg_root() );
80 SGPath ambient = path;
81 ambient.append( "Lighting/ambient" );
82 SGPath diffuse = path;
83 diffuse.append( "Lighting/diffuse" );
85 sky.append( "Lighting/sky" );
87 // initialize ambient table
88 ambient_tbl = new SGInterpTable( ambient.str() );
90 // initialize diffuse table
91 diffuse_tbl = new SGInterpTable( diffuse.str() );
93 // initialize sky table
94 sky_tbl = new SGInterpTable( sky.str() );
98 // update lighting parameters based on current sun position
99 void fgLIGHT::Update( void ) {
101 // if the 4th field is 0.0, this specifies a direction ...
102 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
104 GLfloat base_sky_color[4] = {0.60, 0.60, 0.90, 1.0};
106 GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
107 double deg, ambient, diffuse, sky_brightness;
109 f = current_aircraft.fdm_state;
111 SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
113 // calculate lighting parameters based on sun's relative angle to
116 deg = sun_angle * SGD_RADIANS_TO_DEGREES;
117 SG_LOG( SG_EVENT, SG_INFO, " Sun angle = " << deg );
119 ambient = ambient_tbl->interpolate( deg );
120 diffuse = diffuse_tbl->interpolate( deg );
121 sky_brightness = sky_tbl->interpolate( deg );
123 SG_LOG( SG_EVENT, SG_INFO,
124 " ambient = " << ambient << " diffuse = " << diffuse
125 << " sky = " << sky_brightness );
127 // sky_brightness = 0.15; // used to force a dark sky (when testing)
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; }
133 scene_ambient[0] = white[0] * ambient;
134 scene_ambient[1] = white[1] * ambient;
135 scene_ambient[2] = white[2] * ambient;
136 scene_ambient[3] = 1.0;
138 scene_diffuse[0] = white[0] * diffuse;
139 scene_diffuse[1] = white[1] * diffuse;
140 scene_diffuse[2] = white[2] * diffuse;
141 scene_diffuse[3] = 1.0;
144 sky_color[0] = base_sky_color[0] * sky_brightness;
145 sky_color[1] = base_sky_color[1] * sky_brightness;
146 sky_color[2] = base_sky_color[2] * sky_brightness;
147 sky_color[3] = base_sky_color[3];
150 fog_color[0] = base_fog_color[0] * sky_brightness;
151 fog_color[1] = base_fog_color[1] * sky_brightness;
152 fog_color[2] = base_fog_color[2] * sky_brightness;
153 fog_color[3] = base_fog_color[3];
157 // calculate fog color adjusted for sunrise/sunset effects
158 void fgLIGHT::UpdateAdjFog( void ) {
160 double sun_angle_deg, rotation, param1[3], param2[3];
162 f = current_aircraft.fdm_state;
164 SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
166 // set fog color (we'll try to match the sunset color in the
167 // direction we are looking
169 // Do some sanity checking ...
170 if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
171 SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
174 if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
175 SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
178 if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
179 globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
180 SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = "
181 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
185 // first determine the difference between our view angle and local
186 // direction to the sun
187 rotation = -(sun_rotation + SGD_PI)
188 - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
189 while ( rotation < 0 ) {
192 while ( rotation > SGD_2PI ) {
195 rotation *= SGD_RADIANS_TO_DEGREES;
196 // fgPrintf( SG_EVENT, SG_INFO,
197 // " View to sun difference in degrees = %.2f\n", rotation);
199 // next check if we are in a sunset/sunrise situation
200 sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
201 if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
203 param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
204 param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
205 param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
206 // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
208 param1[0] = param1[1] = param1[2] = 0.0;
211 if ( rotation - 180.0 <= 0.0 ) {
212 param2[0] = param1[0] * (180.0 - rotation) / 180.0;
213 param2[1] = param1[1] * (180.0 - rotation) / 180.0;
214 param2[2] = param1[2] * (180.0 - rotation) / 180.0;
215 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
217 param2[0] = param1[0] * (rotation - 180.0) / 180.0;
218 param2[1] = param1[1] * (rotation - 180.0) / 180.0;
219 param2[2] = param1[2] * (rotation - 180.0) / 180.0;
220 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
223 adj_fog_color[0] = fog_color[0] + param2[0];
224 if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
226 adj_fog_color[1] = fog_color[1] + param2[1];
227 if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
229 adj_fog_color[2] = fog_color[2] + param2[2];
230 if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
232 adj_fog_color[3] = fog_color[3];
237 fgLIGHT::~fgLIGHT( void ) {