fixing minor issues

This commit is contained in:
Yggdrasil75
2025-12-30 08:27:05 -05:00
parent 4cb1bd2530
commit 05f709c00b
3 changed files with 41 additions and 44 deletions

1
glm

Submodule glm deleted from 8f6213d379

View File

@@ -1,12 +1,16 @@
#include "../vectorlogic/vec3.hpp" #include "../vectorlogic/vec3.hpp"
#include "../vectorlogic/vec4.hpp" #include "../vectorlogic/vec4.hpp"
#include "../vecmat/mat4.hpp" #include "../vecmat/mat4.hpp"
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cmath>
#include "../output/frame.hpp" #include "../output/frame.hpp"
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
struct Voxel { struct Voxel {
bool active; bool active;
Vec3f color; Vec3f color;
@@ -43,28 +47,6 @@ public:
} }
} }
// Create a simple test pattern
void createTestPattern() {
// Create a hollow cube
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int z = 0; z < depth; z++) {
if (x == 0 || x == width-1 || y == 0 || y == height-1 || z == 0 || z == depth-1) {
float r = float(x) / width;
float g = float(y) / height;
float b = float(z) / depth;
set(x, y, z, true, Vec3f(r, g, b));
}
}
}
}
// Add some interior voxels
for (int i = 5; i < 10; i++) {
set(i, i, i, true, Vec3f(1.0f, 0.0f, 0.0f));
}
}
// Amanatides & Woo ray-grid traversal algorithm // Amanatides & Woo ray-grid traversal algorithm
bool rayCast(const Vec3f& rayOrigin, const Vec3f& rayDirection, float maxDistance, bool rayCast(const Vec3f& rayOrigin, const Vec3f& rayDirection, float maxDistance,
Vec3f& hitPos, Vec3f& hitNormal, Vec3f& hitColor) const { Vec3f& hitPos, Vec3f& hitNormal, Vec3f& hitColor) const {
@@ -87,7 +69,7 @@ public:
if (rayDirection[i] >= 0) { if (rayDirection[i] >= 0) {
t0[i] = (0 - rayOrigin[i]) / rayDirection[i]; t0[i] = (0 - rayOrigin[i]) / rayDirection[i];
t1[i] = (width - rayOrigin[i]) / rayDirection[i]; t1[i] = (width - rayOrigin[i]) / rayDirection[i];
} else { } else {
t0[i] = (width - rayOrigin[i]) / rayDirection[i]; t0[i] = (width - rayOrigin[i]) / rayDirection[i];
t1[i] = (0 - rayOrigin[i]) / rayDirection[i]; t1[i] = (0 - rayOrigin[i]) / rayDirection[i];
} }
@@ -102,7 +84,7 @@ public:
if (tEnter > 0) { if (tEnter > 0) {
voxel = Vec3f((rayOrigin + rayDirection * tEnter).floor()); voxel = Vec3f((rayOrigin + rayDirection * tEnter).floor());
} }
} }
// Initialize step and tMax based on ray direction // Initialize step and tMax based on ray direction
@@ -111,7 +93,7 @@ public:
step[i] = -1; step[i] = -1;
tMax[i] = ((float)voxel[i] - rayOrigin[i]) / rayDirection[i]; tMax[i] = ((float)voxel[i] - rayOrigin[i]) / rayDirection[i];
tDelta[i] = -1.0f / rayDirection[i]; tDelta[i] = -1.0f / rayDirection[i];
} else { } else {
step[i] = 1; step[i] = 1;
tMax[i] = ((float)(voxel[i] + 1) - rayOrigin[i]) / rayDirection[i]; tMax[i] = ((float)(voxel[i] + 1) - rayOrigin[i]) / rayDirection[i];
tDelta[i] = 1.0f / rayDirection[i]; tDelta[i] = 1.0f / rayDirection[i];
@@ -120,7 +102,10 @@ public:
// Main traversal loop // Main traversal loop
float distance = 0; float distance = 0;
while (distance < maxDistance) { int maxSteps = width + height + depth;
int steps = 0;
while (distance < maxDistance && steps < maxSteps) {
steps++;
// Check current voxel // Check current voxel
if (voxel.x >= 0 && voxel.x < width && if (voxel.x >= 0 && voxel.x < width &&
voxel.y >= 0 && voxel.y < height && voxel.y >= 0 && voxel.y < height &&
@@ -146,18 +131,26 @@ public:
} }
// Move to next voxel // Move to next voxel
if (tMax.x < tMax.y && tMax.x < tMax.z) { if (tMax.x < tMax.y) {
distance = tMax.x; if (tMax.x < tMax.z) {
tMax.x += tDelta.x; distance = tMax.x;
voxel.x += step.x; tMax.x += tDelta.x;
} else if (tMax.y < tMax.z) { voxel.x += step.x;
distance = tMax.y; } else {
tMax.y += tDelta.y; distance = tMax.z;
voxel.y += step.y; tMax.z += tDelta.z;
voxel.z += step.z;
}
} else { } else {
distance = tMax.z; if (tMax.y < tMax.z) {
tMax.z += tDelta.z; distance = tMax.y;
voxel.z += step.z; tMax.y += tDelta.y;
voxel.y += step.y;
} else {
distance = tMax.z;
tMax.z += tDelta.z;
voxel.z += step.z;
}
} }
} }
@@ -192,11 +185,14 @@ public:
} }
void rotate(float yaw, float pitch) { void rotate(float yaw, float pitch) {
forward = (Vec3f( // Clamp pitch to avoid gimbal lock
pitch = std::clamp(pitch, -89.0f * (static_cast<float>(M_PI) / 180.0f), 89.0f * (static_cast<float>(M_PI) / 180.0f));
forward = Vec3f(
cos(yaw) * cos(pitch), cos(yaw) * cos(pitch),
sin(pitch), sin(pitch),
sin(yaw) * cos(pitch) sin(yaw) * cos(pitch)
).normalized()); ).normalized();
} }
}; };
@@ -207,9 +203,7 @@ private:
public: public:
VoxelGrid grid; VoxelGrid grid;
Camera camera; Camera camera;
VoxelRenderer() : grid(20, 20, 20) { VoxelRenderer() : grid(20, 20, 20) { }
grid.createTestPattern();
}
// Render to a frame object // Render to a frame object
frame renderToFrame(int screenWidth, int screenHeight) { frame renderToFrame(int screenWidth, int screenHeight) {

View File

@@ -201,6 +201,10 @@ public:
return Vec3(std::floor(x), std::floor(y), std::floor(z)); return Vec3(std::floor(x), std::floor(y), std::floor(z));
} }
Vec3i floorToI() const {
return Vec3i(static_cast<int>(std::floor(x)), static_cast<int>(std::floor(x)), static_cast<int>(std::floor(z)))
}
Vec3 ceil() const { Vec3 ceil() const {
return Vec3(std::ceil(x), std::ceil(y), std::ceil(z)); return Vec3(std::ceil(x), std::ceil(y), std::ceil(z));
} }