fixed a lot, added some more, made it faster.

This commit is contained in:
Yggdrasil75
2026-01-20 12:45:23 -05:00
parent 58555f96c8
commit a0b9fab5f9

View File

@@ -251,7 +251,7 @@ public:
return (voxl >= 0 && voxl.x < gridSize.x && voxl.y < gridSize.y && voxl.z < gridSize.z); return (voxl >= 0 && voxl.x < gridSize.x && voxl.y < gridSize.y && voxl.z < gridSize.z);
} }
void voxelTraverse(const Vec3d& origin, const Vec3d& end, std::vector<Vec3i>& visitedVoxel, int maxDist = 10000000) const { void voxelTraverse(const Vec3d& origin, const Vec3d& end, Voxel& outVoxel, int maxDist = 10000000) const {
Vec3i cv = (origin / binSize).floorToI(); Vec3i cv = (origin / binSize).floorToI();
Vec3i lv = (end / binSize).floorToI(); Vec3i lv = (end / binSize).floorToI();
Vec3d ray = end - origin; Vec3d ray = end - origin;
@@ -266,64 +266,44 @@ public:
ray.z != 0 ? binSize / ray.z * step.z : INF); ray.z != 0 ? binSize / ray.z * step.z : INF);
float dist = 0; float dist = 0;
outVoxel.alpha = 0.0;
//float alphaDiff = 1;
while (lv != cv && visitedVoxel.size() < 10 && dist < maxDist && inGrid(cv)) { while (lv != cv && outVoxel.alpha < 1 && dist < maxDist && inGrid(cv)) {
Vec3i currentChunk = getChunkCoord(cv); //Vec3i currentChunk = getChunkCoord(cv);
auto it = activeChunks.find(currentChunk); //auto it = activeChunks.find(currentChunk);
bool isChunkActive = (it != activeChunks.end() && it->second); //bool isChunkActive = (it != activeChunks.end() && it->second);
Voxel curv = get(cv);
if (get(cv).active) { if (curv.active) {
visitedVoxel.push_back(cv); outVoxel.active = true;
outVoxel.color = curv.color;
outVoxel.alpha = curv.alpha;
// float covCon = curv.alpha * alphaDiff;
// outVoxel.color = outVoxel.color + curv.color * covCon;
// outVoxel.alpha += covCon;
// alphaDiff *= (1.f-curv.alpha);
} }
if (!isChunkActive) { if (tMax.x < tMax.y) {
Vec3f chunkStep = step * CHUNK_THRESHOLD / 2; if (tMax.x < tMax.z) {
Vec3d chunkDelta = tDelta * CHUNK_THRESHOLD / 2; dist += tDelta.x;
if (tMax.x < tMax.y) { cv.x += step.x;
if (tMax.x < tMax.z) { tMax.x += tDelta.x;
dist += chunkDelta.x;
cv.x += chunkStep.x;
tMax.x += chunkDelta.x;
} else {
dist += chunkDelta.z;
cv.z += chunkStep.z;
tMax.z += chunkDelta.z;
}
} else { } else {
if (tMax.y < tMax.z) { dist += tDelta.z;
dist += chunkDelta.y; cv.z += step.z;
cv.y += chunkStep.y; tMax.z += tDelta.z;
tMax.y += chunkDelta.y;
} else {
dist += chunkDelta.z;
cv.z += chunkStep.z;
tMax.z += chunkDelta.z;
}
} }
continue;
} else { } else {
if (tMax.x < tMax.y) { if (tMax.y < tMax.z) {
if (tMax.x < tMax.z) { dist += tDelta.y;
dist += tDelta.x; cv.y += step.y;
cv.x += step.x; tMax.y += tDelta.y;
tMax.x += tDelta.x;
} else {
dist += tDelta.z;
cv.z += step.z;
tMax.z += tDelta.z;
}
} else { } else {
if (tMax.y < tMax.z) { dist += tDelta.z;
dist += tDelta.y; cv.z += step.z;
cv.y += step.y; tMax.z += tDelta.z;
tMax.y += tDelta.y;
} else {
dist += tDelta.z;
cv.z += step.z;
tMax.z += tDelta.z;
}
} }
} }
} }
@@ -356,26 +336,14 @@ public:
for (int y = 0; y < resolution.x; y++) { for (int y = 0; y < resolution.x; y++) {
float v = (static_cast<float>(y) / static_cast<float>(resolution.x - 1)) - 0.5f; float v = (static_cast<float>(y) / static_cast<float>(resolution.x - 1)) - 0.5f;
for (int x = 0; x < resolution.y; x++) { for (int x = 0; x < resolution.y; x++) {
std::vector<Vec3i> hitVoxels; Voxel outVoxel(0,false,0.f,Vec3ui8(10, 10, 255));
float u = (static_cast<float>(x) / static_cast<float>(resolution.y - 1)) - 0.5f; float u = (static_cast<float>(x) / static_cast<float>(resolution.y - 1)) - 0.5f;
Vec3f rayDirWorld = (forward + right * (u * viewW) + upCor * (v * viewH)).normalized(); Vec3f rayDirWorld = (forward + right * (u * viewW) + upCor * (v * viewH)).normalized();
Vec3f rayEnd = cam.posfor.origin + rayDirWorld * maxDist; Vec3f rayEnd = cam.posfor.origin + rayDirWorld * maxDist;
Vec3d rayStartGrid = cam.posfor.origin.toDouble() / binSize; Vec3d rayStartGrid = cam.posfor.origin.toDouble() / binSize;
Vec3d rayEndGrid = rayEnd.toDouble() / binSize; Vec3d rayEndGrid = rayEnd.toDouble() / binSize;
voxelTraverse(rayStartGrid, rayEndGrid, hitVoxels); voxelTraverse(rayStartGrid, rayEndGrid, outVoxel);
Vec3ui8 hitColor(10, 10, 255); Vec3ui8 hitColor = outVoxel.color;
for (const Vec3i& voxelPos : hitVoxels) {
if (inGrid(voxelPos)) {
const Voxel& voxel = get(voxelPos);
if (voxel.active) {
hitColor = voxel.color;
break;
}
}
}
hitVoxels.clear();
hitVoxels.shrink_to_fit();
// Set pixel color in buffer // Set pixel color in buffer
switch (colorformat) { switch (colorformat) {
case frame::colormap::BGRA: { case frame::colormap::BGRA: {