]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.cxx
Added static port system and a new altimeter model connected to it.
[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 #include <Main/viewer.hxx>
60
61 #include "light.hxx"
62 #include "sunpos.hxx"
63
64
65 fgLIGHT cur_light_params;
66
67
68 // Constructor
69 fgLIGHT::fgLIGHT( void ) {
70 }
71
72
73 // initialize lighting tables
74 void fgLIGHT::Init( void ) {
75     SG_LOG( SG_EVENT, SG_INFO, 
76             "Initializing Lighting interpolation tables." );
77
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" );
84     SGPath specular = path;
85     specular.append( "Lighting/specular" );
86     SGPath sky = path;
87     sky.append( "Lighting/sky" );
88
89     // initialize ambient table
90     ambient_tbl = new SGInterpTable( ambient.str() );
91
92     // initialize diffuse table
93     diffuse_tbl = new SGInterpTable( diffuse.str() );
94     
95     // initialize diffuse table
96     specular_tbl = new SGInterpTable( specular.str() );
97     
98     // initialize sky table
99     sky_tbl = new SGInterpTable( sky.str() );
100 }
101
102
103 // update lighting parameters based on current sun position
104 void fgLIGHT::Update( void ) {
105     FGInterface *f;
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.60, 0.60, 0.90, 1.0 };
110     // base fog color
111     GLfloat base_fog_color[4] = { 0.90, 0.90, 1.00, 1.0 };
112     double deg, ambient, diffuse, specular, sky_brightness;
113
114     f = current_aircraft.fdm_state;
115
116     SG_LOG( SG_EVENT, SG_INFO, "Updating light parameters." );
117
118     // calculate lighting parameters based on sun's relative angle to
119     // local up
120
121     deg = sun_angle * SGD_RADIANS_TO_DEGREES;
122     SG_LOG( SG_EVENT, SG_INFO, "  Sun angle = " << deg );
123
124     ambient = ambient_tbl->interpolate( deg );
125     diffuse = diffuse_tbl->interpolate( deg );
126     specular = specular_tbl->interpolate( deg );
127     sky_brightness = sky_tbl->interpolate( deg );
128
129     SG_LOG( SG_EVENT, SG_INFO, 
130             "  ambient = " << ambient << "  diffuse = " << diffuse 
131             << "  specular = " << specular << "  sky = " << sky_brightness );
132
133     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
134
135     // if ( ambient < 0.02 ) { ambient = 0.02; }
136     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
137     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
138
139     scene_ambient[0] = white[0] * ambient;
140     scene_ambient[1] = white[1] * ambient;
141     scene_ambient[2] = white[2] * ambient;
142     scene_ambient[3] = 1.0;
143
144     scene_diffuse[0] = white[0] * diffuse;
145     scene_diffuse[1] = white[1] * diffuse;
146     scene_diffuse[2] = white[2] * diffuse;
147     scene_diffuse[3] = 1.0;
148
149     scene_specular[0] = white[0] * specular;
150     scene_specular[1] = white[1] * specular;
151     scene_specular[2] = white[2] * specular;
152     scene_specular[3] = 1.0;
153
154     // set sky color
155     sky_color[0] = base_sky_color[0] * sky_brightness;
156     sky_color[1] = base_sky_color[1] * sky_brightness;
157     sky_color[2] = base_sky_color[2] * sky_brightness;
158     sky_color[3] = base_sky_color[3];
159
160     // set fog color
161     fog_color[0] = base_fog_color[0] * sky_brightness;
162     fog_color[1] = base_fog_color[1] * sky_brightness;
163     fog_color[2] = base_fog_color[2] * sky_brightness;
164     fog_color[3] = base_fog_color[3];
165 }
166
167
168 // calculate fog color adjusted for sunrise/sunset effects
169 void fgLIGHT::UpdateAdjFog( void ) {
170     FGInterface *f;
171     double sun_angle_deg, rotation, param1[3], param2[3];
172
173     f = current_aircraft.fdm_state;
174
175     SG_LOG( SG_EVENT, SG_DEBUG, "Updating adjusted fog parameters." );
176
177     // set fog color (we'll try to match the sunset color in the
178     // direction we are looking
179
180     // Do some sanity checking ...
181     if ( sun_rotation < -2.0 * SGD_2PI || sun_rotation > 2.0 * SGD_2PI ) {
182         SG_LOG( SG_EVENT, SG_ALERT, "Sun rotation bad = " << sun_rotation );
183         exit(-1);
184     }
185     if ( f->get_Psi() < -2.0 * SGD_2PI || f->get_Psi() > 2.0 * SGD_2PI ) {
186         SG_LOG( SG_EVENT, SG_ALERT, "Psi rotation bad = " << f->get_Psi() );
187         exit(-1);
188     }
189     if ( globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS < -2.0 * SGD_2PI ||
190          globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS > 2.0 * SGD_2PI ) {
191         SG_LOG( SG_EVENT, SG_ALERT, "current view()->view offset bad = " 
192                 << globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS );
193         exit(-1);
194     }
195
196     // first determine the difference between our view angle and local
197     // direction to the sun
198     rotation = -(sun_rotation + SGD_PI) 
199         - (f->get_Psi() - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS);
200     while ( rotation < 0 ) {
201         rotation += SGD_2PI;
202     }
203     while ( rotation > SGD_2PI ) {
204         rotation -= SGD_2PI;
205     }
206     rotation *= SGD_RADIANS_TO_DEGREES;
207     // fgPrintf( SG_EVENT, SG_INFO, 
208     //           "  View to sun difference in degrees = %.2f\n", rotation);
209
210     // next check if we are in a sunset/sunrise situation
211     sun_angle_deg = sun_angle * SGD_RADIANS_TO_DEGREES;
212     if ( (sun_angle_deg > 80.0) && (sun_angle_deg < 100.0) ) {
213         /* 0.0 - 0.6 */
214         param1[0] = (10.0 - fabs(90.0 - sun_angle_deg)) / 20.0;
215         param1[1] = (10.0 - fabs(90.0 - sun_angle_deg)) / 40.0;
216         param1[2] = (10.0 - fabs(90.0 - sun_angle_deg)) / 30.0;
217         // param2[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
218     } else {
219         param1[0] = param1[1] = param1[2] = 0.0;
220     }
221
222     if ( rotation - 180.0 <= 0.0 ) {
223         param2[0] = param1[0] * (180.0 - rotation) / 180.0;
224         param2[1] = param1[1] * (180.0 - rotation) / 180.0;
225         param2[2] = param1[2] * (180.0 - rotation) / 180.0;
226         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
227     } else {
228         param2[0] = param1[0] * (rotation - 180.0) / 180.0;
229         param2[1] = param1[1] * (rotation - 180.0) / 180.0;
230         param2[2] = param1[2] * (rotation - 180.0) / 180.0;
231         // printf("param1[0] = %.2f param2[0] = %.2f\n", param1[0], param2[0]);
232     }
233
234     adj_fog_color[0] = fog_color[0] + param2[0];
235     if ( adj_fog_color[0] > 1.0 ) { adj_fog_color[0] = 1.0; }
236
237     adj_fog_color[1] = fog_color[1] + param2[1];
238     if ( adj_fog_color[1] > 1.0 ) { adj_fog_color[1] = 1.0; }
239
240     adj_fog_color[2] = fog_color[2] + param2[2];
241     if ( adj_fog_color[2] > 1.0 ) { adj_fog_color[2] = 1.0; }
242
243     adj_fog_color[3] = fog_color[3];
244 }
245
246
247 // Destructor
248 fgLIGHT::~fgLIGHT( void ) {
249 }
250
251
252
253