CedricGuillemet / ImGuizmo

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

Guizmos not showing up on the screen #219

Closed FSY1901 closed 2 years ago

FSY1901 commented 2 years ago

I wanted to implement Imguizmo in on of my projects and for this I followed this video: https://www.youtube.com/watch?v=Pegb5CZuibU&t=736s But I don't see any guizmos. Here is the code that I wrote:

   ImGui::Image((void*)FBOTexture, size, ImVec2{ 0, 1 }, ImVec2{ 1, 0 }); //The scene is rendered with a Framebuffer

   if (selectedObject) {
      ImGuizmo::SetOrthographic(false);
      ImGuizmo::SetDrawlist();
      ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
      //GetTransformationMatrix() returns the transformation matrix(translate*rotation*scale)
      glm::mat4 viewMat = glm::inverse(m_sceneCamera.GetTransformationMatrix());
      glm::mat4 transform = selectedObject->GetTransformationMatrix();

      ImGuizmo::Manipulate(glm::value_ptr(viewMat), glm::value_ptr(projection), ImGuizmo::OPERATION::TRANSLATE, ImGuizmo::LOCAL, 
      glm::value_ptr(transform));
   }

'projection' is calculated like this:

projection = glm::perspective(glm::radians(45.0f), (float)m_width / (float)m_height, 0.01f, 100.0f);

What do I need to change in my code for it to work?

neo-mashiro commented 2 years ago

Where is your window positioned and what's the size you set? Perhaps the size is too small? If you haven't created a window at all, the gizmo should be rendered in a debug window. Also try to set ImGuizmo::SetOrthographic(true) to true and see if it's there.

FSY1901 commented 2 years ago

When I set it to Orthographic, they show up, but not at the right place. So I assume my Matrices aren't correct. Dyou have any idea what could be wrong?

FSY1901 commented 2 years ago

Ok, so I got them on the screen now, but I can't use them. Here's the code:

               if (selectedObject != nullptr) {
            ImGuizmo::SetOrthographic(true);
            ImGuizmo::SetDrawlist();
            ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());

            glm::mat4 viewMat = glm::inverse(m_sceneCamera.GetTransformationMatrix());
            glm::mat4 transform = selectedObject->GetTransformationMatrix();

            ImGuizmo::Manipulate(glm::value_ptr(viewMat), glm::value_ptr(projection*view), ImGuizmo::OPERATION::TRANSLATE, ImGuizmo::MODE::WORLD, glm::value_ptr(transform));
        }
FSY1901 commented 2 years ago

Uhhhh, my mistake. I can actually use them, I just had to add this code:

                        if (ImGuizmo::IsUsing()) {
                glm::vec3 pos = glm::vec3(transform[3]);
                selectedObject->position.x = pos.x;
                selectedObject->position.y = pos.y;
                selectedObject->position.z = pos.z;
            }

Thanks @neo-mashiro for helping me!