fixed an oversight. was too small a morton code.

This commit is contained in:
Yggdrasil75
2026-01-23 13:10:39 -05:00
parent 4f409cedc5
commit c77d3b25ba
2 changed files with 55 additions and 55 deletions

View File

@@ -20,6 +20,7 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "../stb/stb_image.h" #include "../stb/stb_image.h"
// theoretical max size: 16384. but it segfaults above 512.
struct defaults { struct defaults {
int outWidth = 512; int outWidth = 512;
int outHeight = 512; int outHeight = 512;
@@ -106,7 +107,7 @@ void setup(defaults config, VoxelGrid& grid) {
grid.printStats(); grid.printStats();
} }
void createGreenSphere(defaults config, VoxelGrid& grid) { void createSphere(defaults config, VoxelGrid& grid) {
TIME_FUNCTION; TIME_FUNCTION;
grid.resize(config.gridWidth, config.gridHeight, config.gridDepth); grid.resize(config.gridWidth, config.gridHeight, config.gridDepth);
std::cout << "Creating green sphere of size " << config.gridWidth << "x" << config.gridHeight << "x" << config.gridDepth << std::endl; std::cout << "Creating green sphere of size " << config.gridWidth << "x" << config.gridHeight << "x" << config.gridDepth << std::endl;
@@ -369,7 +370,7 @@ int main() {
float rotationSpeedZ = 0.05f; // Speed for Z rotation float rotationSpeedZ = 0.05f; // Speed for Z rotation
float autoRotationTime = 0.0f; // Timer for auto-rotation float autoRotationTime = 0.0f; // Timer for auto-rotation
Vec3f initialViewDir = Vec3f(0, 0, 1); // Initial view direction Vec3f initialViewDir = Vec3f(0, 0, 1); // Initial view direction
float rotationRadius = 50.0f; // Distance from center for rotation float rotationRadius = 255.0f; // Distance from center for rotation
float yawSpeed = 0.5f; // Horizontal rotation speed (degrees per second) float yawSpeed = 0.5f; // Horizontal rotation speed (degrees per second)
float pitchSpeed = 0.3f; // Vertical rotation speed float pitchSpeed = 0.3f; // Vertical rotation speed
float rollSpeed = 0.2f; // Roll rotation speed (optional) float rollSpeed = 0.2f; // Roll rotation speed (optional)
@@ -436,9 +437,9 @@ int main() {
} }
if (ImGui::CollapsingHeader("Grid Settings", ImGuiTreeNodeFlags_DefaultOpen)) { if (ImGui::CollapsingHeader("Grid Settings", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::SliderInt("#Width", &config.gridWidth, 64, 512); ImGui::SliderInt("#Width", &config.gridWidth, 64, 1024);
ImGui::SliderInt("#Height", &config.gridHeight, 64, 512); ImGui::SliderInt("#Height", &config.gridHeight, 64, 1024);
ImGui::SliderInt("#Depth", &config.gridDepth, 64, 512); ImGui::SliderInt("#Depth", &config.gridDepth, 64, 1024);
} }
ImGui::Separator(); ImGui::Separator();
@@ -461,13 +462,13 @@ int main() {
// Add the new green sphere button // Add the new green sphere button
if (ImGui::Button("Create Green Sphere")) { if (ImGui::Button("Create Green Sphere")) {
createGreenSphere(config, grid); createSphere(config, grid);
gridInitialized = true; gridInitialized = true;
// Reset camera to center of grid // Reset camera to edge of grid
camX = config.gridWidth / 2.0f; camX = config.gridWidth - 1;
camY = config.gridHeight / 2.0f; camY = config.gridHeight - 1;
camZ = config.gridDepth / 2.0f; camZ = config.gridDepth -1;
// Update camera position // Update camera position
cam.posfor.origin = Vec3f(camX, camY, camZ); cam.posfor.origin = Vec3f(camX, camY, camZ);

View File

@@ -334,7 +334,7 @@ private:
} }
Vec3i getChunkCoord(const Vec3i& voxelPos) const { Vec3i getChunkCoord(const Vec3i& voxelPos) const {
return voxelPos / CHUNK_THRESHOLD; return (voxelPos.toFloat() / CHUNK_THRESHOLD).floorToI();
} }
void updateChunkStatus(const Vec3i& pos, bool isActive) { void updateChunkStatus(const Vec3i& pos, bool isActive) {
@@ -402,26 +402,26 @@ private:
} }
static size_t chunkMortonIndex(const Vec3i& chunkpos) { static size_t chunkMortonIndex(const Vec3i& chunkpos) {
uint8_t x = static_cast<uint8_t>(chunkpos.x) & 0x0F; uint32_t x = static_cast<uint32_t>(chunkpos.x) & 0x03FF;
uint8_t y = static_cast<uint8_t>(chunkpos.y) & 0x0F; uint32_t y = static_cast<uint32_t>(chunkpos.y) & 0x03FF;
uint8_t z = static_cast<uint8_t>(chunkpos.z) & 0x0F; uint32_t z = static_cast<uint32_t>(chunkpos.z) & 0x03FF;
uint16_t xx = x; x = (x | (x << 16)) & 0x030000FF;
xx = (xx | (xx << 4)) & 0x0F0F; x = (x | (x << 8)) & 0x0300F00F;
xx = (xx | (xx << 2)) & 0x3333; x = (x | (x << 4)) & 0x030C30C3;
xx = (xx | (xx << 1)) & 0x5555; x = (x | (x << 2)) & 0x09249249;
uint16_t yy = y; y = (y | (y << 16)) & 0x030000FF;
yy = (yy | (yy << 4)) & 0x0F0F; y = (y | (y << 8)) & 0x0300F00F;
yy = (yy | (yy << 2)) & 0x3333; y = (y | (y << 4)) & 0x030C30C3;
yy = (yy | (yy << 1)) & 0x5555; y = (y | (y << 2)) & 0x09249249;
uint16_t zz = z; z = (z | (z << 16)) & 0x030000FF;
zz = (zz | (zz << 4)) & 0x0F0F; z = (z | (z << 8)) & 0x0300F00F;
zz = (zz | (zz << 2)) & 0x3333; z = (z | (z << 4)) & 0x030C30C3;
zz = (zz | (zz << 1)) & 0x5555; z = (z | (z << 2)) & 0x09249249;
return xx | (yy << 1) | (zz << 2); return x | (y << 1) | (z << 2);
} }
bool intersectRayAABB(const Vec3f& origin, const Vec3f& dir, const Vec3f& boxMin, const Vec3f& boxMax, float& tNear, float& tFar) const { bool intersectRayAABB(const Vec3f& origin, const Vec3f& dir, const Vec3f& boxMin, const Vec3f& boxMax, float& tNear, float& tFar) const {
@@ -479,9 +479,9 @@ private:
// Pre-allocate chunks // Pre-allocate chunks
Vec3i chunkGridSize = (gridSize + CHUNK_THRESHOLD - 1) / CHUNK_THRESHOLD; Vec3i chunkGridSize = (gridSize + CHUNK_THRESHOLD - 1) / CHUNK_THRESHOLD;
Vec3i maxChunkPos = chunkGridSize - 1; Vec3i maxChunkPos = chunkGridSize - 1;
size_t maxChunkIdx = chunkMortonIndex(maxChunkPos); //size_t maxChunkIdx = chunkMortonIndex(maxChunkPos);
chunks.resize(maxChunkIdx + 1); chunks.resize(4096);
activeChunks.resize(maxChunkIdx + 1, false); activeChunks.resize(4096, false);
for (int z = 0; z < gridSize.z; ++z) { for (int z = 0; z < gridSize.z; ++z) {
//if z mod 16 then make a new chunk //if z mod 16 then make a new chunk
@@ -744,7 +744,7 @@ public:
} }
continue; continue;
} else { }
size_t idx = mortonEncode(cv.x, cv.y, cv.z); size_t idx = mortonEncode(cv.x, cv.y, cv.z);
if (voxels[idx].active) { if (voxels[idx].active) {
activeIndices.push_back(idx); activeIndices.push_back(idx);
@@ -768,8 +768,6 @@ public:
cv.z += step.z; cv.z += step.z;
break; break;
} }
continue;
}
} }
// Second pass: process only active voxels // Second pass: process only active voxels
@@ -857,6 +855,7 @@ public:
Vec3f rayStartGrid = cam.posfor.origin; Vec3f rayStartGrid = cam.posfor.origin;
Vec3f rayEnd = rayStartGrid + rayDirWorld * tFar; Vec3f rayEnd = rayStartGrid + rayDirWorld * tFar;
Vec3f ray = rayEnd - rayStartGrid; Vec3f ray = rayEnd - rayStartGrid;
//rayStartGrid = rayStartGrid + (tNear + 0.0001f);
voxelTraverse(rayStartGrid, rayEnd, outVoxel, 512); voxelTraverse(rayStartGrid, rayEnd, outVoxel, 512);
Vec3ui8 hitColor = outVoxel.color; Vec3ui8 hitColor = outVoxel.color;