]> git.mxchange.org Git - flightgear.git/blob - Time/light.cxx
C++ - ifiing the code a bit.
[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 #include <config.h>
26
27 #ifdef HAVE_WINDOWS_H
28 #  include <windows.h>
29 #endif
30
31 #include <GL/glut.h>
32 #include <XGL/xgl.h>
33 #include <string.h>
34
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>
43
44 #include "fg_time.h"
45 #include "light.hxx"
46 #include "sunpos.hxx"
47
48
49 fgLIGHT cur_light_params;
50
51 static fgINTERPTABLE *ambient_tbl;
52 static fgINTERPTABLE *diffuse_tbl;
53 static fgINTERPTABLE *sky_tbl;
54
55
56 // initialize lighting tables
57 void fgLightInit( void ) {
58     fgGENERAL *g;
59     char path[256];
60
61     fgPrintf( FG_EVENT, FG_INFO, 
62              "Initializing Lighting interpolation tables.\n" );
63
64     g = &general;
65
66     // build the path name to the ambient lookup table
67     path[0] = '\0';
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);
73
74     // build the path name to the diffuse lookup table
75     path[0] = '\0';
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);
81     
82     // build the path name to the sky lookup table
83     path[0] = '\0';
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);
89 }
90
91
92 // update lighting parameters based on current sun position
93 void fgLightUpdate( void ) {
94     struct fgLIGHT *l;
95     struct fgTIME *t;
96     struct fgVIEW *v;
97     /* if the 4th field is 0.0, this specifies a direction ... */
98     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
99     /* base sky color */
100     GLfloat base_sky_color[4] =        {0.60, 0.60, 0.90, 1.0};
101     /* base fog color */
102     GLfloat base_fog_color[4] = {0.70, 0.70, 0.70, 1.0};
103     double deg, ambient, diffuse, sky_brightness;
104
105     l = &cur_light_params;
106     t = &cur_time_params;
107     v = &current_view;
108
109     fgPrintf( FG_EVENT, FG_INFO, "Updating light parameters.\n" );
110
111     /* calculate lighting parameters based on sun's relative angle to
112      * local up */
113
114     deg = l->sun_angle * 180.0 / FG_PI;
115     fgPrintf( FG_EVENT, FG_INFO, "  Sun angle = %.2f.\n", deg );
116
117     ambient = ambient_tbl->interpolate( deg );
118     diffuse = diffuse_tbl->interpolate( deg );
119     sky_brightness = sky_tbl->interpolate( deg );
120
121     fgPrintf( FG_EVENT, FG_INFO, 
122               "  ambient = %.2f  diffuse = %.2f  sky = %.2f\n", 
123               ambient, diffuse, sky_brightness );
124
125     // sky_brightness = 0.15;  // used to force a dark sky (when testing)
126
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; }
130
131     l->scene_ambient[0] = white[0] * ambient;
132     l->scene_ambient[1] = white[1] * ambient;
133     l->scene_ambient[2] = white[2] * ambient;
134
135     l->scene_diffuse[0] = white[0] * diffuse;
136     l->scene_diffuse[1] = white[1] * diffuse;
137     l->scene_diffuse[2] = white[2] * diffuse;
138
139     /* set fog color */
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];
144
145     /* set sky color */
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];
150 }
151
152
153 // $Log$
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.
157 //
158 //