[Bf-blender-cvs] [e789dd4d4c1] soc-2021-simulation-display: Cleanup: collision display: faces of box, cone and cylinder

soumya pochiraju noreply at git.blender.org
Wed Aug 18 09:31:19 CEST 2021


Commit: e789dd4d4c13b799323070c4f48fc8ea7e359e88
Author: soumya pochiraju
Date:   Tue Aug 17 11:57:31 2021 +0530
Branches: soc-2021-simulation-display
https://developer.blender.org/rBe789dd4d4c13b799323070c4f48fc8ea7e359e88

Cleanup: collision display: faces of box, cone and cylinder

-Moved code for checking for faces of cone and cylinder to same function as the box
-Cached colliding faces
-Removed unneccesary shader files and used GPU batches instead, for drawing  faces

===================================================================

M	intern/rigidbody/RBI_api.h
M	intern/rigidbody/rb_bullet_api.cpp
M	source/blender/blenkernel/BKE_pointcache.h
M	source/blender/blenkernel/intern/pointcache.c
M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/overlay/overlay_extra.c
M	source/blender/draw/engines/overlay/overlay_private.h
M	source/blender/draw/engines/overlay/overlay_shader.c
D	source/blender/draw/engines/overlay/shaders/collision_display_box_vert.glsl
D	source/blender/draw/engines/overlay/shaders/collision_display_cylinder_vert.glsl
M	source/blender/draw/intern/draw_cache.c
M	source/blender/draw/intern/draw_cache.h
M	source/blender/makesdna/DNA_pointcache_types.h

===================================================================

diff --git a/intern/rigidbody/RBI_api.h b/intern/rigidbody/RBI_api.h
index 461d3bc073c..3b614b07e6c 100644
--- a/intern/rigidbody/RBI_api.h
+++ b/intern/rigidbody/RBI_api.h
@@ -276,6 +276,8 @@ void RB_shape_trimesh_update(rbCollisionShape *shape,
                              float max[3]);
 /* Get scale data */
 void RB_box_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extents);
+void RB_cone_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extents);
+void RB_cylinder_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extents);
 
 /* ********************************** */
 /* Constraints */
diff --git a/intern/rigidbody/rb_bullet_api.cpp b/intern/rigidbody/rb_bullet_api.cpp
index f5a82910c71..79f0e473a2a 100644
--- a/intern/rigidbody/rb_bullet_api.cpp
+++ b/intern/rigidbody/rb_bullet_api.cpp
@@ -1030,6 +1030,18 @@ void RB_box_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extent
     copy_v3_btvec3(r_half_extents, box->getHalfExtentsWithMargin());
 }
 
+void RB_cone_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extents)
+{
+    btConeShapeZ *cone = (btConeShapeZ*)shape->cshape;
+    copy_v3_btvec3(r_half_extents, btVector3(cone->getRadius(), cone->getRadius(), cone->getHeight()*0.5));
+}
+
+void RB_cylinder_shape_get_half_extents(rbCollisionShape *shape, float *r_half_extents)
+{
+    btCylinderShapeZ *box = (btCylinderShapeZ*)shape->cshape;
+    copy_v3_btvec3(r_half_extents, box->getHalfExtentsWithMargin());
+}
+
 /* ********************************** */
 /* Constraints */
 
diff --git a/source/blender/blenkernel/BKE_pointcache.h b/source/blender/blenkernel/BKE_pointcache.h
index a3ab0cde8d8..43881413d02 100644
--- a/source/blender/blenkernel/BKE_pointcache.h
+++ b/source/blender/blenkernel/BKE_pointcache.h
@@ -109,6 +109,7 @@ typedef struct PTCacheData {
   struct sim_data_vec fric_forces[3];
   struct sim_data_vec vec_locations[3];
   float pvel[3];
+  int colliding_faces[3];
 } PTCacheData;
 
 typedef struct PTCacheFile {
diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c
index f640f59545e..ddb4b000294 100644
--- a/source/blender/blenkernel/intern/pointcache.c
+++ b/source/blender/blenkernel/intern/pointcache.c
@@ -132,6 +132,7 @@ static int ptcache_data_size[] = {
     sizeof(sim_data_vec[3]), /* BPHYS_DATA_FRIC_FORCES */
     sizeof(sim_data_vec[3]), /* BPHYS_DATA_VEC_LOCATIONS */
     sizeof(float[3]),        /* BHYS_DATA_PREV_VELOCITY */
+    sizeof(int[3]),          /* BHYS_DATA_COLLIDING_FACES */
 };
 
 static int ptcache_extra_datasize[] = {
@@ -791,7 +792,7 @@ static int ptcache_rigidbody_write(int index, void *rb_v, void **data, int UNUSE
       PTCACHE_DATA_FROM(data, BPHYS_DATA_FRIC_FORCES, rbo->fric_forces);
       PTCACHE_DATA_FROM(data, BPHYS_DATA_VEC_LOCATIONS, rbo->vec_locations);
       PTCACHE_DATA_FROM(data, BPHYS_DATA_PREV_VELOCITY, rbo->pvel);
-
+      PTCACHE_DATA_FROM(data, BPHYS_DATA_COLLIDING_FACES, rbo->colliding_faces);
     }
   }
 
@@ -821,6 +822,7 @@ static void ptcache_rigidbody_read(
         memcpy(rbo->fric_forces, data + 28, sizeof(sim_data_vec[3]));
         memcpy(rbo->vec_locations, data + 37, sizeof(sim_data_vec[3]));
         memcpy(rbo->pvel, data + 46, sizeof(float[3]));
+        memcpy(rbo->colliding_faces, data + 49, sizeof(int[3]));
       }
       else {
         PTCACHE_DATA_TO(data, BPHYS_DATA_LOCATION, 0, rbo->pos);
@@ -831,6 +833,7 @@ static void ptcache_rigidbody_read(
         PTCACHE_DATA_TO(data, BPHYS_DATA_FRIC_FORCES, 0, rbo->fric_forces);
         PTCACHE_DATA_TO(data, BPHYS_DATA_VEC_LOCATIONS, 0, rbo->vec_locations);
         PTCACHE_DATA_TO(data, BPHYS_DATA_PREV_VELOCITY, 0, rbo->pvel);
+        PTCACHE_DATA_TO(data, BPHYS_DATA_COLLIDING_FACES, 0, rbo->colliding_faces);
       }
     }
   }
@@ -1121,7 +1124,8 @@ void BKE_ptcache_id_from_rigidbody(PTCacheID *pid, Object *ob, RigidBodyWorld *r
   pid->data_types = (1 << BPHYS_DATA_LOCATION) | (1 << BPHYS_DATA_ROTATION) |
                     (1 << BPHYS_DATA_VELOCITY) | (1 << BPHYS_DATA_EFF_FORCES) |
                     (1 << BPHYS_DATA_NORM_FORCES) | (1 << BPHYS_DATA_FRIC_FORCES) |
-                    (1 << BPHYS_DATA_VEC_LOCATIONS) | (1<<BPHYS_DATA_PREV_VELOCITY);
+                    (1 << BPHYS_DATA_VEC_LOCATIONS) | (1 << BPHYS_DATA_PREV_VELOCITY) |
+                    (1 << BPHYS_DATA_COLLIDING_FACES);
   pid->info_types = 0;
 
   pid->stack_index = pid->cache->index;
@@ -1774,6 +1778,9 @@ static void ptcache_file_pointers_init(PTCacheFile *pf)
   pf->cur[BPHYS_DATA_PREV_VELOCITY] = (data_types & (1 << BPHYS_DATA_PREV_VELOCITY)) ?
                                           &pf->data.pvel :
                                           NULL;
+  pf->cur[BPHYS_DATA_COLLIDING_FACES] = (data_types & (1 << BPHYS_DATA_COLLIDING_FACES)) ?
+                                          &pf->data.pvel :
+                                          NULL;
 }
 
 /* Check to see if point number "index" is in pm, uses binary search for index data. */
@@ -3874,6 +3881,7 @@ static const char *ptcache_data_struct[] = {
     "sim_data_vec", //BPHYS_DATA_FRIC_FORCES:
     "sim_data_vec", //BPHYS_DATA_VEC_LOCATIONS:
     "",             //BPHYS_DATA_PREV_VELOCITY:
+    "",             //BPHYS_DATA_COLLIDING_FACES
 };
 static const char *ptcache_extra_struct[] = {
     "",
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index 235ca446590..ff497da3207 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -2061,7 +2061,7 @@ static void rigidbody_update_simulation_post_step(Depsgraph *depsgraph, RigidBod
   FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
 }
 
-static void rigidbody_debug_draw_get_colliding_face(Object *ob, float points[3][3], float forces[3][3]) {
+static void rigidbody_debug_draw_get_colliding_face(Object *ob, const float points[3][3], const float forces[3][3]) {
 
     /* Unit Box vertices. */
     float box_shape[8][3] = {
@@ -2097,77 +2097,103 @@ static void rigidbody_debug_draw_get_colliding_face(Object *ob, float points[3][
     };
 
     float transform_mat[4][4] = {{0.0f}};
-    float size[3] = {1.0f, 1.0f, 1.0f};
-    float pos[3];
-    float rot[4];
-   // BoundBox *bb = NULL;
+    float size[3] = {1.0f};
+    float pos[3] = {0.0f};
+    float rot[4] = {0.0f};
 
-    if(ob->rigidbody_object->shared->physics_object) {
-        RB_body_get_position(ob->rigidbody_object->shared->physics_object, pos);
-        RB_body_get_orientation(ob->rigidbody_object->shared->physics_object, rot);
-    }
-    if(ob->rigidbody_object->shared->physics_shape) {
-        RB_box_shape_get_half_extents(ob->rigidbody_object->shared->physics_shape, size);
+    float isect_co[3] ={0.0f};
+    float point[3] = {0.0f};
+    float dir[3];
+    int stored_faces = 0;
+    int max_faces = 0;
+
+    RigidBodyOb *rbo = ob->rigidbody_object;
+    if(rbo->shared->physics_object) {
+        RB_body_get_position(rbo->shared->physics_object, pos);
+        RB_body_get_orientation(rbo->shared->physics_object, rot);
     }
 
+    switch(rbo->shape) {
+      case RB_SHAPE_BOX:
+        max_faces = 3;
+        if(rbo->shared->physics_shape) {
+            RB_box_shape_get_half_extents(rbo->shared->physics_shape, size);
+        }
+        break;
+      case RB_SHAPE_CYLINDER:
+        max_faces = 2;
+        if(rbo->shared->physics_shape) {
+            RB_cylinder_shape_get_half_extents(rbo->shared->physics_shape, size);
+        }
+        break;
+      case RB_SHAPE_CONE:
+        max_faces = 1;
+        if(rbo->shared->physics_shape) {
+            RB_cone_shape_get_half_extents(rbo->shared->physics_shape, size);
+        }
+        break;
+    }
 
-   // mul_v3_fl(size, 0.5f);
     loc_quat_size_to_mat4(transform_mat, pos, rot, size);
-    printf("obloc: %f %f %f", pos[0], pos[1], pos[2]);
-    printf("obsize: %f %f %f", size[0], size[1], size[2]);
 
     /* Transform the box to correct location, orientaion and scale. */
     for (int i = 0; i < 8; i++) {
       mul_m4_v3(transform_mat, box_shape[i]);
     }
-
-    if(ob->rigidbody_object->shape == RB_SHAPE_BOX) {
-
-    float isect_co[3] ={0.0f};
-    float point[3] = {0.0f};
-    float dir[3];
-    int stored_faces = 0;
-    for(int i=0; i<3; i++) {
-        if(stored_faces > 2) {
+    if(ELEM(rbo->shape, RB_SHAPE_BOX, RB_SHAPE_CYLINDER, RB_SHAPE_CONE)) {
+      for(int i=0; i<3; i++) {
+        if(stored_faces >= max_faces) {
             break;
         }
         copy_v3_v3(point, points[i]);
         normalize_v3_v3(dir, forces[i]);
         /* If face has already collided don't overwrite. */
-        if(ob->rigidbody_object->colliding_faces[i] > -1) {
+        if(rbo->colliding_faces[i] > -1) {
             stored_faces++;
         }
-      for (int j = 0; j < 6; j++) {
-        if (isect_point_tri_v3(point,
-                               box_shape[box_shape_tris[2 * j][0]],
-                               box_shape[box_shape_tris[2 * j][1]],
-                               box_shape[box_shape_tris[2 * j][2]],
-                               isect_co) ||
-            isect_point_tri_v3(point,
-                               box_shape[box_shape_tris[2 * j + 1][0]],
-                               box_shape[box_shape_tris[2 * j + 1][1]],
-                               box_shape[box_shape_tris[2 * j + 1][2]],
-                               isect_co)) {
-          /* Find normal to the face. */
-          float edge1[3], edge2[3], norm[3];
-          sub_v3_v3v3(edge1, box_shape[box_shape_tris[2 * j][0]], box_shape[box_shape_tris[2 * j][1]]);
-          sub_v3_v3v3(edge2, box_shape[box_shape_tris[2 * j][2]], box_shape[box_shape_tris[2 * j][1]]);
-          cross_v3_v3v3(norm, edge1, edge2);
-          normalize_v3(norm);
-          if ((len_manhattan_v3v3(point, isect_co) <= ob->rigidbody_object->margin) &&
-              (fabsf(dot_v3v3(norm, dir)) > 0.99))
-          {
-            ob->rigidbody_object->colliding_faces[stored_faces] = j;
-            break;
+        for (int j = 0; j < 6; j++) {
+          /* The cylinder and cone have fewer faces. */
+          if(rbo->shape == RB

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list