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/editors | |
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/editors')
-rw-r--r-- | source/blender/editors/curve/editcurve.c | 2 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_edit.c | 20 | ||||
-rw-r--r-- | source/blender/editors/mesh/editmesh_knife.c | 12 | ||||
-rw-r--r-- | source/blender/editors/mesh/editmesh_select.c | 2 | ||||
-rw-r--r-- | source/blender/editors/mesh/editmesh_tools.c | 18 | ||||
-rw-r--r-- | source/blender/editors/metaball/mball_edit.c | 1 | ||||
-rw-r--r-- | source/blender/editors/physics/particle_edit.c | 9 | ||||
-rw-r--r-- | source/blender/editors/transform/transform_generics.c | 1 | ||||
-rw-r--r-- | source/blender/editors/uvedit/uvedit_unwrap_ops.c | 6 |
9 files changed, 41 insertions, 30 deletions
diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c index 5504b431c50..08cae94fc93 100644 --- a/source/blender/editors/curve/editcurve.c +++ b/source/blender/editors/curve/editcurve.c @@ -5402,8 +5402,6 @@ static void selectrandom_curve(ListBase *editnurb, float randfac) BPoint *bp; int a; - BLI_srand(BLI_rand()); /* random seed */ - for (nu = editnurb->first; nu; nu = nu->next) { if (nu->type == CU_BEZIER) { bezt = nu->bezt; diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index ad32c5fcccf..f12e6da59b2 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -562,7 +562,7 @@ static void gp_timing_data_add_point(tGpTimingData *gtd, double stroke_inittime, #define MIN_TIME_DELTA 0.02f /* Loop over next points to find the end of the stroke, and compute */ -static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, int idx, int nbr_gaps, int *nbr_done_gaps, +static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, RNG *rng, int idx, int nbr_gaps, int *nbr_done_gaps, float tot_gaps_time, float delta_time, float *next_delta_time) { int j; @@ -598,7 +598,7 @@ static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, int idx, int nbr_gaps, /* Clamp max between [0.0, gap_randomness], with lower delta giving higher max */ max = gtd->gap_randomness - delta; CLAMP(max, 0.0f, gtd->gap_randomness); - *next_delta_time += gtd->gap_duration + (BLI_frand() * (max - min)) + min; + *next_delta_time += gtd->gap_duration + (BLI_rng_get_float(rng) * (max - min)) + min; } } else { @@ -613,7 +613,7 @@ static int gp_find_end_of_stroke_idx(tGpTimingData *gtd, int idx, int nbr_gaps, return j - 1; } -static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, int *nbr_gaps, float *tot_gaps_time) +static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, RNG *rng, int *nbr_gaps, float *tot_gaps_time) { int i; float delta_time = 0.0f; @@ -637,12 +637,12 @@ static void gp_stroke_path_animation_preprocess_gaps(tGpTimingData *gtd, int *nb printf("%f, %f, %f, %d\n", gtd->tot_time, delta_time, *tot_gaps_time, *nbr_gaps); } if (gtd->gap_randomness > 0.0f) { - BLI_srandom(gtd->seed); + BLI_rng_srandom(rng, gtd->seed); } } static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu, - Curve *cu, tGpTimingData *gtd, float time_range, + Curve *cu, tGpTimingData *gtd, RNG *rng, float time_range, int nbr_gaps, float tot_gaps_time) { /* Use actual recorded timing! */ @@ -669,7 +669,7 @@ static void gp_stroke_path_animation_add_keyframes(ReportList *reports, PointerR start_stroke_idx = i; delta_time = next_delta_time; /* find end of that new stroke */ - end_stroke_idx = gp_find_end_of_stroke_idx(gtd, i, nbr_gaps, &nbr_done_gaps, + end_stroke_idx = gp_find_end_of_stroke_idx(gtd, rng, i, nbr_gaps, &nbr_done_gaps, tot_gaps_time, delta_time, &next_delta_time); /* This one should *never* be negative! */ end_stroke_time = time_start + ((gtd->times[end_stroke_idx] + delta_time) / gtd->tot_time * time_range); @@ -777,6 +777,7 @@ static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu } else { /* Use actual recorded timing! */ + RNG *rng = BLI_rng_new(0); float time_range; /* CustomGaps specific */ @@ -784,7 +785,7 @@ static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu /* Pre-process gaps, in case we don't want to keep their original timing */ if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { - gp_stroke_path_animation_preprocess_gaps(gtd, &nbr_gaps, &tot_gaps_time); + gp_stroke_path_animation_preprocess_gaps(gtd, rng, &nbr_gaps, &tot_gaps_time); } if (gtd->realtime) { @@ -798,8 +799,11 @@ static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu printf("GP Stroke Path Conversion: Starting keying!\n"); } - gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, time_range, + gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, + rng, time_range, nbr_gaps, tot_gaps_time); + + BLI_rng_free(rng); } /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */ diff --git a/source/blender/editors/mesh/editmesh_knife.c b/source/blender/editors/mesh/editmesh_knife.c index 69deafabc42..171cbb6859c 100644 --- a/source/blender/editors/mesh/editmesh_knife.c +++ b/source/blender/editors/mesh/editmesh_knife.c @@ -1860,12 +1860,12 @@ typedef struct facenet_entry { KnifeEdge *kfe; } facenet_entry; -static void rnd_offset_co(float co[3], float scale) +static void rnd_offset_co(RNG *rng, float co[3], float scale) { int i; for (i = 0; i < 3; i++) { - co[i] += (BLI_frand() - 0.5) * scale; + co[i] += (BLI_rng_get_float(rng) - 0.5) * scale; } } @@ -1966,6 +1966,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd) BMFace **faces = MEM_callocN(sizeof(BMFace *) * bm->totface, "faces knife"); MemArena *arena = BLI_memarena_new(1 << 16, "knifenet_fill_faces"); SmallHash shash; + RNG *rng; int i, j, k = 0, totface = bm->totface; BMO_push(bm, NULL); @@ -2065,7 +2066,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd) } } - BLI_srand(0); + rng = BLI_rng_new(0); for (i = 0; i < totface; i++) { SmallHash *hash = &shash; @@ -2086,7 +2087,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd) if (!BLI_smallhash_haskey(hash, (intptr_t)entry->kfe->v1)) { sf_vert = BLI_scanfill_vert_add(&sf_ctx, entry->kfe->v1->v->co); sf_vert->poly_nr = 0; - rnd_offset_co(sf_vert->co, rndscale); + rnd_offset_co(rng, sf_vert->co, rndscale); sf_vert->tmp.p = entry->kfe->v1->v; BLI_smallhash_insert(hash, (intptr_t)entry->kfe->v1, sf_vert); } @@ -2094,7 +2095,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd) if (!BLI_smallhash_haskey(hash, (intptr_t)entry->kfe->v2)) { sf_vert = BLI_scanfill_vert_add(&sf_ctx, entry->kfe->v2->v->co); sf_vert->poly_nr = 0; - rnd_offset_co(sf_vert->co, rndscale); + rnd_offset_co(rng, sf_vert->co, rndscale); sf_vert->tmp.p = entry->kfe->v2->v; BLI_smallhash_insert(hash, (intptr_t)entry->kfe->v2, sf_vert); } @@ -2201,6 +2202,7 @@ static void knifenet_fill_faces(KnifeTool_OpData *kcd) if (faces) MEM_freeN(faces); BLI_memarena_free(arena); + BLI_rng_free(rng); BMO_error_clear(bm); /* remerge_faces sometimes raises errors, so make sure to clear them */ diff --git a/source/blender/editors/mesh/editmesh_select.c b/source/blender/editors/mesh/editmesh_select.c index 486401eeec9..8d3086c66a3 100644 --- a/source/blender/editors/mesh/editmesh_select.c +++ b/source/blender/editors/mesh/editmesh_select.c @@ -3227,8 +3227,6 @@ static int edbm_select_random_exec(bContext *C, wmOperator *op) BMIter iter; const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f; - BLI_srand(BLI_rand()); /* random seed */ - if (!RNA_boolean_get(op->ptr, "extend")) EDBM_flag_disable_all(em, BM_ELEM_SELECT); diff --git a/source/blender/editors/mesh/editmesh_tools.c b/source/blender/editors/mesh/editmesh_tools.c index 33c665576c6..d43120f998e 100644 --- a/source/blender/editors/mesh/editmesh_tools.c +++ b/source/blender/editors/mesh/editmesh_tools.c @@ -3249,7 +3249,7 @@ static void sort_bmelem_flag(Scene *scene, Object *ob, if (totelem[0]) { /* Re-init random generator for each element type, to get consistent random when * enabling/disabling an element type. */ - BLI_srandom(seed); + RNG *rng = BLI_rng_new_srandom(seed); pb = pblock[0] = MEM_callocN(sizeof(char) * totelem[0], "sort_bmelem vert pblock"); sb = sblock[0] = MEM_callocN(sizeof(BMElemSort) * totelem[0], "sort_bmelem vert sblock"); @@ -3257,16 +3257,18 @@ static void sort_bmelem_flag(Scene *scene, Object *ob, if (BM_elem_flag_test(ve, flag)) { pb[i] = false; sb[affected[0]].org_idx = i; - sb[affected[0]++].srt = BLI_frand(); + sb[affected[0]++].srt = BLI_rng_get_float(rng); } else { pb[i] = true; } } + + BLI_rng_free(rng); } if (totelem[1]) { - BLI_srandom(seed); + RNG *rng = BLI_rng_new_srandom(seed); pb = pblock[1] = MEM_callocN(sizeof(char) * totelem[1], "sort_bmelem edge pblock"); sb = sblock[1] = MEM_callocN(sizeof(BMElemSort) * totelem[1], "sort_bmelem edge sblock"); @@ -3274,16 +3276,18 @@ static void sort_bmelem_flag(Scene *scene, Object *ob, if (BM_elem_flag_test(ed, flag)) { pb[i] = false; sb[affected[1]].org_idx = i; - sb[affected[1]++].srt = BLI_frand(); + sb[affected[1]++].srt = BLI_rng_get_float(rng); } else { pb[i] = true; } } + + BLI_rng_free(rng); } if (totelem[2]) { - BLI_srandom(seed); + RNG *rng = BLI_rng_new_srandom(seed); pb = pblock[2] = MEM_callocN(sizeof(char) * totelem[2], "sort_bmelem face pblock"); sb = sblock[2] = MEM_callocN(sizeof(BMElemSort) * totelem[2], "sort_bmelem face sblock"); @@ -3291,12 +3295,14 @@ static void sort_bmelem_flag(Scene *scene, Object *ob, if (BM_elem_flag_test(fa, flag)) { pb[i] = false; sb[affected[2]].org_idx = i; - sb[affected[2]++].srt = BLI_frand(); + sb[affected[2]++].srt = BLI_rng_get_float(rng); } else { pb[i] = true; } } + + BLI_rng_free(rng); } } diff --git a/source/blender/editors/metaball/mball_edit.c b/source/blender/editors/metaball/mball_edit.c index 3fe8b93ada3..de0af1d1bb8 100644 --- a/source/blender/editors/metaball/mball_edit.c +++ b/source/blender/editors/metaball/mball_edit.c @@ -190,7 +190,6 @@ static int select_random_metaelems_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; ml = mb->editelems->first; - BLI_srand(BLI_rand()); /* Random seed */ /* Stupid version of random selection. Should be improved. */ while (ml) { diff --git a/source/blender/editors/physics/particle_edit.c b/source/blender/editors/physics/particle_edit.c index 4825416f6e9..f12df7aa3dd 100644 --- a/source/blender/editors/physics/particle_edit.c +++ b/source/blender/editors/physics/particle_edit.c @@ -3254,12 +3254,13 @@ static int brush_add(PEData *data, short number) short size= pset->brush[PE_BRUSH_ADD].size; short size2= size*size; DerivedMesh *dm=0; + RNG *rng; invert_m4_m4(imat, ob->obmat); if (psys->flag & PSYS_GLOBAL_HAIR) return 0; - BLI_srandom(psys->seed+data->mval[0]+data->mval[1]); + rng = BLI_rng_new_srandom(psys->seed+data->mval[0]+data->mval[1]); sim.scene= scene; sim.ob= ob; @@ -3281,8 +3282,8 @@ static int brush_add(PEData *data, short number) /* rejection sampling to get points in circle */ while (dmx*dmx + dmy*dmy > size2) { - dmx= (2.0f*BLI_frand() - 1.0f)*size; - dmy= (2.0f*BLI_frand() - 1.0f)*size; + dmx= (2.0f*BLI_rng_get_float(rng) - 1.0f)*size; + dmy= (2.0f*BLI_rng_get_float(rng) - 1.0f)*size; } } else { @@ -3458,6 +3459,8 @@ static int brush_add(PEData *data, short number) if (!psmd->dm->deformedOnly) dm->release(dm); + BLI_rng_free(rng); + return n; } diff --git a/source/blender/editors/transform/transform_generics.c b/source/blender/editors/transform/transform_generics.c index 43930e3c063..444410cf94e 100644 --- a/source/blender/editors/transform/transform_generics.c +++ b/source/blender/editors/transform/transform_generics.c @@ -1759,7 +1759,6 @@ void calculatePropRatio(TransInfo *t) td->factor = (float)sqrt(2 * dist - dist * dist); break; case PROP_RANDOM: - BLI_srand(BLI_rand()); /* random seed */ td->factor = BLI_frand() * dist; break; default: diff --git a/source/blender/editors/uvedit/uvedit_unwrap_ops.c b/source/blender/editors/uvedit/uvedit_unwrap_ops.c index 087e8eed0c9..69f6daeeb19 100644 --- a/source/blender/editors/uvedit/uvedit_unwrap_ops.c +++ b/source/blender/editors/uvedit/uvedit_unwrap_ops.c @@ -231,6 +231,7 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh BMFace *efa; BMLoop *l; BMEdge *eed; + RNG *rng; BMIter iter, liter; const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV); @@ -250,7 +251,7 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh /* we need the vert indices */ BM_mesh_elem_index_ensure(em->bm, BM_VERT); - BLI_srand(0); + rng = BLI_rng_new(0); BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) { ScanFillVert *sf_vert = NULL, *sf_vert_last, *sf_vert_first; @@ -311,7 +312,7 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh /* add small random offset */ for (i = 0; i < 3; i++) { - sf_vert->co[i] += (BLI_frand() - 0.5f) * FLT_EPSILON * 50; + sf_vert->co[i] += (BLI_rng_get_float(rng) - 0.5f) * FLT_EPSILON * 50; } sf_vert->tmp.p = l; @@ -362,6 +363,7 @@ static ParamHandle *construct_param_handle(Scene *scene, Object *ob, BMEditMesh } param_construct_end(handle, fill, implicit); + BLI_rng_free(rng); return handle; } |