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++
37 #include <simgear/compiler.h>
39 #include <simgear/props/props.hxx>
40 #include <simgear/structure/subsystem_mgr.hxx>
41 #include <simgear/props/tiedpropertylist.hxx>
42 #include <simgear/math/interpolater.hxx>
45 // Define a structure containing the global lighting parameters
46 class FGLight : public SGSubsystem
52 * Lighting look up tables (based on sun angle with local horizon)
54 SGInterpTable *_ambient_tbl, *_diffuse_tbl, *_specular_tbl;
55 SGInterpTable *_sky_tbl;
58 * position of the sun and moon in various forms
61 // in geocentric coordinates
62 double _sun_lon, _sun_lat;
63 double _moon_lon, _moon_gc_lat;
65 // (in view coordinates)
66 SGVec4f _sun_vec, _moon_vec;
68 // inverse (in view coordinates)
69 SGVec4f _sun_vec_inv, _moon_vec_inv;
71 // the angle between the celestial object and the local horizontal
73 double _sun_angle, _moon_angle;
74 double _prev_sun_angle;
76 // the rotation around our vertical axis of the sun (relative to
77 // due south with positive numbers going in the counter clockwise
78 // direction.) This is the direction we'd need to face if we
79 // wanted to travel towards celestial object.
80 double _sun_rotation, _moon_rotation;
83 * Derived lighting values
86 // ambient, diffuse and specular component
87 SGVec4f _scene_ambient;
88 SGVec4f _scene_diffuse;
89 SGVec4f _scene_specular;
90 SGVec4f _scene_chrome;
92 // clear sky, fog and cloud color
98 // clear sky and fog color adjusted for sunset effects
99 SGVec4f _adj_fog_color;
100 SGVec4f _adj_sky_color;
102 // input parameters affected by the weather system
109 void update_sky_color ();
110 void update_adj_fog_color ();
114 // properties for chrome light; not a tie because I want to fire
115 // property listeners when the values change.
116 SGPropertyNode_ptr _chromeProps[4];
118 SGPropertyNode_ptr _sunAngleRad;
120 SGPropertyNode_ptr _humidity;
122 simgear::TiedPropertyList _tiedProperties;
125 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
127 template <typename T>
128 void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue)
130 _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue);
138 virtual void init ();
139 virtual void reinit ();
140 virtual void bind ();
141 virtual void unbind ();
142 virtual void update ( double dt );
145 // Color related functions
147 inline const SGVec4f& scene_ambient () const { return _scene_ambient; }
148 inline const SGVec4f& scene_diffuse () const { return _scene_diffuse; }
149 inline const SGVec4f& scene_specular () const { return _scene_specular; }
150 inline const SGVec4f& scene_chrome () const { return _scene_chrome; }
152 inline const SGVec4f& sky_color () const { return _sky_color; }
153 inline const SGVec4f& cloud_color () const { return _cloud_color; }
154 inline const SGVec4f& adj_fog_color () const { return _adj_fog_color; }
155 inline const SGVec4f& adj_sky_color () const { return _adj_sky_color; }
157 // Sun related functions
159 inline double get_sun_angle () const { return _sun_angle; }
160 inline void set_sun_angle (double a) { _sun_angle = a; }
162 inline double get_sun_rotation () const { return _sun_rotation; }
163 inline void set_sun_rotation (double r) { _sun_rotation = r; }
165 inline double get_sun_lon () const { return _sun_lon; }
166 inline void set_sun_lon (double l) { _sun_lon = l; }
168 inline double get_sun_lat () const { return _sun_lat; }
169 inline void set_sun_lat (double l) { _sun_lat = l; }
171 inline SGVec4f& sun_vec () { return _sun_vec; }
172 inline SGVec4f& sun_vec_inv () { return _sun_vec_inv; }
175 // Moon related functions
177 inline double get_moon_angle () const { return _moon_angle; }
178 inline void set_moon_angle (double a) { _moon_angle = a; }
180 inline double get_moon_rotation () const { return _moon_rotation; }
181 inline void set_moon_rotation (double r) { _moon_rotation = r; }
183 inline double get_moon_lon () const { return _moon_lon; }
184 inline void set_moon_lon (double l) { _moon_lon = l; }
186 inline double get_moon_gc_lat () const { return _moon_gc_lat; }
187 inline void set_moon_gc_lat (double l) { _moon_gc_lat = l; }
189 inline const SGVec4f& moon_vec () const { return _moon_vec; }
190 inline const SGVec4f& moon_vec_inv () const { return _moon_vec_inv; }