X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=f448bbc2f8b719c5dd0651d0632408f758df874b;hb=137d40d96fe923600d8378b8e138e3c276f27ff4;hp=027f4bded5c3bd6b8dc9f2b9f1ed038a15b3ab2b;hpb=b4a83d4fcab39bee5a8cd6e8e6eec06314864e5b;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index 027f4bd..f448bbc 100644 --- a/scene.h +++ b/scene.h @@ -17,7 +17,8 @@ struct scene_vert v3f co; /* 3*32 */ v2f uv; /* 2*32 */ i8 norm[4]; /* 4*8 */ - u16 unused[4]; + u16 flags; /* only for the cpu. its junk on the gpu */ + u16 unused[3]; }; #pragma pack(pop) @@ -40,7 +41,7 @@ struct scene_context mdl_submesh submesh; }; -VG_STATIC u32 scene_mem_required( scene_context *ctx ) +static u32 scene_mem_required( scene_context *ctx ) { u32 vertex_length = vg_align8(ctx->max_vertices * sizeof(scene_vert)), index_length = vg_align8(ctx->max_indices * sizeof(u32)); @@ -48,7 +49,7 @@ VG_STATIC u32 scene_mem_required( scene_context *ctx ) return vertex_length + index_length; } -VG_STATIC +static void scene_init( scene_context *ctx, u32 max_vertices, u32 max_indices ) { ctx->vertex_count = 0; @@ -72,7 +73,7 @@ void scene_supply_buffer( scene_context *ctx, void *buffer ) ctx->arrindices = (u32*)(((u8*)buffer) + vertex_length); } -VG_STATIC void scene_vert_pack_norm( scene_vert *vert, v3f norm ) +static void scene_vert_pack_norm( scene_vert *vert, v3f norm ) { v3f n; v3_muls( norm, 127.0f, n ); @@ -87,7 +88,7 @@ VG_STATIC void scene_vert_pack_norm( scene_vert *vert, v3f norm ) /* * Append a model into the scene with a given transform */ -VG_STATIC void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, +static void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, mdl_submesh *sm, m4x3f transform ) { if( ctx->vertex_count + sm->vertex_count > ctx->max_vertices ){ @@ -110,8 +111,8 @@ VG_STATIC void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, /* Transform and place vertices */ boxf bbxnew; - box_copy( sm->bbx, bbxnew ); - m4x3_transform_aabb( transform, bbxnew ); + box_init_inf( bbxnew ); + m4x3_expand_aabb_aabb( transform, bbxnew, sm->bbx ); box_concat( ctx->bbx, bbxnew ); m3x3f normal_matrix; @@ -133,17 +134,39 @@ VG_STATIC void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, v2_copy( src->uv, pvert->uv ); } - for( u32 i=0; iindice_count; i++ ) - dst_indices[i] = src_indices[i] + ctx->vertex_count; + u32 real_indices = 0; + for( u32 i=0; iindice_count/3; i++ ){ + u32 *src = &src_indices[i*3], + *dst = &dst_indices[real_indices]; + + v3f ab, ac, tn; + v3_sub( src_verts[src[2]].co, src_verts[src[0]].co, ab ); + v3_sub( src_verts[src[1]].co, src_verts[src[0]].co, ac ); + v3_cross( ac, ab, tn ); + +#if 0 + if( v3_length2( tn ) <= 0.00001f ) + continue; +#endif + + dst[0] = src[0] + ctx->vertex_count; + dst[1] = src[1] + ctx->vertex_count; + dst[2] = src[2] + ctx->vertex_count; + + real_indices += 3; + } + + if( real_indices != sm->indice_count ) + vg_warn( "Zero area triangles in model\n" ); ctx->vertex_count += sm->vertex_count; - ctx->indice_count += sm->indice_count; + ctx->indice_count += real_indices; } /* * One by one adders for simplified access (mostly procedural stuff) */ -VG_STATIC void scene_push_tri( scene_context *ctx, u32 tri[3] ) +static void scene_push_tri( scene_context *ctx, u32 tri[3] ) { if( ctx->indice_count + 3 > ctx->max_indices ) vg_fatal_error( "Scene indice buffer overflow (%u exceeds %u)\n", @@ -158,7 +181,7 @@ VG_STATIC void scene_push_tri( scene_context *ctx, u32 tri[3] ) ctx->indice_count += 3; } -VG_STATIC void scene_push_vert( scene_context *ctx, scene_vert *v ) +static void scene_push_vert( scene_context *ctx, scene_vert *v ) { if( ctx->vertex_count + 1 > ctx->max_vertices ) vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n", @@ -170,7 +193,7 @@ VG_STATIC void scene_push_vert( scene_context *ctx, scene_vert *v ) ctx->vertex_count ++; } -VG_STATIC void scene_copy_slice( scene_context *ctx, mdl_submesh *sm ) +static void scene_copy_slice( scene_context *ctx, mdl_submesh *sm ) { sm->indice_start = ctx->submesh.indice_start; sm->indice_count = ctx->indice_count - sm->indice_start; @@ -182,12 +205,18 @@ VG_STATIC void scene_copy_slice( scene_context *ctx, mdl_submesh *sm ) ctx->submesh.vertex_start = ctx->vertex_count; } +static void scene_set_vertex_flags( scene_context *ctx, + u32 start, u32 count, u16 flags ){ + for( u32 i=0; iarrvertices[ start + i ].flags = flags; +} + struct scene_upload_info{ scene_context *ctx; glmesh *mesh; }; -VG_STATIC void async_scene_upload( void *payload, u32 size ) +static void async_scene_upload( void *payload, u32 size ) { struct scene_upload_info *info = payload; @@ -236,7 +265,7 @@ VG_STATIC void async_scene_upload( void *payload, u32 size ) vg_info( " verts:%u\n", ctx->vertex_count ); } -VG_STATIC void scene_upload_async( scene_context *ctx, glmesh *mesh ) +static void scene_upload_async( scene_context *ctx, glmesh *mesh ) { vg_async_item *call = vg_async_alloc( sizeof(struct scene_upload_info) ); @@ -247,7 +276,7 @@ VG_STATIC void scene_upload_async( scene_context *ctx, glmesh *mesh ) vg_async_dispatch( call, async_scene_upload ); } -VG_STATIC +static vg_async_item *scene_alloc_async( scene_context *scene, glmesh *mesh, u32 max_vertices, u32 max_indices ) { @@ -273,7 +302,7 @@ vg_async_item *scene_alloc_async( scene_context *scene, glmesh *mesh, * BVH implementation */ -VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) +static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) { scene_context *s = user; scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ], @@ -285,7 +314,7 @@ VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) box_addpt( bound, pc->co ); } -VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis ) +static float scene_bh_centroid( void *user, u32 item_index, int axis ) { scene_context *s = user; scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ], @@ -308,7 +337,7 @@ VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis ) #endif } -VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib ) +static void scene_bh_swap( void *user, u32 ia, u32 ib ) { scene_context *s = user; @@ -329,7 +358,7 @@ VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib ) tj[2] = temp[2]; } -VG_STATIC void scene_bh_debug( void *user, u32 item_index ) +static void scene_bh_debug( void *user, u32 item_index ) { scene_context *s = user; u32 idx = item_index*3; @@ -342,30 +371,7 @@ VG_STATIC void scene_bh_debug( void *user, u32 item_index ) vg_line( pc->co, pa->co, 0xff0000ff ); } -VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co, - v3f dir, ray_hit *hit ) -{ - scene_context *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] ); - - float t; - if(ray_tri( positions, co, dir, &t )){ - if( t < hit->dist ){ - hit->dist = t; - hit->tri = tri; - return 1; - } - } - - return 0; -} - -VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest ) +static void scene_bh_closest( void *user, u32 index, v3f point, v3f closest ) { scene_context *s = user; @@ -377,25 +383,46 @@ VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest ) closest_on_triangle_1( point, positions, closest ); } -VG_STATIC bh_system bh_system_scene = +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 }; /* * An extra step is added onto the end to calculate the hit normal */ -VG_STATIC int scene_raycast( scene_context *s, bh_tree *bh, - v3f co, v3f dir, ray_hit *hit ) +static int scene_raycast( scene_context *s, bh_tree *bh, + v3f co, v3f dir, ray_hit *hit, u16 ignore ) { - int count = bh_ray( bh, co, dir, hit ); + hit->tri = NULL; + + bh_iter it; + bh_iter_init_ray( 0, &it, co, dir, hit->dist ); + i32 idx; + + while( bh_next( bh, &it, &idx ) ){ + u32 *tri = &s->arrindices[ idx*3 ]; + + if( s->arrvertices[tri[0]].flags & ignore ) continue; + + v3f vs[3]; + for( u32 i=0; i<3; i++ ) + v3_copy( s->arrvertices[tri[i]].co, vs[i] ); + + f32 t; + if( ray_tri( vs, co, dir, &t ) ){ + if( t < hit->dist ){ + hit->dist = t; + hit->tri = tri; + } + } + } - if( count ){ + if( hit->tri ){ v3f v0, v1; float *pa = s->arrvertices[hit->tri[0]].co, @@ -409,10 +436,10 @@ VG_STATIC int scene_raycast( scene_context *s, bh_tree *bh, v3_muladds( co, dir, hit->dist, hit->pos ); } - return count; + return hit->tri?1:0; } -VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene_context *s ) +static bh_tree *scene_bh_create( void *lin_alloc, scene_context *s ) { u32 triangle_count = s->indice_count / 3; return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 );