X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=fa1ce4dbc8bc782a23953c22889aa20d7c27d384;hb=409edea2cf6271956137918e4e0b4f1c2addf620;hp=b4491fa083f3d2000906171e07642944b24339f1;hpb=47a76e9a5151ef020e8554d805e313650718981f;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index b4491fa..fa1ce4d 100644 --- a/scene.h +++ b/scene.h @@ -5,11 +5,27 @@ #include "model.h" #include "bvh.h" -typedef struct scene scene; +typedef struct scene scene; +typedef struct scene_vert scene_vert; + +#pragma pack(push,1) + +/* 32 byte vertexs, we don't care about the normals too much, + * maybe possible to bring down uv to i16s too */ +struct scene_vert +{ + v3f co; /* 3*32 */ + v2f uv; /* 2*32 */ + i8 norm[4]; /* 4*8 */ + u16 lights[4]; /* 4*16 */ +}; + +#pragma pack(pop) struct scene { - mdl_vert *arrvertices; + scene_vert *arrvertices; + u32 *arrindices; u32 vertex_count, indice_count, @@ -22,13 +38,13 @@ struct scene /* Initialize a scene description with bounded buffers */ VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices ) { - u32 vertex_length = max_verts * sizeof(mdl_vert), + u32 vertex_length = max_verts * sizeof(scene_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->arrvertices = (scene_vert *)(pscene+1); pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts ); pscene->vertex_count = 0; @@ -44,11 +60,23 @@ VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices ) return pscene; } +VG_STATIC void scene_vert_pack_norm( scene_vert *vert, v3f norm ) +{ + 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] = 0; /* free byte :D */ +} + /* * 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 ) +VG_STATIC void scene_add_mdl_submesh( scene *pscene, mdl_context *mdl, + mdl_submesh *sm, m4x3f transform ) { if( pscene->vertex_count + sm->vertex_count > pscene->max_vertices ) { @@ -70,8 +98,8 @@ VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl, vg_fatal_exit_loop( "Scene index buffer overflow" ); } - mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ), - *dst_verts = &pscene->arrvertices[ pscene->vertex_count ]; + mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ); + scene_vert *dst_verts = &pscene->arrvertices[ pscene->vertex_count ]; u32 *src_indices = mdl_submesh_indices( mdl, sm ), *dst_indices = &pscene->arrindices[ pscene->indice_count ]; @@ -90,27 +118,20 @@ VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl, for( u32 i=0; ivertex_count; i++ ) { - mdl_vert *pvert = &dst_verts[ i ], - *src = &src_verts[ i ]; + mdl_vert *src = &src_verts[ i ]; + scene_vert *pvert = &dst_verts[ i ]; m4x3_mulv( transform, src->co, pvert->co ); - m3x3_mulv( normal_matrix, src->norm, pvert->norm ); + + v3f normal; + m3x3_mulv( normal_matrix, src->norm, normal ); + scene_vert_pack_norm( pvert, normal ); - 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; iindice_count; i++ ) - { dst_indices[i] = src_indices[i] + pscene->vertex_count; - } pscene->vertex_count += sm->vertex_count; pscene->indice_count += sm->indice_count; @@ -133,12 +154,12 @@ VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] ) pscene->indice_count += 3; } -VG_STATIC void scene_push_vert( scene *pscene, mdl_vert *v ) +VG_STATIC void scene_push_vert( scene *pscene, scene_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 ]; + scene_vert *dst = &pscene->arrvertices[ pscene->vertex_count ]; *dst = *v; pscene->vertex_count ++; @@ -160,21 +181,22 @@ VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm ) __attribute__((warn_unused_result)) VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene ) { - return pscene; + /* FIXME: Why is this disabled? */ + u32 vertex_count = pscene->vertex_count, indice_count = pscene->indice_count, - vertex_length = vertex_count * sizeof(mdl_vert), + vertex_length = vertex_count * sizeof(scene_vert), index_length = indice_count * sizeof(u32), - tot_size = sizeof(scene) + vertex_length + index_length; + tot_size = sizeof(scene) + vertex_length + index_length; /* copy down index data */ - void *dst_indices = pscene->arrvertices + vertex_length; + 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 = (mdl_vert *)(pscene+1); + pscene->arrvertices = (scene_vert *)(pscene+1); pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count); pscene->max_vertices = vertex_count; pscene->max_indices = indice_count; @@ -205,13 +227,51 @@ VG_STATIC scene *scene_free_offline_buffers( void *lin_alloc, scene *pscene ) VG_STATIC void scene_upload( scene *pscene, glmesh *mesh ) { - mesh_upload( mesh, - pscene->arrvertices, pscene->vertex_count, - pscene->arrindices, pscene->indice_count ); + //assert( mesh->loaded == 0 ); - vg_info( "Scene upload\n" ); + 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, pscene->vertex_count*stride, + pscene->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 ); + + /* 0: coordinates */ + glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 ); + glEnableVertexAttribArray( 0 ); + + /* 1: normal */ + glVertexAttribPointer( 1, 3, 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 ); + + /* 3: light cluster */ + glVertexAttribIPointer( 3, 4, GL_UNSIGNED_SHORT, + stride, (void *)offsetof(scene_vert, lights) ); + glEnableVertexAttribArray( 3 ); + + VG_CHECK_GL_ERR(); + + mesh->indice_count = pscene->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( " verts:%u\n", pscene->vertex_count ); } /* @@ -221,9 +281,9 @@ VG_STATIC void scene_upload( scene *pscene, glmesh *mesh ) VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) { scene *s = user; - 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] ]; + 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] ]; box_addpt( bound, pa->co ); box_addpt( bound, pb->co ); @@ -233,11 +293,24 @@ VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis ) { scene *s = user; - 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] ]; + 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] ]; + + #if 0 + + float min, max; + + min = vg_minf( pa->co[axis], pb->co[axis] ); + max = vg_maxf( pa->co[axis], pb->co[axis] ); + min = vg_minf( min, pc->co[axis] ); + max = vg_maxf( max, pc->co[axis] ); + + return (min+max) * 0.5f; + #else return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f); + #endif } VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib ) @@ -265,9 +338,9 @@ VG_STATIC void scene_bh_debug( void *user, u32 item_index ) { scene *s = user; u32 idx = item_index*3; - mdl_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ], - *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ], - *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ]; + scene_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 );