]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/cloudfield.cxx
Stuart :
[simgear.git] / simgear / scene / sky / cloudfield.cxx
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 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <osg/Fog>
28 #include <osg/Texture2D>
29 #include <osg/PositionAttitudeTransform>
30 #include <osg/Vec4f>
31
32 #include <simgear/compiler.h>
33
34 #include <plib/sg.h>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/math/polar3d.hxx>
38
39 #include <algorithm>
40 #include <vector>
41
42 using std::vector;
43
44 #include <simgear/environment/visual_enviro.hxx>
45 #include <simgear/scene/util/RenderConstants.hxx>
46 #include <simgear/scene/util/SGUpdateVisitor.hxx>
47 #include "sky.hxx"
48 #include "newcloud.hxx"
49 #include "cloudfield.hxx"
50
51 #if defined(__MINGW32__)
52 #define isnan(x) _isnan(x)
53 #endif
54
55 #if defined (__FreeBSD__)
56 #  if __FreeBSD_version < 500000
57      extern "C" {
58        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
59      }
60 #  endif
61 #endif
62
63 using namespace simgear;
64
65
66 #if defined (__CYGWIN__)
67 #include <ieeefp.h>
68 #endif
69
70 float SGCloudField::fieldSize = 50000.0f;
71 float SGCloudField::coverage = 1.0f;
72 double SGCloudField::timer_dt = 0.0;
73 float SGCloudField::view_distance = 20000.0f;
74 sgVec3 SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
75 SGCloudField::StateSetMap SGCloudField::cloudTextureMap;
76
77 // reposition the cloud layer at the specified origin and orientation
78 bool SGCloudField::reposition( const SGVec3f& p, const SGVec3f& up, double lon, double lat,
79                                double dt, int asl )
80 {
81     osg::Matrix T, LON, LAT;
82     
83     // Always update the altitude transform, as this allows
84     // the clouds to rise and fall smoothly depending on environment updates.
85     altitude_transform->setPosition(osg::Vec3d(0.0, 0.0, (double) asl));
86     
87     // Calculating the reposition information is expensive. 
88     // Only perform the reposition every 60 frames.
89     reposition_count = (reposition_count + 1) % 60;
90     if ((reposition_count != 0) || !defined3D) return false;
91     
92     SGGeoc pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
93     
94     double dist = SGGeodesy::distanceM(cld_pos, pos);
95     
96     if (dist > (fieldSize * 2)) {
97         // First time or very large distance
98         SGVec3<double> cart;
99         SGGeodesy::SGGeodToCart(SGGeod::fromRad(lon, lat), cart);
100         T.makeTranslate(cart.osg());
101         
102         LON.makeRotate(lon, osg::Vec3(0, 0, 1));
103         LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - lat, osg::Vec3(0, 1, 0));
104
105         field_transform->setMatrix( LAT*LON*T );
106         cld_pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
107     } else if (dist > fieldSize) {
108         // Distance requires repositioning of cloud field.
109         // We can easily work out the direction to reposition
110         // from the course between the cloud position and the
111         // camera position.
112         SGGeoc pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
113         
114         float crs = SGGeoc::courseDeg(cld_pos, pos);
115         if ((crs < 45.0) || (crs > 315.0)) {
116             SGGeodesy::advanceRadM(cld_pos, 0.0, fieldSize, cld_pos);
117         }
118         
119         if ((crs > 45.0) && (crs < 135.0)) {
120             SGGeodesy::advanceRadM(cld_pos, SGD_PI_2, fieldSize, cld_pos);
121         }
122
123         if ((crs > 135.0) && (crs < 225.0)) {
124             SGGeodesy::advanceRadM(cld_pos, SGD_PI, fieldSize, cld_pos);
125         }
126         
127         if ((crs > 225.0) && (crs < 315.0)) {
128             SGGeodesy::advanceRadM(cld_pos, SGD_PI + SGD_PI_2, fieldSize, cld_pos);
129         }
130         
131         SGVec3<double> cart;
132         SGGeodesy::SGGeodToCart(SGGeod::fromRad(cld_pos.getLongitudeRad(), cld_pos.getLatitudeRad()), cart);
133         T.makeTranslate(cart.osg());
134         
135         LON.makeRotate(cld_pos.getLongitudeRad(), osg::Vec3(0, 0, 1));
136         LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - cld_pos.getLatitudeRad(), osg::Vec3(0, 1, 0));
137
138         field_transform->setMatrix( LAT*LON*T );
139     }
140     
141     field_root->getStateSet()->setRenderBinDetails(asl, "RenderBin");
142
143     return true;
144 }
145
146 SGCloudField::SGCloudField() :
147         field_root(new osg::Group),
148         field_transform(new osg::MatrixTransform),
149         altitude_transform(new osg::PositionAttitudeTransform),
150         deltax(0.0),
151         deltay(0.0),
152         last_course(0.0),
153         last_coverage(0.0),
154         defined3D(false),
155         reposition_count(0)
156 {
157     cld_pos = SGGeoc();
158     field_root->addChild(field_transform.get());
159     field_root->setName("3D Cloud field root");
160     osg::StateSet *rootSet = field_root->getOrCreateStateSet();
161     rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
162     rootSet->setAttributeAndModes(getFog());
163     
164     osg::ref_ptr<osg::Group> quad_root = new osg::Group();
165     
166     for (int i = 0; i < BRANCH_SIZE; i++) {
167         for (int j = 0; j < BRANCH_SIZE; j++) {
168             quad[i][j] = new osg::LOD();
169             quad[i][j]->setName("Quad");
170             quad_root->addChild(quad[i][j].get());
171         }
172     }
173     
174     int leafs = QUADTREE_SIZE / BRANCH_SIZE;
175
176     for (int x = 0; x < QUADTREE_SIZE; x++) {
177         for (int y = 0; y < QUADTREE_SIZE; y++) {
178             field_group[x][y]= new osg::Switch;
179             field_group[x][y]->setName("3D cloud group");
180             
181             // Work out where to put this node in the quad tree
182             int i = x / leafs;
183             int j = y / leafs;
184             quad[i][j]->addChild(field_group[x][y].get(), 0.0f, view_distance);
185         }
186     }
187     
188     field_transform->addChild(altitude_transform.get());
189             
190     // We duplicate the defined field group in a 3x3 array. This way,
191     // we can simply shift entire groups around.
192     // TODO: "Bend" the edge groups so when shifted they line up.
193     // Currently the clouds "jump down" when we reposition them.
194     for(int x = -1 ; x <= 1 ; x++) {
195         for(int y = -1 ; y <= 1 ; y++ ) {
196             osg::ref_ptr<osg::PositionAttitudeTransform> transform =
197                     new osg::PositionAttitudeTransform;
198             transform->addChild(quad_root.get());
199             transform->setPosition(osg::Vec3(x*fieldSize, y * fieldSize, 0.0));
200             
201             altitude_transform->addChild(transform.get());
202         }
203     }
204 }
205
206 SGCloudField::~SGCloudField() {
207 }
208
209
210 void SGCloudField::clear(void) {
211     for (int x = 0; x < QUADTREE_SIZE; x++) {
212         for (int y = 0; y < QUADTREE_SIZE; y++) {
213             int num_children = field_group[x][y]->getNumChildren();
214
215             for (int i = 0; i < num_children; i++) {
216                 field_group[x][y]->removeChild(i);
217             }
218         }
219     }
220     
221     SGCloudField::defined3D = false;
222 }
223
224 // use a table or else we see poping when moving the slider...
225 static int densTable[][10] = {
226         {0,0,0,0,0,0,0,0,0,0},
227         {1,0,0,0,0,0,0,0,0,0},
228         {1,0,0,0,1,0,0,0,0,0},
229         {1,0,0,0,1,0,0,1,0,0}, // 30%
230         {1,0,1,0,1,0,0,1,0,0},
231         {1,0,1,0,1,0,1,1,0,0}, // 50%
232         {1,0,1,0,1,0,1,1,0,1},
233         {1,0,1,1,1,0,1,1,0,1}, // 70%
234         {1,1,1,1,1,0,1,1,0,1},
235         {1,1,1,1,1,0,1,1,1,1}, // 90%
236         {1,1,1,1,1,1,1,1,1,1}
237 };
238
239 void SGCloudField::applyCoverage(void) {
240
241         int row = (int) (coverage * 10.0);
242         if (row > 10) row = 9;
243         int col = 0;
244
245         if (coverage != last_coverage) {
246             for (int x = 0; x < QUADTREE_SIZE; x++) {
247                 for (int y = 0; y < QUADTREE_SIZE; y++) {
248                 // Switch on/off the children depending on the required coverage.
249                     int num_children = field_group[x][y]->getNumChildren();
250                     for (int i = 0; i < num_children; i++) {
251                         if (++col > 9) col = 0;
252                         if ( densTable[row][col] ) {
253                             field_group[x][y]->setValue(i, true);
254                         } else {
255                             field_group[x][y]->setValue(i, false);
256                         }
257                     }
258                 }
259             }
260         }
261
262         last_coverage = coverage;
263 }
264
265 void SGCloudField::addCloud( SGVec3f& pos, SGNewCloud *cloud) {
266         defined3D = true;
267         osg::ref_ptr<osg::Geode> geode = cloud->genCloud();
268         
269         // Determine which quadtree to put it in.
270         int x = (int) floor((pos.x() + fieldSize/2.0) * QUADTREE_SIZE / fieldSize);
271         if (x >= QUADTREE_SIZE) x = (QUADTREE_SIZE - 1);
272         if (x < 0) x = 0;
273         
274         int y = (int) floor((pos.y() + fieldSize/2.0) * QUADTREE_SIZE / fieldSize);
275         if (y >= QUADTREE_SIZE) y = (QUADTREE_SIZE - 1);
276         if (y < 0) y = 0;
277         
278         osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
279
280         transform->setPosition(pos.osg());
281         transform->addChild(geode.get());
282         
283         field_group[x][y]->addChild(transform.get(), true);
284 }
285
286 void SGCloudField::applyVisRange(void) { 
287     
288     for (int x = 0; x < BRANCH_SIZE; x++) {
289         for (int y = 0; y < BRANCH_SIZE; y++) {
290             int num_children = quad[x][y]->getNumChildren(); 
291             for (int i = 0; i < num_children; i++) { 
292                 quad[x][y]->setRange(i, 0.0f, view_distance);
293             }
294         }
295     }
296 }
297
298 SGCloudField::CloudFog::CloudFog()
299 {
300     fog = new osg::Fog;
301     fog->setMode(osg::Fog::EXP2);
302     fog->setDataVariance(osg::Object::DYNAMIC);
303 }
304
305 void SGCloudField::updateFog(double visibility, const osg::Vec4f& color)
306 {
307     const double sqrt_m_log01 = sqrt(-log(0.01));
308     osg::Fog* fog = CloudFog::instance()->fog.get();
309     fog->setColor(color);
310     fog->setDensity(sqrt_m_log01 / visibility);
311 }