- 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 );
- }