- * Append a model into the scene with a given transform
- */
-VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl,
- mdl_submesh *sm, m4x3f transform )
-{
- 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 );
-
- vg_warn( "%p ... %p\n", pscene, sm );
- vg_fatal_exit_loop( "Scene vertex buffer overflow" );
- }
-
- 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 */
- 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; i<sm->vertex_count; i++ )
- {
- mdl_vert *pvert = &dst_verts[ i ],
- *src = &src_verts[ i ];
-
- m4x3_mulv( transform, src->co, pvert->co );
- m3x3_mulv( normal_matrix, src->norm, pvert->norm );
-
- pvert->colour[0] = src->colour[0];
- 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; i<sm->indice_count; i++ )
- {
- 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)
- */
-VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] )
-{
- if( pscene->indice_count + 3 > pscene->max_indices )
- vg_fatal_exit_loop( "Scene vertex buffer overflow" );
-
- u32 *dst = &pscene->arrindices[ pscene->indice_count ];
-
- dst[0] = tri[0];
- dst[1] = tri[1];
- dst[2] = tri[2];
-
- pscene->indice_count += 3;
-}
-
-VG_STATIC void scene_push_vert( scene *pscene, mdl_vert *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 ++;
-}
-
-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;
-
- sm->vertex_start = pscene->submesh.vertex_start;
- sm->vertex_count = pscene->vertex_count - sm->vertex_start;
-
- pscene->submesh.indice_start = pscene->indice_count;
- pscene->submesh.vertex_start = pscene->vertex_count;
-}
-
-/* finalization: tightly pack data */
-__attribute__((warn_unused_result))
-VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
-{
- 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;
-}
-
-#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 )
-{
- u32 tot_size = sizeof(scene);
-
- scene *src_scene = pscene;
- mdl_vert *src_verts = pscene->arrvertices;
- u32 *src_indices = pscene->arrindices;
-
- scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
- memcpy( dst_scene, src_scene, sizeof(scene) );
-
- dst_scene->arrindices = NULL;
- dst_scene->arrvertices = NULL;
-
- return dst_scene;
-}
-#endif
-
-VG_STATIC void scene_upload( scene *pscene, glmesh *mesh )
-{
- 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