1 // a layer of 3d clouds
3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
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.
24 # include <simgear_config.h>
28 #include <osg/Texture2D>
29 #include <osg/PositionAttitudeTransform>
32 #include <simgear/compiler.h>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/math/polar3d.hxx>
44 #include <simgear/environment/visual_enviro.hxx>
45 #include <simgear/scene/util/RenderConstants.hxx>
46 #include <simgear/scene/util/SGUpdateVisitor.hxx>
48 #include "newcloud.hxx"
49 #include "cloudfield.hxx"
51 #if defined(__MINGW32__)
52 #define isnan(x) _isnan(x)
55 #if defined (__FreeBSD__)
56 # if __FreeBSD_version < 500000
58 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
63 using namespace simgear;
66 #if defined (__CYGWIN__)
70 float SGCloudField::fieldSize = 50000.0f;
71 double SGCloudField::timer_dt = 0.0;
72 float SGCloudField::view_distance = 20000.0f;
73 sgVec3 SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
74 SGCloudField::StateSetMap SGCloudField::cloudTextureMap;
76 // reposition the cloud layer at the specified origin and orientation
77 bool SGCloudField::reposition( const SGVec3f& p, const SGVec3f& up, double lon, double lat,
80 osg::Matrix T, LON, LAT;
82 // Always update the altitude transform, as this allows
83 // the clouds to rise and fall smoothly depending on environment updates.
84 altitude_transform->setPosition(osg::Vec3d(0.0, 0.0, (double) asl));
86 // Calculating the reposition information is expensive.
87 // Only perform the reposition every 60 frames.
88 reposition_count = (reposition_count + 1) % 60;
89 if ((reposition_count != 0) || !defined3D) return false;
91 SGGeoc pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
93 double dist = SGGeodesy::distanceM(cld_pos, pos);
95 if (dist > (fieldSize * 2)) {
96 // First time or very large distance
98 SGGeodesy::SGGeodToCart(SGGeod::fromRad(lon, lat), cart);
99 T.makeTranslate(toOsg(cart));
101 LON.makeRotate(lon, osg::Vec3(0, 0, 1));
102 LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - lat, osg::Vec3(0, 1, 0));
104 field_transform->setMatrix( LAT*LON*T );
105 cld_pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
106 } else if (dist > fieldSize) {
107 // Distance requires repositioning of cloud field.
108 // We can easily work out the direction to reposition
109 // from the course between the cloud position and the
111 SGGeoc pos = SGGeoc::fromGeod(SGGeod::fromRad(lon, lat));
113 float crs = SGGeoc::courseDeg(cld_pos, pos);
114 if ((crs < 45.0) || (crs > 315.0)) {
115 SGGeodesy::advanceRadM(cld_pos, 0.0, fieldSize, cld_pos);
118 if ((crs > 45.0) && (crs < 135.0)) {
119 SGGeodesy::advanceRadM(cld_pos, SGD_PI_2, fieldSize, cld_pos);
122 if ((crs > 135.0) && (crs < 225.0)) {
123 SGGeodesy::advanceRadM(cld_pos, SGD_PI, fieldSize, cld_pos);
126 if ((crs > 225.0) && (crs < 315.0)) {
127 SGGeodesy::advanceRadM(cld_pos, SGD_PI + SGD_PI_2, fieldSize, cld_pos);
131 SGGeodesy::SGGeodToCart(SGGeod::fromRad(cld_pos.getLongitudeRad(), cld_pos.getLatitudeRad()), cart);
132 T.makeTranslate(toOsg(cart));
134 LON.makeRotate(cld_pos.getLongitudeRad(), osg::Vec3(0, 0, 1));
135 LAT.makeRotate(90.0 * SGD_DEGREES_TO_RADIANS - cld_pos.getLatitudeRad(), osg::Vec3(0, 1, 0));
137 field_transform->setMatrix( LAT*LON*T );
140 field_root->getStateSet()->setRenderBinDetails(asl, "DepthSortedBin");
145 SGCloudField::SGCloudField() :
146 field_root(new osg::Group),
147 field_transform(new osg::MatrixTransform),
148 altitude_transform(new osg::PositionAttitudeTransform),
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());
164 osg::ref_ptr<osg::Group> quad_root = new osg::Group();
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());
174 int leafs = QUADTREE_SIZE / BRANCH_SIZE;
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");
181 // Work out where to put this node in the quad tree
184 quad[i][j]->addChild(field_group[x][y].get(), 0.0f, view_distance);
188 field_transform->addChild(altitude_transform.get());
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));
201 altitude_transform->addChild(transform.get());
206 SGCloudField::~SGCloudField() {
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 field_group[x][y]->removeChildren(0, num_children);
218 SGCloudField::defined3D = false;
221 // use a table or else we see poping when moving the slider...
222 static int densTable[][10] = {
223 {0,0,0,0,0,0,0,0,0,0},
224 {1,0,0,0,0,0,0,0,0,0},
225 {1,0,0,0,1,0,0,0,0,0},
226 {1,0,0,0,1,0,0,1,0,0}, // 30%
227 {1,0,1,0,1,0,0,1,0,0},
228 {1,0,1,0,1,0,1,1,0,0}, // 50%
229 {1,0,1,0,1,0,1,1,0,1},
230 {1,0,1,1,1,0,1,1,0,1}, // 70%
231 {1,1,1,1,1,0,1,1,0,1},
232 {1,1,1,1,1,0,1,1,1,1}, // 90%
233 {1,1,1,1,1,1,1,1,1,1}
236 void SGCloudField::applyCoverage(void) {
238 int row = (int) (coverage * 10.0);
239 if (row > 9) row = 9;
242 if (coverage != last_coverage) {
243 for (int x = 0; x < QUADTREE_SIZE; x++) {
244 for (int y = 0; y < QUADTREE_SIZE; y++) {
245 // Switch on/off the children depending on the required coverage.
246 int num_children = field_group[x][y]->getNumChildren();
247 for (int i = 0; i < num_children; i++) {
248 if (++col > 9) col = 0;
249 if ( densTable[row][col] ) {
250 field_group[x][y]->setValue(i, true);
252 field_group[x][y]->setValue(i, false);
259 last_coverage = coverage;
262 void SGCloudField::addCloud( SGVec3f& pos, SGNewCloud *cloud) {
264 osg::ref_ptr<osg::Geode> geode = cloud->genCloud();
266 // Determine which quadtree to put it in.
267 int x = (int) floor((pos.x() + fieldSize/2.0) * QUADTREE_SIZE / fieldSize);
268 if (x >= QUADTREE_SIZE) x = (QUADTREE_SIZE - 1);
271 int y = (int) floor((pos.y() + fieldSize/2.0) * QUADTREE_SIZE / fieldSize);
272 if (y >= QUADTREE_SIZE) y = (QUADTREE_SIZE - 1);
275 osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
277 transform->setPosition(toOsg(pos));
278 transform->addChild(geode.get());
280 field_group[x][y]->addChild(transform.get(), true);
283 void SGCloudField::applyVisRange(void) {
285 for (int x = 0; x < BRANCH_SIZE; x++) {
286 for (int y = 0; y < BRANCH_SIZE; y++) {
287 int num_children = quad[x][y]->getNumChildren();
288 for (int i = 0; i < num_children; i++) {
289 quad[x][y]->setRange(i, 0.0f, view_distance);
295 SGCloudField::CloudFog::CloudFog()
298 fog->setMode(osg::Fog::EXP2);
299 fog->setDataVariance(osg::Object::DYNAMIC);
302 void SGCloudField::updateFog(double visibility, const osg::Vec4f& color)
304 const double sqrt_m_log01 = sqrt(-log(0.01));
305 osg::Fog* fog = CloudFog::instance()->fog.get();
306 fog->setColor(color);
307 fog->setDensity(sqrt_m_log01 / visibility);