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>
31 #include <osgSim/Impostor>
33 #include <simgear/compiler.h>
35 #include <simgear/math/sg_random.h>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/scene/util/SGSceneUserData.hxx>
47 //#include <simgear/environment/visual_enviro.hxx>
48 #include <simgear/scene/util/RenderConstants.hxx>
49 #include <simgear/scene/util/SGUpdateVisitor.hxx>
51 #include "newcloud.hxx"
52 #include "cloudfield.hxx"
54 using namespace simgear;
56 float SGCloudField::fieldSize = 50000.0f;
57 double SGCloudField::timer_dt = 0.0;
58 float SGCloudField::view_distance = 20000.0f;
59 bool SGCloudField::wrap = true;
60 float SGCloudField::MAX_CLOUD_DEPTH = 2000.0f;
61 bool SGCloudField::use_impostors = false;
62 float SGCloudField::lod1_range = 8000.0f;
63 float SGCloudField::lod2_range = 4000.0f;
64 float SGCloudField::impostor_distance = 15000.0f;
66 int impostorcount = 0;
70 SGVec3f SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
73 // Reposition the cloud layer at the specified origin and orientation
74 bool SGCloudField::reposition( const SGVec3f& p, const SGVec3f& up, double lon, double lat,
75 double dt, int asl, float speed, float direction ) {
76 // Determine any movement of the placed clouds
77 if (placed_root->getNumChildren() == 0) return false;
80 SGGeod new_pos = SGGeod::fromRadFt(lon, lat, 0.0f);
82 SGGeodesy::SGGeodToCart(new_pos, cart);
83 osg::Vec3f osg_pos = toOsg(cart);
84 osg::Quat orient = toOsg(SGQuatd::fromLonLatRad(lon, lat) * SGQuatd::fromRealImag(0, SGVec3d(0, 1, 0)));
86 // Always update the altitude transform, as this allows
87 // the clouds to rise and fall smoothly depending on environment updates.
88 altitude_transform->setPosition(osg::Vec3f(0.0f, 0.0f, (float) asl));
90 // Similarly, always determine the effects of the wind
91 osg::Vec3f wind = osg::Vec3f(-cos((direction + 180)* SGD_DEGREES_TO_RADIANS) * speed * dt,
92 sin((direction + 180)* SGD_DEGREES_TO_RADIANS) * speed * dt,
94 osg::Vec3f windosg = field_transform->getAttitude() * wind;
95 field_transform->setPosition(field_transform->getPosition() + windosg);
98 // If we're not wrapping the cloudfield, then we make no effort to reposition
102 if ((old_pos - osg_pos).length() > fieldSize*2) {
103 // Big movement - reposition centered to current location.
104 field_transform->setPosition(osg_pos);
105 field_transform->setAttitude(orient);
108 osg::Quat fta = field_transform->getAttitude();
109 osg::Quat ftainv = field_transform->getAttitude().inverse();
111 // delta is the vector from the old position to the new position in cloud-coords
112 // osg::Vec3f delta = ftainv * (osg_pos - old_pos);
115 // Check if any of the clouds should be moved.
116 for(CloudHash::const_iterator itr = cloud_hash.begin(), end = cloud_hash.end();
120 osg::ref_ptr<osg::PositionAttitudeTransform> pat = itr->second;
126 osg::Vec3f currpos = field_transform->getPosition() + fta * pat->getPosition();
128 // Determine the vector from the new position to the cloud in cloud-space.
129 osg::Vec3f w = ftainv * (currpos - toOsg(cart));
131 // Determine a course if required. Note that this involves some axis translation.
134 if (w.x() > 0.6*fieldSize) { y = fieldSize; }
135 if (w.x() < -0.6*fieldSize) { y = -fieldSize; }
136 if (w.y() > 0.6*fieldSize) { x = -fieldSize; }
137 if (w.y() < -0.6*fieldSize) { x = fieldSize; }
139 if ((x != 0.0) || (y != 0.0)) {
140 removeCloudFromTree(pat);
141 SGGeod p = SGGeod::fromCart(toSG(field_transform->getPosition() +
142 fta * pat->getPosition()));
143 addCloudToTree(pat, p, x, y);
148 // Render the clouds in order from farthest away layer to nearest one.
149 field_root->getStateSet()->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
153 SGCloudField::SGCloudField() :
154 field_root(new osg::Group),
155 field_transform(new osg::PositionAttitudeTransform),
156 altitude_transform(new osg::PositionAttitudeTransform)
158 old_pos = osg::Vec3f(0.0f, 0.0f, 0.0f);
159 field_root->addChild(field_transform.get());
160 field_root->setName("3D Cloud field root");
161 osg::StateSet *rootSet = field_root->getOrCreateStateSet();
162 rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
163 rootSet->setAttributeAndModes(getFog());
165 field_transform->addChild(altitude_transform.get());
166 placed_root = new osg::Group();
167 altitude_transform->addChild(placed_root);
173 SGCloudField::~SGCloudField() {
177 void SGCloudField::clear(void) {
179 for(CloudHash::const_iterator itr = cloud_hash.begin(), end = cloud_hash.end();
182 removeCloudFromTree(itr->second);
188 void SGCloudField::applyVisAndLoDRange(void)
190 for (unsigned int i = 0; i < placed_root->getNumChildren(); i++) {
191 osg::ref_ptr<osg::LOD> lodnode1 = (osg::LOD*) placed_root->getChild(i);
192 for (unsigned int j = 0; j < lodnode1->getNumChildren(); j++) {
193 lodnode1->setRange(j, 0.0f, lod1_range + lod2_range + view_distance + MAX_CLOUD_DEPTH);
194 osg::ref_ptr<osg::LOD> lodnode2 = (osg::LOD*) lodnode1->getChild(j);
195 for (unsigned int k = 0; k < lodnode2->getNumChildren(); k++) {
196 lodnode2->setRange(k, 0.0f, view_distance + MAX_CLOUD_DEPTH);
202 bool SGCloudField::addCloud(float lon, float lat, float alt, int index, osg::ref_ptr<EffectGeode> geode) {
203 return addCloud(lon, lat, alt, 0.0f, 0.0f, index, geode);
206 bool SGCloudField::addCloud(float lon, float lat, float alt, float x, float y, int index, osg::ref_ptr<EffectGeode> geode) {
207 // If this cloud index already exists, don't replace it.
208 if (cloud_hash[index]) return false;
210 osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
212 transform->addChild(geode.get());
213 addCloudToTree(transform, lon, lat, alt, x, y);
214 cloud_hash[index] = transform;
218 // Remove a give cloud from inside the tree, without removing it from the cloud hash
219 void SGCloudField::removeCloudFromTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform)
226 osg::ref_ptr<osg::Group> lodnode = transform->getParent(0);
227 lodnode->removeChild(transform);
230 if (lodnode->getNumChildren() == 0) {
231 osg::ref_ptr<osg::Group> lodnode1 = lodnode->getParent(0);
232 osg::ref_ptr<osgSim::Impostor> impostornode = (osgSim::Impostor*) lodnode1->getParent(0);
234 lodnode1->removeChild(lodnode);
237 if (lodnode1->getNumChildren() == 0) {
238 impostornode->removeChild(lodnode1);
239 placed_root->removeChild(impostornode);
245 void SGCloudField::addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform,
246 float lon, float lat, float alt, float x, float y) {
248 // Get the base position
249 SGGeod loc = SGGeod::fromDegFt(lon, lat, alt);
250 addCloudToTree(transform, loc, x, y);
254 void SGCloudField::addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform,
255 SGGeod loc, float x, float y) {
257 float alt = loc.getElevationFt();
258 // Determine any shift by x/y
259 if ((x != 0.0f) || (y != 0.0f)) {
260 double crs = 90.0 - SG_RADIANS_TO_DEGREES * atan2(y, x);
261 double dst = sqrt(x*x + y*y);
264 SGGeod base_pos = SGGeod::fromGeodFt(loc, 0.0f);
265 SGGeodesy::direct(base_pos, crs, dst, loc, endcrs);
268 // The direct call provides the position at 0 alt, so adjust as required.
269 loc.setElevationFt(alt);
270 addCloudToTree(transform, loc);
274 void SGCloudField::addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform, SGGeod loc) {
275 // Work out where this cloud should go in OSG coordinates.
277 SGGeodesy::SGGeodToCart(loc, cart);
278 osg::Vec3f pos = toOsg(cart);
281 if (old_pos == osg::Vec3f(0.0f, 0.0f, 0.0f)) {
283 SGVec3<double> fieldcenter;
284 SGGeodesy::SGGeodToCart(SGGeod::fromDegFt(loc.getLongitudeDeg(), loc.getLatitudeDeg(), 0.0f), fieldcenter);
285 // Convert to the scenegraph orientation where we just rotate around
286 // the y axis 180 degrees.
287 osg::Quat orient = toOsg(SGQuatd::fromLonLatDeg(loc.getLongitudeDeg(), loc.getLatitudeDeg()) * SGQuatd::fromRealImag(0, SGVec3d(0, 1, 0)));
289 osg::Vec3f osg_pos = toOsg(fieldcenter);
291 field_transform->setPosition(osg_pos);
292 field_transform->setAttitude(orient);
296 // Convert position to cloud-coordinates
297 pos = pos - field_transform->getPosition();
298 pos = field_transform->getAttitude().inverse() * pos;
300 // We have a two level dynamic quad tree which the cloud will be added
301 // to. If there are no appropriate nodes in the quad tree, they are
302 // created as required.
304 osg::ref_ptr<osg::LOD> lodnode1;
305 osg::ref_ptr<osg::LOD> lodnode;
306 osg::ref_ptr<osgSim::Impostor> impostornode;
308 for (unsigned int i = 0; (!found) && (i < placed_root->getNumChildren()); i++) {
309 lodnode1 = (osg::LOD*) placed_root->getChild(i);
310 if ((lodnode1->getCenter() - pos).length2() < lod1_range*lod1_range) {
311 // New cloud is within RADIUS_LEVEL_1 of the center of the LOD node.
318 impostornode = new osgSim::Impostor();
319 impostornode->setImpostorThreshold(impostor_distance);
320 //impostornode->setImpostorThresholdToBound();
321 //impostornode->setCenter(pos);
322 placed_root->addChild(impostornode.get());
323 lodnode1 = (osg::ref_ptr<osg::LOD>) impostornode;
325 lodnode1 = new osg::LOD();
326 placed_root->addChild(lodnode1.get());
331 // Now check if there is a second level LOD node at an appropriate distance
334 for (unsigned int j = 0; (!found) && (j < lodnode1->getNumChildren()); j++) {
335 lodnode = (osg::LOD*) lodnode1->getChild(j);
336 if ((lodnode->getCenter() - pos).length2() < lod2_range*lod2_range) {
337 // We've found the right leaf LOD node
343 // No suitable leaf node was found, so we need to add one.
344 lodnode = new osg::LOD();
345 lodnode1->addChild(lodnode, 0.0f, lod1_range + lod2_range + view_distance + MAX_CLOUD_DEPTH);
349 transform->setPosition(pos);
350 lodnode->addChild(transform.get(), 0.0f, view_distance + MAX_CLOUD_DEPTH);
352 SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "Impostors: " << impostorcount <<
353 " LoD: " << lodcount <<
354 " Clouds: " << cloudcount);
356 lodnode->dirtyBound();
357 lodnode1->dirtyBound();
358 field_root->dirtyBound();
361 bool SGCloudField::deleteCloud(int identifier) {
362 osg::ref_ptr<osg::PositionAttitudeTransform> transform = cloud_hash[identifier];
363 if (transform == 0) return false;
365 removeCloudFromTree(transform);
366 cloud_hash.erase(identifier);
371 bool SGCloudField::repositionCloud(int identifier, float lon, float lat, float alt) {
372 return repositionCloud(identifier, lon, lat, alt, 0.0f, 0.0f);
375 bool SGCloudField::repositionCloud(int identifier, float lon, float lat, float alt, float x, float y) {
376 osg::ref_ptr<osg::PositionAttitudeTransform> transform = cloud_hash[identifier];
378 if (transform == NULL) return false;
380 removeCloudFromTree(transform);
381 addCloudToTree(transform, lon, lat, alt, x, y);
385 bool SGCloudField::isDefined3D(void) {
386 return (! cloud_hash.empty());
389 SGCloudField::CloudFog::CloudFog() {
391 fog->setMode(osg::Fog::EXP2);
392 fog->setDataVariance(osg::Object::DYNAMIC);
395 void SGCloudField::updateFog(double visibility, const osg::Vec4f& color) {
396 const double sqrt_m_log01 = sqrt(-log(0.01));
397 osg::Fog* fog = CloudFog::instance()->fog.get();
398 fog->setColor(color);
399 fog->setDensity(sqrt_m_log01 / visibility);