CedricGuillemet / ImGuizmo

Immediate mode 3D gizmo for scene editing and other controls based on Dear Imgui
MIT License
3.11k stars 877 forks source link

Custom camera based on GLM functions is moving the gizmos as the camera moves #254

Open GasimGasimzada opened 2 years ago

GasimGasimzada commented 2 years ago

I have a custom camera that is based on GLM functions:

camera.projectionMatrix = glm::perspective(glm::radians(mFov), mWidth / mHeight, mNear, Far);

// Needed because of Vulkan's Y axis is reverted
camera.projectionMatrix[1][1] *= -1;

camera.viewMatrix = glm::lookAt(mEye, mCenter, mUp);
camera.projectionViewMatrix = camera.projectionMatrix * camera.viewMatrix;

So, I take my camera component and render my gizmo with identity transform for testing purposes:

glm::mat4 identity{1.0f};

const auto &pos = ImGui::GetItemRectMin();
const auto &size = ImGui::GetItemRectSize();

// Set draw list for current window
ImGuizmo::SetDrawlist();

// Gizmo is being drawn on an Imgui image
// so, the same is used for Guizmo
ImGuizmo::SetRect(pos.x, pos.y, size.x, size.y);

ImGuizmo::Manipulate(glm::value_ptr(camera.viewMatrix),
   glm::value_ptr(camera.projectionMatrix),
   ImGuizmo::TRANSLATE, ImGuizmo::WORLD,
   glm::value_ptr(identity), nullptr, nullptr,
   nullptr);

Moving the axis horizontally works as expected; however, when I move it vertically, the gizmo moves with the camera:

Animation

I compared my view matrix with the result of LookAt function in the example with my provided example functions and the values are exactly the same.

Can someone help me understand what I am doing wrong here?

GasimGasimzada commented 2 years ago

I found the issue but I am not why that causes the problem. I copied the Perspective function from the example to calculate the perspective and every looked like it is working. So, I dug deep into the values between glm::perspective and Perspective.

For the following Perspective Lens data:

glm::perspective gives me vastly different values than the Perspective function:

// Perspective function from example:
(0.237889245, 0, 0, 0)
(0, 0.363970518, 0, 0)
(0, -1.00000191, -1.0, 0)
(0, 0, -0.00200000196, 0)

// glm::perspectiveRH
(0.933430076, 0, 0, 0)
(0, -1.42814803, 0, 0)
(0, 0, -1.00000095, -1.0)
(0, 0, -0.00100000098, 0)

// glm::perspectiveLH
(0.933430076, 0, 0, 0)
(0, 1.42814803, 0, 0)
(0, 0, 1.00000095, 1.0)
(0, 0, 0.00100000098, 0)

The values between them is vastly different in terms of magnitude.