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.

Axis gizmo: a click on an end snaps to that side view, orthographic, as in Blender (#43)

islandmedb03b51bff7f0
diff --git a/src/app/App.cpp b/src/app/App.cpp
index c995e471..1f9fad36 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -1143,6 +1143,7 @@ void App::onMouseDown(float x, float y, Uint8 button) {
if (blenderStyle() && blenderNavigationDown(button)) return; // middle / Alt+left / right drags navigate
if (mayaStyle() && mayaNavigationDown(button)) return; // Alt+left / middle / right: orbit / pan / zoom
if (button == SDL_BUTTON_LEFT) {
+ if (axisGizmoMouseDown(x, y)) return; // the axis gizmo's ends snap to a side view (#43)
if (zsphereMouseDown(x, y, mod)) return; // a ZSphere SubTool takes clicks on it (PanelZSpheres.cpp)
if (polygroupMouseDown(x, y, mod)) return;
if (maskSelectMouseDown(x, y, mod)) return;
@@ -2943,26 +2944,47 @@ bool App::drawSurfaceCursor(ImDrawList* dl, float radiusPx, ImU32 col) {
return true;
}
-void App::drawAxisGizmo() {
- // The world axes as the camera sees them, top left of the canvas, under the UI: red X, green Y, blue Z;
- // the axes pointing away are drawn first and dimmer.
- ImDrawList* dl = ImGui::GetBackgroundDrawList();
+std::array<AxisGizmoEnd, 6> App::axisGizmoLayout() const {
+ // Top left of the canvas: the timeline strip is at the bottom.
const CanvasRect canvas = canvasRect();
const float s = 34.0f * uiScale_;
- const ImVec2 c(canvas.x + s + 18.0f * uiScale_, canvas.y + s + 18.0f * uiScale_); // top left: the timeline strip is at the bottom
- const glm::mat3 rot(camera_.view());
- struct Axis { glm::vec3 d; ImU32 col; const char* name; };
- Axis axes[3] = {{rot * glm::vec3(1, 0, 0), IM_COL32(230, 80, 80, 255), "X"},
- {rot * glm::vec3(0, 1, 0), IM_COL32(110, 200, 90, 255), "Y"},
- {rot * glm::vec3(0, 0, 1), IM_COL32(80, 140, 240, 255), "Z"}};
- std::sort(std::begin(axes), std::end(axes), [](const Axis& a, const Axis& b) { return a.d.z < b.d.z; });
- for (const Axis& a : axes) {
- const ImVec2 tip(c.x + a.d.x * s, c.y - a.d.y * s);
- const ImU32 col = a.d.z < -0.2f ? (a.col & 0x00ffffffu) | 0x90000000u : a.col;
- dl->AddLine(c, tip, col, 2.0f * uiScale_);
- dl->AddCircleFilled(tip, 7.0f * uiScale_, col);
- const ImVec2 ts = ImGui::CalcTextSize(a.name);
- dl->AddText(ImVec2(tip.x - ts.x * 0.5f, tip.y - ts.y * 0.5f), IM_COL32(20, 20, 22, 255), a.name);
+ return axisGizmoEnds(glm::mat3(camera_.view()), glm::vec2(canvas.x + s + 18.0f * uiScale_, canvas.y + s + 18.0f * uiScale_), s);
+}
+
+bool App::axisGizmoMouseDown(float x, float y) {
+ // A click on an end snaps to that side view, orthographic (#43, as in Blender's navigation gizmo).
+ if (!uiPrefs_.showAxes) return false;
+ const auto ends = axisGizmoLayout();
+ const int hit = axisGizmoHit(ends, x, y, 9.0f * uiScale_);
+ if (hit < 0) return false;
+ if (const Action* a = actions_.find(axisGizmoView(ends[size_t(hit)].axis, ends[size_t(hit)].negative).action); a && a->run) a->run();
+ return true;
+}
+
+void App::drawAxisGizmo() {
+ // The world axes as the camera sees them, under the UI: red X, green Y, blue Z, a labelled ball at the
+ // positive end and a plain one at the negative end; the ends pointing away are drawn first and dimmer. A click
+ // on an end snaps to that side (axisGizmoMouseDown); the end under the mouse gets a ring.
+ ImDrawList* dl = ImGui::GetBackgroundDrawList();
+ auto ends = axisGizmoLayout();
+ const glm::vec2 c = (ends[0].at + ends[3].at) * 0.5f;
+ const ImVec2 mouse = ImGui::GetIO().MousePos;
+ const int hover = ImGui::GetIO().WantCaptureMouse ? -1 : axisGizmoHit(ends, mouse.x, mouse.y, 9.0f * uiScale_);
+ const AxisGizmoEnd hovered = hover >= 0 ? ends[size_t(hover)] : AxisGizmoEnd{};
+ std::sort(ends.begin(), ends.end(), [](const AxisGizmoEnd& a, const AxisGizmoEnd& b) { return a.depth < b.depth; });
+ static constexpr ImU32 colours[3] = {IM_COL32(230, 80, 80, 255), IM_COL32(110, 200, 90, 255), IM_COL32(80, 140, 240, 255)};
+ static constexpr const char* names[3] = {"X", "Y", "Z"};
+ for (const AxisGizmoEnd& e : ends) {
+ const ImVec2 tip(e.at.x, e.at.y);
+ ImU32 col = e.depth < -0.2f ? (colours[e.axis] & 0x00ffffffu) | 0x90000000u : colours[e.axis];
+ if (e.negative) col = (col & 0xff000000u) | ((col & 0x00fefefeu) >> 1); // the negative end: darker
+ else dl->AddLine(ImVec2(c.x, c.y), tip, col, 2.0f * uiScale_);
+ dl->AddCircleFilled(tip, (e.negative ? 5.5f : 7.0f) * uiScale_, col);
+ if (hover >= 0 && e.axis == hovered.axis && e.negative == hovered.negative)
+ dl->AddCircle(tip, 9.0f * uiScale_, IM_COL32(255, 255, 255, 220), 0, 1.5f * uiScale_);
+ if (e.negative) continue;
+ const ImVec2 ts = ImGui::CalcTextSize(names[e.axis]);
+ dl->AddText(ImVec2(tip.x - ts.x * 0.5f, tip.y - ts.y * 0.5f), IM_COL32(20, 20, 22, 255), names[e.axis]);
}
}
diff --git a/src/app/App.h b/src/app/App.h
index 2caf9e74..361a23ad 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -5,6 +5,7 @@
#include "ai/BrushAssistant.h"
#include "app/Actions.h"
#include "app/AssetLibrary.h"
+#include "app/AxisGizmo.h"
#include "app/CommandPalette.h"
#include "app/Config.h"
#include "app/Console.h"
@@ -1147,6 +1148,8 @@ private:
bool rememberWindow_ = false;
bool fullscreenAfterFirstFrame_ = false; // macOS: the saved full screen, entered once the window is on screen (#41)
void drawAxisGizmo();
+ std::array<AxisGizmoEnd, 6> axisGizmoLayout() const; // the gizmo's ends on screen now (AxisGizmo.h)
+ bool axisGizmoMouseDown(float x, float y); // a click on an end: that side view (#43)
// Insert on the model (#34): the primitive chosen in Append > Insert; a drag on the surface places it.
std::string insertType_;
glm::vec3 insertAt_{0.0f}, insertNormal_{0.0f, 1.0f, 0.0f};
diff --git a/src/app/AxisGizmo.h b/src/app/AxisGizmo.h
new file mode 100644
index 00000000..b7dab5f5
--- /dev/null
+++ b/src/app/AxisGizmo.h
@@ -0,0 +1,59 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// The axis gizmo in the canvas corner (Draw > Camera > Axes, #31): where its six ends sit on screen for a view,
+// which end a click lands on, and the side view each end snaps to (#43, as in Blender). Pure arithmetic (no
+// ImGui), so it can be unit tested.
+#pragma once
+
+#include <glm/glm.hpp>
+
+#include <array>
+
+namespace sf {
+
+struct AxisGizmoEnd {
+ glm::vec2 at{0.0f}; // on screen, pixels
+ float depth = 0.0f; // towards the viewer: > 0 points out of the screen
+ int axis = 0; // 0 X, 1 Y, 2 Z
+ bool negative = false;
+};
+
+// The ends of the world axes as the view `rotation` (the camera's view matrix, rotation part) shows them,
+// around `centre`, `length` pixels long; screen y grows downwards. Order: +X +Y +Z -X -Y -Z.
+inline std::array<AxisGizmoEnd, 6> axisGizmoEnds(const glm::mat3& rotation, glm::vec2 centre, float length) {
+ std::array<AxisGizmoEnd, 6> ends;
+ for (int i = 0; i < 6; ++i) {
+ glm::vec3 axis(0.0f);
+ axis[i % 3] = i < 3 ? 1.0f : -1.0f;
+ const glm::vec3 d = rotation * axis;
+ ends[size_t(i)] = {centre + glm::vec2(d.x, -d.y) * length, d.z, i % 3, i >= 3};
+ }
+ return ends;
+}
+
+// The end under (x, y) within `radius` pixels, the one nearest the viewer when ends overlap; -1 for none.
+inline int axisGizmoHit(const std::array<AxisGizmoEnd, 6>& ends, float x, float y, float radius) {
+ int best = -1;
+ for (int i = 0; i < 6; ++i) {
+ const glm::vec2 d = ends[size_t(i)].at - glm::vec2(x, y);
+ if (glm::dot(d, d) > radius * radius) continue;
+ if (best < 0 || ends[size_t(i)].depth > ends[size_t(best)].depth) best = i;
+ }
+ return best;
+}
+
+// The view a click on an end snaps to: looking from that end towards the centre (+X: the right side, +Y: the
+// top, +Z: the front). The action id and the camera's yaw and pitch in degrees, as Input.cpp's view actions.
+struct AxisGizmoView {
+ const char* action;
+ float yaw, pitch;
+};
+inline AxisGizmoView axisGizmoView(int axis, bool negative) {
+ static constexpr AxisGizmoView views[6] = {{"view.right", 90.0f, 0.0f}, {"view.top", 0.0f, -90.0f},
+ {"view.front", 0.0f, 0.0f}, {"view.left", -90.0f, 0.0f},
+ {"view.bottom", 0.0f, 90.0f}, {"view.back", 180.0f, 0.0f}};
+ return views[(negative ? 3 : 0) + (axis < 0 || axis > 2 ? 0 : axis)];
+}
+
+} // namespace sf
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index cbc571c6..7776a00d 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -32,6 +32,7 @@
// gizmo_hover HANDLE move the mouse onto a Gizmo 3D handle (it lights up, for screenshots)
// drag_landmark N DX DY [S] left-drag Make sculptable's face landmark N (0-7, FaceLandmarks order) by DX DY pixels
// orbit DX DY right-drag on the background, in pixels (with the held modifiers)
+// axis_click +x|-x|+y|-y|+z|-z click that end of the axis gizmo in the canvas corner (it snaps to that side view)
// button_drag middle|right X0 Y0 X1 Y1 [S] drag with that button in window fractions (with the held modifiers)
// expect_shortcut ACTION KEYS the action's effective keys, e.g. expect_shortcut view.front Numpad 1
// camera YAW PITCH set the view's angles in degrees (0 0 = the front view), e.g. for guide screenshots
@@ -829,6 +830,24 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
}, seconds);
run([this] { SDL_SetModState(held_); });
wait(0.1f);
+ } else if (verb == "axis_click") {
+ // axis_click +x|-x|+y|-y|+z|-z: click that end of the axis gizmo (Draw > Camera > Axes, #43)
+ if (!need(1)) return false;
+ static const char* const names[6] = {"+x", "+y", "+z", "-x", "-y", "-z"};
+ const auto found = std::find_if(std::begin(names), std::end(names), [&](const char* n) { return w[1] == n; });
+ if (found == std::end(names)) {
+ error = "axis_click takes +x, -x, +y, -y, +z or -z";
+ return false;
+ }
+ const size_t end = size_t(found - std::begin(names));
+ auto at = std::make_shared<ImVec2>();
+ run([this, at, end] {
+ const AxisGizmoEnd e = app_.axisGizmoLayout()[end];
+ *at = ImVec2(e.at.x, e.at.y);
+ });
+ stroke([at](float) { return *at; }, 0.2f);
+ run([this] { SDL_SetModState(held_); });
+ wait(0.1f);
} else if (verb == "click_subtool") {
if (!need(1)) return false;
const std::string name = w[1];
diff --git a/tests/test_axis_gizmo.cpp b/tests/test_axis_gizmo.cpp
new file mode 100644
index 00000000..88150d2f
--- /dev/null
+++ b/tests/test_axis_gizmo.cpp
@@ -0,0 +1,49 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// The axis gizmo (#43): where its ends sit for a view, which end a click picks, and that each end snaps to the
+// side view looking from it.
+#include "TestHarness.h"
+
+#include "app/AxisGizmo.h"
+#include "render/Camera.h"
+
+#include <glm/glm.hpp>
+
+#include <cstring>
+
+using namespace sf;
+
+TEST(axis_gizmo_ends_follow_the_view) {
+ Camera c; // the front view
+ const auto ends = axisGizmoEnds(glm::mat3(c.view()), glm::vec2(100.0f, 100.0f), 30.0f);
+ CHECK(ends[0].axis == 0 && !ends[0].negative && ends[0].at.x > 120.0f); // +X to the right
+ CHECK(ends[3].axis == 0 && ends[3].negative && ends[3].at.x < 80.0f); // -X to the left
+ CHECK(ends[1].at.y < 80.0f); // +Y up (screen y grows down)
+ CHECK(ends[2].depth > 0.9f && ends[5].depth < -0.9f); // +Z towards the viewer
+}
+
+TEST(axis_gizmo_hit_prefers_the_near_end) {
+ Camera c;
+ const auto ends = axisGizmoEnds(glm::mat3(c.view()), glm::vec2(100.0f, 100.0f), 30.0f);
+ CHECK(axisGizmoHit(ends, 131.0f, 99.0f, 9.0f) == 0); // +X
+ CHECK(axisGizmoHit(ends, 70.0f, 101.0f, 9.0f) == 3); // -X
+ CHECK(axisGizmoHit(ends, 100.0f, 100.0f, 9.0f) == 2); // +Z and -Z overlap in the middle: the near one
+ CHECK(axisGizmoHit(ends, 115.0f, 85.0f, 9.0f) == -1);
+}
+
+TEST(axis_gizmo_ends_snap_to_the_side_they_point_at) {
+ for (int i = 0; i < 6; ++i) {
+ glm::vec3 axis(0.0f);
+ axis[i % 3] = i < 3 ? 1.0f : -1.0f;
+ const AxisGizmoView v = axisGizmoView(i % 3, i >= 3);
+ CHECK(v.action != nullptr && std::strncmp(v.action, "view.", 5) == 0);
+ Camera c;
+ c.yaw = glm::radians(v.yaw);
+ c.pitch = glm::radians(v.pitch);
+ // The camera sits out along the clicked end, looking back at the centre, so that end faces the viewer.
+ CHECK(glm::length(glm::normalize(c.position() - c.target) - axis) < 1e-3f);
+ const auto ends = axisGizmoEnds(glm::mat3(c.view()), glm::vec2(0.0f), 30.0f);
+ CHECK(ends[size_t(i)].depth > 0.99f);
+ }
+}