I made multitude of fixes based on the comments provided online:
[blender.git] / source / blender / blenkernel / intern / effect.c
index f4fea8990059f27752603d17042ea80c05729668..468f39bf731b214ec613ec089cb3e48a98c3e015 100644 (file)
@@ -17,7 +17,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenkernel/intern/effect.c
+ *  \ingroup bke
+ */
+
+
+#include <stddef.h>
 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
 
 #include <math.h>
@@ -58,6 +64,7 @@
 #include "BLI_listbase.h"
 #include "BLI_noise.h"
 #include "BLI_rand.h"
+#include "BLI_utildefines.h"
 
 #include "PIL_time.h"
 
@@ -85,8 +92,7 @@
 #include "BKE_object.h"
 #include "BKE_particle.h"
 #include "BKE_scene.h"
-#include "BKE_screen.h"
-#include "BKE_utildefines.h"
+
 
 #include "RE_render_ext.h"
 #include "RE_shader_ext.h"
@@ -165,7 +171,7 @@ PartEff *give_parteff(Object *ob)
                if(paf->type==EFF_PARTICLE) return paf;
                paf= paf->next;
        }
-       return 0;
+       return NULL;
 }
 
 void free_effect(Effect *eff)
@@ -222,7 +228,7 @@ static void precalculate_effector(EffectorCache *eff)
                                makeDispListCurveTypes(eff->scene, eff->ob, 0);
 
                        if(cu->path && cu->path->data) {
-                               where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius);
+                               where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL);
                                mul_m4_v3(eff->ob->obmat, eff->guide_loc);
                                mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir);
                        }
@@ -235,6 +241,16 @@ static void precalculate_effector(EffectorCache *eff)
        }
        else if(eff->psys)
                psys_update_particle_tree(eff->psys, eff->scene->r.cfra);
+
+       /* Store object velocity */
+       if(eff->ob) {
+               float old_vel[3];
+
+               where_is_object_time(eff->scene, eff->ob, cfra - 1.0f);
+               copy_v3_v3(old_vel, eff->ob->obmat[3]); 
+               where_is_object_time(eff->scene, eff->ob, cfra);
+               sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel);
+       }
 }
 static EffectorCache *new_effector_cache(Scene *scene, Object *ob, ParticleSystem *psys, PartDeflect *pd)
 {
@@ -264,6 +280,9 @@ static void add_object_to_effectors(ListBase **effectors, Scene *scene, Effector
 
        eff = new_effector_cache(scene, ob, NULL, ob->pd);
 
+       /* make sure imat is up to date */
+       invert_m4_m4(ob->imat, ob->obmat);
+
        BLI_addtail(*effectors, eff);
 }
 static void add_particles_to_effectors(ListBase **effectors, Scene *scene, EffectorWeights *weights, Object *ob, ParticleSystem *psys, ParticleSystem *psys_src)
@@ -352,12 +371,18 @@ void pdEndEffectors(ListBase **effectors)
 
 void pd_point_from_particle(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, EffectedPoint *point)
 {
+       ParticleSettings *part = sim->psys->part;
        point->loc = state->co;
        point->vel = state->vel;
        point->index = pa - sim->psys->particles;
        point->size = pa->size;
-       /* TODO: point->charge */
-       point->charge = 1.0f;
+       point->charge = 0.0f;
+       
+       if(part->pd && part->pd->forcefield == PFIELD_CHARGE)
+               point->charge += part->pd->f_strength;
+
+       if(part->pd2 && part->pd2->forcefield == PFIELD_CHARGE)
+               point->charge += part->pd2->f_strength;
 
        point->vel_to_sec = 1.0f;
        point->vel_to_frame = psys_get_timestep(sim);
@@ -410,7 +435,7 @@ void pd_point_from_soft(Scene *scene, float *loc, float *vel, int index, Effecte
 /************************************************/
 
 // triangle - ray callback function
-static void eff_tri_ray_hit(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
+static void eff_tri_ray_hit(void *UNUSED(userData), int UNUSED(index), const BVHTreeRay *UNUSED(ray), BVHTreeRayHit *hit)
 {      
        // whenever we hit a bounding box, we don't check further
        hit->dist = -1;
@@ -429,13 +454,12 @@ static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, Effect
                return visibility;
 
        if(!colls)
-               colls = get_collider_cache(eff->scene, NULL);
+               colls = get_collider_cache(eff->scene, eff->ob, NULL);
 
        if(!colls)
                return visibility;
-       
-       VECCOPY(norm, efd->vec_to_point);
-       negate_v3(norm);
+
+       negate_v3_v3(norm, efd->vec_to_point);
        len = normalize_v3(norm);
        
        // check all collision objects
@@ -481,7 +505,7 @@ static float wind_func(struct RNG *rng, float strength)
        float ret;
        float sign = 0;
        
-       sign = ((float)random > 64.0) ? 1.0: -1.0; // dividing by 2 is not giving equal sign distribution
+       sign = ((float)random > 64.0f) ? 1.0f: -1.0f; // dividing by 2 is not giving equal sign distribution
        
        ret = sign*((float)random / force)*strength/128.0f;
        
@@ -503,7 +527,7 @@ static float falloff_func(float fac, int usemin, float mindist, int usemax, floa
        if(!usemin)
                mindist = 0.0;
 
-       return pow((double)1.0+fac-mindist, (double)-power);
+       return pow((double)(1.0f+fac-mindist), (double)(-power));
 }
 
 static float falloff_func_dist(PartDeflect *pd, float fac)
@@ -516,7 +540,7 @@ static float falloff_func_rad(PartDeflect *pd, float fac)
        return falloff_func(fac, pd->flag&PFIELD_USEMINR, pd->minrad, pd->flag&PFIELD_USEMAXR, pd->maxrad, pd->f_power_r);
 }
 
-float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, EffectorWeights *weights)
+float effector_falloff(EffectorCache *eff, EffectorData *efd, EffectedPoint *UNUSED(point), EffectorWeights *weights)
 {
        float temp[3];
        float falloff = weights ? weights->weight[0] * weights->weight[eff->pd->forcefield] : 1.0f;
@@ -566,20 +590,20 @@ int closest_point_on_surface(SurfaceModifierData *surmd, float *co, float *surfa
        BLI_bvhtree_find_nearest(surmd->bvhtree->tree, co, &nearest, surmd->bvhtree->nearest_callback, surmd->bvhtree);
 
        if(nearest.index != -1) {
-               VECCOPY(surface_co, nearest.co);
+               copy_v3_v3(surface_co, nearest.co);
 
                if(surface_nor) {
-                       VECCOPY(surface_nor, nearest.no);
+                       copy_v3_v3(surface_nor, nearest.no);
                }
 
                if(surface_vel) {
                        MFace *mface = CDDM_get_face(surmd->dm, nearest.index);
                        
-                       VECCOPY(surface_vel, surmd->v[mface->v1].co);
-                       add_v3_v3v3(surface_vel, surface_vel, surmd->v[mface->v2].co);
-                       add_v3_v3v3(surface_vel, surface_vel, surmd->v[mface->v3].co);
+                       copy_v3_v3(surface_vel, surmd->v[mface->v1].co);
+                       add_v3_v3(surface_vel, surmd->v[mface->v2].co);
+                       add_v3_v3(surface_vel, surmd->v[mface->v3].co);
                        if(mface->v4)
-                               add_v3_v3v3(surface_vel, surface_vel, surmd->v[mface->v4].co);
+                               add_v3_v3(surface_vel, surmd->v[mface->v4].co);
 
                        mul_v3_fl(surface_vel, mface->v4 ? 0.25f : 0.333f);
                }
@@ -600,7 +624,7 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                /* using velocity corrected location allows for easier sliding over effector surface */
                copy_v3_v3(vec, point->vel);
                mul_v3_fl(vec, point->vel_to_frame);
-               add_v3_v3v3(vec, vec, point->loc);
+               add_v3_v3(vec, point->loc);
 
                ret = closest_point_on_surface(eff->surmd, vec, efd->loc, efd->nor, real_velocity ? efd->vel : NULL);
 
@@ -626,7 +650,6 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                }
        }
        else if(eff->psys) {
-               ParticleSimulationData sim = {eff->scene, eff->ob, eff->psys, NULL, NULL};
                ParticleData *pa = eff->psys->particles + *efd->index;
                ParticleKey state;
 
@@ -634,8 +657,13 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                if(eff->psys == point->psys && *efd->index == point->index)
                        ;
                else {
+                       ParticleSimulationData sim= {NULL};
+                       sim.scene= eff->scene;
+                       sim.ob= eff->ob;
+                       sim.psys= eff->psys;
+
                        /* TODO: time from actual previous calculated frame (step might not be 1) */
-                       state.time = cfra - 1.0;
+                       state.time = cfra - 1.0f;
                        ret = psys_get_particle_state(&sim, *efd->index, &state, 0);
 
                        /* TODO */
@@ -644,11 +672,15 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                        //              eff->flag |= PE_VELOCITY_TO_IMPULSE;
                        //}
 
-                       VECCOPY(efd->loc, state.co);
-                       VECCOPY(efd->nor, state.vel);
-                       if(real_velocity) {
-                               VECCOPY(efd->vel, state.vel);
-                       }
+                       copy_v3_v3(efd->loc, state.co);
+
+                       /* rather than use the velocity use rotated x-axis (defaults to velocity) */
+                       efd->nor[0] = 1.f;
+                       efd->nor[1] = efd->nor[2] = 0.f;
+                       mul_qt_v3(state.rot, efd->nor);
+               
+                       if(real_velocity)
+                               copy_v3_v3(efd->vel, state.vel);
 
                        efd->size = pa->size;
                }
@@ -658,33 +690,26 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                Object *ob = eff->ob;
                Object obcopy = *ob;
 
-               /* XXX this is not thread-safe, but used from multiple threads by
-                  particle system */
-               where_is_object_time(eff->scene, ob, cfra);
-
                /* use z-axis as normal*/
-               VECCOPY(efd->nor, ob->obmat[2]);
-               normalize_v3(efd->nor);
+               normalize_v3_v3(efd->nor, ob->obmat[2]);
 
-               /* for vortex the shape chooses between old / new force */
                if(eff->pd && eff->pd->shape == PFIELD_SHAPE_PLANE) {
-                       /* efd->loc is closes point on effector xy-plane */
-                       float temp[3];
+                       float temp[3], translate[3];
                        sub_v3_v3v3(temp, point->loc, ob->obmat[3]);
-                       project_v3_v3v3(efd->loc, temp, efd->nor);
-                       sub_v3_v3v3(efd->loc, point->loc, efd->loc);
+                       project_v3_v3v3(translate, temp, efd->nor);
+
+                       /* for vortex the shape chooses between old / new force */
+                       if(eff->pd->forcefield == PFIELD_VORTEX)
+                               add_v3_v3v3(efd->loc, ob->obmat[3], translate);
+                       else /* normally efd->loc is closest point on effector xy-plane */
+                               sub_v3_v3v3(efd->loc, point->loc, translate);
                }
                else {
-                       VECCOPY(efd->loc, ob->obmat[3]);
+                       copy_v3_v3(efd->loc, ob->obmat[3]);
                }
 
-               if(real_velocity) {
-                       VECCOPY(efd->vel, ob->obmat[3]);
-
-                       where_is_object_time(eff->scene, ob, cfra - 1.0);
-
-                       sub_v3_v3v3(efd->vel, efd->vel, ob->obmat[3]);
-               }
+               if(real_velocity)
+                       copy_v3_v3(efd->vel, eff->velocity);
 
                *eff->ob = obcopy;
 
@@ -702,20 +727,19 @@ int get_effector_data(EffectorCache *eff, EffectorData *efd, EffectedPoint *poin
                        mul_v3_fl(efd->vec_to_point, (efd->distance-eff->pd->f_size)/efd->distance);
 
                if(eff->flag & PE_USE_NORMAL_DATA) {
-                       VECCOPY(efd->vec_to_point2, efd->vec_to_point);
-                       VECCOPY(efd->nor2, efd->nor);
+                       copy_v3_v3(efd->vec_to_point2, efd->vec_to_point);
+                       copy_v3_v3(efd->nor2, efd->nor);
                }
                else {
                        /* for some effectors we need the object center every time */
                        sub_v3_v3v3(efd->vec_to_point2, point->loc, eff->ob->obmat[3]);
-                       VECCOPY(efd->nor2, eff->ob->obmat[2]);
-                       normalize_v3(efd->nor2);
+                       normalize_v3_v3(efd->nor2, eff->ob->obmat[2]);
                }
        }
 
        return ret;
 }
-static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p)
+static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, int *tot, int *p, int *step)
 {
        if(eff->pd->shape == PFIELD_SHAPE_POINTS) {
                efd->index = p;
@@ -748,6 +772,13 @@ static void get_effector_tot(EffectorCache *eff, EffectorData *efd, EffectedPoin
                        *p= point->index % eff->psys->totpart;
                        *tot= *p + 1;
                }
+
+               if(eff->psys->part->effector_amount) {
+                       int totpart = eff->psys->totpart;
+                       int amount = eff->psys->part->effector_amount;
+
+                       *step = (totpart > amount) ? totpart/amount : 1;
+               }
        }
        else {
                *p = 0;
@@ -765,11 +796,11 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
        if(!eff->pd->tex)
                return;
 
-       result[0].nor = result[1].nor = result[2].nor = result[3].nor = 0;
+       result[0].nor = result[1].nor = result[2].nor = result[3].nor = NULL;
 
        strength= eff->pd->f_strength * efd->falloff;
 
-       VECCOPY(tex_co,point->loc);
+       copy_v3_v3(tex_co,point->loc);
 
        if(eff->pd->flag & PFIELD_TEX_2D) {
                float fac=-dot_v3v3(tex_co, efd->nor);
@@ -777,10 +808,10 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
        }
 
        if(eff->pd->flag & PFIELD_TEX_OBJECT) {
-               mul_mat3_m4_v3(eff->ob->obmat, tex_co);
+               mul_m4_v3(eff->ob->imat, tex_co);
        }
 
-       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL,NULL, 1, result);
+       hasrgb = multitex_ext(eff->pd->tex, tex_co, NULL,NULL, 0, result);
 
        if(hasrgb && mode==PFIELD_TEX_RGB) {
                force[0] = (0.5f - result->tr) * strength;
@@ -791,15 +822,15 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
                strength/=nabla;
 
                tex_co[0] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 1, result+1);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+1);
 
                tex_co[0] -= nabla;
                tex_co[1] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 1, result+2);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+2);
 
                tex_co[1] -= nabla;
                tex_co[2] += nabla;
-               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 1, result+3);
+               multitex_ext(eff->pd->tex, tex_co, NULL, NULL, 0, result+3);
 
                if(mode == PFIELD_TEX_GRAD || !hasrgb) { /* if we dont have rgb fall back to grad */
                        force[0] = (result[0].tin - result[1].tin) * strength;
@@ -827,9 +858,9 @@ static void do_texture_effector(EffectorCache *eff, EffectorData *efd, EffectedP
                VECADDFAC(force, force, efd->nor, fac);
        }
 
-       add_v3_v3v3(total_force, total_force, force);
+       add_v3_v3(total_force, force);
 }
-void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
+static void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *point, float *total_force)
 {
        PartDeflect *pd = eff->pd;
        RNG *rng = pd->rng;
@@ -847,11 +878,11 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                        damp += wind_func(rng, noise_factor);
        }
 
-       VECCOPY(force, efd->vec_to_point);
+       copy_v3_v3(force, efd->vec_to_point);
 
        switch(pd->forcefield){
                case PFIELD_WIND:
-                       VECCOPY(force, efd->nor);
+                       copy_v3_v3(force, efd->nor);
                        mul_v3_fl(force, strength * efd->falloff);
                        break;
                case PFIELD_FORCE:
@@ -874,7 +905,7 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                                mul_v3_fl(force, strength * efd->falloff);
                                
                                VECADDFAC(temp, temp, point->vel, -point->vel_to_sec);
-                               add_v3_v3v3(force, force, temp);
+                               add_v3_v3(force, temp);
                        }
                        break;
                case PFIELD_MAGNET:
@@ -893,7 +924,7 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                        mul_v3_fl(force, -strength * efd->falloff);
                        copy_v3_v3(temp, point->vel);
                        mul_v3_fl(temp, -damp * 2.0f * (float)sqrt(fabs(strength)) * point->vel_to_sec);
-                       add_v3_v3v3(force, force, temp);
+                       add_v3_v3(force, temp);
                        break;
                case PFIELD_CHARGE:
                        mul_v3_fl(force, point->charge * strength * efd->falloff);
@@ -901,10 +932,10 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                case PFIELD_LENNARDJ:
                        fac = pow((efd->size + point->size) / efd->distance, 6.0);
                        
-                       fac = - fac * (1.0 - fac) / efd->distance;
+                       fac = - fac * (1.0f - fac) / efd->distance;
 
                        /* limit the repulsive term drastically to avoid huge forces */
-                       fac = ((fac>2.0) ? 2.0 : fac);
+                       fac = ((fac>2.0f) ? 2.0f : fac);
 
                        mul_v3_fl(force, strength * fac);
                        break;
@@ -913,7 +944,7 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                        return;
                case PFIELD_TURBULENCE:
                        if(pd->flag & PFIELD_GLOBAL_CO) {
-                               VECCOPY(temp, point->loc);
+                               copy_v3_v3(temp, point->loc);
                        }
                        else {
                                VECADD(temp, efd->vec_to_point2, efd->nor2);
@@ -924,7 +955,7 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                        mul_v3_fl(force, strength * efd->falloff);
                        break;
                case PFIELD_DRAG:
-                       VECCOPY(force, point->vel);
+                       copy_v3_v3(force, point->vel);
                        fac = normalize_v3(force) * point->vel_to_sec;
 
                        strength = MIN2(strength, 2.0f);
@@ -950,20 +981,20 @@ void do_physical_effector(EffectorCache *eff, EffectorData *efd, EffectedPoint *
                if(pd->f_flow != 0.0f) {
                        VECADDFAC(dave, dave, point->ave, -pd->f_flow * efd->falloff);
                }
-               add_v3_v3v3(point->ave, point->ave, dave);
+               add_v3_v3(point->ave, dave);
        }
 }
 
 /*  -------- pdDoEffectors() --------
-    generic force/speed system, now used for particles and softbodies
-    scene       = scene where it runs in, for time and stuff
+       generic force/speed system, now used for particles and softbodies
+       scene       = scene where it runs in, for time and stuff
        lb                      = listbase with objects that take part in effecting
        opco            = global coord, as input
-    force              = force accumulator
-    speed              = actual current speed which can be altered
+       force           = force accumulator
+       speed           = actual current speed which can be altered
        cur_time        = "external" time in frames, is constant for static particles
        loc_time        = "local" time in frames, range <0-1> for the lifetime of particle
-    par_layer  = layer the caller is in
+       par_layer       = layer the caller is in
        flags           = only used for softbody wind now
        guide           = old speed of particle
 
@@ -985,7 +1016,7 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
 */
        EffectorCache *eff;
        EffectorData efd;
-       int p=0, tot = 1;
+       int p=0, tot = 1, step = 1;
 
        /* Cycle through collected objects, get total of (1/(gravity_strength * dist^gravity_power)) */
        /* Check for min distance here? (yes would be cool to add that, ton) */
@@ -993,9 +1024,9 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
        if(effectors) for(eff = effectors->first; eff; eff=eff->next) {
                /* object effectors were fully checked to be OK to evaluate! */
 
-               get_effector_tot(eff, &efd, point, &tot, &p);
+               get_effector_tot(eff, &efd, point, &tot, &p, &step);
 
-               for(; p<tot; p++) {
+               for(; p<tot; p+=step) {
                        if(get_effector_data(eff, &efd, point, 0)) {
                                efd.falloff= effector_falloff(eff, &efd, point, weights);
                                
@@ -1008,7 +1039,7 @@ void pdDoEffectors(ListBase *effectors, ListBase *colliders, EffectorWeights *we
                                        do_texture_effector(eff, &efd, point, force);
                                else {
                                        float temp1[3]={0,0,0}, temp2[3];
-                                       VECCOPY(temp1, force);
+                                       copy_v3_v3(temp1, force);
 
                                        do_physical_effector(eff, &efd, point, force);