1 // light.hxx -- lighting routines
3 // Written by Curtis Olson, started April 1998.
5 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 # error This library requires C++
41 #include <simgear/compiler.h>
45 #include <plib/sg.h> // plib include
47 #include <simgear/structure/subsystem_mgr.hxx>
48 #include <simgear/math/interpolater.hxx>
49 #include <simgear/math/point3d.hxx>
52 // Define a structure containing the global lighting parameters
53 class FGLight : public SGSubsystem
59 * Lighting look up tables (based on sun angle with local horizon)
61 SGInterpTable *_ambient_tbl, *_diffuse_tbl, *_specular_tbl;
62 SGInterpTable *_sky_tbl;
65 * position of the sun and moon in various forms
68 // in geocentric coordinates
69 double _sun_lon, _sun_gc_lat;
70 double _moon_lon, _moon_gc_lat;
72 // in cartesian coordiantes
73 SGVec3d _sunpos, _moonpos;
75 // (in view coordinates)
76 sgVec4 _sun_vec, _moon_vec;
78 // inverse (in view coordinates)
79 sgVec4 _sun_vec_inv, _moon_vec_inv;
81 // the angle between the celestial object and the local horizontal
83 double _sun_angle, _moon_angle;
84 double _prev_sun_angle;
86 // the rotation around our vertical axis of the sun (relative to
87 // due south with positive numbers going in the counter clockwise
88 // direction.) This is the direction we'd need to face if we
89 // wanted to travel towards celestial object.
90 double _sun_rotation, _moon_rotation;
93 * Derived lighting values
96 // ambient, diffuse and specular component
97 GLfloat _scene_ambient[4];
98 GLfloat _scene_diffuse[4];
99 GLfloat _scene_specular[4];
101 // clear sky, fog and cloud color
102 GLfloat _sky_color[4];
103 GLfloat _fog_color[4];
104 GLfloat _cloud_color[4];
106 // clear sky and fog color adjusted for sunset effects
107 GLfloat _adj_fog_color[4];
108 GLfloat _adj_sky_color[4];
112 void update_sky_color ();
113 void update_adj_fog_color ();
120 virtual void init ();
121 virtual void reinit ();
122 virtual void bind ();
123 virtual void unbind ();
124 virtual void update ( double dt );
127 // Color related functions
129 inline float *scene_ambient () const { return (float *)_scene_ambient; }
130 inline float *scene_diffuse () const { return (float *)_scene_diffuse; }
131 inline float *scene_specular () const { return (float *)_scene_specular; }
133 inline float *sky_color () const { return (float *)_sky_color; }
134 inline float *cloud_color () const { return (float *)_cloud_color; }
135 inline float *adj_fog_color () const { return (float *)_adj_fog_color; }
138 // Sun related functions
140 inline double get_sun_angle () const { return _sun_angle; }
141 inline void set_sun_angle (double a) { _sun_angle = a; }
143 inline double get_sun_rotation () const { return _sun_rotation; }
144 inline void set_sun_rotation (double r) { _sun_rotation = r; }
146 inline double get_sun_lon () const { return _sun_lon; }
147 inline void set_sun_lon (double l) { _sun_lon = l; }
149 inline double get_sun_gc_lat () const { return _sun_gc_lat; }
150 inline void set_sun_gc_lat (double l) { _sun_gc_lat = l; }
152 inline const SGVec3d& get_sunpos () const { return _sunpos; }
153 inline void set_sunpos (const SGVec3d& p) { _sunpos = p; }
155 inline float *sun_vec () const { return (float *)_sun_vec; }
156 inline float *sun_vec_inv () const { return (float *)_sun_vec_inv; }
159 // Moon related functions
161 inline double get_moon_angle () const { return _moon_angle; }
162 inline void set_moon_angle (double a) { _moon_angle = a; }
164 inline double get_moon_rotation () const { return _moon_rotation; }
165 inline void set_moon_rotation (double r) { _moon_rotation = r; }
167 inline double get_moon_lon () const { return _moon_lon; }
168 inline void set_moon_lon (double l) { _moon_lon = l; }
170 inline double get_moon_gc_lat () const { return _moon_gc_lat; }
171 inline void set_moon_gc_lat (double l) { _moon_gc_lat = l; }
173 inline const SGVec3d& get_moonpos () const { return _moonpos; }
174 inline void set_moonpos (const SGVec3d& p) { _moonpos = p; }
176 inline float *moon_vec () const { return (float *)_moon_vec; }
177 inline float *moon_vec_inv () const { return (float *)_moon_vec_inv; }