Skip to content

Commit 7d98ad0

Browse files
author
jbikker
committed
Approaching initial tiny_bvh release.
1 parent 5c1f1bc commit 7d98ad0

File tree

19 files changed

+3772
-631
lines changed

19 files changed

+3772
-631
lines changed

examples/features/game.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Game::Tick( float /* deltaTime */ )
7373
Mesh* mesh = scene.meshPool[0];
7474
for( int y = 0; y < 256; y++ ) for( int x = 0; x < 256; x++ )
7575
{
76-
tinybvh::Ray r( float3( x - 128.0f, 128.0f - y, -500.0f ) * 0.001f, float3( 0, 0, 1 ) );
76+
tinybvh::Ray r( tinybvh::bvhvec3( x - 128.0f, 128.0f - y, -500.0f ) * 0.001f, tinybvh::bvhvec3( 0, 0, 1 ) );
7777
mesh->bvh->Intersect( r );
7878
screen->Plot( x + 800, y + 100, r.hit.t < 1e30f ? 0xffffff : 0 );
7979
}

examples/features/lib/tiny_bvh.h

Lines changed: 404 additions & 59 deletions
Large diffs are not rendered by default.

examples/features/template/scene.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ void Mesh::UpdateBVH()
785785
if (!bvh)
786786
{
787787
bvh = new BVH();
788-
bvh->Build( vertices.data(), (uint)vertices.size() / 3 );
788+
bvh->Build( (tinybvh::bvhvec4*)vertices.data(), (uint)vertices.size() / 3 );
789789
}
790790
else
791791
{
@@ -801,9 +801,12 @@ int Mesh::Intersect( Ray& ray )
801801
{
802802
// backup ray and transform original
803803
Ray backupRay = ray;
804-
ray.O = TransformPosition( ray.O, invTransform );
805-
ray.D = TransformVector( ray.D, invTransform );
806-
ray.rD = float3( safercp( ray.D.x ), safercp( ray.D.y ), safercp( ray.D.z ) );
804+
float3* O = (float3*)&ray.O;
805+
float3* D = (float3*)&ray.D;
806+
float3* rD = (float3*)&ray.rD;
807+
*O = TransformPosition( *O, invTransform );
808+
*D = TransformVector( *D, invTransform );
809+
*rD = float3( safercp( D->x ), safercp( D->y ), safercp( D->z ) );
807810
// trace ray through BVH
808811
uint steps = bvh->Intersect( ray );
809812
// restore ray origin and direction
@@ -819,7 +822,8 @@ int Mesh::Intersect( Ray& ray )
819822
void Mesh::UpdateWorldBounds()
820823
{
821824
worldBounds.Reset();
822-
float3 bmin = bvh->bvhNode[0].aabbMin, bmax = bvh->bvhNode[0].aabbMax;
825+
float3 bmin = *(float3*)&bvh->bvhNode[0].aabbMin;
826+
float3 bmax = *(float3*)&bvh->bvhNode[0].aabbMax;
823827
for (int i = 0; i < 8; i++)
824828
{
825829
float3 corner( i & 1 ? bmax.x : bmin.x, i & 2 ? bmax.y : bmin.y, i & 4 ? bmax.z : bmin.z );
@@ -2420,7 +2424,7 @@ void Scene::InitializeGPUData()
24202424
}
24212425
}
24222426
// allocate buffers for GPU data
2423-
bvhNodeData = new Buffer( nodeCount * sizeof( BVH::BVHNode ) );
2427+
bvhNodeData = new Buffer( nodeCount * sizeof( tinybvh::BVH::BVHNode ) );
24242428
triangleData = new Buffer( primCount * 3 * sizeof( float4 ) );
24252429
triangleIdxData = new Buffer( idxCount * sizeof( uint ) );
24262430
offsetData = new Buffer( (int)meshPool.size() * 2 * sizeof( uint4 ) );
@@ -2438,12 +2442,12 @@ void Scene::InitializeGPUData()
24382442
uint4* offset = (uint4*)offsetData->GetHostPtr();
24392443
if (tlas)
24402444
{
2441-
memcpy( bvhPtr, tlas->bvhNode, tlas->newNodePtr * sizeof( BVH::BVHNode ) );
2445+
memcpy( bvhPtr, tlas->bvhNode, tlas->newNodePtr * sizeof( tinybvh::BVH::BVHNode ) );
24422446
memcpy( idxPtr, tlas->triIdx, meshPool.size() * sizeof( uint ) );
24432447
}
24442448
for (int s = sky->width * sky->height, i = 0; i < s; i++)
24452449
((float4*)skyData->GetHostPtr())[i] = float4( sky->pixels[i], 0 );
2446-
bvhPtr += 2 * (int)meshPool.size() * sizeof( BVH::BVHNode );
2450+
bvhPtr += 2 * (int)meshPool.size() * sizeof( tinybvh::BVH::BVHNode );
24472451
idxPtr += (int)meshPool.size() * sizeof( uint );
24482452
for (int s = (int)meshPool.size(), i = 0; i < s; i++)
24492453
{
@@ -2553,7 +2557,7 @@ int Scene::Intersect( Ray& ray )
25532557
else
25542558
{
25552559
// use a local stack instead of a recursive function
2556-
BVH::BVHNode* node = &tlas->bvhNode[0], * stack[128];
2560+
tinybvh::BVH::BVHNode* node = &tlas->bvhNode[0], * stack[128];
25572561
uint stackPtr = 0, steps = 0;
25582562
// traversl loop; terminates when the stack is empty
25592563
while (1)
@@ -2573,8 +2577,8 @@ int Scene::Intersect( Ray& ray )
25732577
continue;
25742578
}
25752579
// current node is an interior node: visit child nodes, ordered
2576-
BVH::BVHNode* child1 = &tlas->bvhNode[node->leftFirst];
2577-
BVH::BVHNode* child2 = &tlas->bvhNode[node->leftFirst + 1];
2580+
tinybvh::BVH::BVHNode* child1 = &tlas->bvhNode[node->leftFirst];
2581+
tinybvh::BVH::BVHNode* child2 = &tlas->bvhNode[node->leftFirst + 1];
25782582
float dist1 = child1->Intersect( ray ), dist2 = child2->Intersect( ray );
25792583
if (dist1 > dist2) { swap( dist1, dist2 ); swap( child1, child2 ); }
25802584
if (dist1 == 1e30f)

0 commit comments

Comments
 (0)