+#ifndef SCENE_H
+#define SCENE_H
+
#include "vg/vg.h"
#include "model.h"
-GLuint tex_dual_noise;
+typedef struct scene scene;
+typedef struct bvh_node bvh_node;
struct scene
{
model_vert *verts;
u32 *indices;
+
+ struct
+ {
+ bvh_node *nodes;
+ u32 node_count;
+ }
+ bvh;
u32 vertex_count,
indice_count,
boxf bbx;
- struct shadower
- {
- sdf_primative sdf;
- esdf_type sdf_type;
- }
- *shadowers;
-
u32 shadower_count,
shadower_cap;
submodel submesh;
};
+GLuint tex_dual_noise;
+
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;
}
}
-/* 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,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 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 * vec4(uMdl * vec4(swaypos,1.0), 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.2 ) discard;"
- "float lighting = 1.0 - aColour.g*0.8;"
-
- "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
- "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
- "vec3 lt = vec3(0.2,0.2,0.2 ) + "
- "vec3(1.0,1.0,0.9)*light1 + "
- "vec3(0.1,0.3,0.4 )*light2;"
-
-
- "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);"
- "colour = vec4(colour.rgb*lt,1.0);"
-
- "vec2 gradUV = vec2(lighting*1.9,aColour.b*0.8);"
- "vec4 gradient_sample = texture( uTexGradients, gradUV );"
- "colour = colour*gradient_sample;"
- "}"
- "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);"
- "vec4 gradient_sample = texture( uTexGradients, gradUV );"
- "colour = gradient_sample*light;"
- "}"
-
- "FragColor = colour;"
- "}"
- ,
- UNIFORMS({ "uPv", "uMode", "uTexMain", "uTexGradients", "uTexNoise", \
- "uTime", "uSwayAmt", "uMdl" })
-)
-
-SHADER_DEFINE( shader_standard_lit,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 uMdl;"
- ""
- "out vec4 aColour;"
- "out vec2 aUv;"
- "out vec3 aNorm;"
- "out vec3 aCo;"
- ""
- "void main()"
- "{"
- "gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );"
- "aColour = a_colour;"
- "aUv = a_uv;"
- "aNorm = mat3(uMdl) * a_norm;"
- "aCo = a_co;"
- "}",
- /* Fragment */
- "out vec4 FragColor;"
- ""
- "uniform sampler2D uTexMain;"
- "uniform vec4 uColour;"
- ""
- "in vec4 aColour;"
- "in vec2 aUv;"
- "in vec3 aNorm;"
- "in vec3 aCo;"
- ""
- "void main()"
- "{"
- "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
-
- "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
- "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
- "diffuse += vec3(0.2,0.2,0.2 ) + "
- "vec3(1.0,1.0,0.9)*light1 + "
- "vec3(0.1,0.3,0.4 )*light2;"
-
- "FragColor = vec4((diffuse*uColour.rgb),"
- "aColour.a*uColour.a);"
- "}"
- ,
- UNIFORMS({ "uColour","uTexMain","uPv","uMdl" })
-)
-
-SHADER_DEFINE( shader_unlit,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 uMdl;"
- ""
- "out vec4 aColour;"
- "out vec2 aUv;"
- "out vec3 aNorm;"
- "out vec3 aCo;"
- ""
- "void main()"
- "{"
- "gl_Position = uPv * vec4(uMdl * vec4(a_co,1.0), 1.0);"
- "aColour = a_colour;"
- "aUv = a_uv;"
- "aNorm = mat3(uMdl) * a_norm;"
- "aCo = a_co;"
- "}",
- /* Fragment */
- "out vec4 FragColor;"
- ""
- "uniform sampler2D uTexMain;"
- "uniform vec4 uColour;"
- ""
- "in vec4 aColour;"
- "in vec2 aUv;"
- "in vec3 aNorm;"
- "in vec3 aCo;"
- ""
- "void main()"
- "{"
- "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
- "FragColor = vec4(pow(diffuse,vec3(1.0)),1.0);"
- "}"
- ,
- UNIFORMS({ "uTexMain", "uPv", "uMdl" })
-)
-
static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
size_t emsize )
{
&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 ++ ];
-
- 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 );
- }
/* Transform and place vertices */
model_vert *src_verts = submodel_vert_data( mdl, submodel );
pscene->indice_count += submodel->indice_count;
}
+static void scene_add_foliage( scene *pscene, model *mdl, submodel *submodel,
+ 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) );
+
+ /* Transform and place vertices */
+ model_vert *src_verts = submodel_vert_data( mdl, submodel );
+ u32 *src_indices = submodel_indice_data( mdl, submodel );
+
+ boxf bbxnew;
+ box_copy( submodel->bbx, bbxnew );
+ m4x3_transform_aabb( transform, bbxnew );
+ box_concat( pscene->bbx, bbxnew );
+
+ float rand_hue = vg_randf();
+ for( u32 i=0; i<submodel->vertex_count; i++ )
+ {
+ model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
+ *src = &src_verts[ i ];
+
+ m4x3_mulv( transform, src->co, pvert->co );
+ m3x3_mulv( transform, src->norm, pvert->norm );
+
+ v4_copy( src->colour, pvert->colour );
+ v2_copy( src->uv, pvert->uv );
+
+ float rel_y = src->co[1] / submodel->bbx[1][1];
+ pvert->colour[0] = rel_y;
+ pvert->colour[2] = rand_hue;
+ }
+
+ for( u32 i=0; i<submodel->indice_count; i++ )
+ {
+ u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
+ *pidx = src_indices[i] + pscene->vertex_count;
+ }
+
+ pscene->vertex_count += submodel->vertex_count;
+ pscene->indice_count += submodel->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 )
+static void scene_upload( scene *pscene )
{
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
+ mesh_upload( &pscene->mesh,
+ pscene->verts, pscene->vertex_count,
+ pscene->indices, pscene->indice_count );
+
+ vg_info( "Scene upload\n" );
+ vg_info( " indices:%u\n", pscene->indice_count );
+ vg_info( " verts:%u\n", pscene->vertex_count );
+}
- v3f delta;
- v3_sub( sphere, vert->co, delta );
+float scene_tree_sway = 0.1f;
- float d = v3_dot( lightdir, delta );
- v3f closest;
+#if 0
+static void scene_foliage_shader_use(void)
+{
+ SHADER_USE( shader_debug_vcol );
- v3_muls( lightdir, d, closest );
- float dist = v3_dist( closest, delta ),
- shading = vg_maxf( dist - params[0], 0.0f );
+ glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uPv" ),
+ 1, GL_FALSE, (float *)vg_pv );
- shading = vg_minf( shading * params[1], 1.0f );
- vert->colour[1] *= shading;
- }
+ 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_shadow_gradient( scene *pscene, int comp,
- float start, float length )
+static void scene_bind( scene *pscene )
{
- float scale = 1.0f / length;
+ mesh_bind( &pscene->mesh );
+}
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
- float shading = start + vert->co[comp] * scale;
+static void scene_draw( scene *pscene )
+{
+ mesh_drawn( 0, pscene->indice_count );
+}
- vert->colour[1] = shading;
- }
+static void scene_register(void)
+{
+}
+
+
+/* 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
+
+ vp[0] = ray[0] - pA[0];
+ vp[1] = ray[2] - pA[2];
+
+ 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];
+
+ if( v2_cross( vp2, v2 ) > 0.f ) return 0;
+
+ 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;
}
/* Temporary */
*pC = pscene->verts[tri[2]].co;
float height;
- if( triangle_raycast( pA, pB, pC, pos, &height ))
+ if( triangle_raycast2d( pA, pB, pC, pos, &height ))
{
pos[1] = height;
u32 *tri = &pscene->indices[i*3];
float height;
- if( triangle_raycast(
+ if( triangle_raycast2d(
pscene->verts[ tri[0] ].co,
pscene->verts[ tri[1] ].co,
pscene->verts[ tri[2] ].co, pos, &height ))
normal[2] = 0.0f;
}
-/*
- * Experimental SDF based shadows
- *
- * https://iquilezles.org/articles/distfunctions/
- */
-static float sd_cone( v3f co, sdf_primative *prim )
+struct bvh_node
{
- 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] };
-
- 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];
+ boxf bbx;
- 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 );
+ /* if il is 0, this is a leaf */
+ u32 il, count;
+ union{ u32 ir, start; };
+};
- 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]) );
+static void bvh_update_bounds( scene *s, u32 inode )
+{
+ bvh_node *node = &s->bvh.nodes[ inode ];
- return sqrtf(d)*vg_signf(s);
+ box_init_inf( node->bbx );
+ for( u32 i=0; i<node->count; 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 );
+ }
}
-#define CACHE_AMBIENT_SHAPES
-
-static float scene_ambient_sample( scene *pscene, v3f pos, v3f dir )
+static void bvh_subdiv( scene *s, u32 inode )
{
- float accum = 0.0f;
+ bvh_node *node = &s->bvh.nodes[ inode ];
-#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 };
+ v3f extent;
+ v3_sub( node->bbx[1], node->bbx[0], extent );
- if( v3_dist2( pos, local_shadower_last ) > 10.0f*10.0f )
- {
- local_shadower_count = 0;
- v3_copy( pos, local_shadower_last );
+ int axis = 0;
+ if( extent[1] > extent[0] ) axis = 1;
+ if( extent[2] > extent[axis] ) axis = 2;
- for( int k=0; k<pscene->shadower_count; k++ )
- {
- struct shadower *shadower = &pscene->shadowers[k];
+ float split = node->bbx[0][axis] + extent[axis]*0.5f;
- if( sd_cone( pos, &shadower->sdf ) <= 20.0f )
- {
- local_shadowers[ local_shadower_count ++ ] = shadower;
- if( local_shadower_count == vg_list_size( local_shadowers ) )
- break;
- }
- }
+ /* To beat: 121,687 / 136,579
+ * 136,375
+ */
+
+ float avg = 0.0;
+ for( u32 t=0; t<node->count; 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;
}
-#endif
+ avg /= (float)node->count;
+
+ split = avg;
- for( int j=0; j<5; j++ )
+ i32 i = node->start,
+ j = i + node->count-1;
+
+ while( i <= j )
{
- v3f tracepos;
- v3_muladds( pos, dir, 1.5f*(float)j, tracepos );
+ u32 *ti = &s->indices[i*3];
- float mindist = 99999.9f;
+ float a = s->verts[ti[0]].co[axis],
+ b = s->verts[ti[1]].co[axis],
+ c = s->verts[ti[2]].co[axis];
-#ifndef CACHE_AMBIENT_SHAPES
+ 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 --;
+ }
+ }
- for( int k=0; k<pscene->shadower_count; k++ ){
- struct shadower *shadower = &pscene->shadowers[k];
-#else
+ u32 left_count = i - node->start;
+ if( left_count == 0 || left_count == node->count ) return;
- for( int k=0; k<local_shadower_count; k++ ){
- struct shadower *shadower = local_shadowers[k];
-#endif
+ u32 il = s->bvh.node_count ++,
+ ir = s->bvh.node_count ++;
- float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
- mindist = vg_minf( mindist, dist );
- }
+ 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;
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
-
- return accum;
-}
+ node->il = il;
+ node->ir = ir;
+ node->count = 0;
-#define DYNAMIC_GRID
-#define JUST_DO_EVERY_VERT
+ bvh_update_bounds( s, il );
+ bvh_update_bounds( s, ir );
+ bvh_subdiv( s, il );
+ bvh_subdiv( s, ir );
+}
-static void scene_compute_occlusion( scene *pscene )
+static void bvh_create( scene *s )
{
- 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.
- */
+ u32 triangle_count = s->indice_count / 3;
+ s->bvh.nodes = malloc( sizeof(struct bvh_node) * (triangle_count*2-1) );
- 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];
+ bvh_node *root = &s->bvh.nodes[0];
+ s->bvh.node_count = 1;
+
+ root->il = 0;
+ root->ir = 0;
+ root->count = triangle_count;
+ root->start = 0;
-#ifndef DYNAMIC_GRID
- float *samplegrid = malloc( ax*ay*az* sizeof(float) );
+ bvh_update_bounds( s, 0 );
+ bvh_subdiv( s, 0 );
- for( int x=0; x<ax; x++ ){
- for( int y=0; y<ay; y++ ){
- for( int z=0; z<az; z++ )
- {
- v3f sample_pos = { x,y,z };
- v3_add( pscene->bbx[0], sample_pos, sample_pos );
- float accum = scene_ambient_sample( pscene, sample_pos, sundir );
+ s->bvh.nodes =
+ realloc( s->bvh.nodes, sizeof(struct bvh_node) * s->bvh.node_count );
- samplegrid[x + y*ax + z*ax*ay] = accum;
- }}}
-#else
- v3i cube_pos = { -999999, -999999, -999999 };
- int cube_resamples = 0, hits = 0, misses = 0;
+ vg_success( "BVH done, size: %u/%u\n", s->bvh.node_count,
+ (triangle_count*2-1) );
+}
- 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
+static void bvh_debug_node( scene *s, u32 inode, v3f pos, u32 colour )
+{
+ struct bvh_node *node = &s->bvh.nodes[ inode ];
- for( int i=0; i<pscene->vertex_count; i++ )
+ 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]) )
{
- 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
-
- 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] )
+ if( !node->count )
{
- 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 ++;
+ vg_line_boxf( node->bbx, colour );
+
+ bvh_debug_node( s, node->il, pos, colour );
+ bvh_debug_node( s, node->ir, pos, colour );
}
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] ),
+ {
+ vg_line_boxf( node->bbx, 0xff00ff00 );
+ for( u32 i=0; i<node->count; i++ )
+ {
+ u32 idx = (node->start+i)*3;
- 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] );
+ model_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
+ *pb = &s->verts[ s->indices[ idx+1 ] ],
+ *pc = &s->verts[ s->indices[ idx+2 ] ];
- vert->colour[1] = s0s1s2s3_s4s5s6s7;
-#else
- vert->colour[1] = scene_ambient_sample( pscene, vert->co, sundir );
-#endif
+ vg_line( pa->co, pb->co, 0xff0000ff );
+ vg_line( pb->co, pc->co, 0xff0000ff );
+ vg_line( pc->co, pa->co, 0xff0000ff );
+ }
+ }
}
+}
-#ifndef DYNAMIC_GRID
- int cube_resamples = -1, misses = 0, hits = 0;
-#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 );
+static void bvh_debug( scene *s, v3f pos )
+{
+ bvh_debug_node( s, 0, pos, 0x4000ffa8 );
+}
- vg_info( "Hits; %d, misses: %d\n", hits, misses );
+typedef struct ray_hit ray_hit;
+struct ray_hit
+{
+ float dist;
+ u32 *tri;
+ v3f pos, normal;
+};
-#ifndef DYNAMIC_GRID
- free( samplegrid );
-#endif
+int ray_aabb( boxf box, v3f co, v3f dir, float dist )
+{
+ v3f v0, v1;
+ float tmin, tmax;
- return;
+ 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;
+}
- for( int i=0; i<pscene->vertex_count; i++ )
+static int bvh_ray_tri( scene *sc, u32 *tri, v3f co, v3f dir, ray_hit *hit )
+{
+ v3f positions[3];
+ for( int i=0; i<3; i++ )
+ v3_copy( sc->verts[tri[i]].co, positions[i] );
+
+ float t;
+ if(ray_tri( positions, co, dir, &t ))
{
- model_vert *vert = &pscene->verts[i];
- float accum = 0.0f;
-
- for( int j=0; j<5; j++ )
+ if( t < hit->dist )
{
- v3f tracepos;
- v3_copy( vert->co, tracepos );
- v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos );
+ hit->dist = t;
+ hit->tri = tri;
+ return 1;
+ }
+ }
- float mindist = 99999.9f;
+ return 0;
+}
- for( int k=0; k<pscene->shadower_count; k++ )
- {
- struct shadower *shadower = &pscene->shadowers[k];
- float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
- mindist = vg_minf( mindist, dist );
- }
+static int bvh_ray( scene *s, u32 inode, v3f co, v3f dir, ray_hit *hit )
+{
+ bvh_node *node = &s->bvh.nodes[ inode ];
+
+ if( !ray_aabb( node->bbx, co, dir, hit->dist ))
+ return 0;
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
+ int count = 0;
- vert->colour[1] = vg_minf( accum, 1.0f );
+ if( node->count )
+ {
+ for( u32 i=0; i<node->count; 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;
}
-static void scene_upload( scene *pscene )
+static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
{
- mesh_upload( &pscene->mesh,
- pscene->verts, pscene->vertex_count,
- pscene->indices, pscene->indice_count );
+ v3f pb;
+ v3_muladds( co, dir, hit->dist, pb );
- vg_info( "Scene upload\n" );
- vg_info( " indices:%u\n", pscene->indice_count );
- vg_info( " verts:%u\n", pscene->vertex_count );
-}
+ int count = bvh_ray( s, 0, co, dir, hit );
-float scene_tree_sway = 0.1f;
+ if( count )
+ {
+ //vg_line( co, pb, 0xff00ffff );
-static void scene_foliage_shader_use(void)
-{
- SHADER_USE( shader_debug_vcol );
+ 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;
+
+ 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 );
+ }
+ else
+ {
+ //vg_line( co, pb, 0xff0000ff );
+ }
- glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uPv" ),
- 1, GL_FALSE, (float *)vg_pv );
+ return count;
+}
- glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uMode" ), debugview );
- glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexMain" ), 0 );
+static int bvh_select_triangles( scene *s, boxf box, u32 *triangles, int len )
+{
+ /* TODO: use this stack system on the raycast function */
+ int count = 0;
+ u32 stack[100];
+ u32 depth = 2;
+
+ stack[0] = 0;
+ stack[1] = s->bvh.nodes[0].il;
+ stack[2] = s->bvh.nodes[0].ir;
- glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexGradients" ), 1 );
- vg_tex2d_bind( &tex_gradients, 1 );
+ while(depth)
+ {
+ bvh_node *inode = &s->bvh.nodes[ stack[depth] ];
+ if( box_overlap( inode->bbx, box ) )
+ {
+ if( inode->count )
+ {
+ if( count + inode->count >= len )
+ {
+ vg_error( "Maximum buffer reached!\n" );
+ return count;
+ }
- glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexNoise" ), 2 );
- glActiveTexture( GL_TEXTURE2 );
- glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
+ for( u32 i=0; i<inode->count; i++ )
+ triangles[ count ++ ] = (inode->start+i)*3;
- glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uTime" ), vg_time );
- glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uSwayAmt" ),
- scene_tree_sway );
+ depth --;
+ }
+ else
+ {
+ if( depth+1 >= vg_list_size(stack) )
+ {
+ vg_error( "Maximum stack reached!\n" );
+ return count;
+ }
+
+ stack[depth] = inode->il;
+ stack[depth+1] = inode->ir;
+ depth ++;
+ }
+ }
+ else
+ {
+ depth --;
+ }
+ }
+
+ return count;
}
-static void scene_bind( scene *pscene )
+static int bvh_scene_sample_node_h( scene *s, u32 inode, v3f pos, v3f norm )
{
- mesh_bind( &pscene->mesh );
+ 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; i<node->count; 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 void scene_draw( scene *pscene )
+static int bvh_scene_sample_h( scene *s, v3f pos, v3f norm)
{
- mesh_drawn( 0, pscene->indice_count );
+ return bvh_scene_sample_node_h( s, 0, pos, norm );
}
-static void scene_debugsdf( scene *pscene )
+static int bvh_scene_sample( scene *s, v3f pos, ray_hit *hit )
{
- for( int i=0; i<pscene->shadower_count; i++ )
- {
- struct shadower *shadower = &pscene->shadowers[i];
+ hit->dist = INFINITY;
- 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];
+ v3f ray_pos;
+ v3_add( pos, (v3f){0.0f,4.0f,0.0f}, ray_pos );
- vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff );
- vg_line2( side, base, 0xff00ff00, 0xff0000ff );
- vg_line( side, shadower->sdf.origin, 0xff00ff00 );
+ if( bvh_raycast( s, ray_pos, (v3f){0.0f,-1.0f,0.0f}, hit ))
+ {
+ pos[1] = hit->pos[1];
+ return 1;
}
- 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] };
-
- 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 );
+ return 0;
}
-static void scene_register(void)
-{
- SHADER_INIT( shader_debug_vcol );
- SHADER_INIT( shader_standard_lit );
- SHADER_INIT( shader_unlit );
-}
+#endif