a fairly major physics update
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index 51ce10315c38fdcb47fe84f49eaec103e8445266..8fffe9780f9232b92af4568ee97219966df68607 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -36,8 +36,7 @@ VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices )
    pscene->max_vertices = max_verts;
    pscene->max_indices  = max_indices;
 
-   pscene->submesh.indice_start = 0;
-   pscene->submesh.indice_count = 0;
+   memset( &pscene->submesh, 0, sizeof(mdl_submesh) );
 
    v3_fill( pscene->bbx[0],  999999.9f );
    v3_fill( pscene->bbx[1], -999999.9f );
@@ -101,6 +100,10 @@ VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl,
       pvert->colour[1] = src->colour[1];
       pvert->colour[2] = src->colour[2];
       pvert->colour[3] = src->colour[3];
+      pvert->weights[0] = src->weights[0];
+      pvert->weights[1] = src->weights[1];
+      pvert->weights[2] = src->weights[2];
+      pvert->weights[3] = src->weights[3];
       v2_copy( src->uv, pvert->uv );
    }
 
@@ -158,29 +161,26 @@ VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm )
 __attribute__((warn_unused_result))
 VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
 {
-   u32 vertex_length = pscene->vertex_count * sizeof(mdl_vert),
-       index_length  = pscene->indice_count * sizeof(u32),
+   return pscene;
+   u32 vertex_count  = pscene->vertex_count,
+       indice_count  = pscene->indice_count,
+       vertex_length = vertex_count * sizeof(mdl_vert),
+       index_length  = indice_count * sizeof(u32),
        tot_size = sizeof(scene) + vertex_length + index_length;
 
-   scene    *src_scene   = pscene;
-   mdl_vert *src_verts   = pscene->arrvertices;
-   u32      *src_indices = pscene->arrindices;
+   /* copy down index data */
+   void *dst_indices = pscene->arrvertices + vertex_length;
+   memmove( dst_indices, pscene->arrindices, index_length );
 
-   scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
-   memcpy( dst_scene, src_scene, sizeof(scene) );
+   /* realloc */
+   pscene = vg_linear_resize( lin_alloc, pscene, tot_size );
 
-   void *dst_verts   = dst_scene+1,
-        *dst_indices = dst_verts + vertex_length;
-
-   memcpy( dst_verts, src_verts, vertex_length );
-   memcpy( dst_indices, src_indices, index_length );
-
-   dst_scene->arrvertices = dst_verts;
-   dst_scene->arrindices  = dst_indices;
-   dst_scene->max_vertices = pscene->vertex_count;
-   dst_scene->max_indices  = pscene->indice_count;
+   pscene->arrvertices = (mdl_vert *)(pscene+1);
+   pscene->arrindices  = (u32 *)(pscene->arrvertices+vertex_count);
+   pscene->max_vertices = vertex_count;
+   pscene->max_indices  = indice_count;
 
-   return dst_scene;
+   return pscene;
 }
 
 #if 0
@@ -300,10 +300,23 @@ VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
    return 0;
 }
 
+VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
+{
+   scene *s = user;
+
+   v3f positions[3];
+   u32 *tri = &s->arrindices[ index*3 ];
+   for( int i=0; i<3; i++ )
+      v3_copy( s->arrvertices[tri[i]].co, positions[i] );
+
+   closest_on_triangle_1( point, positions, closest );
+}
+
 VG_STATIC bh_system bh_system_scene = 
 {
    .expand_bound = scene_bh_expand_bound,
    .item_centroid = scene_bh_centroid,
+   .item_closest = scene_bh_closest,
    .item_swap = scene_bh_swap,
    .item_debug = scene_bh_debug,
    .cast_ray = scene_bh_ray
@@ -338,7 +351,7 @@ VG_STATIC int scene_raycast( scene *s, bh_tree *bh,
 VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s )
 {
    u32 triangle_count = s->indice_count / 3;
-   return bh_create( lin_alloc, &bh_system_scene, s, triangle_count );
+   return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 );
 }
 
 #endif