]> git.mxchange.org Git - flightgear.git/blob - Time/light.cxx
Wrote access functions for current fgOPTIONS.
[flightgear.git] / 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 <XGL/xgl.h>
35
36 #include <string.h>
37
38 #include <Debug/fg_debug.h>
39 #include <Include/fg_constants.h>
40 #include <Main/options.hxx>
41 #include <Main/views.hxx>
42 #include <Math/fg_geodesy.h>
43 #include <Math/interpolater.hxx>
44 #include <Math/mat3.h>
45 #include <Math/polar3d.hxx>
46
47 #include "fg_time.hxx"
48 #include "light.hxx"
49 #include "sunpos.hxx"
50
51
52 fgLIGHT cur_light_params;
53
54
55 // Constructor
56 fgLIGHT::fgLIGHT( void ) {
57 }
58
59
60 // initialize lighting tables
61 void fgLIGHT::Init( void ) {
62     char path[256];
63
64     fgPrintf( FG_EVENT, FG_INFO, 
65              "Initializing Lighting interpolation tables.\n" );
66
67     // build the path name to the ambient lookup table
68     current_options.get_fg_root(path);
69     strcat(path, "/Scenery/");
70     strcat(path, "Ambient");
71     // initialize ambient table
72     ambient_tbl = new fgINTERPTABLE(path);
73
74     // build the path name to the diffuse lookup table
75     current_options.get_fg_root(path);
76     strcat(path, "/Scenery/");
77     strcat(path, "Diffuse");
78     // initialize diffuse table
79     diffuse_tbl = new fgINTERPTABLE(path);
80     
81     // build the path name to the sky lookup table
82     current_options.get_fg_root(path);
83     strcat(path, "/Scenery/");
84     strcat(path, "Sky");
85     // initialize sky table
86     sky_tbl = new fgINTERPTABLE(path);
87 }
88
89
90 // update lighting parameters based on current sun position
91 void fgLIGHT::Update( void ) {
92     fgTIME *t;
93     fgVIEW *v;
94     // if the 4th field is 0.0, this specifies a direction ...
95     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
96     // base sky color
97     GLfloat base_sky_color[4] =        {0.60, 0.60, 0.90, 1.0};
98     // base fog color
99     GLfloat base_fog_color[4] = {0.90, 0.90, 1.00, 1.0};
100     double deg, ambient, diffuse, sky_brightness;
101
102     t = &cur_time_params;
103     v = &current_view;
104
105     fgPrintf( FG_EVENT, FG_INFO, "Updating light parameters.\n" );
106
107     // calculate lighting parameters based on sun's relative angle to
108     // local up
109
110     deg = sun_angle * 180.0 / FG_PI;
111     fgPrintf( FG_EVENT, FG_INFO, "  Sun angle = %.2f.\n", deg );
112
113     ambient = ambient_tbl->interpolate( deg );
114     diffuse = diffuse_tbl->interpolate( deg );
115     sky_brightness = sky_tbl->interpolate( deg );
116
117     fgPrintf( FG_EVENT, FG_INFO, 
118               "  ambient = %.2f  diffuse = %.2f  sky = %.2f\n", 
119               ambient, diffuse, sky_brightness );
120
121     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
122
123     // if ( ambient < 0.02 ) { ambient = 0.02; }
124     // if ( diffuse < 0.0 ) { diffuse = 0.0; }
125     // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
126
127     scene_ambient[0] = white[0] * ambient;
128     scene_ambient[1] = white[1] * ambient;
129     scene_ambient[2] = white[2] * ambient;
130
131     scene_diffuse[0] = white[0] * diffuse;
132     scene_diffuse[1] = white[1] * diffuse;
133     scene_diffuse[2] = white[2] * diffuse;
134
135     // set fog color
136     fog_color[0] = base_fog_color[0] * sky_brightness;
137     fog_color[1] = base_fog_color[1] * sky_brightness;
138     fog_color[2] = base_fog_color[2] * sky_brightness;
139     fog_color[3] = base_fog_color[3];
140
141     // set sky color
142     sky_color[0] = base_sky_color[0] * sky_brightness;
143     sky_color[1] = base_sky_color[1] * sky_brightness;
144     sky_color[2] = base_sky_color[2] * sky_brightness;
145     sky_color[3] = base_sky_color[3];
146 }
147
148
149 // Destructor
150 fgLIGHT::~fgLIGHT( void ) {
151 }
152
153
154 // wrapper function for updating light parameters via the event scheduler
155 void fgLightUpdate ( void ) {
156     fgLIGHT *l;
157     l = &cur_light_params;
158    
159     l->Update();
160 }
161
162
163 // $Log$
164 // Revision 1.11  1998/07/13 21:02:08  curt
165 // Wrote access functions for current fgOPTIONS.
166 //
167 // Revision 1.10  1998/07/08 14:48:38  curt
168 // polar3d.h renamed to polar3d.hxx
169 //
170 // Revision 1.9  1998/05/29 20:37:51  curt
171 // Renamed <Table>.table to be <Table> so we can add a .gz under DOS.
172 //
173 // Revision 1.8  1998/05/20 20:54:16  curt
174 // Converted fgLIGHT to a C++ class.
175 //
176 // Revision 1.7  1998/05/13 18:26:50  curt
177 // Root path info moved to fgOPTIONS.
178 //
179 // Revision 1.6  1998/05/11 18:18:51  curt
180 // Made fog color slightly bluish.
181 //
182 // Revision 1.5  1998/05/03 00:48:38  curt
183 // polar.h -> polar3d.h
184 //
185 // Revision 1.4  1998/04/28 01:22:18  curt
186 // Type-ified fgTIME and fgVIEW.
187 //
188 // Revision 1.3  1998/04/26 05:10:04  curt
189 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
190 //
191 // Revision 1.2  1998/04/24 00:52:30  curt
192 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
193 // Fog color fixes.
194 // Separated out lighting calcs into their own file.
195 //
196 // Revision 1.1  1998/04/22 13:24:06  curt
197 // C++ - ifiing the code a bit.
198 // Starting to reorginize some of the lighting calcs to use a table lookup.
199 //