[Bf-blender-cvs] [3c14b78dacc] soc-2021-simulation-display: Physics: Implemented darwing convex hull collisions shapes using mesh_batch_cache functions

soumya pochiraju noreply at git.blender.org
Wed Jul 7 09:43:41 CEST 2021


Commit: 3c14b78dacc757cfba022aecc10c656fddcdb514
Author: soumya pochiraju
Date:   Wed Jul 7 13:12:26 2021 +0530
Branches: soc-2021-simulation-display
https://developer.blender.org/rB3c14b78dacc757cfba022aecc10c656fddcdb514

Physics: Implemented darwing convex hull collisions shapes using mesh_batch_cache functions

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

M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/draw/engines/overlay/overlay_extra.c
M	source/blender/draw/intern/draw_cache.c

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

diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index d25a0667540..745a4853ac1 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -430,7 +430,7 @@ static void rigidbody_store_convex_hull_draw_data(Object *ob) {
     const int num_edges = num_verts == 2 ? 1 : num_verts < 2 ? 0 : num_loops / 2;
     hull_draw_data = BKE_mesh_new_nomain(num_verts, num_edges, 0, num_loops, num_faces);
 
-     for (int i=0; i<num_verts; i++) {
+    for (int i=0; i<num_verts; i++) {
       float co[3];
       int original_index;
       plConvexHullGetVertex(hull, i, co, &original_index);
@@ -444,21 +444,53 @@ static void rigidbody_store_convex_hull_draw_data(Object *ob) {
       }
     }
 
+   MLoop *mloop_src = MEM_mallocN(num_loops * sizeof(MLoop), __func__);
+
     uint edge_index = 0;
     for (int i=0; i<num_loops; i++) {
       int v_from;
       int v_to;
       plConvexHullGetLoop(hull, i, &v_from, &v_to);
 
-
+      mloop_src[i].v = v_from;
       if (v_from < v_to) {
         hull_draw_data->medge[edge_index].v1 = v_from;
         hull_draw_data->medge[edge_index].v2 = v_to;
         hull_draw_data->medge[edge_index].flag = ME_EDGEDRAW | ME_EDGERENDER;
+
+        int reverse_index = plConvexHullGetReversedLoopIndex(hull, i);
+        mloop_src[i].e = edge_index;
+        mloop_src[reverse_index].e = edge_index;
         edge_index++;
       }
     }
 
+    /* Copy faces. */
+    int *loops;
+    int j = 0;
+    MLoop *loop = hull_draw_data->mloop;
+    for (int i=0; i<num_faces; i++) {
+      const int len = plConvexHullGetFaceSize(hull, i);
+
+      BLI_assert(len > 2);
+
+      /* Get face loop indices. */
+      loops = MEM_mallocN(sizeof(int)*len, __func__);
+      plConvexHullGetFaceLoops(hull, i, loops);
+
+      MPoly *face = &(hull_draw_data->mpoly[i]);
+      face->loopstart = j;
+      face->totloop = len;
+      for (int k=0; k<len; k++) {
+        MLoop src_loop = mloop_src[loops[k]];
+        loop->v = src_loop.v;
+        loop->e = src_loop.e;
+        loop++;
+      }
+      j += len;
+      MEM_freeN(loops);
+    }
+    MEM_freeN(mloop_src);
     ob->rigidbody_object->col_shape_draw_data = hull_draw_data;
 
 }
diff --git a/source/blender/draw/engines/overlay/overlay_extra.c b/source/blender/draw/engines/overlay/overlay_extra.c
index 21986972f0f..55ee1b67f51 100644
--- a/source/blender/draw/engines/overlay/overlay_extra.c
+++ b/source/blender/draw/engines/overlay/overlay_extra.c
@@ -37,6 +37,7 @@
 #include "BKE_tracking.h"
 
 #include "BLI_listbase.h"
+#include "BLI_task.h"
 
 #include "DNA_camera_types.h"
 #include "DNA_constraint_types.h"
@@ -58,6 +59,7 @@
 
 #include "overlay_private.h"
 
+#include "draw_cache_impl.h"
 #include "draw_common.h"
 #include "draw_manager_text.h"
 
@@ -376,13 +378,19 @@ static void OVERLAY_convex_hull_collision_shape(OVERLAY_ExtraCallBuffers *cb,
 {
 
     float color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
-    GPUBatch *geom = DRW_convex_hull_batch_get(ob);
-    if(geom){
-      GPUShader *sh = OVERLAY_shader_uniform_color();
-      DRWShadingGroup *grp = DRW_shgroup_create(sh, data->psl->extra_ps[0]);
-      DRW_shgroup_uniform_vec4_copy(grp, "color", color);
+    if(ob->rigidbody_object){
+          if(ob->rigidbody_object->col_shape_draw_data){
+              const DRWContextState *draw_ctx = DRW_context_state_get();
+              DRW_mesh_batch_cache_validate(ob->rigidbody_object->col_shape_draw_data);
 
-      DRW_shgroup_call_obmat(grp, geom, ob->obmat);
+              GPUBatch *geom = DRW_mesh_batch_cache_get_all_edges(ob->rigidbody_object->col_shape_draw_data);
+
+              if(geom){
+                  OVERLAY_extra_wire(cb, geom, ob->obmat, color);
+              }
+              struct TaskGraph *task_graph = BLI_task_graph_create();
+              DRW_mesh_batch_cache_create_requested(task_graph, ob, ob->rigidbody_object->col_shape_draw_data, draw_ctx->scene, false, false);
+          }
     }
 
 
diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c
index 25791b862e7..63468fe8f72 100644
--- a/source/blender/draw/intern/draw_cache.c
+++ b/source/blender/draw/intern/draw_cache.c
@@ -2828,42 +2828,6 @@ GPUBatch *DRW_cache_camera_distances_get(void)
   return SHC.drw_camera_distances;
 }
 
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \name Convex Hull batch
- * \{ */
-
-GPUBatch *DRW_convex_hull_batch_get(Object *ob) {
-    if(ob->rigidbody_object->col_shape_draw_data){
-    Mesh *hull = ob->rigidbody_object->col_shape_draw_data;
-    const int num_edges = hull->totedge;
-    float color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
-    GPUVertFormat format = extra_vert_format();
-
-    GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
-    GPU_vertbuf_data_alloc(vbo, num_edges*2+1);
-
-    int v = 0;
-    for(int i=0; i<num_edges; i++){
-        int v_from = hull->medge[i].v1;
-        int v_to = hull->medge[i].v2;
-
-         GPU_vertbuf_vert_set(vbo, v++, &(Vert){{hull->mvert[v_from].co[0],
-                                                 hull->mvert[v_from].co[1],
-                                                 hull->mvert[v_from].co[2]}, VCLASS_EMPTY_SCALED});
-         GPU_vertbuf_vert_set(vbo, v++, &(Vert){{hull->mvert[v_to].co[0],
-                                                 hull->mvert[v_to].co[1],
-                                                 hull->mvert[v_to].co[2]}, VCLASS_EMPTY_SCALED});
-    }
-    GPUBatch *geom = GPU_batch_create_ex(GPU_PRIM_LINES, vbo, NULL, GPU_BATCH_OWNS_VBO);
-    return geom;
-    }
-    return NULL;
-}
-
-
-
 /** \} */
 
 /* -------------------------------------------------------------------- */



More information about the Bf-blender-cvs mailing list