25be1e5fe0e152be57fef01d060c3a5af93393f5
8 * create a bh_system with functions filled out for expand, centroid, and swap.
9 * optionally include item_debug and cast_ray functions if needed, otherwise,
12 * create a bh_tree struct with:
13 * user: a pointer back the base of the data you are ordering
14 * system: the system we created above which will deal with the data
16 * call bh_create( bh_tree *bh, u32 item_count )
17 * static int bh_ray( bh_tree *bh, u32 inode, v3f co, v3f dir, ray_hit *hit )
18 * static int bh_select( bh_tree *bh, boxf box, u32 *buffer, int len )
21 typedef struct bh_node bh_node
;
22 typedef struct bh_tree bh_tree
;
23 typedef struct bh_system bh_system
;
29 /* if il is 0, this is a leaf */
31 union{ u32 ir
, start
; };
45 void (*expand_bound
)( void *user
, boxf bound
, u32 item_index
);
46 float (*item_centroid
)( void *user
, u32 item_index
, int axis
);
47 void (*item_swap
)( void *user
, u32 ia
, u32 ib
);
51 * item_debug - draw this item quickly usually with lines
52 * cast_ray - shoot a ray against the object, if this is not set,
53 * raycasts will simply return the hit on the bvh node
56 void (*item_debug
)( void *user
, u32 item_index
);
57 int (*cast_ray
)( void *user
, u32 index
, v3f co
, v3f dir
, ray_hit
*hit
);
60 static void bh_update_bounds( bh_tree
*bh
, u32 inode
)
62 bh_node
*node
= &bh
->nodes
[ inode
];
64 box_init_inf( node
->bbx
);
65 for( u32 i
=0; i
<node
->count
; i
++ )
67 u32 idx
= node
->start
+i
;
68 bh
->system
->expand_bound( bh
->user
, node
->bbx
, idx
);
72 static void bh_subdivide( bh_tree
*bh
, u32 inode
)
74 bh_node
*node
= &bh
->nodes
[ inode
];
77 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
80 if( extent
[1] > extent
[0] ) axis
= 1;
81 if( extent
[2] > extent
[axis
] ) axis
= 2;
83 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
86 for( u32 t
=0; t
<node
->count
; t
++ )
88 u32 idx
= node
->start
+t
;
89 avg
+= bh
->system
->item_centroid( bh
->user
, idx
, axis
);
91 avg
/= (float)node
->count
;
96 j
= i
+ node
->count
-1;
100 if( bh
->system
->item_centroid( bh
->user
, i
, axis
) < split
)
104 bh
->system
->item_swap( bh
->user
, i
, j
);
109 u32 left_count
= i
- node
->start
;
110 if( left_count
== 0 || left_count
== node
->count
) return;
112 u32 il
= bh
->node_count
++,
113 ir
= bh
->node_count
++;
115 bh_node
*lnode
= &bh
->nodes
[il
],
116 *rnode
= &bh
->nodes
[ir
];
118 lnode
->start
= node
->start
;
119 lnode
->count
= left_count
;
121 rnode
->count
= node
->count
- left_count
;
127 /* TODO: Implement max depth, or stack */
128 bh_update_bounds( bh
, il
);
129 bh_update_bounds( bh
, ir
);
130 bh_subdivide( bh
, il
);
131 bh_subdivide( bh
, ir
);
134 static void bh_create( bh_tree
*bh
, bh_system
*sys
, void *user
, u32 item_count
)
138 bh
->nodes
= vg_alloc( sizeof(bh_node
) * (item_count
*2-1) );
140 bh_node
*root
= &bh
->nodes
[0];
145 root
->count
= item_count
;
148 bh_update_bounds( bh
, 0 );
149 bh_subdivide( bh
, 0 );
151 bh
->nodes
= vg_realloc( bh
->nodes
, sizeof(bh_node
) * bh
->node_count
);
152 vg_success( "BVH done, size: %u/%u\n", bh
->node_count
, (item_count
*2-1) );
155 vg_fatal_exit_loop( "Test crash from loader" );
159 static void bh_debug_node( bh_tree
*bh
, u32 inode
, v3f pos
, u32 colour
)
161 bh_node
*node
= &bh
->nodes
[ inode
];
163 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
164 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
168 vg_line_boxf( node
->bbx
, colour
);
170 bh_debug_node( bh
, node
->il
, pos
, colour
);
171 bh_debug_node( bh
, node
->ir
, pos
, colour
);
175 vg_line_boxf( node
->bbx
, 0xff00ff00 );
177 if( bh
->system
->item_debug
)
179 for( u32 i
=0; i
<node
->count
; i
++ )
181 u32 idx
= node
->start
+i
;
182 bh
->system
->item_debug( bh
->user
, idx
);
189 static int bh_ray( bh_tree
*bh
, u32 inode
, v3f co
, v3f dir
, ray_hit
*hit
)
196 stack
[1] = bh
->nodes
[0].il
;
197 stack
[2] = bh
->nodes
[0].ir
;
201 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
202 if( ray_aabb( inode
->bbx
, co
, dir
, hit
->dist
) )
206 for( u32 i
=0; i
<inode
->count
; i
++ )
208 u32 idx
= inode
->start
+i
;
210 if( bh
->system
->cast_ray
)
211 count
+= bh
->system
->cast_ray( bh
->user
, idx
, co
, dir
, hit
);
220 if( depth
+1 >= vg_list_size(stack
) )
222 vg_error( "Maximum stack reached!\n" );
226 stack
[depth
] = inode
->il
;
227 stack
[depth
+1] = inode
->ir
;
240 static int bh_select( bh_tree
*bh
, boxf box
, u32
*buffer
, int len
)
247 stack
[1] = bh
->nodes
[0].il
;
248 stack
[2] = bh
->nodes
[0].ir
;
252 bh_node
*inode
= &bh
->nodes
[ stack
[depth
] ];
253 if( box_overlap( inode
->bbx
, box
) )
257 if( count
+ inode
->count
>= len
)
260 for( u32 i
=0; i
<inode
->count
; i
++ )
261 buffer
[ count
++ ] = inode
->start
+i
;
267 if( depth
+1 >= vg_list_size(stack
) )
269 vg_error( "Maximum stack reached!\n" );
273 stack
[depth
] = inode
->il
;
274 stack
[depth
+1] = inode
->ir
;