From 68797e51c66d44d9a3178b659c9f970d996796e1 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 1 Feb 2010 10:05:14 +0000 Subject: [PATCH 1/2] implement a round-robin scheme for random objects to make sure the same type of object is placed at the same spot all the time --- simgear/scene/material/matmodel.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/simgear/scene/material/matmodel.cxx b/simgear/scene/material/matmodel.cxx index bdad512e..c625d9b0 100644 --- a/simgear/scene/material/matmodel.cxx +++ b/simgear/scene/material/matmodel.cxx @@ -139,8 +139,9 @@ SGMatModel::get_random_model( SGPropertyNode *prop_root ) { load_models( prop_root ); // comment this out if preloading models int nModels = _models.size(); - int index = int(sg_random() * nModels); - if (index >= nModels) + // int index = int(sg_random() * nModels); + static int index = -1; + if (++index >= nModels) index = 0; return _models[index].get(); } From 675c86582d8b25270897db0a36d0e02388f09bf4 Mon Sep 17 00:00:00 2001 From: fredb Date: Sun, 25 Apr 2010 10:47:37 +0000 Subject: [PATCH 2/2] Stuart Buchanan: Fix a bug in the random object placement where the model selected in the case of multiple object definitions in material.xml was random, rather than seeded. --- simgear/scene/material/matmodel.cxx | 8 ++------ simgear/scene/material/matmodel.hxx | 2 +- simgear/scene/tgdb/obj.cxx | 2 +- simgear/scene/tgdb/userdata.cxx | 4 ++-- simgear/scene/tgdb/userdata.hxx | 3 ++- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/simgear/scene/material/matmodel.cxx b/simgear/scene/material/matmodel.cxx index c625d9b0..93cbd494 100644 --- a/simgear/scene/material/matmodel.cxx +++ b/simgear/scene/material/matmodel.cxx @@ -135,15 +135,11 @@ SGMatModel::load_models( SGPropertyNode *prop_root ) } osg::Node* -SGMatModel::get_random_model( SGPropertyNode *prop_root ) +SGMatModel::get_random_model( SGPropertyNode *prop_root, mt seed ) { load_models( prop_root ); // comment this out if preloading models int nModels = _models.size(); - // int index = int(sg_random() * nModels); - static int index = -1; - if (++index >= nModels) - index = 0; - return _models[index].get(); + return _models[mt_rand(&seed) * nModels].get(); } double diff --git a/simgear/scene/material/matmodel.hxx b/simgear/scene/material/matmodel.hxx index 9d06427a..3e7d84a9 100644 --- a/simgear/scene/material/matmodel.hxx +++ b/simgear/scene/material/matmodel.hxx @@ -82,7 +82,7 @@ public: * * @return A randomly select model from the variants. */ - osg::Node *get_random_model( SGPropertyNode *prop_root ); + osg::Node *get_random_model( SGPropertyNode *prop_root, mt seed ); /** diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index 74b4a071..001cbf71 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -610,7 +610,7 @@ SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool i < tileGeometryBin.randomModels.getNumModels(); i++) { SGMatModelBin::MatModel obj = tileGeometryBin.randomModels.getMatModel(i); - osg::Node* node = sgGetRandomModel(obj.model); + osg::Node* node = sgGetRandomModel(obj.model, seed); // Create a matrix to place the object in the correct // location, and then apply the rotation matrix created diff --git a/simgear/scene/tgdb/userdata.cxx b/simgear/scene/tgdb/userdata.cxx index 11d69e12..6aa71821 100644 --- a/simgear/scene/tgdb/userdata.cxx +++ b/simgear/scene/tgdb/userdata.cxx @@ -60,8 +60,8 @@ void sgUserDataInit( SGPropertyNode *p ) { root_props = p; } -osg::Node* sgGetRandomModel(SGMatModel *obj) { - return obj->get_random_model( root_props ); +osg::Node* sgGetRandomModel(SGMatModel *obj, mt seed) { + return obj->get_random_model( root_props, seed ); } namespace simgear diff --git a/simgear/scene/tgdb/userdata.hxx b/simgear/scene/tgdb/userdata.hxx index ab0a6a55..fa5209e6 100644 --- a/simgear/scene/tgdb/userdata.hxx +++ b/simgear/scene/tgdb/userdata.hxx @@ -26,6 +26,7 @@ #define _SG_USERDATA_HXX #include +#include #include @@ -42,7 +43,7 @@ void sgUserDataInit(SGPropertyNode *p); /** * Get a random model. */ -osg::Node* sgGetRandomModel(SGMatModel *obj); +osg::Node* sgGetRandomModel(SGMatModel *obj, mt seed); namespace simgear {