#include "model.h"
#include "bvh.h"
-typedef struct scene scene;
+typedef struct scene_context scene_context;
typedef struct scene_vert scene_vert;
#pragma pack(push,1)
#pragma pack(pop)
-struct scene
+/*
+ * 1. this should probably be a CONTEXT based approach unlike this mess.
+ * take a bit of the mdl_context ideas and redo this header. its messed up
+ * pretty bad right now.
+ */
+
+struct scene_context
{
scene_vert *arrvertices;
-
u32 *arrindices;
u32 vertex_count, indice_count,
mdl_submesh submesh;
};
-/* Initialize a scene description with bounded buffers */
-VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices )
+VG_STATIC u32 scene_mem_required( scene_context *ctx )
{
- u32 vertex_length = max_verts * sizeof(scene_vert),
- index_length = max_indices * sizeof(u32),
- tot_size = sizeof(scene) + vertex_length + index_length;
+ u32 vertex_length = vg_align8(ctx->max_vertices * sizeof(scene_vert)),
+ index_length = vg_align8(ctx->max_indices * sizeof(u32));
- scene *pscene = vg_linear_alloc( lin_alloc, tot_size );
+ return vertex_length + index_length;
+}
- pscene->arrvertices = (scene_vert *)(pscene+1);
- pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts );
+VG_STATIC
+void scene_init( scene_context *ctx, u32 max_vertices, u32 max_indices )
+{
+ ctx->vertex_count = 0;
+ ctx->indice_count = 0;
+ ctx->max_vertices = max_vertices;
+ ctx->max_indices = max_indices;
+ ctx->arrindices = NULL; /* must be filled out by user */
+ ctx->arrvertices = NULL;
- pscene->vertex_count = 0;
- pscene->indice_count = 0;
- pscene->max_vertices = max_verts;
- pscene->max_indices = max_indices;
+ memset( &ctx->submesh, 0, sizeof(mdl_submesh) );
- memset( &pscene->submesh, 0, sizeof(mdl_submesh) );
+ v3_fill( ctx->bbx[0], 999999.9f );
+ v3_fill( ctx->bbx[1], -999999.9f );
+}
- v3_fill( pscene->bbx[0], 999999.9f );
- v3_fill( pscene->bbx[1], -999999.9f );
+void scene_supply_buffer( scene_context *ctx, void *buffer )
+{
+ u32 vertex_length = vg_align8( ctx->max_vertices * sizeof(scene_vert) );
- return pscene;
+ ctx->arrvertices = buffer;
+ ctx->arrindices = (u32*)(((u8*)buffer) + vertex_length);
}
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 *pscene, mdl_context *mdl,
+VG_STATIC void scene_add_mdl_submesh( scene_context *ctx, 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( ctx->vertex_count + sm->vertex_count > ctx->max_vertices ){
+ vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
+ ctx->vertex_count + sm->vertex_count,
+ ctx->max_vertices );
}
- 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" );
+ if( ctx->indice_count + sm->indice_count > ctx->max_indices ){
+ vg_fatal_error( "Scene index buffer overflow (%u exceeds %u)\n",
+ ctx->indice_count + sm->indice_count,
+ ctx->max_indices );
}
mdl_vert *src_verts = mdl_arritm( &mdl->verts, sm->vertex_start );
- scene_vert *dst_verts = &pscene->arrvertices[ pscene->vertex_count ];
+ scene_vert *dst_verts = &ctx->arrvertices[ ctx->vertex_count ];
u32 *src_indices = mdl_arritm( &mdl->indices, sm->indice_start ),
- *dst_indices = &pscene->arrindices[ pscene->indice_count ];
+ *dst_indices = &ctx->arrindices[ ctx->indice_count ];
/* Transform and place vertices */
boxf bbxnew;
- box_copy( sm->bbx, bbxnew );
- m4x3_transform_aabb( transform, bbxnew );
- box_concat( pscene->bbx, bbxnew );
+ box_init_inf( bbxnew );
+ m4x3_expand_aabb_aabb( transform, bbxnew, sm->bbx );
+ box_concat( ctx->bbx, bbxnew );
m3x3f normal_matrix;
m3x3_copy( transform, normal_matrix );
v2_copy( src->uv, pvert->uv );
}
- for( u32 i=0; i<sm->indice_count; i++ )
- dst_indices[i] = src_indices[i] + pscene->vertex_count;
+ u32 real_indices = 0;
+ for( u32 i=0; i<sm->indice_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" );
- pscene->vertex_count += sm->vertex_count;
- pscene->indice_count += sm->indice_count;
+ ctx->vertex_count += sm->vertex_count;
+ ctx->indice_count += real_indices;
}
/*
* One by one adders for simplified access (mostly procedural stuff)
*/
-VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] )
+VG_STATIC void scene_push_tri( scene_context *ctx, u32 tri[3] )
{
- if( pscene->indice_count + 3 > pscene->max_indices )
- vg_fatal_exit_loop( "Scene vertex buffer overflow" );
+ if( ctx->indice_count + 3 > ctx->max_indices )
+ vg_fatal_error( "Scene indice buffer overflow (%u exceeds %u)\n",
+ ctx->indice_count+3, ctx->max_indices );
- u32 *dst = &pscene->arrindices[ pscene->indice_count ];
+ u32 *dst = &ctx->arrindices[ ctx->indice_count ];
dst[0] = tri[0];
dst[1] = tri[1];
dst[2] = tri[2];
- pscene->indice_count += 3;
+ ctx->indice_count += 3;
}
-VG_STATIC void scene_push_vert( scene *pscene, scene_vert *v )
+VG_STATIC void scene_push_vert( scene_context *ctx, scene_vert *v )
{
- if( pscene->vertex_count + 1 > pscene->max_vertices )
- vg_fatal_exit_loop( "Scene vertex buffer overflow" );
+ if( ctx->vertex_count + 1 > ctx->max_vertices )
+ vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
+ ctx->vertex_count+1, ctx->max_vertices );
- scene_vert *dst = &pscene->arrvertices[ pscene->vertex_count ];
+ scene_vert *dst = &ctx->arrvertices[ ctx->vertex_count ];
*dst = *v;
- pscene->vertex_count ++;
+ ctx->vertex_count ++;
}
-VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm )
+VG_STATIC void scene_copy_slice( scene_context *ctx, mdl_submesh *sm )
{
- sm->indice_start = pscene->submesh.indice_start;
- sm->indice_count = pscene->indice_count - sm->indice_start;
+ sm->indice_start = ctx->submesh.indice_start;
+ sm->indice_count = ctx->indice_count - sm->indice_start;
- sm->vertex_start = pscene->submesh.vertex_start;
- sm->vertex_count = pscene->vertex_count - sm->vertex_start;
+ sm->vertex_start = ctx->submesh.vertex_start;
+ sm->vertex_count = ctx->vertex_count - sm->vertex_start;
- pscene->submesh.indice_start = pscene->indice_count;
- pscene->submesh.vertex_start = pscene->vertex_count;
+ ctx->submesh.indice_start = ctx->indice_count;
+ ctx->submesh.vertex_start = ctx->vertex_count;
}
-/* finalization: tightly pack data */
-__attribute__((warn_unused_result))
-VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
-{
- /* FIXME: Why is this disabled? */
-
- u32 vertex_count = pscene->vertex_count,
- indice_count = pscene->indice_count,
- vertex_length = vertex_count * sizeof(scene_vert),
- index_length = indice_count * sizeof(u32),
- tot_size = vg_align8(sizeof(scene) + vertex_length + index_length);
-
- /* copy down index data */
- void *dst_indices = pscene->arrvertices + vertex_count;
- memmove( dst_indices, pscene->arrindices, index_length );
-
- /* realloc */
- pscene = vg_linear_resize( lin_alloc, pscene, tot_size );
-
- pscene->arrvertices = (scene_vert *)(pscene+1);
- pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count);
- pscene->max_vertices = vertex_count;
- pscene->max_indices = indice_count;
-
- return pscene;
-}
+struct scene_upload_info{
+ scene_context *ctx;
+ glmesh *mesh;
+};
-#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 )
+VG_STATIC void async_scene_upload( void *payload, u32 size )
{
- 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
+ struct scene_upload_info *info = payload;
-VG_STATIC void scene_upload( scene *pscene, glmesh *mesh )
-{
//assert( mesh->loaded == 0 );
+
+ glmesh *mesh = info->mesh;
+ scene_context *ctx = info->ctx;
glGenVertexArrays( 1, &mesh->vao );
glGenBuffers( 1, &mesh->vbo );
size_t stride = sizeof(scene_vert);
glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
- glBufferData( GL_ARRAY_BUFFER, pscene->vertex_count*stride,
- pscene->arrvertices, GL_STATIC_DRAW );
+ glBufferData( GL_ARRAY_BUFFER, ctx->vertex_count*stride,
+ ctx->arrvertices, GL_STATIC_DRAW );
glBindVertexArray( mesh->vao );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
- glBufferData( GL_ELEMENT_ARRAY_BUFFER, pscene->indice_count*sizeof(u32),
- pscene->arrindices, GL_STATIC_DRAW );
+ glBufferData( GL_ELEMENT_ARRAY_BUFFER, ctx->indice_count*sizeof(u32),
+ ctx->arrindices, GL_STATIC_DRAW );
/* 0: coordinates */
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
stride, (void *)offsetof(scene_vert, uv) );
glEnableVertexAttribArray( 2 );
-#if 0
- /* 3: light cluster */
- glVertexAttribIPointer( 3, 4, GL_UNSIGNED_SHORT,
- stride, (void *)offsetof(scene_vert, lights) );
- glEnableVertexAttribArray( 3 );
-#endif
-
VG_CHECK_GL_ERR();
- mesh->indice_count = pscene->indice_count;
+ mesh->indice_count = ctx->indice_count;
mesh->loaded = 1;
vg_info( "Scene upload ( XYZ_f32 UV_f32 XYZW_i8 )[ u32 ]\n" );
- vg_info( " indices:%u\n", pscene->indice_count );
- vg_info( " verts:%u\n", pscene->vertex_count );
+ vg_info( " indices:%u\n", ctx->indice_count );
+ vg_info( " verts:%u\n", ctx->vertex_count );
+}
+
+VG_STATIC void scene_upload_async( scene_context *ctx, glmesh *mesh )
+{
+ vg_async_item *call = vg_async_alloc( sizeof(struct scene_upload_info) );
+
+ struct scene_upload_info *info = call->payload;
+ info->mesh = mesh;
+ info->ctx = ctx;
+
+ vg_async_dispatch( call, async_scene_upload );
}
+VG_STATIC
+vg_async_item *scene_alloc_async( scene_context *scene, glmesh *mesh,
+ u32 max_vertices, u32 max_indices )
+{
+ scene_init( scene, max_vertices, max_indices );
+ u32 buf_size = scene_mem_required( scene );
+
+ u32 hdr_size = vg_align8(sizeof(struct scene_upload_info));
+ vg_async_item *call = vg_async_alloc( hdr_size + buf_size );
+
+ struct scene_upload_info *info = call->payload;
+
+ info->mesh = mesh;
+ info->ctx = scene;
+
+ void *buffer = ((u8*)call->payload)+hdr_size;
+ scene_supply_buffer( scene, buffer );
+
+ return call;
+}
+
+
/*
* BVH implementation
*/
VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
{
- scene *s = user;
+ scene_context *s = user;
scene_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] ];
VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis )
{
- scene *s = user;
+ scene_context *s = user;
scene_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] ];
VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib )
{
- scene *s = user;
+ scene_context *s = user;
u32 *ti = &s->arrindices[ia*3];
u32 *tj = &s->arrindices[ib*3];
VG_STATIC void scene_bh_debug( void *user, u32 item_index )
{
- scene *s = user;
+ scene_context *s = user;
u32 idx = item_index*3;
scene_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ],
*pb = &s->arrvertices[ s->arrindices[ idx+1 ] ],
VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
v3f dir, ray_hit *hit )
{
- scene *s = user;
+ scene_context *s = user;
v3f positions[3];
u32 *tri = &s->arrindices[ index*3 ];
v3_copy( s->arrvertices[tri[i]].co, positions[i] );
float t;
- if(ray_tri( positions, co, dir, &t ))
- {
- if( t < hit->dist )
- {
+ if(ray_tri( positions, co, dir, &t )){
+ if( t < hit->dist ){
hit->dist = t;
hit->tri = tri;
return 1;
VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
{
- scene *s = user;
+ scene_context *s = user;
v3f positions[3];
u32 *tri = &s->arrindices[ index*3 ];
/*
* An extra step is added onto the end to calculate the hit normal
*/
-VG_STATIC int scene_raycast( scene *s, bh_tree *bh,
+VG_STATIC int scene_raycast( scene_context *s, bh_tree *bh,
v3f co, v3f dir, ray_hit *hit )
{
int count = bh_ray( bh, co, dir, hit );
- if( count )
- {
+ if( count ){
v3f v0, v1;
float *pa = s->arrvertices[hit->tri[0]].co,
return count;
}
-VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s )
+VG_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 );