From 0f32b4b276a5768ead05cd1752e9777489b2342c Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Sat, 23 Mar 2024 21:11:37 +0100 Subject: [PATCH] VOXEL: moved context creation into one function to reduce code duplication --- src/modules/voxel/SurfaceExtractor.cpp | 11 +++++++++++ src/modules/voxel/SurfaceExtractor.h | 6 ++++++ src/modules/voxelformat/private/mesh/MeshFormat.cpp | 6 ++---- src/modules/voxelpathtracer/PathTracer.cpp | 5 ++--- src/modules/voxelrender/MeshState.cpp | 5 +---- src/tools/voxconvert/VoxConvert.cpp | 7 +++---- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/modules/voxel/SurfaceExtractor.cpp b/src/modules/voxel/SurfaceExtractor.cpp index c49ff2e105..2f28382279 100644 --- a/src/modules/voxel/SurfaceExtractor.cpp +++ b/src/modules/voxel/SurfaceExtractor.cpp @@ -5,6 +5,7 @@ #include "SurfaceExtractor.h" #include "voxel/MaterialColor.h" #include "voxel/Region.h" +#include "voxel/RawVolume.h" #include "voxel/private/CubicSurfaceExtractor.h" #include "voxel/private/MarchingCubesSurfaceExtractor.h" @@ -36,4 +37,14 @@ void extractSurface(SurfaceExtractionContext &ctx) { } } +voxel::SurfaceExtractionContext createContext(voxel::SurfaceExtractionType type, const voxel::RawVolume *volume, + const voxel::Region ®ion, const palette::Palette &palette, + voxel::ChunkMesh &mesh, const glm::ivec3 &translate, bool mergeQuads, + bool reuseVertices, bool ambientOcclusion) { + if (type == voxel::SurfaceExtractionType::MarchingCubes) { + return voxel::buildMarchingCubesContext(volume, region, mesh, palette); + } + return voxel::buildCubicContext(volume, region, mesh, translate, mergeQuads, reuseVertices, ambientOcclusion); +} + } // namespace voxel diff --git a/src/modules/voxel/SurfaceExtractor.h b/src/modules/voxel/SurfaceExtractor.h index d62788f308..503303c7ac 100644 --- a/src/modules/voxel/SurfaceExtractor.h +++ b/src/modules/voxel/SurfaceExtractor.h @@ -43,4 +43,10 @@ SurfaceExtractionContext buildMarchingCubesContext(const RawVolume *volume, cons void extractSurface(SurfaceExtractionContext &ctx); +voxel::SurfaceExtractionContext createContext(voxel::SurfaceExtractionType type, const voxel::RawVolume *volume, + const voxel::Region ®ion, const palette::Palette &palette, + voxel::ChunkMesh &mesh, const glm::ivec3 &translate, + bool mergeQuads = true, bool reuseVertices = true, + bool ambientOcclusion = true); + } // namespace voxel diff --git a/src/modules/voxelformat/private/mesh/MeshFormat.cpp b/src/modules/voxelformat/private/mesh/MeshFormat.cpp index 46bd21a54a..3f81671298 100644 --- a/src/modules/voxelformat/private/mesh/MeshFormat.cpp +++ b/src/modules/voxelformat/private/mesh/MeshFormat.cpp @@ -548,10 +548,8 @@ bool MeshFormat::saveGroups(const scenegraph::SceneGraph &sceneGraph, const core app::async([&, volume = sceneGraph.resolveVolume(node), region = sceneGraph.resolveRegion(node)]() { voxel::ChunkMesh *mesh = new voxel::ChunkMesh(); voxel::SurfaceExtractionContext ctx = - type == voxel::SurfaceExtractionType::MarchingCubes - ? voxel::buildMarchingCubesContext(volume, region, *mesh, node.palette()) - : voxel::buildCubicContext(volume, region, *mesh, glm::ivec3(0), mergeQuads, reuseVertices, - ambientOcclusion); + voxel::createContext(type, volume, region, node.palette(), *mesh, {0, 0, 0}, mergeQuads, + reuseVertices, ambientOcclusion); voxel::extractSurface(ctx); if (withNormals) { mesh->calculateNormals(); diff --git a/src/modules/voxelpathtracer/PathTracer.cpp b/src/modules/voxelpathtracer/PathTracer.cpp index d076cc3aa5..9c7556e8d4 100644 --- a/src/modules/voxelpathtracer/PathTracer.cpp +++ b/src/modules/voxelpathtracer/PathTracer.cpp @@ -263,9 +263,8 @@ bool PathTracer::createScene(const scenegraph::SceneGraph &sceneGraph, const vid voxel::Region region = v->region(); voxel::SurfaceExtractionContext ctx = - type == voxel::SurfaceExtractionType::MarchingCubes - ? voxel::buildMarchingCubesContext(v, region, mesh, node.palette()) - : voxel::buildCubicContext(v, region, mesh, v->region().getLowerCorner()); + voxel::createContext(type, v, region, node.palette(), mesh, v->region().getLowerCorner()); + voxel::extractSurface(ctx); setupGlowMaterial(node); diff --git a/src/modules/voxelrender/MeshState.cpp b/src/modules/voxelrender/MeshState.cpp index 85bbe8f9de..83c8d88437 100644 --- a/src/modules/voxelrender/MeshState.cpp +++ b/src/modules/voxelrender/MeshState.cpp @@ -207,10 +207,7 @@ bool MeshState::scheduleExtractions(size_t maxExtraction) { finalRegion, this]() { ++_runningExtractorTasks; voxel::ChunkMesh mesh(65536, 65536, true); - voxel::SurfaceExtractionContext ctx = - type == voxel::SurfaceExtractionType::MarchingCubes - ? voxel::buildMarchingCubesContext(&movedCopy, finalRegion, mesh, movedPal) - : voxel::buildCubicContext(&movedCopy, finalRegion, mesh, mins); + voxel::SurfaceExtractionContext ctx = voxel::createContext(type, &movedCopy, finalRegion, movedPal, mesh, mins); voxel::extractSurface(ctx); _pendingQueue.emplace(mins, idx, core::move(mesh)); Log::debug("Enqueue mesh for idx: %i (%i:%i:%i)", idx, mins.x, mins.y, mins.z); diff --git a/src/tools/voxconvert/VoxConvert.cpp b/src/tools/voxconvert/VoxConvert.cpp index fbdee1629a..b4e91283f9 100644 --- a/src/tools/voxconvert/VoxConvert.cpp +++ b/src/tools/voxconvert/VoxConvert.cpp @@ -726,10 +726,9 @@ VoxConvert::NodeStats VoxConvert::dumpNode_r(const scenegraph::SceneGraph& scene const voxel::SurfaceExtractionType type = (voxel::SurfaceExtractionType)core::Var::getSafe(cfg::VoxelMeshMode)->intVal(); voxel::ChunkMesh mesh; voxel::SurfaceExtractionContext ctx = - type == voxel::SurfaceExtractionType::MarchingCubes - ? voxel::buildMarchingCubesContext(node.volume(), node.region(), mesh, node.palette()) - : voxel::buildCubicContext(node.volume(), node.region(), mesh, glm::ivec3(0), mergeQuads, reuseVertices, - ambientOcclusion); + voxel::createContext(type, node.volume(), node.region(), node.palette(), mesh, {0, 0, 0}, mergeQuads, + reuseVertices, ambientOcclusion); + voxel::extractSurface(ctx); const size_t vertices = mesh.mesh[0].getNoOfVertices() + mesh.mesh[1].getNoOfVertices(); const size_t indices = mesh.mesh[0].getNoOfIndices() + mesh.mesh[1].getNoOfIndices();