]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
Enable two sided lighting calculations when specular highlight is enabled
[flightgear.git] / src / Time / light.cxx
1 //
2 // light.cxx -- lighting routines
3 //
4 // Written by Curtis Olson, started April 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include <GL/gl.h>
34
35 #include <simgear/compiler.h>
36
37 #ifdef SG_MATH_EXCEPTION_CLASH
38 #  define exception c_exception
39 #endif
40
41 #ifdef SG_HAVE_STD_INCLUDES
42 #  include <cmath>
43 #else
44 #  include <math.h>
45 #endif
46
47 #include <string>
48 SG_USING_STD(string);
49
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/scene/sky/sky.hxx>
56 #include <simgear/screen/colors.hxx>
57
58 #include <Main/main.hxx>
59 #include <Main/globals.hxx>
60 #include <Main/fg_props.hxx>
61 #include <Main/viewer.hxx>
62
63 #include "light.hxx"
64 #include "sunpos.hxx"
65
66 fgLIGHT cur_light_params;
67
68
69 // Constructor
70 fgLIGHT::fgLIGHT( void ) {
71 }
72
73
74 // initialize lighting tables
75 void fgLIGHT::Init( void ) {
76     SG_LOG( SG_EVENT, SG_INFO, 
77             "Initializing Lighting interpolation tables." );
78
79     // build the path name to the ambient lookup table
80     SGPath path( globals->get_fg_root() );
81     SGPath ambient = path;
82     ambient.append( "Lighting/ambient" );
83     SGPath diffuse = path;
84     diffuse.append( "Lighting/diffuse" );
85     SGPath specular = path;
86     specular.append( "Lighting/specular" );
87     SGPath sky = path;
88     sky.append( "Lighting/sky" );
89
90     // initialize ambient table
91     ambient_tbl = new SGInterpTable( ambient.str() );
92
93     // initialize diffuse table
94     diffuse_tbl = new SGInterpTable( diffuse.str() );
95     
96     // initialize diffuse table
97     specular_tbl = new SGInterpTable( specular.str() );
98     
99     // initialize sky table
100     sky_tbl = new SGInterpTable( sky.str() );
101 }
102
103
104 // update lighting parameters based on current sun position
105 void fgLIGHT::Update( void ) {
106     // if the 4th field is 0.0, this specifies a direction ...
107     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
108     // base sky color
109     GLfloat base_sky_color[4] = { 0.39, 0.50, 0.74, 1.0 };
110     // base fog color
111     GLfloat base_fog_color[4] = { 0.84, 0.87, 1.0, 1.0 };
112     float deg, ambient, diffuse, specular, sky_brightness;
113
114     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
115
116     // calculate lighting parameters based on sun's relative angle to
117     // local up
118
119     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
120     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
121
122     ambient = ambient_tbl->interpolate( deg );
123     diffuse = diffuse_tbl->interpolate( deg );
124     specular = specular_tbl->interpolate( deg );
125     sky_brightness = sky_tbl->interpolate( deg );
126
127     SG_LOG( SG_EVENT, SG_INFO, 
128             "  ambient = " << ambient << "  diffuse = " << diffuse 
129             << "  specular = " << specular << "  sky = " << sky_brightness );
130
131     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
132
133     // if ( ambient < 0.02 ) { ambient = 0.02; }
134     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
135     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
136
137     // set sky color
138     sky_color[0] = base_sky_color[0] * sky_brightness;
139     sky_color[1] = base_sky_color[1] * sky_brightness;
140     sky_color[2] = base_sky_color[2] * sky_brightness;
141     sky_color[3] = base_sky_color[3];
142     gamma_correct_rgb( sky_color );
143
144     // set cloud and fog color
145     cloud_color[0] = fog_color[0] = base_fog_color[0] * sky_brightness;
146     cloud_color[1] = fog_color[1] = base_fog_color[1] * sky_brightness;
147     cloud_color[2] = fog_color[2] = base_fog_color[2] * sky_brightness;
148     cloud_color[3] = fog_color[3] = base_fog_color[3];
149     gamma_correct_rgb( fog_color );
150
151     // adjust the cloud colors for sunrise/sunset effects (darken them)
152     if (sun_angle > 1.0) {
153        float sun2 = sqrt(sun_angle);
154        cloud_color[0] /= sun2;
155        cloud_color[1] /= sun2;
156        cloud_color[2] /= sun2;
157     }
158     gamma_correct_rgb( cloud_color );
159
160     if (fgGetBool("/test/scene_lighting")) {
161         float *sun_color = thesky->get_sun_color();
162
163         scene_ambient[0] = ((sun_color[0]*0.25 + cloud_color[0]*0.75) + ambient) / 2;
164         scene_ambient[1] = ((sun_color[1]*0.25 + cloud_color[1]*0.75) + ambient) / 2;
165         scene_ambient[2] = ((sun_color[2]*0.25 + cloud_color[2]*0.75) + ambient) / 2;
166         // scene_ambient[0] = white[0] * ambient;
167         // scene_ambient[1] = white[0] * ambient;
168         // scene_ambient[2] = white[0] * ambient;
169         scene_ambient[3] = 1.0;
170
171         scene_diffuse[0] = (sun_color[0]*0.25 + fog_color[0]*0.75) * diffuse;
172         scene_diffuse[1] = (sun_color[1]*0.25 + fog_color[1]*0.75) * diffuse;
173         scene_diffuse[2] = (sun_color[2]*0.25 + fog_color[2]*0.75) * diffuse;
174         scene_diffuse[3] = 1.0;
175
176         scene_specular[0] = sun_color[0] * specular;
177         scene_specular[1] = sun_color[1] * specular;
178         scene_specular[2] = sun_color[2] * specular;
179         scene_specular[3] = 1.0;
180
181     } else {
182
183         scene_ambient[0] = white[0] * ambient;
184         scene_ambient[1] = white[1] * ambient;
185         scene_ambient[2] = white[2] * ambient;
186         scene_ambient[3] = 1.0;
187
188         scene_diffuse[0] = white[0] * diffuse;
189         scene_diffuse[1] = white[1] * diffuse;
190         scene_diffuse[2] = white[2] * diffuse;
191         scene_diffuse[3] = 1.0;
192         
193         scene_specular[0] = white[0] * ambient;
194         scene_specular[1] = white[1] * ambient;
195         scene_specular[2] = white[2] * ambient;
196         scene_specular[3] = 1.0;
197     }
198 }
199
200
201 // calculate fog color adjusted for sunrise/sunset effects
202 void fgLIGHT::UpdateAdjFog( void ) {
203
204     double heading = globals->get_current_view()->getHeading_deg()
205                      * SGD_DEGREES_TO_RADIANS;
206     double heading_offset = globals->get_current_view()->getHeadingOffset_deg()
207                             * SGD_DEGREES_TO_RADIANS;
208
209     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
210
211     // set fog color (we'll try to match the sunset color in the
212     // direction we are looking
213
214     // Do some sanity checking ...
215     if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
216         SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
217         exit(-1);
218     }
219
220     if ( heading < -2.0 * SGD_2PI || heading > 2.0 * SGD_2PI ) {
221         SG_LOG( SG_EVENT, SG_ALERT, "Heading rotation bad = " << heading );
222         exit(-1);
223     }
224
225     if ( heading_offset < -2.0 * SGD_2PI || heading_offset > 2.0 * SGD_2PI ) {
226         SG_LOG( SG_EVENT, SG_ALERT, "Heading offset bad = " << heading_offset );
227         exit(-1);
228     }
229
230     double rotation;
231
232     // first determine the difference between our view angle and local
233     // direction to the sun
234     rotation = -(sun_rotation + SGD_PI) - heading + heading_offset;
235     while ( rotation < 0 ) {
236         rotation += SGD_2PI;
237     }
238     while ( rotation > SGD_2PI ) {
239         rotation -= SGD_2PI;
240     }
241
242     // revert to unmodified values before usign them.
243     //
244     float *sun_color = thesky->get_sun_color();
245
246     gamma_restore_rgb( fog_color );
247
248     // Calculate the fog color in the direction of the sun for
249     // sunrise/sunset effects.
250     //
251     float s_red =   (fog_color[0] + 2 * sun_color[0]*sun_color[0]) / 3;
252     float s_green = (fog_color[1] + 2 * sun_color[1]*sun_color[1]) / 3;
253     float s_blue =  (fog_color[2] + 2 * sun_color[2]) / 3;
254
255     // interpolate beween the sunrise/sunset color and the color
256     // at the opposite direction of this effect. Take in account
257     // the current visibility.
258     //
259     float av = thesky->get_visibility();
260     if (av > 45000)
261        av = 45000;
262
263     float avf = 0.87 - (45000 - av) / 83333.33;
264     float sif = 0.5 - cos(sun_angle*2)/2;
265
266     float rf1 = fabs((rotation - SGD_PI) / SGD_PI);             // 0.0 .. 1.0
267     float rf2 = avf * pow(rf1 * rf1, 1/sif);
268     float rf3 = 0.94 - rf2;
269
270     adj_fog_color[0] = rf3 * fog_color[0] + rf2 * s_red;
271     adj_fog_color[1] = rf3 * fog_color[1] + rf2 * s_green;
272     adj_fog_color[2] = rf3 * fog_color[2] + rf2 * s_blue;
273     gamma_correct_rgb( adj_fog_color );
274
275     // make sure the colors have their original value before they are being
276     // used by the rest of the program.
277     //
278     gamma_correct_rgb( fog_color );
279 }
280
281
282 // Destructor
283 fgLIGHT::~fgLIGHT( void ) {
284 }
285
286