X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=8fffe9780f9232b92af4568ee97219966df68607;hb=2a238d32da833812e837cf38e16a7685c98db5c3;hp=a4f6bd1f005e02cfcc221a7d426df3344eebd6bb;hpb=b9dedb4dd2a1e94ae76a3986716ee3c57e568213;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index a4f6bd1..8fffe97 100644 --- a/scene.h +++ b/scene.h @@ -9,70 +9,88 @@ typedef struct scene scene; struct scene { - glmesh mesh; + mdl_vert *arrvertices; + u32 *arrindices; - mdl_vert *verts; - u32 *indices; - - bh_tree bhtris; - u32 vertex_count, - indice_count, - vertex_cap, - indice_cap; + u32 vertex_count, indice_count, + max_vertices, max_indices; boxf bbx; - - u32 shadower_count, - shadower_cap; - mdl_submesh submesh; }; -static void scene_init( scene *pscene ) +/* Initialize a scene description with bounded buffers */ +VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices ) { - pscene->verts = NULL; - pscene->indices = NULL; + u32 vertex_length = max_verts * sizeof(mdl_vert), + index_length = max_indices * sizeof(u32), + tot_size = sizeof(scene) + vertex_length + index_length; + + scene *pscene = vg_linear_alloc( lin_alloc, tot_size ); + + pscene->arrvertices = (mdl_vert *)(pscene+1); + pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts ); + pscene->vertex_count = 0; pscene->indice_count = 0; - pscene->shadower_count = 0; - pscene->shadower_cap = 0; - pscene->submesh.indice_start = 0; - pscene->submesh.indice_count = 0; + pscene->max_vertices = max_verts; + pscene->max_indices = max_indices; + + memset( &pscene->submesh, 0, sizeof(mdl_submesh) ); v3_fill( pscene->bbx[0], 999999.9f ); v3_fill( pscene->bbx[1], -999999.9f ); + + return pscene; } /* * Append a model into the scene with a given transform */ -static void scene_add_submesh( scene *pscene, mdl_header *mdl, - mdl_submesh *sm, m4x3f transform ) +VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl, + mdl_submesh *sm, m4x3f transform ) { - pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, - &pscene->vertex_cap, sm->vertex_count, sizeof(mdl_vert) ); + if( pscene->vertex_count + sm->vertex_count > pscene->max_vertices ) + { + vg_error( "%u(current) + %u > %u\n", pscene->vertex_count, + sm->vertex_count, + pscene->max_vertices ); - pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count, - &pscene->indice_cap, sm->indice_count, sizeof(u32) ); + vg_warn( "%p ... %p\n", pscene, sm ); + vg_fatal_exit_loop( "Scene vertex buffer overflow" ); + } - m3x3f normal_matrix; - m3x3_copy( transform, normal_matrix ); - v3_normalize( normal_matrix[0] ); - v3_normalize( normal_matrix[1] ); - v3_normalize( normal_matrix[2] ); + if( pscene->indice_count + sm->indice_count > pscene->max_indices ) + { + vg_error( "%u(current) + %u > %u\n", pscene->indice_count, + sm->indice_count, + pscene->max_indices ); + vg_warn( "%p ... %p\n", pscene, sm ); + + vg_fatal_exit_loop( "Scene index buffer overflow" ); + } + + mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ), + *dst_verts = &pscene->arrvertices[ pscene->vertex_count ]; + + u32 *src_indices = mdl_submesh_indices( mdl, sm ), + *dst_indices = &pscene->arrindices[ pscene->indice_count ]; /* Transform and place vertices */ - mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ); - u32 *src_indices = mdl_submesh_indices( mdl, sm ); - boxf bbxnew; box_copy( sm->bbx, bbxnew ); m4x3_transform_aabb( transform, bbxnew ); box_concat( pscene->bbx, bbxnew ); + + m3x3f normal_matrix; + m3x3_copy( transform, normal_matrix ); + v3_normalize( normal_matrix[0] ); + v3_normalize( normal_matrix[1] ); + v3_normalize( normal_matrix[2] ); for( u32 i=0; ivertex_count; i++ ) { - mdl_vert *pvert = &pscene->verts[ pscene->vertex_count+i ], + mdl_vert *pvert = &dst_verts[ i ], *src = &src_verts[ i ]; m4x3_mulv( transform, src->co, pvert->co ); @@ -82,28 +100,33 @@ static void scene_add_submesh( scene *pscene, mdl_header *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 ); } for( u32 i=0; iindice_count; i++ ) { - u32 *pidx = &pscene->indices[ pscene->indice_count+i ]; - *pidx = src_indices[i] + pscene->vertex_count; + dst_indices[i] = src_indices[i] + pscene->vertex_count; } pscene->vertex_count += sm->vertex_count; pscene->indice_count += sm->indice_count; + } /* * One by one adders for simplified access (mostly procedural stuff) */ -static void scene_push_tri( scene *pscene, u32 tri[3] ) +VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] ) { - pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count, - &pscene->indice_cap, 3, sizeof(u32) ); + if( pscene->indice_count + 3 > pscene->max_indices ) + vg_fatal_exit_loop( "Scene vertex buffer overflow" ); + + u32 *dst = &pscene->arrindices[ pscene->indice_count ]; - u32 *dst = &pscene->indices[pscene->indice_count]; dst[0] = tri[0]; dst[1] = tri[1]; dst[2] = tri[2]; @@ -111,14 +134,18 @@ static void scene_push_tri( scene *pscene, u32 tri[3] ) pscene->indice_count += 3; } -static void scene_push_vert( scene *pscene, mdl_vert *v ) +VG_STATIC void scene_push_vert( scene *pscene, mdl_vert *v ) { - pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, - &pscene->vertex_cap, 1, sizeof(mdl_vert) ); - pscene->verts[pscene->vertex_count ++] = *v; + if( pscene->vertex_count + 1 > pscene->max_vertices ) + vg_fatal_exit_loop( "Scene vertex buffer overflow" ); + + mdl_vert *dst = &pscene->arrvertices[ pscene->vertex_count ]; + *dst = *v; + + pscene->vertex_count ++; } -static void scene_copy_slice( scene *pscene, mdl_submesh *sm ) +VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm ) { sm->indice_start = pscene->submesh.indice_start; sm->indice_count = pscene->indice_count - sm->indice_start; @@ -130,79 +157,96 @@ static void scene_copy_slice( scene *pscene, mdl_submesh *sm ) pscene->submesh.vertex_start = pscene->vertex_count; } -static void scene_fix( scene *pscene ) +/* finalization: tightly pack data */ +__attribute__((warn_unused_result)) +VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene ) { - buffer_fix( pscene->verts, pscene->vertex_count, - &pscene->vertex_cap, sizeof( mdl_vert )); - - buffer_fix( pscene->indices, pscene->indice_count, - &pscene->indice_cap, sizeof( mdl_vert )); + 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; + + /* copy down index data */ + void *dst_indices = pscene->arrvertices + vertex_length; + memmove( dst_indices, pscene->arrindices, index_length ); + + /* realloc */ + pscene = vg_linear_resize( lin_alloc, pscene, tot_size ); + + pscene->arrvertices = (mdl_vert *)(pscene+1); + pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count); + pscene->max_vertices = vertex_count; + pscene->max_indices = indice_count; + + return pscene; } -static void scene_upload( scene *pscene ) +#if 0 +/* finalization: delete any offline buffers and reduce size */ +__attribute__((warn_unused_result)) +VG_STATIC scene *scene_free_offline_buffers( void *lin_alloc, scene *pscene ) { - mesh_upload( &pscene->mesh, - pscene->verts, pscene->vertex_count, - pscene->indices, pscene->indice_count ); + u32 tot_size = sizeof(scene); - vg_info( "Scene upload\n" ); - vg_info( " indices:%u\n", pscene->indice_count ); - vg_info( " verts:%u\n", pscene->vertex_count ); -} + scene *src_scene = pscene; + mdl_vert *src_verts = pscene->arrvertices; + u32 *src_indices = pscene->arrindices; -static void scene_bind( scene *pscene ) -{ - mesh_bind( &pscene->mesh ); -} + scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size ); + memcpy( dst_scene, src_scene, sizeof(scene) ); -static void scene_draw( scene *pscene ) -{ - mesh_drawn( 0, pscene->indice_count ); -} + dst_scene->arrindices = NULL; + dst_scene->arrvertices = NULL; -static void scene_free_offline_buffers( scene *pscene ) -{ - vg_free( pscene->verts ); - vg_free( pscene->indices ); + return dst_scene; } +#endif -static void scene_free( scene *pscene ) +VG_STATIC void scene_upload( scene *pscene, glmesh *mesh ) { - scene_free_offline_buffers( pscene ); + mesh_upload( mesh, + pscene->arrvertices, pscene->vertex_count, + pscene->arrindices, pscene->indice_count ); + + vg_info( "Scene upload\n" ); + vg_info( " indices:%u\n", pscene->indice_count ); + vg_info( " verts:%u\n", pscene->vertex_count ); } /* * BVH implementation */ -static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) +VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) { scene *s = user; - mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ], - *pb = &s->verts[ s->indices[item_index*3+1] ], - *pc = &s->verts[ s->indices[item_index*3+2] ]; + mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ], + *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ], + *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ]; box_addpt( bound, pa->co ); box_addpt( bound, pb->co ); box_addpt( bound, pc->co ); } -static float scene_bh_centroid( void *user, u32 item_index, int axis ) +VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis ) { scene *s = user; - mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ], - *pb = &s->verts[ s->indices[item_index*3+1] ], - *pc = &s->verts[ s->indices[item_index*3+2] ]; + mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ], + *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ], + *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ]; return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f); } -static void scene_bh_swap( void *user, u32 ia, u32 ib ) +VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib ) { scene *s = user; - u32 *ti = &s->indices[ia*3]; - u32 *tj = &s->indices[ib*3]; + u32 *ti = &s->arrindices[ia*3]; + u32 *tj = &s->arrindices[ib*3]; u32 temp[3]; temp[0] = ti[0]; @@ -218,28 +262,29 @@ static void scene_bh_swap( void *user, u32 ia, u32 ib ) tj[2] = temp[2]; } -static void scene_bh_debug( void *user, u32 item_index ) +VG_STATIC void scene_bh_debug( void *user, u32 item_index ) { scene *s = user; u32 idx = item_index*3; - mdl_vert *pa = &s->verts[ s->indices[ idx+0 ] ], - *pb = &s->verts[ s->indices[ idx+1 ] ], - *pc = &s->verts[ s->indices[ idx+2 ] ]; + mdl_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ], + *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ], + *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ]; vg_line( pa->co, pb->co, 0xff0000ff ); vg_line( pb->co, pc->co, 0xff0000ff ); vg_line( pc->co, pa->co, 0xff0000ff ); } -static int scene_bh_ray( void *user, u32 index, v3f co, v3f dir, ray_hit *hit ) +VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co, + v3f dir, ray_hit *hit ) { scene *s = user; v3f positions[3]; - u32 *tri = &s->indices[ index*3 ]; + u32 *tri = &s->arrindices[ index*3 ]; for( int i=0; i<3; i++ ) - v3_copy( s->verts[tri[i]].co, positions[i] ); + v3_copy( s->arrvertices[tri[i]].co, positions[i] ); float t; if(ray_tri( positions, co, dir, &t )) @@ -255,10 +300,23 @@ static int scene_bh_ray( void *user, u32 index, v3f co, v3f dir, ray_hit *hit ) return 0; } -static bh_system bh_system_scene = +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 @@ -267,17 +325,18 @@ static bh_system bh_system_scene = /* * An extra step is added onto the end to calculate the hit normal */ -static int scene_raycast( scene *s, v3f co, v3f dir, ray_hit *hit ) +VG_STATIC int scene_raycast( scene *s, bh_tree *bh, + v3f co, v3f dir, ray_hit *hit ) { - int count = bh_ray( &s->bhtris, 0, co, dir, hit ); + int count = bh_ray( bh, co, dir, hit ); if( count ) { v3f v0, v1; - float *pa = s->verts[hit->tri[0]].co, - *pb = s->verts[hit->tri[1]].co, - *pc = s->verts[hit->tri[2]].co; + float *pa = s->arrvertices[hit->tri[0]].co, + *pb = s->arrvertices[hit->tri[1]].co, + *pc = s->arrvertices[hit->tri[2]].co; v3_sub( pa, pb, v0 ); v3_sub( pc, pb, v1 ); @@ -289,10 +348,10 @@ static int scene_raycast( scene *s, v3f co, v3f dir, ray_hit *hit ) return count; } -static void scene_bh_create( scene *s ) +VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s ) { u32 triangle_count = s->indice_count / 3; - bh_create( &s->bhtris, &bh_system_scene, s, triangle_count ); + return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 ); } #endif