aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2025-07-16 03:14:53 +0000
committernathan <nathansmith@disroot.org>2025-07-16 03:14:53 +0000
commit9ffea3734b995edccb8fe649409d7136aa19f26b (patch)
treeb045783a253efb3a2b9df264a93e7eb0b4aa8653
parentcead4fadf830cdb910ae0175c39cad339a835b8e (diff)
downloadFindThings-9ffea3734b995edccb8fe649409d7136aa19f26b.tar.gz
FindThings-9ffea3734b995edccb8fe649409d7136aa19f26b.tar.bz2
FindThings-9ffea3734b995edccb8fe649409d7136aa19f26b.zip
Brain not braining
-rw-r--r--src/world.c201
-rw-r--r--src/world.h2
2 files changed, 21 insertions, 182 deletions
diff --git a/src/world.c b/src/world.c
index 29da6a2..cd34c55 100644
--- a/src/world.c
+++ b/src/world.c
@@ -153,124 +153,37 @@ size_t buildWorldBVHLeafs(BVHNode leafs[WORLD_ENTITY_MAX], const World* world)
return leafsSize;
}
-BVHNode buildWorldBVHTree(BVHNode* nodes, size_t nodesSize,
- int taken[BVH_MAX_BRANCH_COUNT],
- BVHNode* level, size_t levelSize,
+BVHNode buildWorldBVHTree(BVHNode* leafs, size_t leafsSize,
const World* world)
{
- BVHNode node;
- memset(&node, 0, sizeof(BVHNode));
+ BVHNode* nodes = leafs;
+ size_t nodesSize = leafsSize;
- for (int index = 1; index < BVH_MAX_BRANCH_COUNT; ++index)
+ // Yet another copy and paste half fix.
+ while (nodesSize > 1)
{
- taken[index] = -1;
- }
-
- // Add first node to branch.
- node.branches[0] = (BVHNode*)FT_MALLOC(sizeof(BVHNode));
-
- if (node.branches[0] == NULL)
- {
- ALLOCATION_ERROR;
- return node;
- }
-
- memcpy(node.branches[0], &nodes[0], sizeof(BVHNode));
- taken[0] = 0;
- node.branchCount = 1;
-
- for (int branchIndex = 1; branchIndex < BVH_MAX_BRANCH_COUNT; ++branchIndex)
- {
- int closest = -1;
- float closestDistance = world->size.x;
-
- // Find closest node.
- for (int nodeIndex = 0; nodeIndex < nodesSize; ++nodeIndex)
+ BVHNode node;
+ int branches[BVH_MAX_BRANCH_COUNT];
+
+ for (int branchIndex = 0; branchIndex < BVH_MAX_BRANCH_COUNT;
+ ++branchIndex)
{
- bool alreadyUsed = false;
-
- for (int index = 0; index < node.branchCount; ++index)
- {
- if (taken[index] == nodeIndex)
- {
- alreadyUsed = true;
- break;
- }
- }
-
- if (alreadyUsed)
- {
- continue;
- }
+ int closest = -1;
+ float closestDistance = Vector3Length(world->size);
- float distance = 0.0;
- BoundingBox box = node.branches[0]->box;
-
- // Get distance and bounding box.
- for (int index = 0; index < branchIndex; ++index)
- {
- distance += Vector3Distance(nodes[nodeIndex].position,
- node.branches[index]->position);
- box.min = Vector3Min(box.min, nodes[nodeIndex].box.min);
- box.min = Vector3Min(box.min, node.branches[index]->box.min);
- box.max = Vector3Max(box.max, nodes[nodeIndex].box.max);
- box.max = Vector3Max(box.max, node.branches[index]->box.max);
- }
-
- distance /= (float)branchIndex;
-
- // Check for overlap.
- bool overlaps = false;
-
- for (int index = 0; index < levelSize; ++index)
+ for (int nodeIndex = 0; nodeIndex < nodesSize; ++nodeIndex)
{
- if (CheckCollisionBoxes(box, level[index].box))
+ // First branch.
+ if (branchIndex == 0)
{
- overlaps = true;
- break;
+ closest = nodeIndex;
+ continue;
}
}
-
- if (!overlaps && distance < closestDistance)
- {
- closest = nodeIndex;
- closestDistance = distance;
- }
- }
-
- if (closest == -1)
- {
- break;
- }
-
- node.branches[branchIndex] = (BVHNode*)FT_MALLOC(sizeof(BVHNode));
-
- 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.
- node.box = node.branches[0]->box;
-
- for (int index = 0; index < BVH_MAX_BRANCH_COUNT; ++index)
- {
- if (node.branches[index] != NULL)
- {
- node.box.min = Vector3Min(node.box.min, node.branches[index]->box.min);
- node.box.max = Vector3Max(node.box.max, node.branches[index]->box.max);
}
}
- node.position = Vector3Scale(Vector3Add(node.box.min, node.box.max), 0.5);
-
- return node;
+ return nodes[0];
}
void buildWorldBVH(World* world)
@@ -278,82 +191,8 @@ void buildWorldBVH(World* world)
Entity* entities = world->entities;
// Get leafs.
- BVHNode nodes[WORLD_ENTITY_MAX];
- size_t nodesSize = buildWorldBVHLeafs(nodes, world);
-
- BVHNode level[WORLD_ENTITY_MAX];
- size_t levelSize = 0;
- int levelCount = 0;
-
- while (true)
- {
- while (nodesSize > 0)
- {
- int taken[BVH_MAX_BRANCH_COUNT];
- BVHNode node = buildWorldBVHTree(nodes, nodesSize, taken, level,
- levelSize, 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;
- }
- }
-
- level[levelSize] = node;
- ++levelSize;
- nodesSize -= removedCount;
- }
-
-#ifdef FT_DEBUG_MODE
- // Check for overlap
- int overlapCount = 0;
-
- for (int outer = 0; outer < levelSize - 1; ++outer)
- {
- for (int inner = outer + 1; inner < levelSize; ++inner)
- {
- if (CheckCollisionBoxes(level[inner].box, level[outer].box))
- {
- ++overlapCount;
- }
- }
- }
-
- printf("BVH level: %d, Size: %ld, Overlap: %d\n", levelCount, levelSize,
- overlapCount);
- ++levelCount;
-#endif
-
- // Reached root node.
- if (levelSize <= 1)
- {
- world->bvh = level[0];
- break;
- }
-
- nodesSize = levelSize;
- levelSize = 0;
- memcpy(nodes, level, nodesSize * sizeof(BVHNode));
- }
+ BVHNode leafs[WORLD_ENTITY_MAX];
+ size_t leafsSize = buildWorldBVHLeafs(leafs, world);
}
World createWorld(int seed)
diff --git a/src/world.h b/src/world.h
index 51631e9..16e6dbf 100644
--- a/src/world.h
+++ b/src/world.h
@@ -17,7 +17,7 @@
#define WORLD_IMAGE_HEIGHT 100
#define WORLD_IMAGE_SCALE 5.0
-// UID for anything in the world.x
+// UID for anything in the world.
typedef int16_t WorldUID;
typedef struct BVHNode {