]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloudfield.hxx
Replace OpenThreads with SGThread to avoid useless OSG dependency.
[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   // Radius of the LoD nodes for the dynamic quadtrees.
76   static float RADIUS_LEVEL_1;
77   static float RADIUS_LEVEL_2;
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 double timer_dt;
150   static float fieldSize;
151   static bool wrap;
152
153   static bool getWrap(void) { return wrap; }
154   static void setWrap(bool w) { wrap = w; }
155
156   static float getVisRange(void) { return view_distance; }
157   static void setVisRange(float d) { view_distance = d; }
158   void applyVisRange(void);
159
160   static osg::Fog* getFog()
161   {
162           return CloudFog::instance()->fog.get();
163   }
164   static void updateFog(double visibility, const osg::Vec4f& color);
165 };
166
167 #endif // _CLOUDFIELD_HXX