Debian Codegraph @

src/Time/light.cxx

raw symbols SCIP
1 //
2 // light.cxx -- lighting routines
3 //
4 // Written by Curtis Olson, started April 1998.
5 //
6 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include <cmath>
32
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/interpolater.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/scene/sky/sky.hxx>
38 #include <simgear/screen/colors.hxx>
39 #include <simgear/timing/sg_time.hxx>
40 #include <simgear/structure/event_mgr.hxx>
41
42 #include <Main/main.hxx>
43 #include <Main/globals.hxx>
44 #include <Main/fg_props.hxx>
45 #include <Viewer/renderer.hxx>
46 #include <Viewer/view.hxx>
47
48 #include "light.hxx"
49 #include "bodysolver.hxx"
50
51 // initialize lighting tables
52 void FGLight::init () {
53 SG_LOG( SG_EVENT, SG_INFO,
54 "Initializing Lighting interpolation tables." );
55
56 // build the path names of the lookup tables
57 SGPath path( globals->get_fg_root() );
58
59 // initialize ambient, diffuse and specular tables
60 SGPath ambient_path = path;
61 ambient_path.append( "Lighting/ambient" );
62 _ambient_tbl = std::make_unique<SGInterpTable>( ambient_path );
63
64 SGPath diffuse_path = path;
65 diffuse_path.append( "Lighting/diffuse" );
66 _diffuse_tbl = std::make_unique<SGInterpTable>( diffuse_path );
67
68 SGPath specular_path = path;
69 specular_path.append( "Lighting/specular" );
70 _specular_tbl = std::make_unique<SGInterpTable>( specular_path );
71
72 // initialize sky table
73 SGPath sky_path = path;
74 sky_path.append( "Lighting/sky" );
75 _sky_tbl = std::make_unique<SGInterpTable>( sky_path );
76
77 // update all solar system body positions of interest
78 globals->get_event_mgr()->addTask("updateObjects",
79 [this](){ this->updateObjects(); }, 0.5 );
80 }
81
82
83 void FGLight::reinit () {
84 _prev_sun_angle = -9999.0;
85 _dt_total = 0;
86
87 _ambient_tbl.reset();
88 _diffuse_tbl.reset();
89 _specular_tbl.reset();
90 _sky_tbl.reset();
91
92 init();
93
94 updateObjects();
95 update_sky_color();
96 update_adj_fog_color();
97 }
98
99 void FGLight::bind () {
100 SGPropertyNode *prop = globals->get_props();
101
102 // Write Only
103 tie(prop,"/rendering/scene/saturation", SGRawValuePointer<float>(&_saturation));
104 tie(prop,"/rendering/scene/scattering", SGRawValuePointer<float>(&_scattering));
105 tie(prop,"/rendering/scene/overcast", SGRawValuePointer<float>(&_overcast));
106
107 _sunAngleRad = prop->getNode("/sim/time/sun-angle-rad", true);
108 _sunAngleRad->setDoubleValue(_sun_angle);
109 _moonAngleRad = prop->getNode("/sim/time/moon-angle-rad", true);
110 _moonAngleRad->setDoubleValue(_moon_angle);
111 _humidity = fgGetNode("/environment/relative-humidity", true);
112
113 // Read Only
114 tie(prop,"/rendering/scene/ambient/red", SGRawValuePointer<float>(&_scene_ambient[0]));
115 tie(prop,"/rendering/scene/ambient/green", SGRawValuePointer<float>(&_scene_ambient[1]));
116 tie(prop,"/rendering/scene/ambient/blue", SGRawValuePointer<float>(&_scene_ambient[2]));
117 tie(prop,"/rendering/scene/diffuse/red", SGRawValuePointer<float>(&_scene_diffuse[0]));
118 tie(prop,"/rendering/scene/diffuse/green", SGRawValuePointer<float>(&_scene_diffuse[1]));
119 tie(prop,"/rendering/scene/diffuse/blue", SGRawValuePointer<float>(&_scene_diffuse[2]));
120 tie(prop,"/rendering/scene/specular/red", SGRawValuePointer<float>(&_scene_specular[0]));
121 tie(prop,"/rendering/scene/specular/green", SGRawValuePointer<float>(&_scene_specular[1]));
122 tie(prop,"/rendering/scene/specular/blue", SGRawValuePointer<float>(&_scene_specular[2]));
123 tie(prop,"/rendering/dome/sun/red", SGRawValuePointer<float>(&_sun_color[0]));
124 tie(prop,"/rendering/dome/sun/green", SGRawValuePointer<float>(&_sun_color[1]));
125 tie(prop,"/rendering/dome/sun/blue", SGRawValuePointer<float>(&_sun_color[2]));
126 tie(prop,"/rendering/dome/sky/red", SGRawValuePointer<float>(&_sky_color[0]));
127 tie(prop,"/rendering/dome/sky/green", SGRawValuePointer<float>(&_sky_color[1]));
128 tie(prop,"/rendering/dome/sky/blue", SGRawValuePointer<float>(&_sky_color[2]));
129 tie(prop,"/rendering/dome/cloud/red", SGRawValuePointer<float>(&_cloud_color[0]));
130 tie(prop,"/rendering/dome/cloud/green", SGRawValuePointer<float>(&_cloud_color[1]));
131 tie(prop,"/rendering/dome/cloud/blue", SGRawValuePointer<float>(&_cloud_color[2]));
132 tie(prop,"/rendering/dome/fog/red", SGRawValuePointer<float>(&_fog_color[0]));
133 tie(prop,"/rendering/dome/fog/green", SGRawValuePointer<float>(&_fog_color[1]));
134 tie(prop,"/rendering/dome/fog/blue", SGRawValuePointer<float>(&_fog_color[2]));
135
136 // Sun vector
137 tie(prop,"/ephemeris/sun/local/x", SGRawValuePointer<float>(&_sun_vec[0]));
138 tie(prop,"/ephemeris/sun/local/y", SGRawValuePointer<float>(&_sun_vec[1]));
139 tie(prop,"/ephemeris/sun/local/z", SGRawValuePointer<float>(&_sun_vec[2]));
140
141 // Moon vector
142 tie(prop,"/ephemeris/moon/local/x", SGRawValuePointer<float>(&_moon_vec[0]));
143 tie(prop,"/ephemeris/moon/local/y", SGRawValuePointer<float>(&_moon_vec[1]));
144 tie(prop,"/ephemeris/moon/local/z", SGRawValuePointer<float>(&_moon_vec[2]));
145
146 // Properties used directly by effects
147 _chromeProps[0] = prop->getNode("/rendering/scene/chrome-light/red", true);
148 _chromeProps[1] = prop->getNode("/rendering/scene/chrome-light/green",
149 true);
150 _chromeProps[2] = prop->getNode("/rendering/scene/chrome-light/blue", true);
151 _chromeProps[3] = prop->getNode("/rendering/scene/chrome-light/alpha",
152 true);
153 for (int i = 0; i < 4; ++i)
154 _chromeProps[i]->setValue(0.0);
155 }
156
157 void FGLight::unbind () {
158 _tiedProperties.Untie();
159
160 for (int i = 0; i < 4; ++i)
161 _chromeProps[i] = SGPropertyNode_ptr();
162 _sunAngleRad = SGPropertyNode_ptr();
163 _moonAngleRad.reset();
164 _humidity = SGPropertyNode_ptr();
165 }
166
167
168 // update lighting parameters based on current sun position
169 void FGLight::update( double dt )
170 {
171 update_adj_fog_color();
172
173 if (_prev_sun_angle != _sun_angle) {
174 _prev_sun_angle = _sun_angle;
175 update_sky_color();
176 }
177 }
178
179 void FGLight::update_sky_color () {
180 const SGVec4f base_sky_color( 0.31, 0.43, 0.69, 1.0 );
181 const SGVec4f base_fog_color( 0.63, 0.72, 0.88, 1.0 );
182
183 // calculate lighting parameters based on sun's relative angle to
184 // local up
185 float av = _humidity->getFloatValue() * 45;
186 float visibility_log = log(av)/11.0;
187 float visibility_inv = (45000.0 - av)/45000.0;
188
189 float deg = _sun_angle * SGD_RADIANS_TO_DEGREES;
190
191 if (_saturation < 0.0) _saturation = 0.0;
192 else if (_saturation > 1.0) _saturation = 1.0;
193 if (_scattering < 0.0) _scattering = 0.0;
194 else if (_scattering > 1.0) _scattering = 1.0;
195 if (_overcast < 0.0) _overcast = 0.0;
196 else if (_overcast > 1.0) _overcast = 1.0;
197
198 float ambient = _ambient_tbl->interpolate( deg ) + visibility_inv/10;
199 float diffuse = _diffuse_tbl->interpolate( deg );
200 float specular = _specular_tbl->interpolate( deg ) * visibility_log;
201 float sky_brightness = _sky_tbl->interpolate( deg );
202
203 ambient *= _saturation;
204 diffuse *= _saturation;
205 specular *= _saturation;
206 sky_brightness *= _saturation;
207
208 // sky_brightness = 0.15; // used to force a dark sky (when testing)
209
210 /** fog color */
211 float sqr_sky_brightness = sky_brightness * sky_brightness * _scattering;
212 _fog_color = base_fog_color * sqr_sky_brightness;
213 _fog_color[3] = base_fog_color[3];
214 gamma_correct_rgb( _fog_color.data() );
215
216 /** sky color */
217 static const SGVec4f one_vec( 1.0f, 1.0f, 1.0f, 1.0f);
218 SGVec4f overcast_color = (one_vec - base_sky_color) * _overcast;
219 _sky_color = (base_sky_color + overcast_color) * sky_brightness;
220 _sky_color[3] = base_sky_color[3];
221 gamma_correct_rgb( _sky_color.data() );
222
223 /** cloud color */
224 _cloud_color = base_fog_color * sky_brightness;
225
226 /** adjust the cloud colors for sunrise/sunset effects (darken them) */
227 if (_sun_angle > 1.0) {
228 float sun2 = 1.0 / sqrt(_sun_angle);
229 _cloud_color *= sun2;
230 }
231 _cloud_color[3] = base_fog_color[3];
232 gamma_correct_rgb( _cloud_color.data() );
233
234 /** ambient light */
235 _scene_ambient = _fog_color * ambient;
236 _scene_ambient[3] = _fog_color[3];
237 gamma_correct_rgb( _scene_ambient.data() );
238
239 /** diffuse light */
240 SGSky* thesky = globals->get_renderer()->getSky();
241 SGVec4f color = thesky->get_scene_color();
242 _scene_diffuse = color * diffuse;
243 _scene_diffuse[3] = color[3];
244 gamma_correct_rgb( _scene_diffuse.data() );
245
246 SGVec4f chrome = _scene_ambient * .4f + _scene_diffuse;
247 chrome[3] = 1.0f;
248 if (chrome != _scene_chrome) {
249 _scene_chrome = chrome;
250 for (int i = 0; i < 4; ++i)
251 _chromeProps[i]->setValue(static_cast<double>(_scene_chrome[i]));
252 }
253
254 /** specular light */
255 _sun_color = thesky->get_sun_color();
256 _scene_specular = _sun_color * specular;
257 _scene_specular[3] = _sun_color[3];
258 gamma_correct_rgb( _scene_specular.data() );
259 }
260
261
262 // calculate fog color adjusted for sunrise/sunset effects
263 void FGLight::update_adj_fog_color () {
264
265 // double pitch = globals->get_current_view()->getPitch_deg()
266 // * SGD_DEGREES_TO_RADIANS;
267 // double pitch_offset = globals->get_current_view()-> getPitchOffset_deg()
268 // * SGD_DEGREES_TO_RADIANS;
269 double heading = globals->get_current_view()->getHeading_deg()
270 * SGD_DEGREES_TO_RADIANS;
271 double heading_offset = globals->get_current_view()->getHeadingOffset_deg()
272 * SGD_DEGREES_TO_RADIANS;
273
274 // set fog color (we'll try to match the sunset color in the
275 // direction we are looking
276
277 // Do some sanity checking ...
278 if ( _sun_rotation < -2.0 * SGD_2PI || _sun_rotation > 2.0 * SGD_2PI ) {
279 SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << _sun_rotation );
280 return;
281 }
282
283 if ( heading < -2.0 * SGD_2PI || heading > 2.0 * SGD_2PI ) {
284 SG_LOG( SG_EVENT, SG_ALERT, "Heading rotation bad = " << heading );
285 return;
286 }
287
288 if ( heading_offset < -2.0 * SGD_2PI || heading_offset > 2.0 * SGD_2PI ) {
289 SG_LOG( SG_EVENT, SG_ALERT, "Heading offset bad = " << heading_offset );
290 return;
291 }
292
293 static float gamma = system_gamma;
294
295 // first determine the difference between our view angle and local
296 // direction to the sun
297 //double vert_rotation = pitch + pitch_offset;
298
299 // revert to unmodified values before using them.
300 //
301 SGSky* thesky = globals->get_renderer()->getSky();
302 SGVec4f color = thesky->get_scene_color();
303
304 gamma_restore_rgb( _fog_color.data(), gamma );
305 gamma_restore_rgb( _sky_color.data(), gamma );
306
307 // Calculate the fog color in the direction of the sun for
308 // sunrise/sunset effects.
309 //
310 _sun_color[0] = color[0]*color[0]*color[0];
311 _sun_color[1] = color[1]*color[1]*color[1];
312 _sun_color[2] = color[2]*color[2];
313
314 // interpolate between the sunrise/sunset color and the color
315 // at the opposite direction of this effect. Take in account
316 // the current visibility.
317 //
318 float av = thesky->get_visibility();
319 if (av > 45000) av = 45000;
320
321 float avf = 0.87 - (45000 - av) / 83333.33;
322 float sif = 0.5 - cos(_sun_angle*2)/2;
323
324 if (sif < 1e-3)
325 sif = 1e-3;
326
327 // determine horizontal angle between current view direction and sun
328 // since _sun_rotation is relative to South, and heading is in the local frame
329 // we need to account for the 180 degrees offset and differing signs
330 // hence the negation and SGD_PI adjustment.
331 double hor_rotation = -_sun_rotation - SGD_PI - heading + heading_offset;
332 if (hor_rotation < 0 )
333 hor_rotation = fmod(hor_rotation, SGD_2PI) + SGD_2PI;
334 else
335 hor_rotation = fmod(hor_rotation, SGD_2PI);
336
337 float rf1 = fabs((hor_rotation - SGD_PI) / SGD_PI); // 0.0 .. 1.0
338 float rf2 = avf * pow(rf1*rf1, 1/sif) * 1.0639 * _saturation * _scattering;
339 float rf3 = 1.0 - rf2;
340
341 gamma = system_gamma * (0.9 - sif*avf);
342 _adj_fog_color = rf3 * _fog_color + rf2 * _sun_color;
343 _adj_fog_color[3] = 0;
344 gamma_correct_rgb( _adj_fog_color.data(), gamma);
345
346 // make sure the colors have their original value before they are being
347 // used by the rest of the program.
348 //
349 gamma_correct_rgb( _fog_color.data(), gamma );
350 gamma_correct_rgb( _sky_color.data(), gamma );
351 }
352
353 // update all solar system bodies of interest
354 void FGLight::updateObjects()
355 {
356 // update the sun position
357 bool sun_not_moon = true;
358 updateBodyPos(sun_not_moon, _sun_lon, _sun_lat,
359 _sun_vec, _sun_vec_inv,
360 _sun_angle, _sunAngleRad,
361 _sun_rotation);
362
363 // update the moon position
364 sun_not_moon = false;
365 updateBodyPos(sun_not_moon, _moon_lon, _moon_gc_lat,
366 _moon_vec, _moon_vec_inv,
367 _moon_angle, _moonAngleRad,
368 _moon_rotation);
369 }
370
371 // update the position of one solar system body
372 void FGLight::updateBodyPos(bool sun_not_moon, double& lon, double& lat,
373 SGVec4f& vec, SGVec4f& vec_inv,
374 double& angle, SGPropertyNode_ptr AngleRad,
375 double& rotation)
376 {
377 SGTime *t = globals->get_time_params();
378
379 // returns lon and lat based on GST
380 fgBodyPositionGST(t->getGst(), lon, lat, sun_not_moon);
381
382 // It might seem that gc_lat needs to be converted to geodetic
383 // latitude here, but it doesn't. The body latitude is the latitude
384 // of the point on the earth where the up vector has the same
385 // angle from geocentric Z as the body direction. But geodetic
386 // latitude is defined as 90 - angle of up vector from Z!
387 SGVec3d bodypos = SGVec3d::fromGeoc(SGGeoc::fromRadM(lon, lat,
388 SGGeodesy::EQURAD));
389
390 // update the body vector
391 vec = SGVec4f(toVec3f(normalize(bodypos)), 0);
392 vec_inv = - vec;
393
394 // calculate the body's relative angle to local up
395 SGQuatd hlOr = SGQuatd::fromLonLat( globals->get_view_position() );
396 SGVec3d world_up = hlOr.backTransform( -SGVec3d::e3() );
397 // cout << "nup = " << nup[0] << "," << nup[1] << ","
398 // << nup[2] << endl;
399 // cout << "nbody = " << nbody[0] << "," << nbody[1] << ","
400 // << nbody[2] << endl;
401
402 SGVec3d nbody = normalize(bodypos);
403 SGVec3d nup = normalize(world_up);
404 angle = acos( dot( nup, nbody ) );
405
406 double signedPI = (angle < 0.0) ? -SGD_PI : SGD_PI;
407 angle = fmod(angle+signedPI, SGD_2PI) - signedPI;
408
409 // Get direction to the body in the local frame.
410 SGVec3d local_vec = hlOr.transform(nbody);
411
412 // Angle from South.
413 // atan2(y,x) returns the angle between the positive X-axis
414 // and the vector with the origin at 0, going through (x,y)
415 // Since the local frame coordinates have x-positive pointing Nord and
416 // y-positive pointing East we need to negate local_vec.x()
417 // rotation is positive counterclockwise from South (body in the East)
418 // and negative clockwise from South (body in the West)
419 rotation = atan2(local_vec.y(), -local_vec.x());
420
421 // cout << " Sky needs to rotate = " << rotation << " rads = "
422 // << rotation * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
423
424 AngleRad->setDoubleValue(angle);
425 }
426
427
428 // Register the subsystem.
429 SGSubsystemMgr::Registrant<FGLight> registrantFGLight(
430 SGSubsystemMgr::DISPLAY);