-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));
-
- return vertex_length + index_length;
-}
-
-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;
-
- memset( &ctx->submesh, 0, sizeof(mdl_submesh) );
-
- v3_fill( ctx->bbx[0], 999999.9f );
- v3_fill( ctx->bbx[1], -999999.9f );
-}
-
-void scene_supply_buffer( scene_context *ctx, void *buffer )
-{
- u32 vertex_length = vg_align8( ctx->max_vertices * sizeof(scene_vert) );
-
- ctx->arrvertices = buffer;
- ctx->arrindices = (u32*)(((u8*)buffer) + vertex_length);
-}
-
-static void scene_vert_pack_norm( scene_vert *vert, v3f norm, f32 blend ){
- v3f n;
- v3_muls( norm, 127.0f, n );
- v3_minv( n, (v3f){ 127.0f, 127.0f, 127.0f }, n );
- v3_maxv( n, (v3f){ -127.0f, -127.0f, -127.0f }, n );
- vert->norm[0] = n[0];
- vert->norm[1] = n[1];
- vert->norm[2] = n[2];
- vert->norm[3] = blend * 127.0f;
-}
-
-/*
- * Append a model into the scene with a given transform
- */
-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 ){
- vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
- ctx->vertex_count + sm->vertex_count,
- ctx->max_vertices );
- }
-
- 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 = &ctx->arrvertices[ ctx->vertex_count ];
-
- u32 *src_indices = mdl_arritm( &mdl->indices, sm->indice_start ),
- *dst_indices = &ctx->arrindices[ ctx->indice_count ];
-
- /* Transform and place vertices */
- boxf 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 );
- 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 *src = &src_verts[i];
- scene_vert *pvert = &dst_verts[i];
-
- m4x3_mulv( transform, src->co, pvert->co );
-
- v3f normal;
- m3x3_mulv( normal_matrix, src->norm, normal );
- scene_vert_pack_norm( pvert, normal, src->colour[0]*(1.0f/255.0f) );
-
- v2_copy( src->uv, pvert->uv );
- }
-
- 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" );
-
- ctx->vertex_count += sm->vertex_count;
- ctx->indice_count += real_indices;
-}
-
-/*
- * One by one adders for simplified access (mostly procedural stuff)
- */
-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",
- ctx->indice_count+3, ctx->max_indices );
-
- u32 *dst = &ctx->arrindices[ ctx->indice_count ];
-
- dst[0] = tri[0];
- dst[1] = tri[1];
- dst[2] = tri[2];
-
- ctx->indice_count += 3;
-}
-
-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",
- ctx->vertex_count+1, ctx->max_vertices );
-
- scene_vert *dst = &ctx->arrvertices[ ctx->vertex_count ];
- *dst = *v;
-
- ctx->vertex_count ++;
-}
-
-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;
-
- sm->vertex_start = ctx->submesh.vertex_start;
- sm->vertex_count = ctx->vertex_count - sm->vertex_start;
-
- ctx->submesh.indice_start = ctx->indice_count;
- 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; i<count; i++ )
- ctx->arrvertices[ start + i ].flags = flags;
-}
-
-struct scene_upload_info{
- scene_context *ctx;
- glmesh *mesh;
-};
-
-static void async_scene_upload( void *payload, u32 size )
-{
- struct scene_upload_info *info = payload;
-
- //assert( mesh->loaded == 0 );
-
- glmesh *mesh = info->mesh;
- scene_context *ctx = info->ctx;
-
- glGenVertexArrays( 1, &mesh->vao );
- glGenBuffers( 1, &mesh->vbo );
- glGenBuffers( 1, &mesh->ebo );
- glBindVertexArray( mesh->vao );
-
- size_t stride = sizeof(scene_vert);
-
- glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
- 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, ctx->indice_count*sizeof(u32),
- ctx->arrindices, GL_STATIC_DRAW );
-
- /* 0: coordinates */
- glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
- glEnableVertexAttribArray( 0 );
-
- /* 1: normal */
- glVertexAttribPointer( 1, 4, GL_BYTE, GL_TRUE,
- stride, (void *)offsetof(scene_vert, norm) );
- glEnableVertexAttribArray( 1 );
-
- /* 2: uv */
- glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
- stride, (void *)offsetof(scene_vert, uv) );
- glEnableVertexAttribArray( 2 );
-
- VG_CHECK_GL_ERR();
-
- 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", ctx->indice_count );
- vg_info( " verts:%u\n", ctx->vertex_count );
-}
-
-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 );
-}
-
-static