X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=a4f6bd1f005e02cfcc221a7d426df3344eebd6bb;hb=5ecf9cca8b5b9bf876d7e7c7fde03d5b187bb42b;hp=3e3829ecc526c3299cdb9bc8b69db7df09468ac7;hpb=9c85e110fa8b965195438d96625ff9753af362a6;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index 3e3829e..a4f6bd1 100644 --- a/scene.h +++ b/scene.h @@ -28,10 +28,6 @@ struct scene mdl_submesh submesh; }; -#if 0 -GLuint tex_dual_noise; -#endif - static void scene_init( scene *pscene ) { pscene->verts = NULL; @@ -45,75 +41,25 @@ static void scene_init( scene *pscene ) v3_fill( pscene->bbx[0], 999999.9f ); v3_fill( pscene->bbx[1], -999999.9f ); - -#if 0 - static int noise_ready = 0; - if( !noise_ready ) - { - noise_ready = 1; - - u8 *buf = malloc( 256*256*2 ); - - for( int i=0; i<256*256; i++ ) - { - u8 val = rand()&0xff; - buf[i*2] = val; - } - - for( int y=0; y<256; y++ ) - { - for( int x=0; x<256; x++ ) - { - u8 *pr = &buf[(y*256+x)*2], - *pg = &buf[(((y+17)&0xff)*256+((x+37)&0xff))*2+1]; - *pg = *pr; - } - } - - /* TODO: This texture should be delted somewhere */ - glGenTextures( 1, &tex_dual_noise ); - glBindTexture( GL_TEXTURE_2D, tex_dual_noise ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 256, 256, 0, GL_RG, - GL_UNSIGNED_BYTE, buf ); - - vg_tex2d_linear(); - vg_tex2d_repeat(); - - free( buf ); - } -#endif -} - -static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, - size_t emsize ) -{ - if( count+amount > *cap ) - { - *cap = VG_MAX( (*cap)*2, (*cap)+amount ); - - return realloc( buffer, (*cap) * emsize ); - } - - return buffer; -} - -static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize ) -{ - *cap = count; - return realloc( buffer, (*cap) * emsize ); } /* * 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 ) + mdl_submesh *sm, m4x3f transform ) { - pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, - &pscene->vertex_cap, sm->vertex_count, sizeof(mdl_vert) ); + pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, + &pscene->vertex_cap, sm->vertex_count, sizeof(mdl_vert) ); + pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count, - &pscene->indice_cap, sm->indice_count, sizeof(u32) ); + &pscene->indice_cap, sm->indice_count, sizeof(u32) ); + + m3x3f normal_matrix; + m3x3_copy( transform, normal_matrix ); + v3_normalize( normal_matrix[0] ); + v3_normalize( normal_matrix[1] ); + v3_normalize( normal_matrix[2] ); /* Transform and place vertices */ mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ); @@ -130,9 +76,12 @@ static void scene_add_submesh( scene *pscene, mdl_header *mdl, *src = &src_verts[ i ]; m4x3_mulv( transform, src->co, pvert->co ); - m3x3_mulv( transform, src->norm, pvert->norm ); - - v4_copy( src->colour, pvert->colour ); + 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]; v2_copy( src->uv, pvert->uv ); } @@ -146,6 +95,29 @@ static void scene_add_submesh( scene *pscene, mdl_header *mdl, 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] ) +{ + pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count, + &pscene->indice_cap, 3, sizeof(u32) ); + + u32 *dst = &pscene->indices[pscene->indice_count]; + dst[0] = tri[0]; + dst[1] = tri[1]; + dst[2] = tri[2]; + + pscene->indice_count += 3; +} + +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; +} + static void scene_copy_slice( scene *pscene, mdl_submesh *sm ) { sm->indice_start = pscene->submesh.indice_start; @@ -170,8 +142,8 @@ static void scene_fix( scene *pscene ) static void scene_upload( scene *pscene ) { mesh_upload( &pscene->mesh, - pscene->verts, pscene->vertex_count, - pscene->indices, pscene->indice_count ); + pscene->verts, pscene->vertex_count, + pscene->indices, pscene->indice_count ); vg_info( "Scene upload\n" ); vg_info( " indices:%u\n", pscene->indice_count ); @@ -188,16 +160,15 @@ static void scene_draw( scene *pscene ) mesh_drawn( 0, pscene->indice_count ); } -static void scene_free( scene *pscene ) +static void scene_free_offline_buffers( scene *pscene ) { - free( pscene->verts ); - free( pscene->indices ); - - /* TODO: bvh */ + vg_free( pscene->verts ); + vg_free( pscene->indices ); } -static void scene_register(void) +static void scene_free( scene *pscene ) { + scene_free_offline_buffers( pscene ); } /*