Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[blender.git] / source / blender / blenkernel / intern / rigidbody.c
index 12a53f5f3e5a1f798ba958e5d4193a96c52e86fd..ab8f9dcb2e4b02566287079adfa48abb4d468c69 100644 (file)
@@ -66,6 +66,8 @@
 #include "BKE_rigidbody.h"
 #include "BKE_scene.h"
 
+#include "DEG_depsgraph.h"
+
 /* ************************************** */
 /* Memory Management */
 
@@ -702,6 +704,39 @@ static void rigidbody_validate_sim_object(RigidBodyWorld *rbw, Object *ob, bool
 
 /* --------------------- */
 
+static void rigidbody_constraint_set_limits(RigidBodyCon *rbc, void (*set_limits)(rbConstraint*,int,float,float))
+{
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+
+       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
+       else
+               set_limits(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+}
+
 /**
  * Create physics sim representation of constraint given rigid body constraint settings
  *
@@ -820,40 +855,13 @@ static void rigidbody_validate_sim_constraint(RigidBodyWorld *rbw, Object *ob, b
                                        RB_constraint_set_damping_6dof_spring(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->spring_damping_ang_z);
 
                                        RB_constraint_set_equilibrium_6dof_spring(rbc->physics_constraint);
-                                       ATTR_FALLTHROUGH;
-                               case RBC_TYPE_6DOF:
-                                       if (rbc->type == RBC_TYPE_6DOF) /* a litte awkward but avoids duplicate code for limits */
-                                               rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, rbc->limit_lin_x_lower, rbc->limit_lin_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_X, 0.0f, -1.0f);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, rbc->limit_lin_y_lower, rbc->limit_lin_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Y, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_LIN_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, rbc->limit_lin_z_lower, rbc->limit_lin_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_LIN_Z, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_X)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, rbc->limit_ang_x_lower, rbc->limit_ang_x_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_X, 0.0f, -1.0f);
-
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Y)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, rbc->limit_ang_y_lower, rbc->limit_ang_y_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Y, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof_spring);
+                                       break;
+                               case RBC_TYPE_6DOF:
+                                       rbc->physics_constraint = RB_constraint_new_6dof(loc, rot, rb1, rb2);
 
-                                       if (rbc->flag & RBC_FLAG_USE_LIMIT_ANG_Z)
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, rbc->limit_ang_z_lower, rbc->limit_ang_z_upper);
-                                       else
-                                               RB_constraint_set_limits_6dof(rbc->physics_constraint, RB_LIMIT_ANG_Z, 0.0f, -1.0f);
+                                       rigidbody_constraint_set_limits(rbc, RB_constraint_set_limits_6dof);
                                        break;
                                case RBC_TYPE_MOTOR:
                                        rbc->physics_constraint = RB_constraint_new_motor(loc, rot, rb1, rb2);
@@ -1229,7 +1237,7 @@ static void rigidbody_update_sim_world(Scene *scene, RigidBodyWorld *rbw)
        rigidbody_update_ob_array(rbw);
 }
 
-static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
+static void rigidbody_update_sim_ob(struct Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, Object *ob, RigidBodyOb *rbo)
 {
        float loc[3];
        float rot[4];
@@ -1277,7 +1285,7 @@ static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Sc
                ListBase *effectors;
 
                /* get effectors present in the group specified by effector_weights */
-               effectors = pdInitEffectors(eval_ctx, scene, ob, NULL, effector_weights, true);
+               effectors = pdInitEffectors(depsgraph, scene, ob, NULL, effector_weights, true);
                if (effectors) {
                        float eff_force[3] = {0.0f, 0.0f, 0.0f};
                        float eff_loc[3], eff_vel[3];
@@ -1318,7 +1326,7 @@ static void rigidbody_update_sim_ob(const struct EvaluationContext *eval_ctx, Sc
  *
  * \param rebuild Rebuild entire simulation
  */
-static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, RigidBodyWorld *rbw, bool rebuild)
+static void rigidbody_update_simulation(struct Depsgraph *depsgraph, Scene *scene, RigidBodyWorld *rbw, bool rebuild)
 {
        /* update world */
        if (rebuild)
@@ -1351,7 +1359,7 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                        /* validate that we've got valid object set up here... */
                        RigidBodyOb *rbo = ob->rigidbody_object;
                        /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
-                       BKE_object_where_is_calc(eval_ctx, scene, ob);
+                       BKE_object_where_is_calc(depsgraph, scene, ob);
 
                        if (rbo == NULL) {
                                /* Since this object is included in the sim group but doesn't have
@@ -1385,7 +1393,7 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                        }
 
                        /* update simulation object... */
-                       rigidbody_update_sim_ob(eval_ctx, scene, rbw, ob, rbo);
+                       rigidbody_update_sim_ob(depsgraph, scene, rbw, ob, rbo);
                }
        }
        FOREACH_GROUP_OBJECT_END;
@@ -1399,7 +1407,7 @@ static void rigidbody_update_simulation(const struct EvaluationContext *eval_ctx
                /* validate that we've got valid object set up here... */
                RigidBodyCon *rbc = ob->rigidbody_constraint;
                /* update transformation matrix of the object so we don't get a frame of lag for simple animations */
-               BKE_object_where_is_calc(eval_ctx, scene, ob);
+               BKE_object_where_is_calc(depsgraph, scene, ob);
 
                if (rbc == NULL) {
                        /* Since this object is included in the group but doesn't have
@@ -1555,7 +1563,7 @@ void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw)
 
 /* Rebuild rigid body world */
 /* NOTE: this needs to be called before frame update to work correctly */
-void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime)
+void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, float ctime)
 {
        RigidBodyWorld *rbw = scene->rigidbody_world;
        PointCache *cache;
@@ -1574,7 +1582,7 @@ void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene
        if (ctime == startframe + 1 && rbw->ltime == startframe) {
                if (cache->flag & PTCACHE_OUTDATED) {
                        BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
-                       rigidbody_update_simulation(eval_ctx, scene, rbw, true);
+                       rigidbody_update_simulation(depsgraph, scene, rbw, true);
                        BKE_ptcache_validate(cache, (int)ctime);
                        cache->last_exact = 0;
                        cache->flag &= ~PTCACHE_REDO_NEEDED;
@@ -1583,7 +1591,7 @@ void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene
 }
 
 /* Run RigidBody simulation for the specified physics world */
-void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime)
+void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, float ctime)
 {
        float timestep;
        RigidBodyWorld *rbw = scene->rigidbody_world;
@@ -1628,7 +1636,7 @@ void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene
                }
 
                /* update and validate simulation */
-               rigidbody_update_simulation(eval_ctx, scene, rbw, false);
+               rigidbody_update_simulation(depsgraph, scene, rbw, false);
 
                /* calculate how much time elapsed since last step in seconds */
                timestep = 1.0f / (float)FPS * (ctime - rbw->ltime) * rbw->time_scale;
@@ -1672,8 +1680,8 @@ void BKE_rigidbody_sync_transforms(RigidBodyWorld *rbw, Object *ob, float ctime)
 void BKE_rigidbody_aftertrans_update(Object *ob, float loc[3], float rot[3], float quat[4], float rotAxis[3], float rotAngle) {}
 bool BKE_rigidbody_check_sim_running(RigidBodyWorld *rbw, float ctime) { return false; }
 void BKE_rigidbody_cache_reset(RigidBodyWorld *rbw) {}
-void BKE_rigidbody_rebuild_world(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {}
-void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene *scene, float ctime) {}
+void BKE_rigidbody_rebuild_world(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
+void BKE_rigidbody_do_simulation(struct Depsgraph *depsgraph, Scene *scene, float ctime) {}
 
 #ifdef __GNUC__
 #  pragma GCC diagnostic pop
@@ -1684,47 +1692,35 @@ void BKE_rigidbody_do_simulation(const struct EvaluationContext *eval_ctx, Scene
 /* -------------------- */
 /* Depsgraph evaluation */
 
-void BKE_rigidbody_rebuild_sim(const struct EvaluationContext *eval_ctx,
+void BKE_rigidbody_rebuild_sim(struct Depsgraph *depsgraph,
                                Scene *scene)
 {
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s at %f\n", __func__, ctime);
-       }
-
+       DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
        /* rebuild sim data (i.e. after resetting to start of timeline) */
        if (BKE_scene_check_rigidbody_active(scene)) {
-               BKE_rigidbody_rebuild_world(eval_ctx, scene, ctime);
+               BKE_rigidbody_rebuild_world(depsgraph, scene, ctime);
        }
 }
 
-void BKE_rigidbody_eval_simulation(const struct EvaluationContext *eval_ctx,
+void BKE_rigidbody_eval_simulation(struct Depsgraph *depsgraph,
                                    Scene *scene)
 {
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s at %f\n", __func__, ctime);
-       }
-
+       DEG_debug_print_eval_time(__func__, scene->id.name, scene, ctime);
        /* evaluate rigidbody sim */
        if (BKE_scene_check_rigidbody_active(scene)) {
-               BKE_rigidbody_do_simulation(eval_ctx, scene, ctime);
+               BKE_rigidbody_do_simulation(depsgraph, scene, ctime);
        }
 }
 
-void BKE_rigidbody_object_sync_transforms(const struct EvaluationContext *UNUSED(eval_ctx),
+void BKE_rigidbody_object_sync_transforms(struct Depsgraph *UNUSED(depsgraph),
                                           Scene *scene,
                                           Object *ob)
 {
        RigidBodyWorld *rbw = scene->rigidbody_world;
        float ctime = BKE_scene_frame_get(scene);
-
-       if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) {
-               printf("%s on %s\n", __func__, ob->id.name);
-       }
-
+       DEG_debug_print_eval_time(__func__, ob->id.name, ob, ctime);
        /* read values pushed into RBO from sim/cache... */
        BKE_rigidbody_sync_transforms(rbw, ob, ctime);
 }