Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Renderer: back-face culling on the GPU too when it can't show; closed-mesh check tolerates unwelded seams

islandmed00ac5690e6ef
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 2bf85246..653c4b44 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -282,6 +282,7 @@ int App::renderHeadless() {
camera_.aspect = float(options_.width) / float(std::max(options_.height, 1));
camera_.pitch = glm::radians(options_.pitchDegrees);
camera_.distance /= options_.zoom;
+ renderSettings_.wireframe = renderSettings_.wireframe || options_.wireframe; // syncScene builds its edges
RenderSettings settings = renderSettings_;
settings.wireframe = options_.wireframe;
settings.showPolygroups = settings.showPolygroups || options_.polygroups;
@@ -1142,7 +1143,7 @@ void App::syncScene() {
SubTool& s = doc_.subtools[size_t(i)];
const bool active = i == doc_.activeSubtool;
Mesh* mesh = active ? &shown : &s.mesh;
- scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh)});
+ scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh), renderSettings_.wireframe});
}
renderer_.syncScene(scene);
renderer_.syncSplats(doc_.splats, doc_.splatsVersion);
diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cpp
index 471625c0..6297b4f0 100644
--- a/src/render/Clusters.cpp
+++ b/src/render/Clusters.cpp
@@ -8,6 +8,9 @@
#include <array>
#include <atomic>
#include <cmath>
+#include <cstring>
+#include <map>
+#include <tuple>
#include <vector>
namespace sf {
@@ -159,27 +162,84 @@ ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t
bool closedOriented(const Mesh& mesh) {
const size_t nv = mesh.vertexCount(), nt = mesh.triangleCount();
if (nt == 0) return false;
- std::atomic<bool> ok{true};
+ // Directed edges out of and into vertex a (to the next and from the previous corner of each triangle).
+ auto fan = [&](uint32_t a, std::vector<uint32_t>& outs, std::vector<uint32_t>& ins) {
+ for (uint32_t t : mesh.vertexTriangles(a)) {
+ if (t >= nt) continue;
+ const glm::uvec3& tri = mesh.triangles[t];
+ for (int c = 0; c < 3; ++c)
+ if (tri[c] == a) {
+ outs.push_back(tri[(c + 1) % 3]);
+ ins.push_back(tri[(c + 2) % 3]);
+ }
+ }
+ };
+ // The vertices whose edges don't pair up.
+ constexpr size_t kBlock = 1 << 14;
+ std::vector<std::vector<uint32_t>> open((nv + kBlock - 1) / kBlock);
+ std::atomic<size_t> openCount{0};
+ const size_t limit = std::max<size_t>(64, nv / 100);
poolFor(nv, [&](size_t v0, size_t v1) {
thread_local std::vector<uint32_t> outs, ins;
- for (size_t a = v0; a < v1 && ok.load(std::memory_order_relaxed); ++a) {
+ std::vector<uint32_t>& list = open[v0 / kBlock];
+ for (size_t a = v0; a < v1 && openCount.load(std::memory_order_relaxed) <= limit; ++a) {
outs.clear();
ins.clear();
- for (uint32_t t : mesh.vertexTriangles(uint32_t(a))) {
- if (t >= nt) continue;
- const glm::uvec3& tri = mesh.triangles[t];
- for (int c = 0; c < 3; ++c)
- if (tri[c] == a) {
- outs.push_back(tri[(c + 1) % 3]);
- ins.push_back(tri[(c + 2) % 3]);
- }
- }
+ fan(uint32_t(a), outs, ins);
std::sort(outs.begin(), outs.end());
std::sort(ins.begin(), ins.end());
- if (outs != ins) ok.store(false, std::memory_order_relaxed);
+ if (outs != ins) {
+ list.push_back(uint32_t(a));
+ openCount.fetch_add(1, std::memory_order_relaxed);
+ }
+ }
+ }, kBlock);
+ if (openCount.load() == 0) return true;
+ if (openCount.load() > limit) return false;
+ // A few: maybe a seam of unwelded vertices (the same position twice), which is closed all the same. Count
+ // vertices at one position as one (the lowest id) and check each such group's edges together.
+ struct Key {
+ uint32_t x, y, z;
+ bool operator<(const Key& o) const { return std::tie(x, y, z) < std::tie(o.x, o.y, o.z); }
+ };
+ auto keyOf = [&](uint32_t v) {
+ Key k{};
+ std::memcpy(&k.x, &mesh.positions[v].x, 4);
+ std::memcpy(&k.y, &mesh.positions[v].y, 4);
+ std::memcpy(&k.z, &mesh.positions[v].z, 4);
+ return k;
+ };
+ std::map<Key, uint32_t> canonical;
+ std::vector<uint32_t> outs, ins;
+ auto note = [&](uint32_t v) {
+ auto [it, added] = canonical.emplace(keyOf(v), v);
+ if (!added) it->second = std::min(it->second, v);
+ };
+ for (const auto& list : open)
+ for (uint32_t a : list) {
+ note(a);
+ outs.clear();
+ ins.clear();
+ fan(a, outs, ins);
+ for (uint32_t u : outs) note(u);
+ for (uint32_t u : ins) note(u);
}
- }, 1 << 14);
- return ok.load();
+ std::map<uint32_t, std::pair<std::vector<uint32_t>, std::vector<uint32_t>>> groups;
+ for (const auto& list : open)
+ for (uint32_t a : list) {
+ auto& [go, gi] = groups[canonical.at(keyOf(a))];
+ outs.clear();
+ ins.clear();
+ fan(a, outs, ins);
+ for (uint32_t u : outs) go.push_back(canonical.at(keyOf(u)));
+ for (uint32_t u : ins) gi.push_back(canonical.at(keyOf(u)));
+ }
+ for (auto& [id, g] : groups) {
+ std::sort(g.first.begin(), g.first.end());
+ std::sort(g.second.begin(), g.second.end());
+ if (g.first != g.second) return false;
+ }
+ return true;
}
CullView CullView::from(const glm::mat4& view, const glm::mat4& proj) {
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index 6d466e09..a3396c98 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -11,9 +11,6 @@
#include <algorithm>
#include <atomic>
-#include <chrono>
-#include <cstdio>
-#include <cstdlib>
#include <limits>
#include <vector>
@@ -1137,15 +1134,9 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
const size_t ns = s.order.size();
const bool ranged = !full && !dirtyRuns_.empty();
const std::span<const uint32_t> order = s.order.fromGpu();
- static const bool prof = std::getenv("SF_PROF");
- auto T0 = std::chrono::steady_clock::now();
if (full || ranged) {
uploadVertices(s, mesh, full);
- auto T1 = std::chrono::steady_clock::now();
updateBounds(s, mesh, full);
- auto T2 = std::chrono::steady_clock::now();
- if (prof) std::fprintf(stderr, "PROF runs %zu verts %.3f bounds %.3f clusters %zu\n", dirtyRuns_.size(),
- std::chrono::duration<double,std::milli>(T1-T0).count(), std::chrono::duration<double,std::milli>(T2-T1).count(), cullScratch_.size());
}
if (full) {
s.closed = closedOriented(mesh);
@@ -1571,6 +1562,7 @@ void Renderer::cull(const MeshSlot& s, const CullView& frame) {
break;
}
}
+ cullBackfaces_ = v.backfaces;
const size_t nc = std::min(s.bounds.size(), s.drawCount.size());
for (uint32_t c = 0; c < nc; ++c) {
if (s.drawCount[c] == 0) continue;
@@ -1664,7 +1656,10 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, const RenderSettings& se
// Constant values for the attribute arrays that are disabled.
if (!s.hasColors) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
if (!s.hasMask) gl::VertexAttrib1f(3, 0.0f);
+ // The back faces left in clusters along the silhouette: the GPU skips them too when they can't show.
+ if (cullBackfaces_) gl::Enable(GL_CULL_FACE);
drawClusters(s, GL_TRIANGLES, false, (features & (kPrim | kTextured | kNormalMap)) ? p->primBase : -1);
+ gl::Disable(GL_CULL_FACE);
}
// ---------------------------------------------------------------------------
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 7dffede4..8c0eed3b 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -186,6 +186,7 @@ private:
std::vector<const void*> drawOffsets_;
std::vector<GLint> drawBases_;
std::vector<uint32_t> drawClusters_;
+ bool cullBackfaces_ = false; // the last cull(): back faces can't show (so the GPU may skip them too)
std::vector<uint32_t> cullScratch_;
size_t lastDrawn_ = 0;
// The frame draw() is drawing: matrices, the view's rectangle (x, y, w, h) and its culling.
diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cpp
index eec5769e..dcaff6f6 100644
--- a/tests/test_render_clusters.cpp
+++ b/tests/test_render_clusters.cpp
@@ -190,6 +190,24 @@ TEST(closed_oriented_meshes) {
holed.rebuildTopology();
CHECK(!closedOriented(holed));
CHECK(!closedOriented(Mesh()));
+ // An unwelded seam (two vertices at one point, each with half the triangles) is still closed; a real
+ // crack is not.
+ Mesh seam = makeIcosphere(2);
+ const uint32_t v = seam.triangles[0].x, twin = uint32_t(seam.vertexCount());
+ seam.positions.push_back(seam.positions[v]);
+ seam.normals.push_back(seam.normals[v]);
+ int moved = 0;
+ for (glm::uvec3& t : seam.triangles)
+ for (int k = 0; k < 3; ++k)
+ if (t[k] == v && moved < 2 && (t.x == v ? 0 : 1) + moved >= 0) {
+ t[k] = twin;
+ ++moved;
+ break;
+ }
+ seam.rebuildTopology();
+ CHECK(closedOriented(seam));
+ seam.positions[twin] += glm::vec3(1e-3f);
+ CHECK(!closedOriented(seam));
}
TEST(cluster_culling_is_conservative) {