diff options
author | nathan <nathan@disroot.org> | 2025-07-12 09:32:37 +0000 |
---|---|---|
committer | nathan <nathan@disroot.org> | 2025-07-12 09:32:37 +0000 |
commit | 63c926eb0bdcb7ce189c64c09e1650f594f5faeb (patch) | |
tree | 95259878ddb4baceedf276967ed1ef1918da712e | |
parent | 3b70a886ebff7f1aaa8cae47994b351ba6c8d7b5 (diff) | |
download | FindThings-63c926eb0bdcb7ce189c64c09e1650f594f5faeb.tar.gz FindThings-63c926eb0bdcb7ce189c64c09e1650f594f5faeb.tar.bz2 FindThings-63c926eb0bdcb7ce189c64c09e1650f594f5faeb.zip |
hehjfkjfkjdkjffdk
-rw-r--r-- | src/world.c | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/src/world.c b/src/world.c index 51f5692..d8d473b 100644 --- a/src/world.c +++ b/src/world.c @@ -154,10 +154,10 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world) return leafsSize; } -BVHNode buildWorldBVHTree(BVHNode* nodes, size_t nodesSize, const World* world) +BVHNode buildWorldBVHTree(BVHNode* nodes, size_t nodesSize, + int taken[BVH_MAX_BRANCH_COUNT], const World* world) { BVHNode node; - int taken[BVH_MAX_BRANCH_COUNT]; memset(&node, 0, sizeof(BVHNode)); for (int index = 1; index < BVH_MAX_BRANCH_COUNT; ++index) @@ -203,8 +203,7 @@ BVHNode buildWorldBVHTree(BVHNode* nodes, size_t nodesSize, const World* world) } float distance = 0.0; - BoundingBox box; - box = nodes[nodeIndex].box; + BoundingBox box = nodes[nodeIndex].box; // Get distance and bounding box. for (int index = 0; index < branchIndex; ++index) @@ -228,18 +227,22 @@ BVHNode buildWorldBVHTree(BVHNode* nodes, size_t nodesSize, const World* world) } } - if (closest != -1) + if (closest == -1) { - node.branches[branchIndex] = (BVHNode*)FT_MALLOC(sizeof(BVHNode)); + break; + } - if (node.branches[branchIndex] == NULL) - { - ALLOCATION_ERROR; - } + node.branches[branchIndex] = (BVHNode*)FT_MALLOC(sizeof(BVHNode)); - memcpy(node.branches[branchIndex], &nodes[closest], sizeof(BVHNode)); - ++node.branchCount; + if (node.branches[branchIndex] == NULL) + { + ALLOCATION_ERROR; + break; } + + memcpy(node.branches[branchIndex], &nodes[closest], sizeof(BVHNode)); + taken[node.branchCount] = closest; + ++node.branchCount; } // Create node bounding box. @@ -263,10 +266,43 @@ void buildWorldBVH(World* world) Entity* entities = world->entities; // Get leafs. - BVHNode leafs[WORLD_ENTITY_MAX]; - size_t leafsSize = buildWorldBVHLeafs(leafs, world); - memcpy(world->bvhTest, leafs, sizeof(leafs)); - world->bvhTestSize = leafsSize; + BVHNode nodes[WORLD_ENTITY_MAX]; + size_t nodesSize = buildWorldBVHLeafs(nodes, world); + /* memcpy(world->bvhTest, leafs, sizeof(leafs)); */ + /* world->bvhTestSize = leafsSize; */ + + while (nodesSize > 0) + { + int taken[BVH_MAX_BRANCH_COUNT]; + BVHNode node = buildWorldBVHTree(nodes, nodesSize, taken, world); + + // Take out taken nodes. + int nodeIndex = 0; + int removedCount = 0; + + for (int nodeCount = 0; nodeCount < nodesSize; ++nodeCount) + { + bool isTaken = false; + + for (int takenIndex = 0; takenIndex < node.branchCount; ++takenIndex) + { + if (taken[takenIndex] == nodeCount) + { + isTaken = true; + ++removedCount; + break; + } + } + + if (!isTaken) + { + nodes[nodeIndex] = nodes[nodeCount]; + ++nodeIndex; + } + } + + nodesSize -= removedCount; + } } World createWorld(int seed) |