]> git.mxchange.org Git - flightgear.git/blob - src/Time/light.hxx
Convert fgLIGHT to FGLight and make it FGSubsystem compatible. Let the subsystem...
[flightgear.git] / src / Time / light.hxx
1 // light.hxx -- lighting routines
2 //
3 // Written by Curtis Olson, started April 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _LIGHT_HXX
25 #define _LIGHT_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <GL/gl.h>
42
43 #include <plib/sg.h>                    // plib include
44
45 #include <simgear/math/interpolater.hxx>
46 #include <simgear/math/point3d.hxx>
47
48
49 // Define a structure containing the global lighting parameters
50 class FGLight : public FGSubsystem
51 {
52
53 private:
54
55     /*
56      * Lighting look up tables (based on sun angle with local horizon)
57      */
58     SGInterpTable *_ambient_tbl, *_diffuse_tbl, *_specular_tbl;
59     SGInterpTable *_sky_tbl;
60
61     /**
62      * position of the sun and moon in various forms
63      */
64
65     // in geocentric coordinates
66     double _sun_lon, _sun_gc_lat;
67     double _moon_lon, _moon_gc_lat;
68
69     // in cartesian coordiantes
70     Point3D _sunpos, _moonpos;
71
72     // (in view coordinates)
73     sgVec4 _sun_vec, _moon_vec;
74
75     // inverse (in view coordinates)
76     sgVec4 _sun_vec_inv, _moon_vec_inv;
77
78     // the angle between the celestial object and the local horizontal
79     // (in radians)
80     double _sun_angle, _moon_angle;
81     double _prev_sun_angle;
82
83     // the rotation around our vertical axis of the sun (relative to
84     // due south with positive numbers going in the counter clockwise
85     // direction.)  This is the direction we'd need to face if we
86     // wanted to travel towards celestial object.
87     double _sun_rotation, _moon_rotation;
88
89     /**
90      * Derived lighting values
91      */
92
93     // ambient, diffuse and specular component
94     GLfloat _scene_ambient[4];
95     GLfloat _scene_diffuse[4];
96     GLfloat _scene_specular[4];
97
98     // clear sky, fog and cloud color
99     GLfloat _sky_color[4];
100     GLfloat _fog_color[4];
101     GLfloat _cloud_color[4];
102
103     // clear sky and fog color adjusted for sunset effects
104     GLfloat _adj_fog_color[4];
105     GLfloat _adj_sky_color[4];
106
107     double _dt_total;
108
109     void update_sky_color ();
110     void update_adj_fog_color ();
111
112 public:
113
114     FGLight ();
115     virtual ~FGLight ();
116
117     virtual void init ();
118     virtual void reinit ();
119     virtual void bind ();
120     virtual void unbind ();
121     virtual void update ( double dt );
122
123
124     // Color related functions
125
126     inline float *scene_ambient () const { return (float *)_scene_ambient; }
127     inline float *scene_diffuse () const { return (float *)_scene_diffuse; }
128     inline float *scene_specular () const { return (float *)_scene_specular; }
129
130     inline float *sky_color () const { return (float *)_sky_color; }
131     inline float *cloud_color () const { return (float *)_cloud_color; }
132     inline float *adj_fog_color () const { return (float *)_adj_fog_color; }
133
134
135     // Sun related functions
136
137     inline double get_sun_angle () const { return _sun_angle; }
138     inline void set_sun_angle (double a) { _sun_angle = a; }
139
140     inline double get_sun_rotation () const { return _sun_rotation; }
141     inline void set_sun_rotation (double r) { _sun_rotation = r; }
142
143     inline double get_sun_lon () const { return _sun_lon; }
144     inline void set_sun_lon (double l) { _sun_lon = l; }
145
146     inline double get_sun_gc_lat () const { return _sun_gc_lat; }
147     inline void set_sun_gc_lat (double l) { _sun_gc_lat = l; }
148
149     inline Point3D get_sunpos () const { return _sunpos; }
150     inline void set_sunpos (Point3D p) { _sunpos = p; }
151
152     inline float *sun_vec () const { return (float *)_sun_vec; }
153     inline float *sun_vec_inv () const { return (float *)_sun_vec_inv; }
154
155
156     // Moon related functions
157
158     inline double get_moon_angle () const { return _moon_angle; }
159     inline void set_moon_angle (double a) { _moon_angle = a; }
160
161     inline double get_moon_rotation () const { return _moon_rotation; }
162     inline void set_moon_rotation (double r) { _moon_rotation = r; }
163
164     inline double get_moon_lon () const { return _moon_lon; }
165     inline void set_moon_lon (double l) { _moon_lon = l; }
166
167     inline double get_moon_gc_lat () const { return _moon_gc_lat; }
168     inline void set_moon_gc_lat (double l) { _moon_gc_lat = l; }
169
170     inline Point3D get_moonpos () const { return _moonpos; }
171     inline void set_moonpos (Point3D p) { _moonpos = p; }
172
173     inline float *moon_vec () const { return (float *)_moon_vec; }
174     inline float *moon_vec_inv () const { return (float *)_moon_vec_inv; }
175 };
176
177 #endif // _LIGHT_HXX
178