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