| 1 | // light.hxx -- lighting routines |
| 2 | // |
| 3 | // Written by Curtis Olson, started April 1998. |
| 4 | // |
| 5 | // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 | #include <simgear/compiler.h> |
| 38 | |
| 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> |
| 43 | |
| 44 | |
| 45 | // Define a structure containing the global lighting parameters |
| 46 | class FGLight : public SGSubsystem |
| 47 | { |
| 48 | private: |
| 49 | /* |
| 50 | * Lighting look up tables (based on sun angle with local horizon) |
| 51 | */ |
| 52 | std::unique_ptr<SGInterpTable> _ambient_tbl, _diffuse_tbl, _specular_tbl; |
| 53 | std::unique_ptr<SGInterpTable> _sky_tbl; |
| 54 | |
| 55 | /** |
| 56 | * position of the sun and moon in various forms |
| 57 | */ |
| 58 | |
| 59 | // in geocentric coordinates |
| 60 | double _sun_lon = 0.0, _sun_lat = 0.0; |
| 61 | double _moon_lon = 0.0, _moon_gc_lat = 0.0; |
| 62 | |
| 63 | // (in view coordinates) |
| 64 | SGVec4f _sun_vec = {0, 0, 0, 0}; |
| 65 | SGVec4f _moon_vec = {0, 0, 0, 0}; |
| 66 | |
| 67 | |
| 68 | // inverse (in view coordinates) |
| 69 | SGVec4f _sun_vec_inv = {0, 0, 0, 0}; |
| 70 | SGVec4f _moon_vec_inv = {0, 0, 0, 0}; |
| 71 | |
| 72 | // the angle between the celestial object and the local horizontal |
| 73 | // (in radians) |
| 74 | double _sun_angle = 0.0 , _moon_angle = 0.0; |
| 75 | double _prev_sun_angle = 0.0; |
| 76 | |
| 77 | // the rotation around our vertical axis of the sun (relative to |
| 78 | // due south with positive numbers going in the counter clockwise |
| 79 | // direction.) This is the direction we'd need to face if we |
| 80 | // wanted to travel towards celestial object. |
| 81 | double _sun_rotation = 0.0, _moon_rotation = 0.0; |
| 82 | |
| 83 | /** |
| 84 | * Derived lighting values |
| 85 | */ |
| 86 | |
| 87 | // ambient, diffuse and specular component |
| 88 | SGVec4f _scene_ambient = {0, 0, 0, 0}; |
| 89 | SGVec4f _scene_diffuse = {0, 0, 0, 0}; |
| 90 | SGVec4f _scene_specular = {0, 0, 0, 0}; |
| 91 | SGVec4f _scene_chrome = {0, 0, 0, 0}; |
| 92 | |
| 93 | // clear sky, fog and cloud color |
| 94 | SGVec4f _sun_color = {1, 1, 1, 0}; |
| 95 | SGVec4f _sky_color = {0, 0, 0, 0}; |
| 96 | SGVec4f _fog_color = {0, 0, 0, 0}; |
| 97 | SGVec4f _cloud_color = {0, 0, 0, 0}; |
| 98 | |
| 99 | // clear sky and fog color adjusted for sunset effects |
| 100 | SGVec4f _adj_fog_color = {0, 0, 0, 0}; |
| 101 | SGVec4f _adj_sky_color = {0, 0, 0, 0}; |
| 102 | |
| 103 | // input parameters affected by the weather system |
| 104 | float _saturation = 1.0f; |
| 105 | float _scattering = 0.8f; |
| 106 | float _overcast = 0.0f; |
| 107 | |
| 108 | double _dt_total = 0.0; |
| 109 | |
| 110 | void update_sky_color (); |
| 111 | void update_adj_fog_color (); |
| 112 | |
| 113 | // update all solar system bodies of interest |
| 114 | void updateObjects(); |
| 115 | |
| 116 | // update the position of one solar system body |
| 117 | void updateBodyPos(bool sun_not_moon, double& lon, double& lat, |
| 118 | SGVec4f& vec, SGVec4f& vec_inv, |
| 119 | double& angle, SGPropertyNode_ptr AngleRad, |
| 120 | double& rotation); |
| 121 | |
| 122 | // properties for chrome light; not a tie because I want to fire |
| 123 | // property listeners when the values change. |
| 124 | SGPropertyNode_ptr _chromeProps[4]; |
| 125 | |
| 126 | SGPropertyNode_ptr _sunAngleRad; |
| 127 | SGPropertyNode_ptr _moonAngleRad; |
| 128 | |
| 129 | SGPropertyNode_ptr _humidity; |
| 130 | |
| 131 | simgear::TiedPropertyList _tiedProperties; |
| 132 | |
| 133 | /** |
| 134 | * Tied-properties helper, record nodes which are tied for easy un-tie-ing |
| 135 | */ |
| 136 | template <typename T> |
| 137 | void tie(SGPropertyNode* aNode, const char* aRelPath, const SGRawValue<T>& aRawValue) |
| 138 | { |
| 139 | _tiedProperties.Tie(aNode->getNode(aRelPath, true), aRawValue); |
| 140 | } |
| 141 | |
| 142 | public: |
| 143 | FGLight () = default; |
| 144 | virtual ~FGLight () = default; |
| 145 | |
| 146 | // Subsystem API. |
| 147 | void bind() override; |
| 148 | void init() override; |
| 149 | void reinit() override; |
| 150 | void unbind() override; |
| 151 | void update(double dt) override; |
| 152 | |
| 153 | // Subsystem identification. |
| 154 | static const char* staticSubsystemClassId() { return "lighting"; } |
| 155 | |
| 156 | // Color related functions |
| 157 | |
| 158 | inline const SGVec4f& scene_ambient () const { return _scene_ambient; } |
| 159 | inline const SGVec4f& scene_diffuse () const { return _scene_diffuse; } |
| 160 | inline const SGVec4f& scene_specular () const { return _scene_specular; } |
| 161 | inline const SGVec4f& scene_chrome () const { return _scene_chrome; } |
| 162 | |
| 163 | inline const SGVec4f& sky_color () const { return _sky_color; } |
| 164 | inline const SGVec4f& cloud_color () const { return _cloud_color; } |
| 165 | inline const SGVec4f& adj_fog_color () const { return _adj_fog_color; } |
| 166 | inline const SGVec4f& adj_sky_color () const { return _adj_sky_color; } |
| 167 | |
| 168 | // Sun related functions |
| 169 | |
| 170 | inline double get_sun_angle () const { return _sun_angle; } |
| 171 | inline void set_sun_angle (double a) { _sun_angle = a; } |
| 172 | |
| 173 | inline double get_sun_rotation () const { return _sun_rotation; } |
| 174 | inline void set_sun_rotation (double r) { _sun_rotation = r; } |
| 175 | |
| 176 | inline double get_sun_lon () const { return _sun_lon; } |
| 177 | inline void set_sun_lon (double l) { _sun_lon = l; } |
| 178 | |
| 179 | inline double get_sun_lat () const { return _sun_lat; } |
| 180 | inline void set_sun_lat (double l) { _sun_lat = l; } |
| 181 | |
| 182 | inline SGVec4f& sun_vec () { return _sun_vec; } |
| 183 | inline SGVec4f& sun_vec_inv () { return _sun_vec_inv; } |
| 184 | |
| 185 | |
| 186 | // Moon related functions |
| 187 | |
| 188 | inline double get_moon_angle () const { return _moon_angle; } |
| 189 | inline void set_moon_angle (double a) { _moon_angle = a; } |
| 190 | |
| 191 | inline double get_moon_rotation () const { return _moon_rotation; } |
| 192 | inline void set_moon_rotation (double r) { _moon_rotation = r; } |
| 193 | |
| 194 | inline double get_moon_lon () const { return _moon_lon; } |
| 195 | inline void set_moon_lon (double l) { _moon_lon = l; } |
| 196 | |
| 197 | inline double get_moon_gc_lat () const { return _moon_gc_lat; } |
| 198 | inline void set_moon_gc_lat (double l) { _moon_gc_lat = l; } |
| 199 | |
| 200 | inline const SGVec4f& moon_vec () const { return _moon_vec; } |
| 201 | inline const SGVec4f& moon_vec_inv () const { return _moon_vec_inv; } |
| 202 | }; |
| 203 | |
| 204 | #endif // _LIGHT_HXX |
| 205 |