- 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; k<pscene->shadower_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 );
-
- float mindist = 99999.9f;
-
-#ifndef CACHE_AMBIENT_SHAPES
-
- for( int k=0; k<pscene->shadower_count; k++ ){
- struct shadower *shadower = &pscene->shadowers[k];
-#else
-
- for( int k=0; k<local_shadower_count; k++ ){
- struct shadower *shadower = local_shadowers[k];
-#endif
-
- float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
- mindist = vg_minf( mindist, dist );
- }
-
-
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
-
- return accum;
-}
-
-#define DYNAMIC_GRID
-#define JUST_DO_EVERY_VERT
-
-static void scene_compute_occlusion( 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; 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 );
-
- 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
-
- for( int i=0; i<pscene->vertex_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
-
- 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;
-#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 );
-
-#ifndef DYNAMIC_GRID
- free( samplegrid );
-#endif
-
- return;
-
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
- float accum = 0.0f;
-
- for( int j=0; j<5; j++ )
- {
- v3f tracepos;
- v3_copy( vert->co, tracepos );
- v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos );
-
- float mindist = 99999.9f;
-
- 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 );
- }
-
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
-
- vert->colour[1] = vg_minf( accum, 1.0f );
- }
-}
-
-static void scene_upload( scene *pscene )
-{
- 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 );
-}
-
-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 );
-}
-
-static void scene_draw( scene *pscene )
-{
- mesh_drawn( 0, pscene->indice_count );
-}
-
-static void scene_debugsdf( scene *pscene )
-{
- for( int i=0; i<pscene->shadower_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] };
-
- 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 );
-}
-
-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 */
-static int sample_scene_height( scene *pscene, v3f pos, v3f norm )
-{
- for( int i=0; i<pscene->indice_count/3; i++ )
- {
- u32 *tri = &pscene->indices[i*3];
-
- 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 );
- }
-
- return 1;
- }
- }
- return 0;
-}
-
-static void sample_scene_normal( scene *pscene, v3f pos, v3f normal )
-{
- for( int i=0; i<pscene->indice_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;
- }
- }
-
- normal[0] = 0.0f;
- normal[1] = 1.0f;
- normal[2] = 0.0f;
-}
-
-struct bvh_node
-{