]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloudfield.hxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[simgear.git] / simgear / scene / sky / cloudfield.hxx
1 // a layer of 3d clouds
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
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 //
22
23 #ifndef _CLOUDFIELD_HXX
24 #define _CLOUDFIELD_HXX
25
26 #include <simgear/compiler.h>
27 #include <vector>
28 #include <map>
29
30 #include <osgDB/ReaderWriter>
31
32 #include <osg/ref_ptr>
33 #include <osg/Array>
34 #include <osg/Geometry>
35 #include <osg/Group>
36 #include <osg/Switch>
37
38 namespace osg
39 {
40         class Fog;
41         class StateSet;
42         class Vec4f;
43 }
44
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/structure/Singleton.hxx>
47 #include <simgear/math/SGMath.hxx>
48
49 using std::vector;
50
51 class SGNewCloud;
52
53 namespace simgear
54 {
55     class EffectGeode;
56 }
57
58 typedef std::map<int, osg::ref_ptr<osg::PositionAttitudeTransform> > CloudHash;
59
60 /**
61  * A layer of 3D clouds, defined by lat/long/alt.
62  */
63 class SGCloudField {
64
65 private:
66         class Cloud  {
67         public:
68                 SGNewCloud      *aCloud;
69                 SGVec3f         pos;
70                 bool            visible;
71         };
72
73   float Rnd(float);
74         
75   // Theoretical maximum cloud depth, used for fudging the LoD
76   // ranges to ensure that clouds become visible at maximum range
77   static float MAX_CLOUD_DEPTH;
78
79   // this is a relative position only, with that we can move all clouds at once
80   SGVec3f relative_position;
81
82   osg::ref_ptr<osg::Group> field_root;
83   osg::ref_ptr<osg::Group> placed_root;
84   osg::ref_ptr<osg::PositionAttitudeTransform> field_transform;
85   osg::ref_ptr<osg::PositionAttitudeTransform> altitude_transform;
86   osg::ref_ptr<osg::LOD> field_lod;
87
88   osg::Vec3f old_pos;
89   CloudHash cloud_hash;
90
91   struct CloudFog : public simgear::Singleton<CloudFog>
92   {
93     CloudFog();
94     osg::ref_ptr<osg::Fog> fog;
95   };
96
97   void removeCloudFromTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform);
98   void addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform, float lon, float lat, float alt, float x, float y);
99   void addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform, SGGeod loc, float x, float y);
100   void addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform, SGGeod loc);
101   void applyVisRangeAndCoverage(void);
102
103 public:
104
105         SGCloudField();
106         ~SGCloudField();
107
108         void clear(void);
109         bool isDefined3D(void);
110
111         // add one cloud, data is not copied, ownership given
112         void addCloud( SGVec3f& pos, osg::ref_ptr<simgear::EffectGeode> cloud);
113
114         /**
115          * Add a new cloud with a given index at a specific point defined by lon/lat and an x/y offset
116          */
117         bool addCloud(float lon, float lat, float alt, int index, osg::ref_ptr<simgear::EffectGeode> geode);
118         bool addCloud(float lon, float lat, float alt, float x, float y, int index, osg::ref_ptr<simgear::EffectGeode> geode);
119
120         // Cloud handling functions.
121   bool deleteCloud(int identifier);
122   bool repositionCloud(int identifier, float lon, float lat, float alt);
123   bool repositionCloud(int identifier, float lon, float lat, float alt, float x, float y);
124
125
126   /**
127     * reposition the cloud layer at the specified origin and
128     * orientation.
129     * @param p position vector
130     * @param up the local up vector
131     * @param lon specifies a rotation about the Z axis
132     * @param lat specifies a rotation about the new Y axis
133     * @param dt the time elapsed since the last call
134     * @param asl altitude of the layer
135     * @param speed of cloud layer movement (due to wind)
136     * @param direction of cloud layer movement (due to wind)
137     */
138   bool reposition( const SGVec3f& p, const SGVec3f& up,
139             double lon, double lat, double dt, int asl, float speed, float direction);
140
141   osg::Group* getNode() { return field_root.get(); }
142
143   // visibility distance for clouds in meters
144   static float CloudVis;
145
146   static SGVec3f view_vec, view_X, view_Y;
147
148   static float view_distance;
149   static float impostor_distance;
150   static float lod1_range;
151   static float lod2_range;
152   static bool use_impostors;
153   static double timer_dt;
154   static float fieldSize;
155   static bool wrap;
156
157   static bool getWrap(void) { return wrap; }
158   static void setWrap(bool w) { wrap = w; }
159
160   static float getImpostorDistance(void) { return impostor_distance; }
161   static void setImpostorDistance(float d) { impostor_distance = d; }
162   static float getLoD1Range(void) { return lod1_range; }
163   static void setLoD1Range(int d) { lod1_range = d; }
164   static float getLoD2Range(void) { return lod2_range; }
165   static void setLoD2Range(int d) { lod2_range = d; }
166   static void setUseImpostors(bool b) { use_impostors = b; }
167   static bool getUseImpostors(void) { return use_impostors; }
168   
169   static float getVisRange(void) { return view_distance; }
170   static void setVisRange(float d) { view_distance = d; }
171   void applyVisAndLoDRange(void);
172
173   static osg::Fog* getFog()
174   {
175           return CloudFog::instance()->fog.get();
176   }
177   static void updateFog(double visibility, const osg::Vec4f& color);
178 };
179
180 #endif // _CLOUDFIELD_HXX