Skip to content

Commit

Permalink
VOXEDIT: improved and fixed gizmo handling
Browse files Browse the repository at this point in the history
switched from local to world position
  • Loading branch information
mgerhardy committed Nov 11, 2023
1 parent 84ed51a commit eed1417
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
Binary file modified data/voxedit/skeleton.vengi
Binary file not shown.
22 changes: 11 additions & 11 deletions src/tools/voxedit/modules/voxedit-ui/Viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,13 @@ void Viewport::lock(const scenegraph::SceneGraphNode &node, scenegraph::KeyFrame
}

void Viewport::handleGizmo(const scenegraph::SceneGraphNode &node, scenegraph::KeyFrameIndex keyFrameIdx,
const glm::mat4 &localMatrix) {
const glm::mat4 &worldMatrix) {
if (ImGuizmo::IsUsing()) {
lock(node, keyFrameIdx);
glm::vec3 translate;
glm::vec3 rotation;
glm::vec3 scale;
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(localMatrix), glm::value_ptr(translate),
ImGuizmo::DecomposeMatrixToComponents(glm::value_ptr(worldMatrix), glm::value_ptr(translate),
glm::value_ptr(rotation), glm::value_ptr(scale));
if (glm::all(glm::greaterThan(scale, glm::vec3(0)))) {
_bounds.maxs = _boundsNode.maxs * scale;
Expand Down Expand Up @@ -565,7 +565,7 @@ bool Viewport::renderSceneAndModelGizmo(const video::Camera &camera) {
const glm::vec3 size = region.getDimensionsInVoxels();
const scenegraph::KeyFrameIndex keyFrameIdx = node.keyFrameForFrame(sceneMgr().currentFrame());

glm::mat4 localMatrix(1.0f);
glm::mat4 worldMatrix(1.0f);
int operation = ImGuizmo::TRANSLATE;
bool bounds = false;
if (sceneMode) {
Expand All @@ -575,7 +575,7 @@ bool Viewport::renderSceneAndModelGizmo(const video::Camera &camera) {
}
const scenegraph::SceneGraphTransform &transform = node.transform(keyFrameIdx);
const glm::vec3 mins = -node.pivot() * size;
localMatrix = transform.localMatrix();
worldMatrix = transform.worldMatrix();
if (glm::any(glm::epsilonNotEqual(mins, _bounds.mins, glm::epsilon<float>()))) {
_bounds.mins = mins;
_bounds.maxs = mins + size;
Expand All @@ -586,21 +586,21 @@ bool Viewport::renderSceneAndModelGizmo(const video::Camera &camera) {
return false;
}

bool shiftRegionBoundaries = true; // TODO: make this an option
bool shiftRegionBoundaries = false; // TODO: make this an option
if (shiftRegionBoundaries) {
const glm::vec3 &shift = region.getLowerCornerf();
localMatrix = glm::translate(localMatrix, shift);
worldMatrix = glm::translate(worldMatrix, shift);
}
const bool manipulated = ImGuizmo::Manipulate(
glm::value_ptr(camera.viewMatrix()), glm::value_ptr(camera.projectionMatrix()), (ImGuizmo::OPERATION)operation,
ImGuizmo::MODE::LOCAL, glm::value_ptr(localMatrix), glm::value_ptr(deltaMatrix),
ImGuizmo::MODE::WORLD, glm::value_ptr(worldMatrix), glm::value_ptr(deltaMatrix),
_gizmoSnap->boolVal() ? snap : nullptr, bounds ? glm::value_ptr(_bounds.mins) : nullptr, boundsSnap);
if (sceneMode) {
if (shiftRegionBoundaries) {
const glm::vec3 &shift = region.getLowerCornerf();
localMatrix = glm::translate(localMatrix, -shift);
worldMatrix = glm::translate(worldMatrix, -shift);
}
handleGizmo(node, keyFrameIdx, localMatrix);
handleGizmo(node, keyFrameIdx, worldMatrix);

if (!_gizmoActivated && node.isModelNode() &&
ImGui::IsKeyPressed(ImGuiKey_LeftShift) && ImGui::IsKeyPressed(ImGuiKey_MouseLeft)) {
Expand All @@ -612,11 +612,11 @@ bool Viewport::renderSceneAndModelGizmo(const video::Camera &camera) {
}
}
if (manipulated) {
sceneMgr().nodeUpdateTransform(activeNode, localMatrix, &deltaMatrix, keyFrameIdx);
sceneMgr().nodeUpdateTransform(activeNode, worldMatrix, &deltaMatrix, keyFrameIdx, false);
}
_gizmoActivated = ImGuizmo::IsUsingAny();
} else {
handleGizmo(node, InvalidKeyFrame, localMatrix);
handleGizmo(node, InvalidKeyFrame, worldMatrix);
_gizmoActivated = ImGuizmo::IsUsingAny();
if (manipulated) {
sceneMgr().shift(activeNode, deltaMatrix[3]);
Expand Down
26 changes: 11 additions & 15 deletions src/tools/voxedit/modules/voxedit-util/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2696,18 +2696,18 @@ bool SceneManager::mouseRayTrace(bool force) {
return true;
}

bool SceneManager::nodeUpdateTransform(int nodeId, const glm::mat4 &localMatrix, const glm::mat4 *deltaMatrix,
scenegraph::KeyFrameIndex keyFrameIdx) {
bool SceneManager::nodeUpdateTransform(int nodeId, const glm::mat4 &matrix, const glm::mat4 *deltaMatrix,
scenegraph::KeyFrameIndex keyFrameIdx, bool local) {
if (nodeId == InvalidNodeId) {
nodeForeachGroup([&] (int nodeId) {
if (scenegraph::SceneGraphNode *node = sceneGraphNode(nodeId)) {
nodeUpdateTransform(*node, localMatrix, deltaMatrix, keyFrameIdx);
nodeUpdateTransform(*node, matrix, deltaMatrix, keyFrameIdx, local);
}
});
return true;
}
if (scenegraph::SceneGraphNode *node = sceneGraphNode(nodeId)) {
return nodeUpdateTransform(*node, localMatrix, deltaMatrix, keyFrameIdx);
return nodeUpdateTransform(*node, matrix, deltaMatrix, keyFrameIdx, local);
}
return false;
}
Expand Down Expand Up @@ -2784,19 +2784,15 @@ bool SceneManager::nodeRemoveKeyFrameByIndex(scenegraph::SceneGraphNode &node, s
return false;
}

bool SceneManager::nodeUpdateTransform(scenegraph::SceneGraphNode &node, const glm::mat4 &localMatrix,
const glm::mat4 *deltaMatrix, scenegraph::KeyFrameIndex keyFrameIdx) {
glm::vec3 translation;
glm::quat orientation;
glm::vec3 scale;
glm::vec3 skew;
glm::vec4 perspective;
bool SceneManager::nodeUpdateTransform(scenegraph::SceneGraphNode &node, const glm::mat4 &matrix,
const glm::mat4 *deltaMatrix, scenegraph::KeyFrameIndex keyFrameIdx, bool local) {
scenegraph::SceneGraphKeyFrame &keyFrame = node.keyFrame(keyFrameIdx);
scenegraph::SceneGraphTransform &transform = keyFrame.transform();
glm::decompose(localMatrix, scale, orientation, translation, skew, perspective);
transform.setLocalTranslation(translation);
transform.setLocalOrientation(orientation);
transform.setLocalScale(scale);
if (local) {
transform.setLocalMatrix(matrix);
} else {
transform.setWorldMatrix(matrix);
}
transform.update(_sceneGraph, node, keyFrame.frameIdx, _transformUpdateChildren->boolVal());

_mementoHandler.markNodeTransform(node, keyFrameIdx);
Expand Down
8 changes: 4 additions & 4 deletions src/tools/voxedit/modules/voxedit-util/SceneManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ class SceneManager : public core::IComponent {
void onNewNodeAdded(int newNodeId, bool isChildren = false);
bool nodeRename(scenegraph::SceneGraphNode &node, const core::String &name);
bool nodeRemove(scenegraph::SceneGraphNode &node, bool recursive);
bool nodeUpdateTransform(scenegraph::SceneGraphNode &node, const glm::mat4 &localMatrix,
const glm::mat4 *deltaMatrix, scenegraph::KeyFrameIndex keyFrameIdx);
bool nodeUpdateTransform(scenegraph::SceneGraphNode &node, const glm::mat4 &matrix,
const glm::mat4 *deltaMatrix, scenegraph::KeyFrameIndex keyFrameIdx, bool local);
bool nodeRemoveKeyFrameByIndex(scenegraph::SceneGraphNode &node, scenegraph::KeyFrameIndex keyFrameIdx);
bool nodeRemoveKeyFrame(scenegraph::SceneGraphNode &node, scenegraph::FrameIndex frameIdx);
bool nodeAddKeyframe(scenegraph::SceneGraphNode &node, scenegraph::FrameIndex frameIdx);
Expand All @@ -416,8 +416,8 @@ class SceneManager : public core::IComponent {
bool nodeUnreference(scenegraph::SceneGraphNode &node);

public:
bool nodeUpdateTransform(int nodeId, const glm::mat4 &localMatrix, const glm::mat4 *deltaMatrix,
scenegraph::KeyFrameIndex keyFrameIdx);
bool nodeUpdateTransform(int nodeId, const glm::mat4 &matrix, const glm::mat4 *deltaMatrix,
scenegraph::KeyFrameIndex keyFrameIdx, bool local);
bool nodeRemoveKeyFrameByIndex(int nodeId, scenegraph::KeyFrameIndex keyFrameIdx);
int nodeReference(int nodeId);
bool nodeRemoveKeyFrame(int nodeId, scenegraph::FrameIndex frameIdx);
Expand Down

0 comments on commit eed1417

Please sign in to comment.