diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-04-16 03:12:40 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-04-16 03:12:40 +0400 |
commit | afb4b65167165613f177a531bd3d4dcb3649c1c6 (patch) | |
tree | 32e1446b5fc3ce8ec00fa0e8b9e0fcb2eedda127 /source/blender/modifiers/intern | |
parent | 638b084f824bc345468bc8e02422b5da65a641a7 (diff) |
Random number generator: replace a bunch of usage of the global random number
generator with a local one. It's not thread safe and will not give repeatable
results, so in most cases it should not be used.
Also fixes #34992 where the noise texture of a displacement modifier was not
properly random in opengl animation render, because the seed got reset to a
fixed value by an unrelated function while for final render it changed each
frame.
Diffstat (limited to 'source/blender/modifiers/intern')
6 files changed, 34 insertions, 13 deletions
diff --git a/source/blender/modifiers/intern/MOD_explode.c b/source/blender/modifiers/intern/MOD_explode.c index 638f8f0ae01..f0eb113e46f 100644 --- a/source/blender/modifiers/intern/MOD_explode.c +++ b/source/blender/modifiers/intern/MOD_explode.c @@ -104,6 +104,7 @@ static void createFacepa(ExplodeModifierData *emd, MVert *mvert = NULL; ParticleData *pa; KDTree *tree; + RNG *rng; float center[3], co[3]; int *facepa = NULL, *vertpa = NULL, totvert = 0, totface = 0, totpart = 0; int i, p, v1, v2, v3, v4 = 0; @@ -114,7 +115,7 @@ static void createFacepa(ExplodeModifierData *emd, totvert = dm->getNumVerts(dm); totpart = psmd->psys->totpart; - BLI_srandom(psys->seed); + rng = BLI_rng_new_srandom(psys->seed); if (emd->facepa) MEM_freeN(emd->facepa); @@ -136,7 +137,7 @@ static void createFacepa(ExplodeModifierData *emd, if (dvert) { const int defgrp_index = emd->vgroup - 1; for (i = 0; i < totvert; i++, dvert++) { - float val = BLI_frand(); + float val = BLI_rng_get_float(rng); val = (1.0f - emd->protect) * val + emd->protect * 0.5f; if (val < defvert_find_weight(dvert, defgrp_index)) vertpa[i] = -1; @@ -182,6 +183,8 @@ static void createFacepa(ExplodeModifierData *emd, if (vertpa) MEM_freeN(vertpa); BLI_kdtree_free(tree); + + BLI_rng_free(rng); } static int edgecut_get(EdgeHash *edgehash, unsigned int v1, unsigned int v2) diff --git a/source/blender/modifiers/intern/MOD_particleinstance.c b/source/blender/modifiers/intern/MOD_particleinstance.c index 63dcb5d942d..8a6a83b65b2 100644 --- a/source/blender/modifiers/intern/MOD_particleinstance.c +++ b/source/blender/modifiers/intern/MOD_particleinstance.c @@ -313,8 +313,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, { float ran = 0.0f; if (pimd->random_position != 0.0f) { - BLI_srandom(psys->seed + p); - ran = pimd->random_position * BLI_frand(); + ran = pimd->random_position * BLI_hash_frand(psys->seed + p); } if (pimd->flag & eParticleInstanceFlag_KeepShape) { diff --git a/source/blender/modifiers/intern/MOD_weightvg_util.c b/source/blender/modifiers/intern/MOD_weightvg_util.c index 7181b5bbb44..a5e63a7832f 100644 --- a/source/blender/modifiers/intern/MOD_weightvg_util.c +++ b/source/blender/modifiers/intern/MOD_weightvg_util.c @@ -59,7 +59,7 @@ * vertex index (in case the weight tables do not cover the whole vertices...). * cmap might be NULL, in which case curve mapping mode will return unmodified data. */ -void weightvg_do_map(int num, float *new_w, short falloff_type, CurveMapping *cmap) +void weightvg_do_map(int num, float *new_w, short falloff_type, CurveMapping *cmap, RNG *rng) { int i; @@ -100,8 +100,7 @@ void weightvg_do_map(int num, float *new_w, short falloff_type, CurveMapping *cm fac = (float)sqrt(2 * fac - fac * fac); break; case MOD_WVG_MAPPING_RANDOM: - BLI_srand(BLI_rand()); /* random seed */ - fac = BLI_frand() * fac; + fac = BLI_rng_get_float(rng) * fac; break; case MOD_WVG_MAPPING_STEP: fac = (fac >= 0.5f) ? 1.0f : 0.0f; diff --git a/source/blender/modifiers/intern/MOD_weightvg_util.h b/source/blender/modifiers/intern/MOD_weightvg_util.h index 209b784d573..c7509a937b6 100644 --- a/source/blender/modifiers/intern/MOD_weightvg_util.h +++ b/source/blender/modifiers/intern/MOD_weightvg_util.h @@ -32,6 +32,8 @@ #ifndef __MOD_WEIGHTVG_UTIL_H__ #define __MOD_WEIGHTVG_UTIL_H__ +#include "BLI_rand.h" + /* so modifier types match their defines */ #include "MOD_modifiertypes.h" @@ -65,7 +67,7 @@ struct Scene; * vertex index (in case the weight tables do not cover the whole vertices...). * cmap might be NULL, in which case curve mapping mode will return unmodified data. */ -void weightvg_do_map(int num, float *new_w, short mode, struct CurveMapping *cmap); +void weightvg_do_map(int num, float *new_w, short mode, struct CurveMapping *cmap, RNG *rng); /* Applies new_w weights to org_w ones, using either a texture, vgroup or constant value as factor. * Return values are in org_w. diff --git a/source/blender/modifiers/intern/MOD_weightvgedit.c b/source/blender/modifiers/intern/MOD_weightvgedit.c index 779ac6b5ecf..dd84dce05c2 100644 --- a/source/blender/modifiers/intern/MOD_weightvgedit.c +++ b/source/blender/modifiers/intern/MOD_weightvgedit.c @@ -29,6 +29,7 @@ */ #include "BLI_utildefines.h" +#include "BLI_ghash.h" #include "BLI_math.h" #include "BLI_string.h" @@ -239,7 +240,15 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der /* Do mapping. */ if (wmd->falloff_type != MOD_WVG_MAPPING_NONE) { - weightvg_do_map(numVerts, new_w, wmd->falloff_type, wmd->cmap_curve); + RNG *rng = NULL; + + if (wmd->falloff_type == MOD_WVG_MAPPING_RANDOM) + rng = BLI_rng_new_srandom(BLI_ghashutil_strhash(ob->id.name)); + + weightvg_do_map(numVerts, new_w, wmd->falloff_type, wmd->cmap_curve, rng); + + if (rng) + BLI_rng_free(rng); } /* Do masking. */ diff --git a/source/blender/modifiers/intern/MOD_weightvgproximity.c b/source/blender/modifiers/intern/MOD_weightvgproximity.c index 71d6d4880ad..9a6b46f81b8 100644 --- a/source/blender/modifiers/intern/MOD_weightvgproximity.c +++ b/source/blender/modifiers/intern/MOD_weightvgproximity.c @@ -30,9 +30,10 @@ #define DO_PROFILE 0 +#include "BLI_utildefines.h" +#include "BLI_ghash.h" #include "BLI_math.h" #include "BLI_string.h" -#include "BLI_utildefines.h" #if DO_PROFILE #include "PIL_time.h" @@ -185,7 +186,7 @@ static float get_ob2ob_distance(const Object *ob, const Object *obr) /** * Maps distances to weights, with an optional "smoothing" mapping. */ -static void do_map(float *weights, const int nidx, const float min_d, const float max_d, short mode) +static void do_map(Object *ob, float *weights, const int nidx, const float min_d, const float max_d, short mode) { const float range_inv = 1.0f / (max_d - min_d); /* invert since multiplication is faster */ unsigned int i = nidx; @@ -210,7 +211,15 @@ static void do_map(float *weights, const int nidx, const float min_d, const floa } if (!ELEM(mode, MOD_WVG_MAPPING_NONE, MOD_WVG_MAPPING_CURVE)) { - weightvg_do_map(nidx, weights, mode, NULL); + RNG *rng = NULL; + + if (mode == MOD_WVG_MAPPING_RANDOM) + rng = BLI_rng_new_srandom(BLI_ghashutil_strhash(ob->id.name)); + + weightvg_do_map(nidx, weights, mode, NULL, rng); + + if (rng) + BLI_rng_free(rng); } } @@ -500,7 +509,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der } /* Map distances to weights. */ - do_map(new_w, numIdx, wmd->min_dist, wmd->max_dist, wmd->falloff_type); + do_map(ob, new_w, numIdx, wmd->min_dist, wmd->max_dist, wmd->falloff_type); /* Do masking. */ weightvg_do_mask(numIdx, indices, org_w, new_w, ob, dm, wmd->mask_constant, |