# include <simgear_config.h>
#endif
+#include <osg/Fog>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
+#include <osg/Vec4f>
#include <simgear/compiler.h>
using std::vector;
#include <simgear/environment/visual_enviro.hxx>
+#include <simgear/scene/util/RenderConstants.hxx>
+#include <simgear/scene/util/SGUpdateVisitor.hxx>
#include "sky.hxx"
#include "newcloud.hxx"
#include "cloudfield.hxx"
# endif
#endif
+using namespace simgear;
+
#if defined (__CYGWIN__)
#include <ieeefp.h>
#endif
float SGCloudField::fieldSize = 50000.0f;
-float SGCloudField::density = 100.0f;
double SGCloudField::timer_dt = 0.0;
+float SGCloudField::view_distance = 20000.0f;
sgVec3 SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
-
-void SGCloudField::set_density(float density) {
- SGCloudField::density = density;
-}
+SGCloudField::StateSetMap SGCloudField::cloudTextureMap;
// reposition the cloud layer at the specified origin and orientation
bool SGCloudField::reposition( const SGVec3f& p, const SGVec3f& up, double lon, double lat,
- double dt )
+ double dt, int asl )
{
osg::Matrix T, LON, LAT;
+ // Always update the altitude transform, as this allows
+ // the clouds to rise and fall smoothly depending on environment updates.
+ altitude_transform->setPosition(osg::Vec3d(0.0, 0.0, (double) asl));
+
// Calculating the reposition information is expensive.
// Only perform the reposition every 60 frames.
reposition_count = (reposition_count + 1) % 60;
field_transform->setMatrix( LAT*LON*T );
}
+
+ field_root->getStateSet()->setRenderBinDetails(asl, "DepthSortedBin");
+
return true;
}
SGCloudField::SGCloudField() :
field_root(new osg::Group),
field_transform(new osg::MatrixTransform),
+ altitude_transform(new osg::PositionAttitudeTransform),
deltax(0.0),
deltay(0.0),
last_course(0.0),
- last_density(0.0),
+ last_coverage(0.0),
+ coverage(0.0),
defined3D(false),
reposition_count(0)
{
cld_pos = SGGeoc();
field_root->addChild(field_transform.get());
field_root->setName("3D Cloud field root");
+ osg::StateSet *rootSet = field_root->getOrCreateStateSet();
+ rootSet->setRenderBinDetails(CLOUDS_BIN, "DepthSortedBin");
+ rootSet->setAttributeAndModes(getFog());
osg::ref_ptr<osg::Group> quad_root = new osg::Group();
- osg::ref_ptr<osg::LOD> quad[BRANCH_SIZE][BRANCH_SIZE];
for (int i = 0; i < BRANCH_SIZE; i++) {
for (int j = 0; j < BRANCH_SIZE; j++) {
// Work out where to put this node in the quad tree
int i = x / leafs;
int j = y / leafs;
- quad[i][j]->addChild(field_group[x][y].get(), 0.0f, 20000.0f);
+ quad[i][j]->addChild(field_group[x][y].get(), 0.0f, view_distance);
}
}
-
+
+ field_transform->addChild(altitude_transform.get());
+
// We duplicate the defined field group in a 3x3 array. This way,
// we can simply shift entire groups around.
// TODO: "Bend" the edge groups so when shifted they line up.
new osg::PositionAttitudeTransform;
transform->addChild(quad_root.get());
transform->setPosition(osg::Vec3(x*fieldSize, y * fieldSize, 0.0));
-
- field_transform->addChild(transform.get());
+
+ altitude_transform->addChild(transform.get());
}
}
}
for (int x = 0; x < QUADTREE_SIZE; x++) {
for (int y = 0; y < QUADTREE_SIZE; y++) {
int num_children = field_group[x][y]->getNumChildren();
-
- for (int i = 0; i < num_children; i++) {
- field_group[x][y]->removeChild(i);
- }
+ field_group[x][y]->removeChildren(0, num_children);
}
}
{1,1,1,1,1,1,1,1,1,1}
};
-void SGCloudField::applyDensity(void) {
+void SGCloudField::applyCoverage(void) {
- int row = (int) (density / 10.0);
+ int row = (int) (coverage * 10.0);
+ if (row > 9) row = 9;
int col = 0;
- if (density != last_density) {
+ if (coverage != last_coverage) {
for (int x = 0; x < QUADTREE_SIZE; x++) {
for (int y = 0; y < QUADTREE_SIZE; y++) {
- // Switch on/off the children depending on the required density.
+ // Switch on/off the children depending on the required coverage.
int num_children = field_group[x][y]->getNumChildren();
for (int i = 0; i < num_children; i++) {
if (++col > 9) col = 0;
}
}
- last_density = density;
+ last_coverage = coverage;
}
void SGCloudField::addCloud( SGVec3f& pos, SGNewCloud *cloud) {
int y = (int) floor((pos.y() + fieldSize/2.0) * QUADTREE_SIZE / fieldSize);
if (y >= QUADTREE_SIZE) y = (QUADTREE_SIZE - 1);
if (y < 0) y = 0;
-
+
osg::ref_ptr<osg::PositionAttitudeTransform> transform = new osg::PositionAttitudeTransform;
transform->setPosition(pos.osg());
field_group[x][y]->addChild(transform.get(), true);
}
+
+void SGCloudField::applyVisRange(void) {
+
+ for (int x = 0; x < BRANCH_SIZE; x++) {
+ for (int y = 0; y < BRANCH_SIZE; y++) {
+ int num_children = quad[x][y]->getNumChildren();
+ for (int i = 0; i < num_children; i++) {
+ quad[x][y]->setRange(i, 0.0f, view_distance);
+ }
+ }
+ }
+}
+
+SGCloudField::CloudFog::CloudFog()
+{
+ fog = new osg::Fog;
+ fog->setMode(osg::Fog::EXP2);
+ fog->setDataVariance(osg::Object::DYNAMIC);
+}
+
+void SGCloudField::updateFog(double visibility, const osg::Vec4f& color)
+{
+ const double sqrt_m_log01 = sqrt(-log(0.01));
+ osg::Fog* fog = CloudFog::instance()->fog.get();
+ fog->setColor(color);
+ fog->setDensity(sqrt_m_log01 / visibility);
+}