kinda works.

This commit is contained in:
yggdrasil75
2026-01-20 21:10:10 -05:00
parent a4e378bbcd
commit 76e0d4e06a
2 changed files with 48 additions and 36 deletions

View File

@@ -148,8 +148,7 @@ void createGreenSphere(defaults config, VoxelGrid& grid) {
} }
if (shouldSet) { if (shouldSet) {
grid.set(Vec3i(x, y, z), true, grid.set(Vec3i(x, y, z), true, Vec3ui8(sphereConfig.r, sphereConfig.g, sphereConfig.b), 0.25);
Vec3ui8(sphereConfig.r, sphereConfig.g, sphereConfig.b));
} }
} }
} }

View File

@@ -218,11 +218,11 @@ public:
resize(newsize.x, newsize.y, newsize.z); resize(newsize.x, newsize.y, newsize.z);
} }
void set(int x, int y, int z, bool active, Vec3ui8 color) { void set(int x, int y, int z, bool active, Vec3ui8 color, float alpha = 1) {
set(Vec3i(x,y,z), active, color); set(Vec3i(x,y,z), active, color, alpha);
} }
void set(Vec3i pos, bool active, Vec3ui8 color) { void set(Vec3i pos, bool active, Vec3ui8 color, float alpha = 1) {
if (pos.x >= 0 && pos.y >= 0 && pos.z >= 0) { if (pos.x >= 0 && pos.y >= 0 && pos.z >= 0) {
if (!(pos.x < gridSize.x)) { if (!(pos.x < gridSize.x)) {
resize(pos.x, gridSize.y, gridSize.z); resize(pos.x, gridSize.y, gridSize.z);
@@ -237,12 +237,13 @@ public:
Voxel& v = get(pos); Voxel& v = get(pos);
v.active = active; v.active = active;
v.color = color; v.color = color;
v.alpha = alpha;
updateChunkStatus(pos, active); updateChunkStatus(pos, active);
} }
} }
void set(Vec3i pos, Vec4ui8 rgbaval) { void set(Vec3i pos, Vec4ui8 rgbaval, float alpha = 1) {
set(pos, static_cast<float>(rgbaval.a / 255), rgbaval.toVec3()); set(pos, static_cast<float>(rgbaval.a / 255), rgbaval.toVec3(), alpha);
} }
template<typename T> template<typename T>
@@ -255,52 +256,64 @@ public:
Vec3i lv = end.floorToI(); Vec3i lv = end.floorToI();
Vec3f ray = end - origin; Vec3f ray = end - origin;
Vec3i step = Vec3i(ray.x >= 0 ? 1 : -1, ray.y >= 0 ? 1 : -1, ray.z >= 0 ? 1 : -1); Vec3i step = Vec3i(ray.x >= 0 ? 1 : -1, ray.y >= 0 ? 1 : -1, ray.z >= 0 ? 1 : -1);
Vec3i nextVox = cv + step;
Vec3f tMax = Vec3f(ray.x != 0 ? (nextVox.x - origin.x) / ray.x : INF,
ray.y != 0 ? (nextVox.y - origin.y) / ray.y : INF,
ray.z != 0 ? (nextVox.z - origin.z) / ray.z : INF);
Vec3f tDelta = Vec3f(ray.x != 0 ? 1 / ray.x * static_cast<float>(step.x) : INF,
ray.y != 0 ? 1 / ray.y * static_cast<float>(step.y) : INF,
ray.z != 0 ? 1 / ray.z * static_cast<float>(step.z) : INF);
float dist = 0; Vec3f tDelta = Vec3f(ray.x != 0 ? std::abs(1.0f / ray.x) : INF,
outVoxel.alpha = 0.0; ray.y != 0 ? std::abs(1.0f / ray.y) : INF,
//float alphaDiff = 1; ray.z != 0 ? std::abs(1.0f / ray.z) : INF);
Vec3f tMax;
if (ray.x > 0) {
tMax.x = (std::floor(origin.x) + 1.0f - origin.x) / ray.x;
} else if (ray.x < 0) {
tMax.x = (origin.x - std::floor(origin.x)) / -ray.x;
} else tMax.x = INF;
if (ray.y > 0) {
tMax.y = (std::floor(origin.y) + 1.0f - origin.y) / ray.y;
} else if (ray.y < 0) {
tMax.y = (origin.y - std::floor(origin.y)) / -ray.y;
} else tMax.y = INF;
if (ray.z > 0) {
tMax.z = (std::floor(origin.z) + 1.0f - origin.z) / ray.z;
} else if (ray.z < 0) {
tMax.z = (origin.z - std::floor(origin.z)) / -ray.z;
} else tMax.z = INF;
float dist = 0.0f;
while (lv != cv && outVoxel.alpha < 1 && dist < maxDist && inGrid(cv)) { outVoxel.alpha = 0.0;
while (lv != cv && dist < 1.f && inGrid(cv)) {
//Vec3i currentChunk = getChunkCoord(cv); const Voxel& curv = get(cv);
//auto it = activeChunks.find(currentChunk);
//bool isChunkActive = (it != activeChunks.end() && it->second);
Voxel curv = get(cv);
if (curv.active) { if (curv.active) {
outVoxel.active = true; outVoxel.active = true;
outVoxel.color = curv.color; float remainingOpacity = 1.f - outVoxel.alpha;
outVoxel.alpha = curv.alpha; float contribution = curv.alpha * remainingOpacity;
// float covCon = curv.alpha * alphaDiff;
// outVoxel.color = outVoxel.color + curv.color * covCon; outVoxel.color = outVoxel.color * outVoxel.alpha + (curv.color * remainingOpacity);
// outVoxel.alpha += covCon; outVoxel.alpha += contribution;
// alphaDiff *= (1.f-curv.alpha); }
}
// Step Logic
if (tMax.x < tMax.y) { if (tMax.x < tMax.y) {
if (tMax.x < tMax.z) { if (tMax.x < tMax.z) {
dist += tDelta.x; dist = tMax.x;
cv.x += step.x; cv.x += step.x;
tMax.x += tDelta.x; tMax.x += tDelta.x;
} else { } else {
dist += tDelta.z; dist = tMax.z;
cv.z += step.z; cv.z += step.z;
tMax.z += tDelta.z; tMax.z += tDelta.z;
} }
} else { } else {
if (tMax.y < tMax.z) { if (tMax.y < tMax.z) {
dist += tDelta.y; dist = tMax.y;
cv.y += step.y; cv.y += step.y;
tMax.y += tDelta.y; tMax.y += tDelta.y;
} else { } else {
dist += tDelta.z; dist = tMax.z;
cv.z += step.z; cv.z += step.z;
tMax.z += tDelta.z; tMax.z += tDelta.z;
} }
@@ -335,7 +348,7 @@ public:
for (int y = 0; y < resolution.y; y++) { for (int y = 0; y < resolution.y; y++) {
float v = (1.f - 2.f * (y+0.5f) / resolution.y) * viewH; float v = (1.f - 2.f * (y+0.5f) / resolution.y) * viewH;
for (int x = 0; x < resolution.x; x++) { for (int x = 0; x < resolution.x; x++) {
Voxel outVoxel(0,false,0.f,Vec3ui8(10, 10, 255)); Voxel outVoxel(0, false, 0.f, Vec3ui8(10, 10, 255));
float u = (2.f * (x+0.5f)/resolution.x - 1.f) * viewW; float u = (2.f * (x+0.5f)/resolution.x - 1.f) * viewW;
Vec3f rayDirWorld = (forward + right * u + cam.up * v).normalized(); Vec3f rayDirWorld = (forward + right * u + cam.up * v).normalized();
Vec3f rayStartGrid = cam.posfor.origin; Vec3f rayStartGrid = cam.posfor.origin;