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.
35 #include <Debug/fg_debug.h>
36 #include <Include/fg_constants.h>
37 #include <Include/general.h>
38 #include <Main/views.hxx>
39 #include <Math/fg_geodesy.h>
40 #include <Math/interpolater.hxx>
41 #include <Math/mat3.h>
42 #include <Math/polar.h>
49 fgLIGHT cur_light_params;
51 static fgINTERPTABLE *ambient_tbl;
52 static fgINTERPTABLE *diffuse_tbl;
53 static fgINTERPTABLE *sky_tbl;
56 // initialize lighting tables
57 void fgLightInit( void ) {
61 fgPrintf( FG_EVENT, FG_INFO,
62 "Initializing Lighting interpolation tables.\n" );
66 // build the path name to the ambient lookup table
68 strcat(path, g->root_dir);
69 strcat(path, "/Scenery/");
70 strcat(path, "Ambient.table");
71 // initialize ambient table
72 ambient_tbl = new fgINTERPTABLE(path);
74 // build the path name to the diffuse lookup table
76 strcat(path, g->root_dir);
77 strcat(path, "/Scenery/");
78 strcat(path, "Diffuse.table");
79 // initialize diffuse table
80 diffuse_tbl = new fgINTERPTABLE(path);
82 // build the path name to the sky lookup table
84 strcat(path, g->root_dir);
85 strcat(path, "/Scenery/");
86 strcat(path, "Sky.table");
87 // initialize sky table
88 sky_tbl = new fgINTERPTABLE(path);
92 // update lighting parameters based on current sun position
93 void fgLightUpdate( void ) {
97 /* if the 4th field is 0.0, this specifies a direction ... */
98 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
100 GLfloat base_sky_color[4] = {0.60, 0.60, 0.90, 1.0};
102 GLfloat base_fog_color[4] = {0.70, 0.70, 0.70, 1.0};
103 double deg, ambient, diffuse, sky_brightness;
105 l = &cur_light_params;
106 t = &cur_time_params;
109 fgPrintf( FG_EVENT, FG_INFO, "Updating light parameters.\n" );
111 /* calculate lighting parameters based on sun's relative angle to
114 deg = l->sun_angle * 180.0 / FG_PI;
115 fgPrintf( FG_EVENT, FG_INFO, " Sun angle = %.2f.\n", deg );
117 ambient = ambient_tbl->interpolate( deg );
118 diffuse = diffuse_tbl->interpolate( deg );
119 sky_brightness = sky_tbl->interpolate( deg );
121 fgPrintf( FG_EVENT, FG_INFO,
122 " ambient = %.2f diffuse = %.2f sky = %.2f\n",
123 ambient, diffuse, sky_brightness );
125 // sky_brightness = 0.15; // used to force a dark sky (when testing)
127 // if ( ambient < 0.02 ) { ambient = 0.02; }
128 // if ( diffuse < 0.0 ) { diffuse = 0.0; }
129 // if ( sky_brightness < 0.1 ) { sky_brightness = 0.1; }
131 l->scene_ambient[0] = white[0] * ambient;
132 l->scene_ambient[1] = white[1] * ambient;
133 l->scene_ambient[2] = white[2] * ambient;
135 l->scene_diffuse[0] = white[0] * diffuse;
136 l->scene_diffuse[1] = white[1] * diffuse;
137 l->scene_diffuse[2] = white[2] * diffuse;
140 l->fog_color[0] = base_fog_color[0] * (ambient + diffuse);
141 l->fog_color[1] = base_fog_color[1] * (ambient + diffuse);
142 l->fog_color[2] = base_fog_color[2] * (ambient + diffuse);
143 l->fog_color[3] = base_fog_color[3];
146 l->sky_color[0] = base_sky_color[0] * sky_brightness;
147 l->sky_color[1] = base_sky_color[1] * sky_brightness;
148 l->sky_color[2] = base_sky_color[2] * sky_brightness;
149 l->sky_color[3] = base_sky_color[3];
154 // Revision 1.1 1998/04/22 13:24:06 curt
155 // C++ - ifiing the code a bit.
156 // Starting to reorginize some of the lighting calcs to use a table lookup.