X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=fa1ce4dbc8bc782a23953c22889aa20d7c27d384;hb=409edea2cf6271956137918e4e0b4f1c2addf620;hp=af24b6cee41cb4c2739407d13af486a9737e4a9b;hpb=1f1d636056450dcd23cce55c0795ec6276272531;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index af24b6c..fa1ce4d 100644 --- a/scene.h +++ b/scene.h @@ -1,993 +1,414 @@ #ifndef SCENE_H #define SCENE_H -#include "vg/vg.h" +#include "common.h" #include "model.h" +#include "bvh.h" -typedef struct scene scene; -typedef struct bvh_node bvh_node; +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 { - glmesh mesh; + scene_vert *arrvertices; - model_vert *verts; - u32 *indices; - - struct - { - bvh_node *nodes; - u32 node_count; - } - bvh; + u32 *arrindices; - u32 vertex_count, - indice_count, - vertex_cap, - indice_cap; + u32 vertex_count, indice_count, + max_vertices, max_indices; boxf bbx; + mdl_submesh submesh; +}; - struct shadower - { - sdf_primative sdf; - esdf_type sdf_type; - } - *shadowers; - - u32 shadower_count, - shadower_cap; +/* 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(scene_vert), + index_length = max_indices * sizeof(u32), + tot_size = sizeof(scene) + vertex_length + index_length; - submodel submesh; -}; + scene *pscene = vg_linear_alloc( lin_alloc, tot_size ); -GLuint tex_dual_noise; + pscene->arrvertices = (scene_vert *)(pscene+1); + pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts ); -static void scene_init( scene *pscene ) -{ - pscene->verts = NULL; - pscene->indices = NULL; pscene->vertex_count = 0; pscene->indice_count = 0; - pscene->shadowers = NULL; - pscene->shadower_count = 0; - pscene->shadower_cap = 0; - pscene->submesh.indice_start = 0; - pscene->submesh.indice_count = 0; + pscene->max_vertices = max_verts; + pscene->max_indices = max_indices; + + memset( &pscene->submesh, 0, sizeof(mdl_submesh) ); v3_fill( pscene->bbx[0], 999999.9f ); v3_fill( pscene->bbx[1], -999999.9f ); - 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 ); - } + return pscene; } -static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, - size_t emsize ) +VG_STATIC void scene_vert_pack_norm( scene_vert *vert, v3f norm ) { - if( count+amount > *cap ) - { - *cap = VG_MAX( (*cap)*2, (*cap)+amount ); - - return realloc( buffer, (*cap) * emsize ); - } - - return buffer; + 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 */ -static void scene_add_model( scene *pscene, model *mdl, submodel *submodel, - v3f pos, float yaw, float scale ) +VG_STATIC void scene_add_mdl_submesh( scene *pscene, mdl_context *mdl, + mdl_submesh *sm, m4x3f transform ) { - pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count, - &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) ); - pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count, - &pscene->indice_cap, submodel->indice_count, sizeof(u32) ); + 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( submodel->sdf_type ) + if( pscene->indice_count + sm->indice_count > pscene->max_indices ) { - pscene->shadowers = buffer_reserve( pscene->shadowers, - pscene->shadower_count, &pscene->shadower_cap, 1, - sizeof( struct shadower )); - - struct shadower *shadower = - &pscene->shadowers[ pscene->shadower_count ++ ]; + vg_error( "%u(current) + %u > %u\n", pscene->indice_count, + sm->indice_count, + pscene->max_indices ); + vg_warn( "%p ... %p\n", pscene, sm ); - shadower->sdf = submodel->sdf; - shadower->sdf_type = submodel->sdf_type; - - v2_muls( shadower->sdf.info, scale, shadower->sdf.info ); - v3_muls( shadower->sdf.origin, scale, shadower->sdf.origin ); - v3_add( pos, shadower->sdf.origin, shadower->sdf.origin ); + vg_fatal_exit_loop( "Scene index buffer overflow" ); } + + 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 ]; /* Transform and place vertices */ - model_vert *src_verts = submodel_vert_data( mdl, submodel ); - u32 *src_indices = submodel_indice_data( mdl, submodel ); - - m4x3f mtx; - m4x3_identity( mtx ); - m4x3_translate( mtx, pos ); - m4x3_rotate_y( mtx, yaw ); - m4x3_scale( mtx, scale ); - boxf bbxnew; - box_copy( submodel->bbx, bbxnew ); - m4x3_transform_aabb( mtx, bbxnew ); + box_copy( sm->bbx, bbxnew ); + m4x3_transform_aabb( transform, bbxnew ); box_concat( pscene->bbx, bbxnew ); - - m3x3f rotation; - m4x3_to_3x3( mtx, rotation ); - float rand_hue = vg_randf(); - - for( u32 i=0; ivertex_count; i++ ) + 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; ivertex_count; i++ ) { - model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ], - *src = &src_verts[ i ]; + mdl_vert *src = &src_verts[ i ]; + scene_vert *pvert = &dst_verts[ i ]; - m4x3_mulv( mtx, src->co, pvert->co ); - m3x3_mulv( rotation, src->norm, pvert->norm ); + m4x3_mulv( transform, src->co, pvert->co ); - v4_copy( src->colour, pvert->colour ); - v2_copy( src->uv, pvert->uv ); + v3f normal; + m3x3_mulv( normal_matrix, src->norm, normal ); + scene_vert_pack_norm( pvert, normal ); - float rel_y = src->co[1] / submodel->bbx[1][1]; - pvert->colour[0] = rel_y; - pvert->colour[2] = rand_hue; + v2_copy( src->uv, pvert->uv ); } - for( u32 i=0; iindice_count; i++ ) - { - u32 *pidx = &pscene->indices[ pscene->indice_count+i ]; - *pidx = src_indices[i] + pscene->vertex_count; - } + for( u32 i=0; iindice_count; i++ ) + dst_indices[i] = src_indices[i] + pscene->vertex_count; - pscene->vertex_count += submodel->vertex_count; - pscene->indice_count += submodel->indice_count; + pscene->vertex_count += sm->vertex_count; + pscene->indice_count += sm->indice_count; } -static void scene_copy_slice( scene *pscene, submodel *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; -} - -static void scene_shadow_sphere( scene *pscene, v3f sphere, - v4f params, v3f lightdir ) +/* + * One by one adders for simplified access (mostly procedural stuff) + */ +VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] ) { - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - - v3f delta; - v3_sub( sphere, vert->co, delta ); + if( pscene->indice_count + 3 > pscene->max_indices ) + vg_fatal_exit_loop( "Scene vertex buffer overflow" ); - float d = v3_dot( lightdir, delta ); - v3f closest; + u32 *dst = &pscene->arrindices[ pscene->indice_count ]; - v3_muls( lightdir, d, closest ); - float dist = v3_dist( closest, delta ), - shading = vg_maxf( dist - params[0], 0.0f ); + dst[0] = tri[0]; + dst[1] = tri[1]; + dst[2] = tri[2]; - shading = vg_minf( shading * params[1], 1.0f ); - vert->colour[1] *= shading; - } + pscene->indice_count += 3; } -static void scene_shadow_gradient( scene *pscene, int comp, - float start, float length ) +VG_STATIC void scene_push_vert( scene *pscene, scene_vert *v ) { - float scale = 1.0f / length; + if( pscene->vertex_count + 1 > pscene->max_vertices ) + vg_fatal_exit_loop( "Scene vertex buffer overflow" ); - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - float shading = start + vert->co[comp] * scale; + scene_vert *dst = &pscene->arrvertices[ pscene->vertex_count ]; + *dst = *v; - vert->colour[1] = shading; - } + pscene->vertex_count ++; } - -/* - * Experimental SDF based shadows - * - * https://iquilezles.org/articles/distfunctions/ - */ -static float sd_cone( v3f co, sdf_primative *prim ) +VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm ) { - float bound = prim->info[1]*1.75f; - if( v3_dist2( prim->origin, co ) > bound*bound ) - return 999999.9f; - - v3f p; - v3_sub( co, prim->origin, p ); - - float h = prim->info[1]; - v2f c = { prim->info[2], prim->info[3] }; + sm->indice_start = pscene->submesh.indice_start; + sm->indice_count = pscene->indice_count - sm->indice_start; - v2f q, w, a, b; - v2_muls( (v2f){ c[0]/c[1], -1.0f }, h, q ); + sm->vertex_start = pscene->submesh.vertex_start; + sm->vertex_count = pscene->vertex_count - sm->vertex_start; - w[0] = v2_length( (v2f){ p[0], p[2] } ); - w[1] = p[1]; - - v2_muladds( w, q, -vg_clampf( v2_dot(w,q)/v2_dot(q,q), 0.0f, 1.0f ), a ); - v2_muladd( w, q, (v2f){ vg_clampf( w[0]/q[0], 0.0f, 1.0f ), 1.0f }, b ); - - float k = vg_signf( q[1] ), - d = vg_minf( v2_dot( a,a ), v2_dot( b,b ) ), - s = vg_maxf( k*(w[0]*q[1]-w[1]*q[0]), k*(w[1]-q[1]) ); - - return sqrtf(d)*vg_signf(s); + pscene->submesh.indice_start = pscene->indice_count; + pscene->submesh.vertex_start = pscene->vertex_count; } -#define CACHE_AMBIENT_SHAPES - -static float scene_ambient_sample( scene *pscene, v3f pos, v3f dir ) +/* finalization: tightly pack data */ +__attribute__((warn_unused_result)) +VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene ) { - float accum = 0.0f; - -#ifdef CACHE_AMBIENT_SHAPES - static struct shadower *local_shadowers[32]; - static int local_shadower_count = 0; - static v3f local_shadower_last = { -99999.9f, -999999.9f, -9999999.9f }; + /* FIXME: Why is this disabled? */ - if( v3_dist2( pos, local_shadower_last ) > 10.0f*10.0f ) - { - local_shadower_count = 0; - v3_copy( pos, local_shadower_last ); + 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 = sizeof(scene) + vertex_length + index_length; - for( int k=0; kshadower_count; k++ ) - { - struct shadower *shadower = &pscene->shadowers[k]; - - if( sd_cone( pos, &shadower->sdf ) <= 20.0f ) - { - local_shadowers[ local_shadower_count ++ ] = shadower; - if( local_shadower_count == vg_list_size( local_shadowers ) ) - break; - } - } - } -#endif - - for( int j=0; j<5; j++ ) - { - v3f tracepos; - v3_muladds( pos, dir, 1.5f*(float)j, tracepos ); + /* copy down index data */ + void *dst_indices = pscene->arrvertices + vertex_count; + memmove( dst_indices, pscene->arrindices, index_length ); - float mindist = 99999.9f; + /* realloc */ + pscene = vg_linear_resize( lin_alloc, pscene, tot_size ); -#ifndef CACHE_AMBIENT_SHAPES + pscene->arrvertices = (scene_vert *)(pscene+1); + pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count); + pscene->max_vertices = vertex_count; + pscene->max_indices = indice_count; - for( int k=0; kshadower_count; k++ ){ - struct shadower *shadower = &pscene->shadowers[k]; -#else - - for( int k=0; ksdf )); - mindist = vg_minf( mindist, dist ); - } - - - accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f; - } - - return accum; + return pscene; } -#define DYNAMIC_GRID -#define JUST_DO_EVERY_VERT - -static void scene_compute_occlusion( scene *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 ) { - v3f sundir = { 0.2f, 0.9f, 0.2f }; - v3_normalize( sundir ); - - /* TODO: Make this sample grid be dynamically required. - * - * 1. Only resample the light grid (1x1x1), when a vertex is outside the - * current cube - * - * 2. Reorder all vertices so that each group of vertices that fit in a - * cube are next to eachother in the buffer. This will save cache - * misses. - * - * for the sorting algorithm, i think we can already assume that *most - * vertices will be quite close to eachother. so instead of doing an - * exhaustive search we can reorder 1k chunks at a time. - */ - - v3f sample_area; - v3_sub( pscene->bbx[1], pscene->bbx[0], sample_area ); - v3_ceil( sample_area, sample_area ); - int ax = sample_area[0], - ay = sample_area[1], - az = sample_area[2]; - -#ifndef DYNAMIC_GRID - float *samplegrid = malloc( ax*ay*az* sizeof(float) ); - - for( int x=0; xbbx[0], sample_pos, sample_pos ); - float accum = scene_ambient_sample( pscene, sample_pos, sundir ); + u32 tot_size = sizeof(scene); - samplegrid[x + y*ax + z*ax*ay] = accum; - }}} -#else - v3i cube_pos = { -999999, -999999, -999999 }; - int cube_resamples = 0, hits = 0, misses = 0; + scene *src_scene = pscene; + mdl_vert *src_verts = pscene->arrvertices; + u32 *src_indices = pscene->arrindices; - float s0=0.0f,s1=0.0f,s2=0.0f,s3=0.0f,s4=0.0f,s5=0.0f,s6=0.0f,s7=0.0f; -#endif + scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size ); + memcpy( dst_scene, src_scene, sizeof(scene) ); - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - v3f rel, q; - -#ifndef DYNAMIC_GRID - v3_sub( vert->co, pscene->bbx[0], q ); -#else - v3_copy( vert->co, q ); -#endif + dst_scene->arrindices = NULL; + dst_scene->arrvertices = NULL; - v3_floor( q, rel ); - v3_sub( q, rel, q ); - - int x=rel[0], - y=rel[1], - z=rel[2]; - -#ifndef JUST_DO_EVERY_VERT -#ifndef DYNAMIC_GRID - x = VG_MIN(x,ax-2); - y = VG_MIN(y,ay-2); - z = VG_MIN(z,az-2); - x = VG_MAX(x,0); - y = VG_MAX(y,0); - z = VG_MAX(z,0); - - float - s0 = samplegrid[ x + y*ax + z*ax*ay], - s1 = samplegrid[(x+1) + y*ax + z*ax*ay], - s2 = samplegrid[ x + (y+1)*ax + z*ax*ay], - s3 = samplegrid[(x+1) + (y+1)*ax + z*ax*ay], - s4 = samplegrid[ x + y*ax + (z+1)*ax*ay], - s5 = samplegrid[(x+1) + y*ax + (z+1)*ax*ay], - s6 = samplegrid[ x + (y+1)*ax + (z+1)*ax*ay], - s7 = samplegrid[(x+1) + (y+1)*ax + (z+1)*ax*ay], -#else - if( x!=cube_pos[0] || y!=cube_pos[1] || z!=cube_pos[2] ) - { - cube_pos[0] = x; - cube_pos[1] = y; - cube_pos[2] = z; - - s0 = scene_ambient_sample( pscene, (v3f){ x,y,z }, sundir ); - s1 = scene_ambient_sample( pscene, (v3f){ x+1,y,z }, sundir ); - s2 = scene_ambient_sample( pscene, (v3f){ x,y+1,z }, sundir ); - s3 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z }, sundir ); - s4 = scene_ambient_sample( pscene, (v3f){ x,y,z+1 }, sundir ); - s5 = scene_ambient_sample( pscene, (v3f){ x+1,y,z+1 }, sundir ); - s6 = scene_ambient_sample( pscene, (v3f){ x,y+1,z+1 }, sundir ); - s7 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z+1 }, sundir ); - - cube_resamples += 8; - misses ++; - } - else - hits ++; - - float -#endif - - s0_s1 = vg_lerpf( s0, s1, q[0] ), - s2_s3 = vg_lerpf( s2, s3, q[0] ), - s4_s5 = vg_lerpf( s4, s5, q[0] ), - s6_s7 = vg_lerpf( s6, s7, q[0] ), - - s0s1_s2s3 = vg_lerpf( s0_s1, s2_s3, q[1] ), - s4s5_s6s7 = vg_lerpf( s4_s5, s6_s7, q[1] ), - s0s1s2s3_s4s5s6s7 = vg_lerpf( s0s1_s2s3, s4s5_s6s7, q[2] ); - - vert->colour[1] = s0s1s2s3_s4s5s6s7; -#else - vert->colour[1] = scene_ambient_sample( pscene, vert->co, sundir ); -#endif - } - -#ifndef DYNAMIC_GRID - int cube_resamples = -1, misses = 0, hits = 0; + return dst_scene; +} #endif - int static_samples = ax*ay*az, - vertex_samples = pscene->vertex_count; - - if( cube_resamples < static_samples ) - vg_success( "Walking cube beat static grid (%d<%d. %d)!\n", - cube_resamples, static_samples, vertex_samples ); - else - vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n", - cube_resamples, static_samples, vertex_samples ); - - vg_info( "Hits; %d, misses: %d\n", hits, misses ); +VG_STATIC void scene_upload( scene *pscene, glmesh *mesh ) +{ + //assert( mesh->loaded == 0 ); -#ifndef DYNAMIC_GRID - free( samplegrid ); -#endif + glGenVertexArrays( 1, &mesh->vao ); + glGenBuffers( 1, &mesh->vbo ); + glGenBuffers( 1, &mesh->ebo ); + glBindVertexArray( mesh->vao ); - return; + size_t stride = sizeof(scene_vert); - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - float accum = 0.0f; + glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo ); + glBufferData( GL_ARRAY_BUFFER, pscene->vertex_count*stride, + pscene->arrvertices, GL_STATIC_DRAW ); - for( int j=0; j<5; j++ ) - { - v3f tracepos; - v3_copy( vert->co, tracepos ); - v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos ); + 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 ); - float mindist = 99999.9f; + /* 1: normal */ + glVertexAttribPointer( 1, 3, GL_BYTE, GL_TRUE, + stride, (void *)offsetof(scene_vert, norm) ); + glEnableVertexAttribArray( 1 ); - for( int k=0; kshadower_count; k++ ) - { - struct shadower *shadower = &pscene->shadowers[k]; - float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf )); - mindist = vg_minf( mindist, dist ); - } + /* 2: uv */ + glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, + stride, (void *)offsetof(scene_vert, uv) ); + glEnableVertexAttribArray( 2 ); - accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f; - } + /* 3: light cluster */ + glVertexAttribIPointer( 3, 4, GL_UNSIGNED_SHORT, + stride, (void *)offsetof(scene_vert, lights) ); + glEnableVertexAttribArray( 3 ); - vert->colour[1] = vg_minf( accum, 1.0f ); - } -} + VG_CHECK_GL_ERR(); -static void scene_upload( scene *pscene ) -{ - mesh_upload( &pscene->mesh, - pscene->verts, pscene->vertex_count, - pscene->indices, pscene->indice_count ); + mesh->indice_count = pscene->indice_count; + mesh->loaded = 1; - vg_info( "Scene upload\n" ); + 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 ); -} - -float scene_tree_sway = 0.1f; - -#if 0 -static void scene_foliage_shader_use(void) -{ - SHADER_USE( shader_debug_vcol ); - - glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uPv" ), - 1, GL_FALSE, (float *)vg_pv ); - - glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uMode" ), debugview ); - glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexMain" ), 0 ); - - glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexGradients" ), 1 ); - vg_tex2d_bind( &tex_gradients, 1 ); - - glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexNoise" ), 2 ); - glActiveTexture( GL_TEXTURE2 ); - glBindTexture( GL_TEXTURE_2D, tex_dual_noise ); - - glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uTime" ), vg_time ); - glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uSwayAmt" ), - scene_tree_sway ); -} -#endif - -static void scene_bind( scene *pscene ) -{ - mesh_bind( &pscene->mesh ); + vg_info( " verts:%u\n", pscene->vertex_count ); } -static void scene_draw( scene *pscene ) -{ - mesh_drawn( 0, pscene->indice_count ); -} +/* + * BVH implementation + */ -static void scene_debugsdf( scene *pscene ) +VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) { - for( int i=0; ishadower_count; i++ ) - { - struct shadower *shadower = &pscene->shadowers[i]; - - v3f base, side; - v3_copy( shadower->sdf.origin, base ); - base[1] -= shadower->sdf.info[1]; - v3_copy( base, side ); - side[0] += shadower->sdf.info[0]; - - vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff ); - vg_line2( side, base, 0xff00ff00, 0xff0000ff ); - vg_line( side, shadower->sdf.origin, 0xff00ff00 ); - } - - v3f p0 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[0][2] }, - p1 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[0][2] }, - p2 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[0][2] }, - p3 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[0][2] }, - - p4 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[1][2] }, - p5 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[1][2] }, - p6 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[1][2] }, - p7 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[1][2] }; + scene *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] ]; - u32 col = 0xffff00c8; - vg_line( p0, p1, col ); - vg_line( p1, p2, col ); - vg_line( p2, p3, col ); - vg_line( p3, p0, col ); - - vg_line( p4, p5, col ); - vg_line( p5, p6, col ); - vg_line( p6, p7, col ); - vg_line( p7, p4, col ); - - vg_line( p0, p4, col ); - vg_line( p1, p5, col ); - vg_line( p2, p6, col ); - vg_line( p3, p7, col ); + box_addpt( bound, pa->co ); + box_addpt( bound, pb->co ); + box_addpt( bound, pc->co ); } -static void scene_register(void) +VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis ) { -} - + scene *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] ]; -/* Physics segment */ - -static int triangle_raycast2d( v3f pA, v3f pB, v3f pC, v3f ray, float *height ) -{ - v2f v0, v1, v2, vp, vp2; - float d, bca = 0.f, bcb = 0.f, bcc = 0.f; - - v0[0] = pB[0] - pA[0]; - v0[1] = pB[2] - pA[2]; - v1[0] = pC[0] - pA[0]; - v1[1] = pC[2] - pA[2]; - v2[0] = pB[0] - pC[0]; - v2[1] = pB[2] - pC[2]; - - d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]); - -#if 0 - /* Backface culling */ - if( v2_cross( v0, v1 ) > 0.f ) - return; -#endif + #if 0 - vp[0] = ray[0] - pA[0]; - vp[1] = ray[2] - pA[2]; + float min, max; - if( v2_cross( v0, vp ) > 0.f ) return 0; - if( v2_cross( vp, v1 ) > 0.f ) return 0; - - vp2[0] = ray[0] - pB[0]; - vp2[1] = ray[2] - pB[2]; + 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] ); - if( v2_cross( vp2, v2 ) > 0.f ) return 0; + return (min+max) * 0.5f; - bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d; - bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d; - bca = 1.f - bcb - bcc; - - *height = pA[1]*bca + pB[1]*bcb + pC[1]*bcc; - return 1; + #else + return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f); + #endif } -/* Temporary */ -static int sample_scene_height( scene *pscene, v3f pos, v3f norm ) +VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib ) { - for( int i=0; iindice_count/3; i++ ) - { - u32 *tri = &pscene->indices[i*3]; + scene *s = user; - float *pA = pscene->verts[tri[0]].co, - *pB = pscene->verts[tri[1]].co, - *pC = pscene->verts[tri[2]].co; - - float height; - if( triangle_raycast2d( pA, pB, pC, pos, &height )) - { - pos[1] = height; - - if( norm ) - { - v3f v0, v1; - v3_sub( pA, pB, v0 ); - v3_sub( pC, pB, v1 ); - v3_cross( v1, v0, norm ); - v3_normalize( norm ); - } + u32 *ti = &s->arrindices[ia*3]; + u32 *tj = &s->arrindices[ib*3]; - return 1; - } - } - return 0; -} + u32 temp[3]; + temp[0] = ti[0]; + temp[1] = ti[1]; + temp[2] = ti[2]; -static void sample_scene_normal( scene *pscene, v3f pos, v3f normal ) -{ - for( int i=0; iindice_count/3; i++ ) - { - u32 *tri = &pscene->indices[i*3]; - - float height; - if( triangle_raycast2d( - pscene->verts[ tri[0] ].co, - pscene->verts[ tri[1] ].co, - pscene->verts[ tri[2] ].co, pos, &height )) - { - v3f v0, v1; - - v3_sub( pscene->verts[ tri[1] ].co, - pscene->verts[ tri[0] ].co, - v0 ); - - v3_sub( pscene->verts[ tri[2] ].co, - pscene->verts[ tri[0] ].co, - v1 ); - - v3_cross( v0, v1, normal ); - v3_normalize( normal ); - return; - } - } + ti[0] = tj[0]; + ti[1] = tj[1]; + ti[2] = tj[2]; - normal[0] = 0.0f; - normal[1] = 1.0f; - normal[2] = 0.0f; + tj[0] = temp[0]; + tj[1] = temp[1]; + tj[2] = temp[2]; } -struct bvh_node +VG_STATIC void scene_bh_debug( void *user, u32 item_index ) { - boxf bbx; - - /* if il is 0, this is a leaf */ - u32 il, count; - union{ u32 ir, start; }; -}; - -static void bvh_update_bounds( scene *s, u32 inode ) -{ - bvh_node *node = &s->bvh.nodes[ inode ]; + scene *s = user; + u32 idx = item_index*3; + scene_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ], + *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ], + *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ]; - box_init_inf( node->bbx ); - for( u32 i=0; icount; i++ ) - { - u32 idx = node->start+i; - model_vert *pa = &s->verts[ s->indices[idx*3+0] ], - *pb = &s->verts[ s->indices[idx*3+1] ], - *pc = &s->verts[ s->indices[idx*3+2] ]; - - box_addpt( node->bbx, pa->co ); - box_addpt( node->bbx, pb->co ); - box_addpt( node->bbx, pc->co ); - } + vg_line( pa->co, pb->co, 0xff0000ff ); + vg_line( pb->co, pc->co, 0xff0000ff ); + vg_line( pc->co, pa->co, 0xff0000ff ); } -static void bvh_subdiv( scene *s, u32 inode ) +VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co, + v3f dir, ray_hit *hit ) { - bvh_node *node = &s->bvh.nodes[ inode ]; - - v3f extent; - v3_sub( node->bbx[1], node->bbx[0], extent ); - - int axis = 0; - if( extent[1] > extent[0] ) axis = 1; - if( extent[2] > extent[axis] ) axis = 2; - - float split = node->bbx[0][axis] + extent[axis]*0.5f; - - /* To beat: 121,687 / 136,579 - * 136,375 - */ - - float avg = 0.0; - for( u32 t=0; tcount; t++ ) - { - u32 *ti = &s->indices[(node->start+t)*3]; - float a = s->verts[ti[0]].co[axis], - b = s->verts[ti[1]].co[axis], - c = s->verts[ti[2]].co[axis]; - avg += (a+b+c)/3.0; - } - avg /= (float)node->count; - - split = avg; - - i32 i = node->start, - j = i + node->count-1; + scene *s = user; + v3f positions[3]; - while( i <= j ) - { - u32 *ti = &s->indices[i*3]; + u32 *tri = &s->arrindices[ index*3 ]; - float a = s->verts[ti[0]].co[axis], - b = s->verts[ti[1]].co[axis], - c = s->verts[ti[2]].co[axis]; - - if( ((a+b+c) / 3.0f) < split ) - i ++; - else - { - /* Swap triangle indices */ - u32 *tj = &s->indices[j*3]; - u32 temp[3]; - temp[0] = ti[0]; - temp[1] = ti[1]; - temp[2] = ti[2]; - - ti[0] = tj[0]; - ti[1] = tj[1]; - ti[2] = tj[2]; - - tj[0] = temp[0]; - tj[1] = temp[1]; - tj[2] = temp[2]; - - j --; - } - } - - u32 left_count = i - node->start; - if( left_count == 0 || left_count == node->count ) return; - - u32 il = s->bvh.node_count ++, - ir = s->bvh.node_count ++; - - struct bvh_node *lnode = &s->bvh.nodes[il], - *rnode = &s->bvh.nodes[ir]; - - lnode->start = node->start; - lnode->count = left_count; - rnode->start = i; - rnode->count = node->count - left_count; - - node->il = il; - node->ir = ir; - node->count = 0; - - bvh_update_bounds( s, il ); - bvh_update_bounds( s, ir ); - bvh_subdiv( s, il ); - bvh_subdiv( s, ir ); -} - -static void bvh_create( scene *s ) -{ - u32 triangle_count = s->indice_count / 3; - s->bvh.nodes = malloc( sizeof(struct bvh_node) * (triangle_count*2-1) ); - - bvh_node *root = &s->bvh.nodes[0]; - s->bvh.node_count = 1; + for( int i=0; i<3; i++ ) + v3_copy( s->arrvertices[tri[i]].co, positions[i] ); - root->il = 0; - root->ir = 0; - root->count = triangle_count; - root->start = 0; - - bvh_update_bounds( s, 0 ); - bvh_subdiv( s, 0 ); - - s->bvh.nodes = - realloc( s->bvh.nodes, sizeof(struct bvh_node) * s->bvh.node_count ); - - vg_success( "BVH done, size: %u/%u\n", s->bvh.node_count, - (triangle_count*2-1) ); -} - -static void bvh_debug_node( scene *s, u32 inode, v3f pos, u32 colour ) -{ - struct bvh_node *node = &s->bvh.nodes[ inode ]; - - if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) && - (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) ) + float t; + if(ray_tri( positions, co, dir, &t )) { - if( !node->count ) + if( t < hit->dist ) { - vg_line_boxf( node->bbx, colour ); - - bvh_debug_node( s, node->il, pos, colour ); - bvh_debug_node( s, node->ir, pos, colour ); - } - else - { - vg_line_boxf( node->bbx, 0xff00ff00 ); - for( u32 i=0; icount; i++ ) - { - u32 idx = (node->start+i)*3; - - model_vert *pa = &s->verts[ s->indices[ idx+0 ] ], - *pb = &s->verts[ s->indices[ idx+1 ] ], - *pc = &s->verts[ s->indices[ idx+2 ] ]; - - vg_line( pa->co, pb->co, 0xff0000ff ); - vg_line( pb->co, pc->co, 0xff0000ff ); - vg_line( pc->co, pa->co, 0xff0000ff ); - } + hit->dist = t; + hit->tri = tri; + return 1; } } -} - -static void bvh_debug( scene *s, v3f pos ) -{ - bvh_debug_node( s, 0, pos, 0x4000ffa8 ); -} - -typedef struct ray_hit ray_hit; -struct ray_hit -{ - float dist; - u32 *tri; - v3f pos, normal; -}; - -int ray_aabb( boxf box, v3f co, v3f dir, float dist ) -{ - v3f v0, v1; - float tmin, tmax; - v3_sub( box[0], co, v0 ); - v3_sub( box[1], co, v1 ); - v3_div( v0, dir, v0 ); - v3_div( v1, dir, v1 ); - - tmin = vg_minf( v0[0], v1[0] ); - tmax = vg_maxf( v0[0], v1[0] ); - tmin = vg_maxf( tmin, vg_minf( v0[1], v1[1] )); - tmax = vg_minf( tmax, vg_maxf( v0[1], v1[1] )); - tmin = vg_maxf( tmin, vg_minf( v0[2], v1[2] )); - tmax = vg_minf( tmax, vg_maxf( v0[2], v1[2] )); - - return tmax >= tmin && tmin < dist && tmax > 0; + return 0; } -static int bvh_ray_tri( scene *sc, u32 *tri, v3f co, v3f dir, ray_hit *hit ) +VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest ) { - float const kEpsilon = 0.00001f; - - v3f v0, v1, h, s, q, n; - float a,f,u,v,t; - - float *pa = sc->verts[tri[0]].co, - *pb = sc->verts[tri[1]].co, - *pc = sc->verts[tri[2]].co; - - v3_sub( pb, pa, v0 ); - v3_sub( pc, pa, v1 ); - v3_cross( dir, v1, h ); - v3_cross( v0, v1, n ); + scene *s = user; - if( v3_dot( n, dir ) > 0.0f ) /* Backface culling */ - return 0; - - /* Parralel */ - a = v3_dot( v0, h ); - if( a > -kEpsilon && a < kEpsilon ) - return 0; - - f = 1.0f/a; - v3_sub( co, pa, s ); - - u = f * v3_dot(s, h); - if( u < 0.0f || u > 1.0f ) - return 0; - - v3_cross( s, v0, q ); - v = f * v3_dot( dir, q ); - if( v < 0.0f || u+v > 1.0f ) - return 0; + v3f positions[3]; + u32 *tri = &s->arrindices[ index*3 ]; + for( int i=0; i<3; i++ ) + v3_copy( s->arrvertices[tri[i]].co, positions[i] ); - t = f * v3_dot(v1, q); - if( t > kEpsilon && t < hit->dist ) - { - hit->dist = t; - hit->tri = tri; - return 1; - } - else return 0; + closest_on_triangle_1( point, positions, closest ); } -static int bvh_ray( scene *s, u32 inode, v3f co, v3f dir, ray_hit *hit ) +VG_STATIC bh_system bh_system_scene = { - bvh_node *node = &s->bvh.nodes[ inode ]; - - if( !ray_aabb( node->bbx, co, dir, hit->dist )) - return 0; - - int count = 0; - - if( node->count ) - { - for( u32 i=0; icount; i++ ) - { - u32 *indices = &s->indices[ (node->start+i)*3 ]; - count += bvh_ray_tri( s, indices, co, dir, hit ); - } - } - else - { - count += bvh_ray( s, node->il, co, dir, hit ); - count += bvh_ray( s, node->ir, co, dir, hit ); - } - - return count; -} + .expand_bound = scene_bh_expand_bound, + .item_centroid = scene_bh_centroid, + .item_closest = scene_bh_closest, + .item_swap = scene_bh_swap, + .item_debug = scene_bh_debug, + .cast_ray = scene_bh_ray +}; -static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit ) +/* + * An extra step is added onto the end to calculate the hit normal + */ +VG_STATIC int scene_raycast( scene *s, bh_tree *bh, + v3f co, v3f dir, ray_hit *hit ) { - v3f pb; - v3_muladds( co, dir, hit->dist, pb ); - - int count = bvh_ray( s, 0, co, dir, hit ); + int count = bh_ray( bh, co, dir, hit ); if( count ) { - //vg_line( co, pb, 0xff00ffff ); - v3f v0, v1; - float *pa = s->verts[hit->tri[0]].co, - *pb = s->verts[hit->tri[1]].co, - *pc = s->verts[hit->tri[2]].co; + float *pa = s->arrvertices[hit->tri[0]].co, + *pb = s->arrvertices[hit->tri[1]].co, + *pc = s->arrvertices[hit->tri[2]].co; v3_sub( pa, pb, v0 ); v3_sub( pc, pb, v1 ); @@ -995,77 +416,14 @@ static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit ) v3_normalize( hit->normal ); v3_muladds( co, dir, hit->dist, hit->pos ); } - else - { - //vg_line( co, pb, 0xff0000ff ); - } return count; } -static int bvh_scene_sample_node_h( scene *s, u32 inode, v3f pos, v3f norm ) -{ - bvh_node *node = &s->bvh.nodes[ inode ]; - - if( (pos[0] >= node->bbx[0][0] && pos[0] <= node->bbx[1][0]) && - (pos[2] >= node->bbx[0][2] && pos[2] <= node->bbx[1][2]) ) - { - if( !node->count ) - { - if( bvh_scene_sample_node_h( s, node->il, pos, norm )) return 1; - if( bvh_scene_sample_node_h( s, node->ir, pos, norm )) return 1; - } - else - { - for( u32 i=0; icount; i++ ) - { - u32 idx = (node->start+i)*3; - model_vert *pa = &s->verts[ s->indices[ idx+0 ] ], - *pb = &s->verts[ s->indices[ idx+1 ] ], - *pc = &s->verts[ s->indices[ idx+2 ] ]; - - float height; - if( triangle_raycast2d( pa->co, pb->co, pc->co, pos, &height )) - { - pos[1] = height; - - if( norm ) - { - v3f v0, v1; - v3_sub( pa->co, pb->co, v0 ); - v3_sub( pc->co, pb->co, v1 ); - v3_cross( v1, v0, norm ); - v3_normalize( norm ); - } - - return 1; - } - } - } - } - - return 0; -} - -static int bvh_scene_sample_h( scene *s, v3f pos, v3f norm) +VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s ) { - return bvh_scene_sample_node_h( s, 0, pos, norm ); -} - -static int bvh_scene_sample( scene *s, v3f pos, ray_hit *hit ) -{ - hit->dist = INFINITY; - - v3f ray_pos; - v3_add( pos, (v3f){0.0f,4.0f,0.0f}, ray_pos ); - - if( bvh_raycast( s, ray_pos, (v3f){0.0f,-1.0f,0.0f}, hit )) - { - pos[1] = hit->pos[1]; - return 1; - } - - return 0; + u32 triangle_count = s->indice_count / 3; + return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 ); } #endif