From: Stuart Buchanan Date: Fri, 9 Nov 2012 20:25:53 +0000 (+0000) Subject: For clouds, use a RNG seed that only changes every 10 minutes. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=279445d6bccaee282c0e5d3391b6352beae5ca0e;p=simgear.git For clouds, use a RNG seed that only changes every 10 minutes. This allows multi-process deploymentes to keep their clouds in sync. --- diff --git a/simgear/math/sg_random.c b/simgear/math/sg_random.c index e9004323..d9f6bcb7 100644 --- a/simgear/math/sg_random.c +++ b/simgear/math/sg_random.c @@ -59,6 +59,11 @@ void mt_init(mt *mt, unsigned int seed) mt->index = MT_N+1; } +void mt_init_time_10(mt *mt) +{ + mt_init(mt, (unsigned int) time(NULL) / 600); +} + unsigned int mt_rand32(mt *mt) { unsigned int i, y; diff --git a/simgear/math/sg_random.h b/simgear/math/sg_random.h index 51fefb65..aa084a63 100644 --- a/simgear/math/sg_random.h +++ b/simgear/math/sg_random.h @@ -47,6 +47,13 @@ typedef struct {unsigned int array[MT_N]; int index; } mt; */ void mt_init(mt *mt, unsigned int seed); +/** + * Initialize a new MT state with a seed that only + * changes every 10 minutes. Used to synchronize + * multi-process deployments. + */ +void mt_init_time_10(mt *mt); + /** * Generate a new 32-bit random number based on the given MT state. */ diff --git a/simgear/scene/sky/CloudShaderGeometry.hxx b/simgear/scene/sky/CloudShaderGeometry.hxx index 3825c843..20d0db81 100644 --- a/simgear/scene/sky/CloudShaderGeometry.hxx +++ b/simgear/scene/sky/CloudShaderGeometry.hxx @@ -46,7 +46,7 @@ class CloudShaderGeometry : public osg::Drawable const static unsigned int USR_ATTR_1 = 10; const static unsigned int USR_ATTR_2 = 11; - const static unsigned int USR_ATTR_3 = 12; + const static unsigned int USR_ATTR_3 = 12; CloudShaderGeometry() { diff --git a/simgear/scene/sky/cloud.cxx b/simgear/scene/sky/cloud.cxx index 5517cff9..3558cac9 100644 --- a/simgear/scene/sky/cloud.cxx +++ b/simgear/scene/sky/cloud.cxx @@ -194,7 +194,10 @@ SGCloudLayer::SGCloudLayer( const string &tex_path ) : ->getWhiteTexture(), osg::StateAttribute::ON); rootSet->setDataVariance(osg::Object::DYNAMIC); - + + // Ensure repeatability of the random seed within 10 minutes, + // to keep multi-computer systems in sync. + sg_srandom_time_10(); base = osg::Vec2(sg_random(), sg_random()); group_top->addChild(layer_transform.get()); group_bottom->addChild(layer_transform.get()); diff --git a/simgear/scene/sky/newcloud.cxx b/simgear/scene/sky/newcloud.cxx index 4632347e..503e1de7 100644 --- a/simgear/scene/sky/newcloud.cxx +++ b/simgear/scene/sky/newcloud.cxx @@ -40,7 +40,6 @@ #include -#include #include #include #include @@ -73,6 +72,10 @@ float SGNewCloud::sprite_density = 1.0; SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def) { + // Set up a RNG that is repeatable within 10 minutes to ensure that clouds + // are synced up in multi-process deployments. + mt_init_time_10(&seed); + min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0); max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2); min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0); @@ -132,7 +135,7 @@ SGNewCloud::~SGNewCloud() { #if 0 // return a random number between -n/2 and n/2, tending to 0 static float Rnd(float n) { - return n * (-0.5f + (sg_random() + sg_random()) / 2.0f); + return n * (-0.5f + (mt_rand(&seed) + mt_rand(&seed)) / 2.0f); } #endif @@ -143,17 +146,17 @@ osg::ref_ptr SGNewCloud::genCloud() { // Determine how big this specific cloud instance is. Note that we subtract // the sprite size because the width/height is used to define the limits of // the center of the sprites, not their edges. - float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width; - float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height; + float width = min_width + mt_rand(&seed) * (max_width - min_width) - min_sprite_width; + float height = min_height + mt_rand(&seed) * (max_height - min_height) - min_sprite_height; if (width < 0.0) { width = 0.0; } if (height < 0.0) { height = 0.0; } // Determine appropriate shading factors - float top_factor = min_top_lighting_factor + sg_random() * (max_top_lighting_factor - min_top_lighting_factor); - float middle_factor = min_middle_lighting_factor + sg_random() * (max_middle_lighting_factor - min_middle_lighting_factor); - float bottom_factor = min_bottom_lighting_factor + sg_random() * (max_bottom_lighting_factor - min_bottom_lighting_factor); - float shade_factor = min_shade_lighting_factor + sg_random() * (max_shade_lighting_factor - min_shade_lighting_factor); + float top_factor = min_top_lighting_factor + mt_rand(&seed) * (max_top_lighting_factor - min_top_lighting_factor); + float middle_factor = min_middle_lighting_factor + mt_rand(&seed) * (max_middle_lighting_factor - min_middle_lighting_factor); + float bottom_factor = min_bottom_lighting_factor + mt_rand(&seed) * (max_bottom_lighting_factor - min_bottom_lighting_factor); + float shade_factor = min_shade_lighting_factor + mt_rand(&seed) * (max_shade_lighting_factor - min_shade_lighting_factor); //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor); @@ -173,7 +176,7 @@ osg::ref_ptr SGNewCloud::genCloud() { float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f; // The number of sprites we actually use is a function of the (user-controlled) density - int n_sprites = num_sprites * sprite_density * (0.5f + sg_random()); + int n_sprites = num_sprites * sprite_density * (0.5f + mt_rand(&seed)); for (int i = 0; i < n_sprites; i++) { @@ -191,16 +194,16 @@ osg::ref_ptr SGNewCloud::genCloud() { y = 0; z = height * 0.5; } else { - float theta = sg_random() * SGD_2PI; - float elev = sg_random() * SGD_PI; + float theta = mt_rand(&seed) * SGD_2PI; + float elev = mt_rand(&seed) * SGD_PI; x = width * cos(theta) * 0.5f * sin(elev); y = width * sin(theta) * 0.5f * sin(elev); z = height * cos(elev) * 0.5f + height * 0.5f; } // Determine the height and width - float sprite_width = min_sprite_width + sg_random() * (max_sprite_width - min_sprite_width); - float sprite_height = min_sprite_height + sg_random() * (max_sprite_height - min_sprite_height); + float sprite_width = min_sprite_width + mt_rand(&seed) * (max_sprite_width - min_sprite_width); + float sprite_height = min_sprite_height + mt_rand(&seed) * (max_sprite_height - min_sprite_height); // Sprites are never taller than square. if (sprite_height > sprite_width ) @@ -223,10 +226,10 @@ osg::ref_ptr SGNewCloud::genCloud() { } // Determine the sprite texture indexes. - int index_x = (int) floor(sg_random() * num_textures_x); + int index_x = (int) floor(mt_rand(&seed) * num_textures_x); if (index_x >= num_textures_x) { index_x = num_textures_x - 1; } - int index_y = (int) floor(sg_random() * num_textures_y); + int index_y = (int) floor(mt_rand(&seed) * num_textures_y); if (height_map_texture) { // The y index depends on the position of the sprite within the cloud. diff --git a/simgear/scene/sky/newcloud.hxx b/simgear/scene/sky/newcloud.hxx index 1f207d41..fd7c905a 100644 --- a/simgear/scene/sky/newcloud.hxx +++ b/simgear/scene/sky/newcloud.hxx @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -100,6 +101,9 @@ private: osg::Geometry* quad; osg::ref_ptr effect; static float sprite_density; + + // RNG seed for this cloud + mt seed; osg::Geometry* createOrthQuad(float w, float h, int varieties_x, int varieties_y); }; diff --git a/simgear/scene/sky/sky.cxx b/simgear/scene/sky/sky.cxx index a096facb..ce3ceb69 100644 --- a/simgear/scene/sky/sky.cxx +++ b/simgear/scene/sky/sky.cxx @@ -29,7 +29,6 @@ #include "cloudfield.hxx" #include "newcloud.hxx" -#include #include #include #include @@ -68,6 +67,10 @@ SGSky::SGSky( void ) { pre_transform = new osg::Group; _ephTransform = new osg::MatrixTransform; + + // Set up a RNG that is repeatable within 10 minutes to ensure that clouds + // are synced up in multi-process deployments. + mt_init_time_10(&seed); } @@ -373,11 +376,11 @@ void SGSky::modify_vis( float alt, float time_factor ) { if ( ratio < 1.0 ) { if ( ! in_puff ) { // calc chance of entering cloud puff - double rnd = sg_random(); + double rnd = mt_rand(&seed); double chance = rnd * rnd * rnd; if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) { in_puff = true; - puff_length = sg_random() * 2.0; // up to 2 seconds + puff_length = mt_rand(&seed) * 2.0; // up to 2 seconds puff_progression = 0.0; } } diff --git a/simgear/scene/sky/sky.hxx b/simgear/scene/sky/sky.hxx index d9079bc1..cc9c191e 100644 --- a/simgear/scene/sky/sky.hxx +++ b/simgear/scene/sky/sky.hxx @@ -34,6 +34,7 @@ #endif #include +#include #include #include @@ -250,6 +251,9 @@ private: // 3D cloud density double clouds_3d_density; + + // RNG seed + mt seed; public: