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 "Include/compiler.h"
38 #ifdef FG_MATH_EXCEPTION_CLASH
39 # define exception c_exception
42 #ifdef FG_HAVE_STD_INCLUDES
51 #include <Aircraft/aircraft.hxx>
52 #include <Debug/logstream.hxx>
53 #include <Include/fg_constants.h>
54 #include <Main/options.hxx>
55 #include <Main/views.hxx>
56 #include <Math/fg_geodesy.hxx>
57 #include <Math/interpolater.hxx>
58 #include <Math/mat3.h>
59 #include <Math/polar3d.hxx>
61 #include "fg_time.hxx"
66 fgLIGHT cur_light_params;
70 fgLIGHT::fgLIGHT( void ) {
74 // initialize lighting tables
75 void fgLIGHT::Init( void ) {
76 FG_LOG( FG_EVENT, FG_INFO,
77 "Initializing Lighting interpolation tables." );
79 // build the path name to the ambient lookup table
80 string path = current_options.get_fg_root();
81 string ambient = path + "/Lighting/ambient";
82 string diffuse = path + "/Lighting/diffuse";
83 string sky = path + "/Lighting/sky";
85 // initialize ambient table
86 ambient_tbl = new fgINTERPTABLE( ambient );
88 // initialize diffuse table
89 diffuse_tbl = new fgINTERPTABLE( diffuse );
91 // initialize sky table
92 sky_tbl = new fgINTERPTABLE( sky );
96 // update lighting parameters based on current sun position
97 void fgLIGHT::Update( void ) {
100 // if the 4th field is 0.0, this specifies a direction ...
101 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
103 GLfloat base_sky_color[4] = {0.60, 0.60, 0.90, 1.0};
105 GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
106 double deg, ambient, diffuse, sky_brightness;
108 f = current_aircraft.fdm_state;
109 t = &cur_time_params;
111 FG_LOG( FG_EVENT, FG_INFO, "Updating light parameters." );
113 // calculate lighting parameters based on sun's relative angle to
116 deg = sun_angle * 180.0 / FG_PI;
117 FG_LOG( FG_EVENT, FG_INFO, " Sun angle = " << deg );
119 ambient = ambient_tbl->interpolate( deg );
120 diffuse = diffuse_tbl->interpolate( deg );
121 sky_brightness = sky_tbl->interpolate( deg );
123 FG_LOG( FG_EVENT, FG_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;
137 scene_diffuse[0] = white[0] * diffuse;
138 scene_diffuse[1] = white[1] * diffuse;
139 scene_diffuse[2] = white[2] * diffuse;
142 sky_color[0] = base_sky_color[0] * sky_brightness;
143 sky_color[1] = base_sky_color[1] * sky_brightness;
144 sky_color[2] = base_sky_color[2] * sky_brightness;
145 sky_color[3] = base_sky_color[3];
148 fog_color[0] = base_fog_color[0] * sky_brightness;
149 fog_color[1] = base_fog_color[1] * sky_brightness;
150 fog_color[2] = base_fog_color[2] * sky_brightness;
151 fog_color[3] = base_fog_color[3];
155 // calculate fog color adjusted for sunrise/sunset effects
156 void fgLIGHT::UpdateAdjFog( void ) {
158 double sun_angle_deg, rotation, param1[3], param2[3];
160 f = current_aircraft.fdm_state;
162 FG_LOG( FG_EVENT, FG_DEBUG, "Updating adjusted fog parameters." );
164 // set fog color (we'll try to match the sunset color in the
165 // direction we are looking
167 // first determine the difference between our view angle and local
168 // direction to the sun
169 rotation = -(sun_rotation + FG_PI)
170 - (f->get_Psi() - current_view.get_view_offset());
171 while ( rotation < 0 ) {
174 while ( rotation > FG_2PI ) {
177 rotation *= RAD_TO_DEG;
178 // fgPrintf( FG_EVENT, FG_INFO,
179 // " View to sun difference in degrees = %.2f\n", rotation);
181 // next check if we are in a sunset/sunrise situation
182 sun_angle_deg = sun_angle * RAD_TO_DEG;
183 if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
185 param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
186 param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
187 param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
188 // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
190 param1[0] = param1[1] = param1[2] = 0.0;
193 if ( rotation - 180.0 <= 0.0 ) {
194 param2[0] = param1[0] * (180.0 - rotation) / 180.0;
195 param2[1] = param1[1] * (180.0 - rotation) / 180.0;
196 param2[2] = param1[2] * (180.0 - rotation) / 180.0;
197 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
199 param2[0] = param1[0] * (rotation - 180.0) / 180.0;
200 param2[1] = param1[1] * (rotation - 180.0) / 180.0;
201 param2[2] = param1[2] * (rotation - 180.0) / 180.0;
202 // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
205 adj_fog_color[0] = fog_color[0] + param2[0];
206 if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
208 adj_fog_color[1] = fog_color[1] + param2[1];
209 if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
211 adj_fog_color[2] = fog_color[2] + param2[2];
212 if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
214 adj_fog_color[3] = fog_color[3];
219 fgLIGHT::~fgLIGHT( void ) {