Skip to content

Commit

Permalink
VOXEL: moved context creation into one function to reduce code duplic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
mgerhardy committed Mar 23, 2024
1 parent fe3f184 commit 0f32b4b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/modules/voxel/SurfaceExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -36,4 +37,14 @@ void extractSurface(SurfaceExtractionContext &ctx) {
}
}

voxel::SurfaceExtractionContext createContext(voxel::SurfaceExtractionType type, const voxel::RawVolume *volume,
const voxel::Region &region, 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
6 changes: 6 additions & 0 deletions src/modules/voxel/SurfaceExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &region, const palette::Palette &palette,
voxel::ChunkMesh &mesh, const glm::ivec3 &translate,
bool mergeQuads = true, bool reuseVertices = true,
bool ambientOcclusion = true);

} // namespace voxel
6 changes: 2 additions & 4 deletions src/modules/voxelformat/private/mesh/MeshFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/modules/voxelpathtracer/PathTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/modules/voxelrender/MeshState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/tools/voxconvert/VoxConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0f32b4b

Please sign in to comment.