-static void player_walkgrid_samplepole( struct grid_sample *s )
-{
- boxf region = {{ s->pos[0] -0.01f, s->pos[1] - 4.0f, s->pos[2] -0.01f},
- { s->pos[0] +0.01f, s->pos[1] + 4.0f, s->pos[2] +0.01f}};
-
-#if 0
- vg_line( region[0],region[1], 0x20ffffff );
-#endif
-
- u32 geo[256];
- v3f tri[3];
- int len = bvh_select_triangles( &world.geo, region, geo, 256 );
-
- const float k_minworld_y = -2000.0f;
-
- float walk_height = k_minworld_y,
- block_height = k_minworld_y;
-
- s->type = k_sample_type_air;
-
- for( int i=0; i<len; i++ )
- {
- u32 *ptri = &world.geo.indices[ geo[i] ];
-
- for( int j=0; j<3; j++ )
- v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
-
- v3f vdown = {0.0f,-1.0f,0.0f};
- v3f sample_from;
- v3_copy( s->pos, sample_from );
- sample_from[1] = region[1][1];
-
- float dist;
- if( ray_tri( tri, sample_from, vdown, &dist ))
- {
- v3f p0;
- v3_muladds( sample_from, vdown, dist, p0 );
-
- if( player_walkgrid_tri_walkable(ptri) )
- {
- if( p0[1] > walk_height )
- {
- walk_height = p0[1];
- }
-
-#if 0
- draw_cross( p0, 0xffffffff, 0.05f );
-#endif
- }
- else
- {
- if( p0[1] > block_height )
- block_height = p0[1];
-#if 0
- draw_cross( p0, 0xff0000ff, 0.05f );
-#endif
- }
- }
- }
-
- s->pos[1] = walk_height;
-
- if( walk_height > k_minworld_y )
- if( block_height > walk_height )
- s->type = k_sample_type_invalid;
- else
- s->type = k_sample_type_valid;
- else
- s->type = k_sample_type_air;
-
-#if 0
- if( s->type == k_sample_type_valid )
- {
- vg_line_pt3( s->pos, 0.01f, 0xff00ff00 );
- }
-#endif
-
-#if 0
- int count = 0;
-
- ray_hit hit;
- hit.dist = 10.0f;
- count = bvh_raycast( &world.geo, sample_pos, vdir, &hit );
-
- if( count )
- {
- v3_copy( hit.pos, s->pos );
-
- if( !player_walkgrid_tri_walkable( hit.tri ) )
- {
- draw_cross( pos, 0xff0000ff, 0.05f );
- return 0;
- }
- else
- {
- draw_cross( pos, 0xff00ff00, 0.05f );
- return count;
- }
- }
- else
- return 0;
-#endif
-}
-
-float const k_gridscale = 0.5f;
-
-enum eclipdir
-{
- k_eclipdir_h = 0,
- k_eclipdir_v = 1
-};
-
-static void player_walkgrid_clip_blocker( struct grid_sample *sa,
- struct grid_sample *sb,
- struct grid_sample *st,
- enum eclipdir dir )
-{
- v3f clipdir, pos;
- int valid_a = sa->type == k_sample_type_valid,
- valid_b = sb->type == k_sample_type_valid;
- struct grid_sample *target = valid_a? sa: sb,
- *other = valid_a? sb: sa;
- v3_copy( target->pos, pos );
- v3_sub( other->pos, target->pos, clipdir );
-
- boxf cell_region;
- v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*2.1f, cell_region[0]);
- v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*2.1f, cell_region[1]);
-
- u32 geo[256];
- v3f tri[3];
- int len = bvh_select_triangles( &world.geo, cell_region, geo, 256 );
-
-
- float start_time = v3_length( clipdir ),
- min_time = start_time;
- v3_normalize( clipdir );
- v3_muls( clipdir, 0.0001f, st->clip[dir] );
-
- for( int i=0; i<len; i++ )
- {
- u32 *ptri = &world.geo.indices[ geo[i] ];
- for( int j=0; j<3; j++ )
- v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
-
- if( player_walkgrid_tri_walkable(ptri) )
- continue;
-
- float dist;
- if(ray_tri( tri, pos, clipdir, &dist ))
- {
- if( dist > 0.0f && dist < min_time )
- {
- min_time = dist;
- sb->type = k_sample_type_air;
- }
- }
- }
-
- if( !(min_time < start_time) )
- min_time = 0.5f * k_gridscale;
-
- min_time = vg_clampf( min_time/k_gridscale, 0.01f, 0.99f );
-
- v3_muls( clipdir, min_time, st->clip[dir] );
-
- v3f p0;
- v3_muladds( target->pos, st->clip[dir], k_gridscale, p0 );
-}
-
-static void player_walkgrid_clip_edge( struct grid_sample *sa,
- struct grid_sample *sb,
- struct grid_sample *st, /* data store */
- enum eclipdir dir )
-{
- v3f clipdir = { 0.0f, 0.0f, 0.0f }, pos;
- int valid_a = sa->type == k_sample_type_valid,
- valid_b = sb->type == k_sample_type_valid;
-
- struct grid_sample *target = valid_a? sa: sb,
- *other = valid_a? sb: sa;
-
- v3_sub( other->pos, target->pos, clipdir );
- clipdir[1] = 0.0f;
-
- v3_copy( target->pos, pos );
-
- boxf cell_region;
- v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, -k_gridscale*1.1f, cell_region[0]);
- v3_muladds( pos, (v3f){1.0f,1.0f,1.0f}, k_gridscale*1.1f, cell_region[1]);
-
- u32 geo[256];
- int len = bvh_select_triangles( &world.geo, cell_region, geo, 256 );
-
- float max_dist = 0.0f;
- v3f tri[3];
- v3f perp;
- v3_cross( clipdir,(v3f){0.0f,1.0f,0.0f},perp );
- v3_muls( clipdir, 0.001f, st->clip[dir] );
-
- for( int i=0; i<len; i++ )
- {
- u32 *ptri = &world.geo.indices[ geo[i] ];
- for( int j=0; j<3; j++ )
- v3_copy( world.geo.verts[ptri[j]].co, tri[j] );
-
- if( !player_walkgrid_tri_walkable(ptri) )
- continue;
-
- for( int k=0; k<3; k++ )
- {
- int ia = k,
- ib = (k+1)%3;
-
- v3f v0, v1;
- v3_sub( tri[ia], pos, v0 );
- v3_sub( tri[ib], pos, v1 );
-
- if( (clipdir[2]*v0[0] - clipdir[0]*v0[2]) *
- (clipdir[2]*v1[0] - clipdir[0]*v1[2]) < 0.0f )
- {
- float da = v3_dot(v0,perp),
- db = v3_dot(v1,perp),
- d = da-db,
- qa = da/d;
-
- v3f p0;
- v3_muls( v1, qa, p0 );
- v3_muladds( p0, v0, 1.0f-qa, p0 );
-
- float h = v3_dot(p0,clipdir)/v3_dot(clipdir,clipdir);
-
- if( h >= max_dist && h <= 1.0f )
- {
- max_dist = h;
- float l = 1.0f/v3_length(clipdir);
- v3_muls( p0, l, st->clip[dir] );
- }
- }
- }
- }
-}
-
-static void player_walkgrid_clip( struct grid_sample *sa,
- struct grid_sample *sb,
- enum eclipdir dir )
-{
- int mintype = VG_MIN( sa->type, sb->type ),
- maxtype = VG_MAX( sa->type, sb->type );
-
- if( maxtype == k_sample_type_valid )
- {
- if( mintype == k_sample_type_air || mintype == k_sample_type_invalid )
- {
- player_walkgrid_clip_edge( sa, sb, sa, dir );
- }
-
-#if 0
- else if( mintype == k_sample_type_invalid )
- {
- player_walkgrid_clip_blocker( sa, sb, dir );
- }
-#endif
- }
-}
-
-static const struct conf
-{
- struct confedge
- {
- /* i: sample index
- * d: data index
- * a: axis index
- * o: the 'other' point to do a A/B test with
- * if its -1, all AB is done.
- */
- int i0, i1,
- d0, d1,
- a0, a1,
- o0, o1;
- }
- edges[2];
- int edge_count;
-}
-k_walkgrid_configs[16] = {
- {{},0},
- {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
- {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
- {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
-
- {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
- {{{ 3,3, 3,0, 1,0, -1,-1 },
- { 1,1, 0,1, 1,0, -1,-1 }}, 2},
- {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
- {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
-
- {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
- {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
- {{{ 2,2, 1,3, 0,1, -1,-1 },
- { 0,0, 0,0, 0,1, -1,-1 }}, 2},
- {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
-
- {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
- {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
- {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
- {{},0},
-};