X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=d8fac1ad524a05672feabee916237ddfbeac8acf;hb=1fd91b77f23ce1593037e01b9abb62859545b400;hp=af9e26add0585bc3d4e1eb4b583547da7c453034;hpb=3f59dc191c7792947cd2d3f6588be06933fd49f0;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index af9e26a..d8fac1a 100644 --- a/scene.h +++ b/scene.h @@ -1,884 +1,448 @@ -typedef struct model model; -typedef struct submodel submodel; -typedef struct model_vert model_vert; -typedef struct scene scene; -typedef struct sdf_primative sdf_primative; -typedef enum esdf_type esdf_type; +#ifndef SCENE_H +#define SCENE_H -GLuint tex_dual_noise; +#include "common.h" +#include "model.h" +#include "bvh.h" -#pragma pack(push,1) -struct model -{ - u32 identifier; +typedef struct scene_context scene_context; +typedef struct scene_vert scene_vert; - u32 vertex_count, - indice_count, - layer_count; -}; +#pragma pack(push,1) -struct sdf_primative +/* 32 byte vertexs, we don't care about the normals too much, + * maybe possible to bring down uv to i16s too */ +struct scene_vert { - v4f origin; /* xyz, yaw */ - /* Cone: - x base scale - y height - */ - v4f info; + v3f co; /* 3*32 */ + v2f uv; /* 2*32 */ + i8 norm[4]; /* 4*8 */ + u16 flags; /* only for the cpu. its junk on the gpu */ + u16 unused[3]; }; -struct submodel -{ - u32 indice_start, - indice_count, - vertex_start, - vertex_count; - - boxf bbx; - sdf_primative sdf; - - enum esdf_type - { - k_sdf_none = 0, - k_sdf_cone, - k_sdf_sphere, - k_sdf_box - } - sdf_type; - - char name[32]; -}; - -struct model_vert -{ - v3f co, - norm; - v4f colour; - v2f uv; -}; #pragma pack(pop) -struct scene -{ - GLuint vao, vbo, ebo; +/* + * 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. + */ - model_vert *verts; - u32 *indices; +struct scene_context +{ + scene_vert *arrvertices; + u32 *arrindices; - u32 vertex_count, - indice_count, - vertex_cap, - indice_cap; + u32 vertex_count, indice_count, + max_vertices, max_indices; boxf bbx; - - struct shadower - { - sdf_primative sdf; - esdf_type sdf_type; - } - *shadowers; - - u32 shadower_count, - shadower_cap; + mdl_submesh submesh; }; -static void scene_init( scene *pscene ) +static u32 scene_mem_required( scene_context *ctx ) { - 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; - - 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; - } + u32 vertex_length = vg_align8(ctx->max_vertices * sizeof(scene_vert)), + index_length = vg_align8(ctx->max_indices * sizeof(u32)); - 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 vertex_length + index_length; } -/* https://www.shadertoy.com/view/4sfGzS */ -#define SHADER_VALUE_NOISE_3D \ -"uniform sampler2D uTexNoise;" \ -"" \ -"float noise( vec3 x )" \ -"{" \ - "vec3 i = floor(x);" \ - "vec3 f = fract(x);" \ - "f = f*f*(3.0-2.0*f);" \ - "vec2 uv = (i.xy+vec2(37.0,17.0)*i.z) + f.xy;" \ - "vec2 rg = texture( uTexNoise, (uv+0.5)/256.0).yx;"\ - "return mix( rg.x, rg.y, f.z );" \ -"}" \ -"" \ -"const mat3 m = mat3( 0.00, 0.80, 0.60," \ - "-0.80, 0.36, -0.48," \ - "-0.60, -0.48, 0.64 );" \ -"" \ -"float fractalNoise( vec3 x )" \ -"{" \ - "vec3 q = 8.0*x;" \ - "float f;" \ - "f = 0.5000*noise( q ); q = m*q*2.01;" \ - "f += 0.2500*noise( q ); q = m*q*2.02;" \ - "f += 0.1250*noise( q ); q = m*q*2.03;" \ - "f += 0.0625*noise( q ); q = m*q*2.01;" \ - "return f;" \ -"}" - -SHADER_DEFINE( shader_debug_vcol, - "layout (location=0) in vec3 a_co;" - "layout (location=1) in vec3 a_norm;" - "layout (location=2) in vec4 a_colour;" - "layout (location=3) in vec2 a_uv;" - "" - "uniform mat4 uPv;" - "uniform mat4 uMdl;" - "uniform float uTime;" - "uniform float uSwayAmt;" - "" - "out vec4 aColour;" - "out vec2 aUv;" - "out vec3 aNorm;" - "out vec3 aCo;" - "" - "vec3 compute_sway( vec3 pos )" - "{" - "vec4 sines = vec4( sin(uTime + pos.x)*1.0," - "sin(uTime*1.2 + pos.z*2.0)*1.1," - "sin(uTime*2.33)*0.5," - "sin(uTime*0.6 + pos.x*0.3)*1.3 );" - - "vec3 offset = vec3( sines.x+sines.y*sines.w, 0.0, sines.x+sines.z );" - "return pos + offset*a_colour.r*uSwayAmt;" - "}" - "" - "void main()" - "{" - "vec3 swaypos = compute_sway( a_co );" - "gl_Position = uPv * uMdl * vec4( swaypos, 1.0 );" - "aColour = a_colour;" - "aUv = a_uv;" - "aNorm = normalize(mat3(uMdl) * a_norm);" - "aCo = a_co;" - "}", - /* Fragment */ - "out vec4 FragColor;" - "" - "uniform int uMode;" - "uniform sampler2D uTexMain;" - "uniform sampler2D uTexGradients;" - "" - /* Include */ SHADER_VALUE_NOISE_3D - "" - "in vec4 aColour;" - "in vec2 aUv;" - "in vec3 aNorm;" - "in vec3 aCo;" - "" - "void main()" - "{" - "vec4 colour = vec4(1.0,0.0,0.5,1.0);" - "vec4 diffuse = texture( uTexMain, aUv );" - - "if( uMode == 1 )" - "{" - "colour = vec4(aNorm * 0.5 + 0.5, 1.0);" - "}" - "if( uMode == 2 )" - "{" - "colour = aColour;" - "}" - "if( uMode == 3 )" - "{" - "float light = dot(aNorm, vec3(0.2,0.8,0.1));" - "vec3 grid3 = fract(aCo);" - - "colour = vec4(vec3(light)*(1.0-grid3*0.3),1.0);" - "}" - "if( uMode == 4 )" - "{" - "colour = vec4( aUv, 0.0, 1.0 );" - "}" - "if( uMode == 5 )" - "{" - "if( diffuse.a < 0.45 ) discard;" - "colour = diffuse;" - "}" - "if( uMode == 6 )" - "{" - "float r1 = fractalNoise(aCo);" - "colour = vec4( vec3(r1), 1.0 );" - "}" - "if( uMode == 7 )" - "{" - "if( diffuse.a < 0.45 ) discard;" - "float lighting = 1.0 - aColour.g;" - "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);" - "}" - "if( uMode == 8 )" - "{" - "if( diffuse.a < 0.45 ) discard;" - "float light = 1.0 - aColour.g;" - "light = pow(light,1.6)*(diffuse.r*0.7+0.5);" - "float r1 = fractalNoise(aCo*0.01);" - - "vec2 gradUV = vec2(light*1.9,r1+aColour.b*0.1);" - "vec4 gradient_sample = texture( uTexGradients, gradUV );" - "colour = aColour*light;" - "}" - - "FragColor = colour;" - "}" - , - UNIFORMS({ "uPv", "uMode", "uTexMain", "uTexGradients", "uTexNoise", \ - "uTime", "uSwayAmt", "uMdl" }) -) - -/* - * Helper functions for file offsets - */ -static submodel *model_get_submodel( model *mdl, int id ) +static +void scene_init( scene_context *ctx, u32 max_vertices, u32 max_indices ) { - return ((submodel*)(mdl+1)) + id; -} + 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; -static model_vert *model_vertex_base( model *mdl ) -{ - return (model_vert *)model_get_submodel( mdl, mdl->layer_count ); -} + memset( &ctx->submesh, 0, sizeof(mdl_submesh) ); -static u32 *model_indice_base( model *mdl ) -{ - return (u32 *)(model_vertex_base( mdl ) + mdl->vertex_count); + v3_fill( ctx->bbx[0], 999999.9f ); + v3_fill( ctx->bbx[1], -999999.9f ); } -static model_vert *submodel_vert_data( model *mdl, submodel *sub ) +void scene_supply_buffer( scene_context *ctx, void *buffer ) { - return model_vertex_base(mdl) + sub->vertex_start; -} + u32 vertex_length = vg_align8( ctx->max_vertices * sizeof(scene_vert) ); -static u32 *submodel_indice_data( model *mdl, submodel *sub ) -{ - return model_indice_base(mdl) + sub->indice_start; + ctx->arrvertices = buffer; + ctx->arrindices = (u32*)(((u8*)buffer) + vertex_length); } -/* Returns -1 if not found */ -static int submodel_get( model *mdl, const char *name ) -{ - for( int i=0; ilayer_count; i++ ) - { - if( !strcmp( model_get_submodel(mdl,i)->name, name )) - { - return i; - } - } - - return -1; -} - -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 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_model( scene *pscene, model *mdl, int id, - v3f pos, float yaw, float scale ) +static void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl, + mdl_submesh *sm, m4x3f transform ) { - submodel *submodel = model_get_submodel( mdl, id ); - - 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( submodel->sdf_type ) - { - pscene->shadowers = buffer_reserve( pscene->shadowers, - pscene->shadower_count, &pscene->shadower_cap, 1, - sizeof( struct shadower )); - - struct shadower *shadower = - &pscene->shadowers[ pscene->shadower_count ++ ]; + 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 ); + } - 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 ); + 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 */ - 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_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 ); + v3_normalize( normal_matrix[0] ); + v3_normalize( normal_matrix[1] ); + v3_normalize( normal_matrix[2] ); - m3x3f rotation; - m4x3_to_3x3( mtx, rotation ); - - float rand_hue = vg_randf(); + for( u32 i=0; ivertex_count; i++ ){ + mdl_vert *src = &src_verts[i]; + scene_vert *pvert = &dst_verts[i]; - for( u32 i=0; ivertex_count; i++ ) - { - model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ], - *src = &src_verts[ i ]; + m4x3_mulv( transform, src->co, pvert->co ); - m4x3_mulv( mtx, src->co, pvert->co ); - m3x3_mulv( rotation, src->norm, pvert->norm ); - - 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, src->colour[0]*(1.0f/255.0f) ); - 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; - } + u32 real_indices = 0; + for( u32 i=0; iindice_count/3; i++ ){ + u32 *src = &src_indices[i*3], + *dst = &dst_indices[real_indices]; - pscene->vertex_count += submodel->vertex_count; - pscene->indice_count += submodel->indice_count; -} + 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 ); -static void scene_shadow_sphere( scene *pscene, v3f sphere, - v4f params, v3f lightdir ) -{ - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; +#if 0 + if( v3_length2( tn ) <= 0.00001f ) + continue; +#endif - v3f delta; - v3_sub( sphere, vert->co, delta ); + dst[0] = src[0] + ctx->vertex_count; + dst[1] = src[1] + ctx->vertex_count; + dst[2] = src[2] + ctx->vertex_count; - float d = v3_dot( lightdir, delta ); - v3f closest; + real_indices += 3; + } - v3_muls( lightdir, d, closest ); - float dist = v3_dist( closest, delta ), - shading = vg_maxf( dist - params[0], 0.0f ); + if( real_indices != sm->indice_count ) + vg_warn( "Zero area triangles in model\n" ); - shading = vg_minf( shading * params[1], 1.0f ); - vert->colour[1] *= shading; - } + ctx->vertex_count += sm->vertex_count; + ctx->indice_count += real_indices; } -static void scene_shadow_gradient( scene *pscene, int comp, - float start, float length ) +/* + * One by one adders for simplified access (mostly procedural stuff) + */ +static void scene_push_tri( scene_context *ctx, u32 tri[3] ) { - float scale = 1.0f / length; + 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 ); - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - float shading = start + vert->co[comp] * scale; + u32 *dst = &ctx->arrindices[ ctx->indice_count ]; - vert->colour[1] = shading; - } -} + dst[0] = tri[0]; + dst[1] = tri[1]; + dst[2] = tri[2]; -/* Temporary */ -static int sample_scene_height( scene *pscene, v3f pos ) -{ - for( int i=0; iindice_count/3; i++ ) - { - u32 *tri = &pscene->indices[i*3]; - - float height; - if( triangle_raycast( - pscene->verts[ tri[0] ].co, - pscene->verts[ tri[1] ].co, - pscene->verts[ tri[2] ].co, pos, &height )) - { - pos[1] = height; - return 1; - } - } - return 0; + ctx->indice_count += 3; } -static void sample_scene_normal( scene *pscene, v3f pos, v3f normal ) +static void scene_push_vert( scene_context *ctx, scene_vert *v ) { - for( int i=0; iindice_count/3; i++ ) - { - u32 *tri = &pscene->indices[i*3]; - - float height; - if( triangle_raycast( - 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; - } - } + 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; - normal[0] = 0.0f; - normal[1] = 1.0f; - normal[2] = 0.0f; + ctx->vertex_count ++; } -/* - * Experimental SDF based shadows - * - * https://iquilezles.org/articles/distfunctions/ - */ -static float sd_cone( v3f co, sdf_primative *prim ) +static void scene_copy_slice( scene_context *ctx, mdl_submesh *sm ) { - float bound = prim->info[1]*1.75f; - if( v3_dist2( prim->origin, co ) > bound*bound ) - return 999999.9f; + sm->indice_start = ctx->submesh.indice_start; + sm->indice_count = ctx->indice_count - sm->indice_start; - v3f p; - v3_sub( co, prim->origin, p ); + sm->vertex_start = ctx->submesh.vertex_start; + sm->vertex_count = ctx->vertex_count - sm->vertex_start; - float h = prim->info[1]; - v2f c = { prim->info[2], prim->info[3] }; - - v2f q, w, a, b; - v2_muls( (v2f){ c[0]/c[1], -1.0f }, h, q ); - - 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]) ); + ctx->submesh.indice_start = ctx->indice_count; + ctx->submesh.vertex_start = ctx->vertex_count; +} - return sqrtf(d)*vg_signf(s); +static void scene_set_vertex_flags( scene_context *ctx, + u32 start, u32 count, u16 flags ){ + for( u32 i=0; iarrvertices[ start + i ].flags = flags; } -#define CACHE_AMBIENT_SHAPES +struct scene_upload_info{ + scene_context *ctx; + glmesh *mesh; +}; -static float scene_ambient_sample( scene *pscene, v3f pos, v3f dir ) +static void async_scene_upload( void *payload, u32 size ) { - 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 }; - - if( v3_dist2( pos, local_shadower_last ) > 10.0f*10.0f ) - { - local_shadower_count = 0; - v3_copy( pos, local_shadower_last ); - - 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 + struct scene_upload_info *info = payload; - for( int j=0; j<5; j++ ) - { - v3f tracepos; - v3_muladds( pos, dir, 1.5f*(float)j, tracepos ); + //assert( mesh->loaded == 0 ); + + glmesh *mesh = info->mesh; + scene_context *ctx = info->ctx; - float mindist = 99999.9f; + glGenVertexArrays( 1, &mesh->vao ); + glGenBuffers( 1, &mesh->vbo ); + glGenBuffers( 1, &mesh->ebo ); + glBindVertexArray( mesh->vao ); -#ifndef CACHE_AMBIENT_SHAPES + size_t stride = sizeof(scene_vert); - for( int k=0; kshadower_count; k++ ){ - struct shadower *shadower = &pscene->shadowers[k]; -#else + glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo ); + glBufferData( GL_ARRAY_BUFFER, ctx->vertex_count*stride, + ctx->arrvertices, GL_STATIC_DRAW ); - for( int k=0; kvao ); + 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 ); - float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf )); - mindist = vg_minf( mindist, dist ); - } + /* 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 ); - accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f; - } - - return accum; -} + VG_CHECK_GL_ERR(); + + mesh->indice_count = ctx->indice_count; + mesh->loaded = 1; -#define DYNAMIC_GRID -#define JUST_DO_EVERY_VERT + 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_compute_occlusion( scene *pscene ) +static void scene_upload_async( scene_context *ctx, glmesh *mesh ) { - 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 ); - - samplegrid[x + y*ax + z*ax*ay] = accum; - }}} -#else - v3i cube_pos = { -999999, -999999, -999999 }; - int cube_resamples = 0, hits = 0, misses = 0; - - 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 + vg_async_item *call = vg_async_alloc( sizeof(struct scene_upload_info) ); - 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 + struct scene_upload_info *info = call->payload; + info->mesh = mesh; + info->ctx = ctx; - 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 ++; + vg_async_dispatch( call, async_scene_upload ); +} - float -#endif +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 ); - 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] ), + u32 hdr_size = vg_align8(sizeof(struct scene_upload_info)); + vg_async_item *call = vg_async_alloc( hdr_size + buf_size ); - 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] ); + struct scene_upload_info *info = call->payload; - vert->colour[1] = s0s1s2s3_s4s5s6s7; -#else - vert->colour[1] = scene_ambient_sample( pscene, vert->co, sundir ); -#endif - } + info->mesh = mesh; + info->ctx = scene; -#ifndef DYNAMIC_GRID - int cube_resamples = -1, misses = 0, hits = 0; -#endif - - int static_samples = ax*ay*az, - vertex_samples = pscene->vertex_count; + void *buffer = ((u8*)call->payload)+hdr_size; + scene_supply_buffer( scene, buffer ); - 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 ); + return call; +} - vg_info( "Hits; %d, misses: %d\n", hits, misses ); -#ifndef DYNAMIC_GRID - free( samplegrid ); -#endif +/* + * BVH implementation + */ - return; +static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index ) +{ + 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] ]; + + box_addpt( bound, pa->co ); + box_addpt( bound, pb->co ); + box_addpt( bound, pc->co ); +} - for( int i=0; ivertex_count; i++ ) - { - model_vert *vert = &pscene->verts[i]; - float accum = 0.0f; +static float scene_bh_centroid( void *user, u32 item_index, int axis ) +{ + 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] ]; - for( int j=0; j<5; j++ ) - { - v3f tracepos; - v3_copy( vert->co, tracepos ); - v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos ); + #if 0 - float mindist = 99999.9f; + float min, max; - 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 ); - } + 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] ); - accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f; - } + return (min+max) * 0.5f; - vert->colour[1] = vg_minf( accum, 1.0f ); - } + #else + return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f); + #endif } -static void scene_upload( scene *pscene ) +static void scene_bh_swap( void *user, u32 ia, u32 ib ) { - glGenVertexArrays( 1, &pscene->vao ); - glGenBuffers( 1, &pscene->vbo ); - glGenBuffers( 1, &pscene->ebo ); - glBindVertexArray( pscene->vao ); - - glBindBuffer( GL_ARRAY_BUFFER, pscene->vbo ); - glBufferData( GL_ARRAY_BUFFER, pscene->vertex_count*sizeof(model_vert), - pscene->verts, GL_STATIC_DRAW ); - - glBindVertexArray( pscene->vao ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pscene->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, pscene->indice_count*sizeof(u32), - pscene->indices, GL_STATIC_DRAW ); - - glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void*)0 ); - glEnableVertexAttribArray( 0 ); + scene_context *s = user; - glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, norm) ); - glEnableVertexAttribArray( 1 ); + u32 *ti = &s->arrindices[ia*3]; + u32 *tj = &s->arrindices[ib*3]; - glVertexAttribPointer( 2, 4, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, colour) ); - glEnableVertexAttribArray( 2 ); + u32 temp[3]; + temp[0] = ti[0]; + temp[1] = ti[1]; + temp[2] = ti[2]; - glVertexAttribPointer( 3, 2, GL_FLOAT, GL_FALSE, - sizeof(model_vert), (void *)offsetof(model_vert, uv) ); - glEnableVertexAttribArray( 3 ); + ti[0] = tj[0]; + ti[1] = tj[1]; + ti[2] = tj[2]; - VG_CHECK_GL(); + tj[0] = temp[0]; + tj[1] = temp[1]; + tj[2] = temp[2]; +} - vg_info( "Scene upload\n" ); - vg_info( " indices:%u\n", pscene->indice_count ); - vg_info( " verts:%u\n", pscene->vertex_count ); +static void scene_bh_debug( void *user, u32 item_index ) +{ + 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 ] ], + *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ]; + + vg_line( pa->co, pb->co, 0xff0000ff ); + vg_line( pb->co, pc->co, 0xff0000ff ); + vg_line( pc->co, pa->co, 0xff0000ff ); } -float scene_tree_sway = 0.1f; -static void scene_draw( scene *pscene, int count, int start ) +static void scene_bh_closest( void *user, u32 index, v3f point, v3f closest ) { - SHADER_USE( shader_debug_vcol ); + scene_context *s = user; - glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uPv" ), - 1, GL_FALSE, (float *)vg_pv ); + 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] ); - 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 ); + closest_on_triangle_1( point, positions, closest ); +} - glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexNoise" ), 2 ); - glActiveTexture( GL_TEXTURE2 ); - glBindTexture( GL_TEXTURE_2D, tex_dual_noise ); +static bh_system bh_system_scene = +{ + .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, + .system_type = 0x1 +}; - glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uTime" ), vg_time ); - glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uSwayAmt" ), - scene_tree_sway ); +/* + * An extra step is added onto the end to calculate the hit normal + */ +static int scene_raycast( scene_context *s, bh_tree *bh, + v3f co, v3f dir, ray_hit *hit, u16 ignore ) +{ + hit->tri = NULL; - glBindVertexArray( pscene->vao ); + bh_iter it; + bh_iter_init_ray( 0, &it, co, dir, hit->dist ); + i32 idx; - if( count == -1 ) - { - glDrawElements( GL_TRIANGLES, pscene->indice_count, - GL_UNSIGNED_INT, - (void *)0 - ); - } - else - { - glDrawElements( GL_TRIANGLES, count, - GL_UNSIGNED_INT, - (void *)(start*sizeof(u32)) - ); - } + while( bh_next( bh, &it, &idx ) ){ + u32 *tri = &s->arrindices[ idx*3 ]; - if( debugsdf ) - { - 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 ); - } + if( s->arrvertices[tri[0]].flags & ignore ) continue; - 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] }, + v3f vs[3]; + for( u32 i=0; i<3; i++ ) + v3_copy( s->arrvertices[tri[i]].co, vs[i] ); + + f32 t; + if( ray_tri( vs, co, dir, &t, 0 ) ){ + if( t < hit->dist ){ + hit->dist = t; + hit->tri = tri; + } + } + } - 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] }; + if( hit->tri ){ + v3f v0, v1; - 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 ); + 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 ); + v3_cross( v1, v0, hit->normal ); + v3_normalize( hit->normal ); + v3_muladds( co, dir, hit->dist, hit->pos ); } + + return hit->tri?1:0; } -static void scene_register(void) +static bh_tree *scene_bh_create( void *lin_alloc, scene_context *s ) { - SHADER_INIT( shader_debug_vcol ); + u32 triangle_count = s->indice_count / 3; + return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 ); } + +#endif