]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
- adjusted for no-value constructor for FGPanel
[flightgear.git] / src / Time / light.cxx
1 //
2 // light.hxx -- 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/glut.h>
34 #include <GL/gl.h>
35
36 #include <simgear/compiler.h>
37
38 #ifdef SG_MATH_EXCEPTION_CLASH
39 #  define exception c_exception
40 #endif
41
42 #ifdef SG_HAVE_STD_INCLUDES
43 #  include <cmath>
44 #else
45 #  include <math.h>
46 #endif
47
48 #include <string>
49 SG_USING_STD(string);
50
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>
56
57 #include <Aircraft/aircraft.hxx>
58 #include <Main/globals.hxx>
59
60 #include "light.hxx"
61 #include "sunpos.hxx"
62
63
64 fgLIGHT cur_light_params;
65
66
67 // Constructor
68 fgLIGHT::fgLIGHT( void ) {
69 }
70
71
72 // initialize lighting tables
73 void fgLIGHT::Init( void ) {
74     SG_LOG( SG_EVENT, SG_INFO, 
75             "Initializing Lighting interpolation tables." );
76
77     // build the path name to the ambient lookup table
78     SGPath path( globals->get_fg_root() );
79     SGPath ambient = path;
80     ambient.append( "Lighting/ambient" );
81     SGPath diffuse = path;
82     diffuse.append( "Lighting/diffuse" );
83     SGPath sky = path;
84     sky.append( "Lighting/sky" );
85
86     // initialize ambient table
87     ambient_tbl = new SGInterpTable( ambient.str() );
88
89     // initialize diffuse table
90     diffuse_tbl = new SGInterpTable( diffuse.str() );
91     
92     // initialize sky table
93     sky_tbl = new SGInterpTable( sky.str() );
94 }
95
96
97 // update lighting parameters based on current sun position
98 void fgLIGHT::Update( void ) {
99     FGInterface *f;
100     // if the 4th field is 0.0, this specifies a direction ...
101     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
102     // base sky color
103     GLfloat base_sky_color[4] =        {0.60, 0.60, 0.90, 1.0};
104     // base fog color
105     GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
106     double deg, ambient, diffuse, sky_brightness;
107
108     f = current_aircraft.fdm_state;
109
110     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
111
112     // calculate lighting parameters based on sun's relative angle to
113     // local up
114
115     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
116     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
117
118     ambient = ambient_tbl->interpolate( deg );
119     diffuse = diffuse_tbl->interpolate( deg );
120     sky_brightness = sky_tbl->interpolate( deg );
121
122     SG_LOG( SG_EVENT, SG_INFO, 
123             "  ambient = " << ambient << "  diffuse = " << diffuse 
124             << "  sky = " << sky_brightness );
125
126     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
127
128     // if ( ambient < 0.02 ) { ambient = 0.02; }
129     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
130     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
131
132     scene_ambient[0] = white[0] * ambient;
133     scene_ambient[1] = white[1] * ambient;
134     scene_ambient[2] = white[2] * ambient;
135     scene_ambient[3] = 1.0;
136
137     scene_diffuse[0] = white[0] * diffuse;
138     scene_diffuse[1] = white[1] * diffuse;
139     scene_diffuse[2] = white[2] * diffuse;
140     scene_diffuse[3] = 1.0;
141
142     // set sky color
143     sky_color[0] = base_sky_color[0] * sky_brightness;
144     sky_color[1] = base_sky_color[1] * sky_brightness;
145     sky_color[2] = base_sky_color[2] * sky_brightness;
146     sky_color[3] = base_sky_color[3];
147
148     // set fog color
149     fog_color[0] = base_fog_color[0] * sky_brightness;
150     fog_color[1] = base_fog_color[1] * sky_brightness;
151     fog_color[2] = base_fog_color[2] * sky_brightness;
152     fog_color[3] = base_fog_color[3];
153 }
154
155
156 // calculate fog color adjusted for sunrise/sunset effects
157 void fgLIGHT::UpdateAdjFog( void ) {
158     FGInterface *f;
159     double sun_angle_deg, rotation, param1[3], param2[3];
160
161     f = current_aircraft.fdm_state;
162
163     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
164
165     // set fog color (we'll try to match the sunset color in the
166     // direction we are looking
167
168     // first determine the difference between our view angle and local
169     // direction to the sun
170     rotation = -(sun_rotation + SGD_PI) 
171         - (f->get_Psi() - globals->get_current_view()->get_view_offset());
172     if ( globals->get_current_view()->get_reverse_view_offset() ) {
173         rotation += SGD_PI;
174     }
175     while ( rotation < 0 ) {
176         rotation += SGD_2PI;
177     }
178     while ( rotation > SGD_2PI ) {
179         rotation -= SGD_2PI;
180     }
181     rotation *= SGD_RADIANS_TO_DEGREES;
182     // fgPrintf( SG_EVENT, SG_INFO, 
183     //           "  View to sun difference in degrees = %.2f\n", rotation);
184
185     // next check if we are in a sunset/sunrise situation
186     sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
187     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
188         /* 0.0 - 0.6 */
189         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
190         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
191         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
192         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
193     } else {
194         param1[0] = param1[1] = param1[2] = 0.0;
195     }
196
197     if ( rotation - 180.0 <= 0.0 ) {
198         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
199         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
200         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
201         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
202     } else {
203         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
204         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
205         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
206         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
207     }
208
209     adj_fog_color[0] = fog_color[0] + param2[0];
210     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
211
212     adj_fog_color[1] = fog_color[1] + param2[1];
213     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
214
215     adj_fog_color[2] = fog_color[2] + param2[2];
216     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
217
218     adj_fog_color[3] = fog_color[3];
219 }
220
221
222 // Destructor
223 fgLIGHT::~fgLIGHT( void ) {
224 }
225
226