fixed an oversight. was too small a morton code.
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "../stb/stb_image.h"
|
||||
|
||||
// theoretical max size: 16384. but it segfaults above 512.
|
||||
struct defaults {
|
||||
int outWidth = 512;
|
||||
int outHeight = 512;
|
||||
@@ -106,7 +107,7 @@ void setup(defaults config, VoxelGrid& grid) {
|
||||
grid.printStats();
|
||||
}
|
||||
|
||||
void createGreenSphere(defaults config, VoxelGrid& grid) {
|
||||
void createSphere(defaults config, VoxelGrid& grid) {
|
||||
TIME_FUNCTION;
|
||||
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;
|
||||
@@ -369,7 +370,7 @@ int main() {
|
||||
float rotationSpeedZ = 0.05f; // Speed for Z rotation
|
||||
float autoRotationTime = 0.0f; // Timer for auto-rotation
|
||||
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 pitchSpeed = 0.3f; // Vertical rotation speed
|
||||
float rollSpeed = 0.2f; // Roll rotation speed (optional)
|
||||
@@ -436,9 +437,9 @@ int main() {
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Grid Settings", ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
ImGui::SliderInt("#Width", &config.gridWidth, 64, 512);
|
||||
ImGui::SliderInt("#Height", &config.gridHeight, 64, 512);
|
||||
ImGui::SliderInt("#Depth", &config.gridDepth, 64, 512);
|
||||
ImGui::SliderInt("#Width", &config.gridWidth, 64, 1024);
|
||||
ImGui::SliderInt("#Height", &config.gridHeight, 64, 1024);
|
||||
ImGui::SliderInt("#Depth", &config.gridDepth, 64, 1024);
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
@@ -461,13 +462,13 @@ int main() {
|
||||
|
||||
// Add the new green sphere button
|
||||
if (ImGui::Button("Create Green Sphere")) {
|
||||
createGreenSphere(config, grid);
|
||||
createSphere(config, grid);
|
||||
gridInitialized = true;
|
||||
|
||||
// Reset camera to center of grid
|
||||
camX = config.gridWidth / 2.0f;
|
||||
camY = config.gridHeight / 2.0f;
|
||||
camZ = config.gridDepth / 2.0f;
|
||||
// Reset camera to edge of grid
|
||||
camX = config.gridWidth - 1;
|
||||
camY = config.gridHeight - 1;
|
||||
camZ = config.gridDepth -1;
|
||||
|
||||
// Update camera position
|
||||
cam.posfor.origin = Vec3f(camX, camY, camZ);
|
||||
|
||||
@@ -334,7 +334,7 @@ private:
|
||||
}
|
||||
|
||||
Vec3i getChunkCoord(const Vec3i& voxelPos) const {
|
||||
return voxelPos / CHUNK_THRESHOLD;
|
||||
return (voxelPos.toFloat() / CHUNK_THRESHOLD).floorToI();
|
||||
}
|
||||
|
||||
void updateChunkStatus(const Vec3i& pos, bool isActive) {
|
||||
@@ -402,26 +402,26 @@ private:
|
||||
}
|
||||
|
||||
static size_t chunkMortonIndex(const Vec3i& chunkpos) {
|
||||
uint8_t x = static_cast<uint8_t>(chunkpos.x) & 0x0F;
|
||||
uint8_t y = static_cast<uint8_t>(chunkpos.y) & 0x0F;
|
||||
uint8_t z = static_cast<uint8_t>(chunkpos.z) & 0x0F;
|
||||
uint32_t x = static_cast<uint32_t>(chunkpos.x) & 0x03FF;
|
||||
uint32_t y = static_cast<uint32_t>(chunkpos.y) & 0x03FF;
|
||||
uint32_t z = static_cast<uint32_t>(chunkpos.z) & 0x03FF;
|
||||
|
||||
uint16_t xx = x;
|
||||
xx = (xx | (xx << 4)) & 0x0F0F;
|
||||
xx = (xx | (xx << 2)) & 0x3333;
|
||||
xx = (xx | (xx << 1)) & 0x5555;
|
||||
x = (x | (x << 16)) & 0x030000FF;
|
||||
x = (x | (x << 8)) & 0x0300F00F;
|
||||
x = (x | (x << 4)) & 0x030C30C3;
|
||||
x = (x | (x << 2)) & 0x09249249;
|
||||
|
||||
uint16_t yy = y;
|
||||
yy = (yy | (yy << 4)) & 0x0F0F;
|
||||
yy = (yy | (yy << 2)) & 0x3333;
|
||||
yy = (yy | (yy << 1)) & 0x5555;
|
||||
y = (y | (y << 16)) & 0x030000FF;
|
||||
y = (y | (y << 8)) & 0x0300F00F;
|
||||
y = (y | (y << 4)) & 0x030C30C3;
|
||||
y = (y | (y << 2)) & 0x09249249;
|
||||
|
||||
uint16_t zz = z;
|
||||
zz = (zz | (zz << 4)) & 0x0F0F;
|
||||
zz = (zz | (zz << 2)) & 0x3333;
|
||||
zz = (zz | (zz << 1)) & 0x5555;
|
||||
z = (z | (z << 16)) & 0x030000FF;
|
||||
z = (z | (z << 8)) & 0x0300F00F;
|
||||
z = (z | (z << 4)) & 0x030C30C3;
|
||||
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 {
|
||||
@@ -479,9 +479,9 @@ private:
|
||||
// Pre-allocate chunks
|
||||
Vec3i chunkGridSize = (gridSize + CHUNK_THRESHOLD - 1) / CHUNK_THRESHOLD;
|
||||
Vec3i maxChunkPos = chunkGridSize - 1;
|
||||
size_t maxChunkIdx = chunkMortonIndex(maxChunkPos);
|
||||
chunks.resize(maxChunkIdx + 1);
|
||||
activeChunks.resize(maxChunkIdx + 1, false);
|
||||
//size_t maxChunkIdx = chunkMortonIndex(maxChunkPos);
|
||||
chunks.resize(4096);
|
||||
activeChunks.resize(4096, false);
|
||||
|
||||
for (int z = 0; z < gridSize.z; ++z) {
|
||||
//if z mod 16 then make a new chunk
|
||||
@@ -744,7 +744,7 @@ public:
|
||||
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
}
|
||||
size_t idx = mortonEncode(cv.x, cv.y, cv.z);
|
||||
if (voxels[idx].active) {
|
||||
activeIndices.push_back(idx);
|
||||
@@ -768,8 +768,6 @@ public:
|
||||
cv.z += step.z;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: process only active voxels
|
||||
@@ -857,6 +855,7 @@ public:
|
||||
Vec3f rayStartGrid = cam.posfor.origin;
|
||||
Vec3f rayEnd = rayStartGrid + rayDirWorld * tFar;
|
||||
Vec3f ray = rayEnd - rayStartGrid;
|
||||
//rayStartGrid = rayStartGrid + (tNear + 0.0001f);
|
||||
|
||||
voxelTraverse(rayStartGrid, rayEnd, outVoxel, 512);
|
||||
Vec3ui8 hitColor = outVoxel.color;
|
||||
|
||||
Reference in New Issue
Block a user