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.

Orthographic view shades like perspective (#24)

islandmed6f0552b6a489
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 754ed25e..87178821 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,14 @@ All notable changes to SculptForge are listed here. The format follows
mesh above a quarter: 78 ms a frame against 14-30), and finds them in parallel (3.7 -> 0.4 ms).
- Starting a stroke no longer re-uploads the whole mesh when there are no LODs.
+### Fixed
+- The orthographic view (Numpad 5) shaded MatCaps and the lights as if inside out (#24): the shader worked out
+ each pixel's view ray as in a perspective view, which in an orthographic one tilted it far off the axis
+ towards the edges. An orthographic view now gives every pixel the axis; flat shading turns its facets
+ towards the viewer by that ray too (the direction from the eye, which an orthographic view can put inside
+ the model, turned them away). UI test ortho_shading (a new `luma@X,Y` value reads the last screenshot): the
+ same sphere in both views matches within 1 brightness level, where 0.9.6 was up to 38 off.
+
## [0.9.6] - 2026-10-31
The same evening as 0.9.5, the next batch from your reports and from askNK's review: Extract and Mirror And Weld,
diff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.md
index 419858df..592dbf1b 100644
--- a/docs/guide/whats-new.md
+++ b/docs/guide/whats-new.md
@@ -14,6 +14,11 @@ with a 200-pixel brush, the brush now trails the pointer by half as far, a frame
first dab appears in half the time, and lifting the pen no longer pauses for up to a second while waiting dabs
are placed. See [Performance](performance.md).
+### Fixes
+
+- The orthographic view (<kbd>Numpad 5</kbd>) shades like the perspective one: MatCaps and the lights no longer
+ look inside out.
+
## Version 0.9.6
### Extract
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 8b19c408..d483a3c4 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -399,6 +399,7 @@ int App::runUiTest(int w, int h, int fps) {
img.height = h;
img.rgba = rgba;
for (size_t i = 3; i < img.rgba.size(); i += 4) img.rgba[i] = 255;
+ demo_->setLastShot(img.rgba, w, h);
if (!img.savePng(shot, err)) SDL_Log("%s", err.c_str());
else SDL_Log("Screenshot %s", shot.c_str());
}
diff --git a/src/app/Demo.h b/src/app/Demo.h
index 76602f1e..994b6e8a 100644
--- a/src/app/Demo.h
+++ b/src/app/Demo.h
@@ -65,6 +65,12 @@ public:
std::string shortcutsMarkdown() const; // dump_shortcuts: the default shortcuts as Markdown tables
// A screenshot requested by a `shot` step: the frame loop saves the next frame there.
std::string takeShotRequest() { return std::exchange(shotRequest_, std::string()); }
+ // The last screenshot's pixels (RGBA, top row first), for the luma@X,Y values.
+ void setLastShot(std::vector<uint8_t> rgba, int w, int h) {
+ lastShot_ = std::move(rgba);
+ lastShotW_ = w;
+ lastShotH_ = h;
+ }
private:
using Step = std::function<bool(float t)>; // returns true when done; t = seconds since the step began
@@ -151,6 +157,8 @@ private:
bool testing_ = false;
std::vector<std::string> failures_;
std::string frameText_, shotRequest_, shotDir_, scriptName_, lineText_;
+ std::vector<uint8_t> lastShot_;
+ int lastShotW_ = 0, lastShotH_ = 0;
std::string scriptDir_; // the .sfui script's folder: relative `drop` paths, $SCRIPT_DIR in env steps
std::string expandLibrary(std::string text) const; // $LIBRARY -> the LightBox library folder (UiTest.cpp)
SDL_Keymod held_ = SDL_KMOD_NONE; // modifiers held by `hold` (Ctrl to mask, Ctrl+Shift to hide...)
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 956aeae7..458ed9d0 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -311,6 +311,19 @@ void Demo::detachVirtualSpaceMouse() {
double Demo::query(const std::string& name, bool& known) const {
const Mesh& m = app_.doc_.mesh;
known = true;
+ if (name.rfind("luma@", 0) == 0) { // luma@X,Y: brightness 0-255 of the last shot at window fractions X, Y
+ float fx = 0.0f, fy = 0.0f;
+ if (std::sscanf(name.c_str() + 5, "%f,%f", &fx, &fy) != 2) {
+ known = false;
+ return 0.0;
+ }
+ if (lastShot_.empty()) return -1.0; // (no shot yet)
+ const int x = std::clamp(int(fx * float(lastShotW_)), 0, lastShotW_ - 1);
+ const int y = std::clamp(int(fy * float(lastShotH_)), 0, lastShotH_ - 1);
+ const size_t i = size_t(y) * size_t(lastShotW_) * 4 + size_t(x) * 4;
+ // (rows as saved: top first)
+ return 0.2126 * lastShot_[i] + 0.7152 * lastShot_[i + 1] + 0.0722 * lastShot_[i + 2];
+ }
if (name == "triangles") return double(m.triangleCount());
if (name == "vertices") return double(m.vertexCount());
if (name == "layers") return double(m.layers.size());
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index b265dfcf..a19b422e 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -205,6 +205,11 @@ vec3 groupColor(uint id) {
vec3 rgb = clamp(abs(fract(hue + vec3(0.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 1.0, 0.0, 1.0);
return val * mix(vec3(1.0), rgb, sat);
}
+// The unit vector from the pixel's point towards the eye, in view space.
+vec3 viewRay() {
+ vec2 ndc = (gl_FragCoord.xy - uViewport.xy) / uViewport.zw * 2.0 - 1.0;
+ return normalize(vec3(-ndc / uProjScale, 1.0));
+}
void main() {
vec3 albedo = uBaseColor;
vec3 tint = vec3(1.0); // multiplies a matcap: polypaint or polygroup colours
@@ -224,7 +229,9 @@ void main() {
// through a hole) keeps it that way unless it is the motion proxy's, which shades back faces turned away.
vec3 n = cross(dFdx(vViewPos), dFdy(vViewPos));
n = dot(n, n) > 1e-30 ? normalize(n) : normalize(vNormalCurv.xyz);
- if (dot(n, vViewPos) > 0.0) n = -n;
+ // Towards the viewer: against the pixel's view ray (in an orthographic view the eye can sit inside the
+ // model, where the direction from it to the point would turn the facet the wrong way).
+ if (dot(n, viewRay()) < 0.0) n = -n;
if (!gl_FrontFacing && uTwoSided == 0) n = -n;
#else
vec3 n = normalize(vNormalCurv.xyz);
@@ -242,8 +249,7 @@ void main() {
#ifndef FLAT
if (!gl_FrontFacing && uTwoSided == 1) n = -n;
#endif
- vec2 ndc = (gl_FragCoord.xy - uViewport.xy) / uViewport.zw * 2.0 - 1.0;
- vec3 v = normalize(vec3(-ndc / uProjScale, 1.0));
+ vec3 v = viewRay();
if (uMatcap == 1) {
// View-aligned basis so the material stays put towards the edges of a perspective view.
vec3 bx = normalize(vec3(v.z, 0.0, -v.x));
@@ -2241,7 +2247,11 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
gl::Uniform3fv(p->gridOrigin, 1, glm::value_ptr(s.grid.origin));
gl::Uniform1f(p->gridStep, s.grid.step);
gl::Uniform4fv(p->viewport, 1, glm::value_ptr(frameViewport_));
- gl::Uniform2f(p->projScale, frameProj_[0][0], frameProj_[1][1]);
+ // The shader turns a pixel into its view ray as (-ndc / scale, 1): right for perspective. An orthographic
+ // view (Numpad 5) looks straight along the axis at every pixel, which an unbounded scale gives (its own
+ // scale tilted the ray at the edges until the matcap and the lights came from the other side, #24).
+ const bool orthographic = frameProj_[3][3] == 1.0f;
+ gl::Uniform2f(p->projScale, orthographic ? 1e20f : frameProj_[0][0], orthographic ? 1e20f : frameProj_[1][1]);
gl::Uniform3fv(p->baseColor, 1, glm::value_ptr(settings.baseColor));
gl::Uniform1i(p->matcap, matcapTexture(settings.matcap) ? 1 : 0);
const MatcapCavity cavity = matcapCavity(settings.matcap);
diff --git a/tests/ui/ortho_shading.sfui b/tests/ui/ortho_shading.sfui
new file mode 100644
index 00000000..cb45f495
--- /dev/null
+++ b/tests/ui/ortho_shading.sfui
@@ -0,0 +1,32 @@
+# The orthographic view (Numpad 5) shades like the perspective one (#24): the matcap and the lights looked inside
+# out, because each pixel's view ray was worked out as in a perspective view. The same sphere, both ways: the
+# brightness matches where 0.9.6 was furthest off (up to 38 levels; now within 1).
+run new_sphere subdivisions=4
+key f
+shot perspective
+remember [email protected],0.63 a
+remember [email protected],0.57 b
+remember [email protected],0.39 c
+remember [email protected],0.52 d
+key numpad5
+expect_value camera_ortho == 1
+key f
+shot orthographic
+expect_value [email protected],0.63 >= $a-8
+expect_value [email protected],0.63 <= $a+8
+expect_value [email protected],0.57 >= $b-8
+expect_value [email protected],0.57 <= $b+8
+expect_value [email protected],0.39 >= $c-8
+expect_value [email protected],0.39 <= $c+8
+expect_value [email protected],0.52 >= $d-8
+expect_value [email protected],0.52 <= $d+8
+# Flat shading too (its facing test used the direction from the eye, which an orthographic view can put
+# inside the model).
+run set_shading mode=flat
+shot flat_orthographic
+remember [email protected],0.52 fo
+key numpad5
+shot flat_perspective
+expect_value camera_ortho == 0
+expect_value [email protected],0.52 >= $fo-8
+expect_value [email protected],0.52 <= $fo+8