2 // light.hxx -- lighting routines
4 // Written by Curtis Olson, started April 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
38 #include <Debug/fg_debug.h>
39 #include <Include/fg_constants.h>
40 #include <Include/general.h>
41 #include <Main/views.hxx>
42 #include <Math/fg_geodesy.h>
43 #include <Math/interpolater.hxx>
44 #include <Math/mat3.h>
45 #include <Math/polar.h>
47 #include "fg_time.hxx"
52 fgLIGHT cur_light_params;
54 static fgINTERPTABLE *ambient_tbl;
55 static fgINTERPTABLE *diffuse_tbl;
56 static fgINTERPTABLE *sky_tbl;
59 // initialize lighting tables
60 void fgLightInit( void ) {
64 fgPrintf( FG_EVENT, FG_INFO,
65 "Initializing Lighting interpolation tables.\n" );
69 // build the path name to the ambient lookup table
71 strcat(path, g->root_dir);
72 strcat(path, "/Scenery/");
73 strcat(path, "Ambient.table");
74 // initialize ambient table
75 ambient_tbl = new fgINTERPTABLE(path);
77 // build the path name to the diffuse lookup table
79 strcat(path, g->root_dir);
80 strcat(path, "/Scenery/");
81 strcat(path, "Diffuse.table");
82 // initialize diffuse table
83 diffuse_tbl = new fgINTERPTABLE(path);
85 // build the path name to the sky lookup table
87 strcat(path, g->root_dir);
88 strcat(path, "/Scenery/");
89 strcat(path, "Sky.table");
90 // initialize sky table
91 sky_tbl = new fgINTERPTABLE(path);
95 // update lighting parameters based on current sun position
96 void fgLightUpdate( void ) {
100 /* if the 4th field is 0.0, this specifies a direction ... */
101 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
103 GLfloat base_sky_color[4] = {0.60, 0.60, 0.90, 1.0};
105 GLfloat base_fog_color[4] = {1.00, 1.00, 1.00, 1.0};
106 double deg, ambient, diffuse, sky_brightness;
108 l = &cur_light_params;
109 t = &cur_time_params;
112 fgPrintf( FG_EVENT, FG_INFO, "Updating light parameters.\n" );
114 /* calculate lighting parameters based on sun's relative angle to
117 deg = l->sun_angle * 180.0 / FG_PI;
118 fgPrintf( FG_EVENT, FG_INFO, " Sun angle = %.2f.\n", deg );
120 ambient = ambient_tbl->interpolate( deg );
121 diffuse = diffuse_tbl->interpolate( deg );
122 sky_brightness = sky_tbl->interpolate( deg );
124 fgPrintf( FG_EVENT, FG_INFO,
125 " ambient = %.2f diffuse = %.2f sky = %.2f\n",
126 ambient, diffuse, sky_brightness );
128 // sky_brightness = 0.15; // used to force a dark sky (when testing)
130 // if ( ambient < 0.02 ) { ambient = 0.02; }
131 // if ( diffuse < 0.0 ) { diffuse = 0.0; }
132 // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
134 l->scene_ambient[0] = white[0] * ambient;
135 l->scene_ambient[1] = white[1] * ambient;
136 l->scene_ambient[2] = white[2] * ambient;
138 l->scene_diffuse[0] = white[0] * diffuse;
139 l->scene_diffuse[1] = white[1] * diffuse;
140 l->scene_diffuse[2] = white[2] * diffuse;
143 // l->fog_color[0] = base_fog_color[0] * (ambient + diffuse);
144 // l->fog_color[1] = base_fog_color[1] * (ambient + diffuse);
145 // l->fog_color[2] = base_fog_color[2] * (ambient + diffuse);
146 // l->fog_color[3] = base_fog_color[3];
147 l->fog_color[0] = base_fog_color[0] * sky_brightness;
148 l->fog_color[1] = base_fog_color[1] * sky_brightness;
149 l->fog_color[2] = base_fog_color[2] * sky_brightness;
150 l->fog_color[3] = base_fog_color[3];
153 l->sky_color[0] = base_sky_color[0] * sky_brightness;
154 l->sky_color[1] = base_sky_color[1] * sky_brightness;
155 l->sky_color[2] = base_sky_color[2] * sky_brightness;
156 l->sky_color[3] = base_sky_color[3];
161 // Revision 1.2 1998/04/24 00:52:30 curt
162 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
164 // Separated out lighting calcs into their own file.
166 // Revision 1.1 1998/04/22 13:24:06 curt
167 // C++ - ifiing the code a bit.
168 // Starting to reorginize some of the lighting calcs to use a table lookup.