X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=scene.h;h=42ca838cc7e28ca3a5e1468f3c9997374ba06e95;hb=06f283ec6d2bb1b768cfa01b7ccd073d4f5a3bb3;hp=ff970007dfb5fce17dbfc9c03f2a3be2b27c9bbb;hpb=afa80c76d03f5e983092e9d7be33a9102a7ab25e;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/scene.h b/scene.h index ff97000..42ca838 100644 --- a/scene.h +++ b/scene.h @@ -1047,6 +1047,56 @@ static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit ) return count; } +static int bvh_select_triangles( scene *s, boxf box, u32 *triangles, int len ) +{ + vg_line_boxf( box, 0xffffff00 ); + + /* TODO: use this stack system on the raycast function */ + int count = 0; + u32 stack[100]; + u32 depth = 1; + + stack[0] = 0; + stack[1] = s->bvh.nodes[0].il; + stack[2] = s->bvh.nodes[0].ir; + + while(depth) + { + bvh_node *inode = &s->bvh.nodes[ stack[depth] ]; + if( box_overlap( inode->bbx, box ) ) + { + if( inode->count ) + { + if( count + inode->count >= len ) + return count; + + for( u32 i=0; icount; i++ ) + triangles[ count ++ ] = (inode->start+i)*3; + + depth --; + } + else + { + if( depth+1 >= vg_list_size(stack) ) + { + vg_error( "Maximum stack reached!" ); + return count; + } + + stack[depth] = inode->il; + stack[depth+1] = inode->ir; + depth ++; + } + } + else + { + depth --; + } + } + + return count; +} + static int bvh_scene_sample_node_h( scene *s, u32 inode, v3f pos, v3f norm ) { bvh_node *node = &s->bvh.nodes[ inode ];