ImGuizmoWidget.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "ImGuizmoWidget.h"
  2. #include <imgui.h>
  3. #include <backends/imgui_impl_glfw.h>
  4. #include <backends/imgui_impl_opengl3.h>
  5. #include <imgui_fonts_droid_sans.h>
  6. #include <GLFW/glfw3.h>
  7. namespace igl{ namespace opengl{ namespace glfw{ namespace imgui{
  8. IGL_INLINE void ImGuizmoWidget::init(Viewer *_viewer, ImGuiPlugin *_plugin)
  9. {
  10. ImGuiWidget::init(_viewer,_plugin);
  11. }
  12. IGL_INLINE void ImGuizmoWidget::draw()
  13. {
  14. if(!visible){ return; }
  15. ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
  16. ImGuizmo::BeginFrame();
  17. ImGui::PopStyleVar();
  18. // Don't draw the Viewer's default menu: draw just the ImGuizmo
  19. Eigen::Matrix4f view = (viewer->core().view / viewer->core().camera_zoom);
  20. Eigen::Matrix4f proj = viewer->core().proj;
  21. if(viewer->core().orthographic){ view(2,3) -= 1000;/* Hack depth */ }
  22. // ImGuizmo doesn't like a lot of scaling in view, shift it to T
  23. const float z = viewer->core().camera_base_zoom;
  24. const Eigen::Matrix4f S =
  25. (Eigen::Matrix4f()<< z,0,0,0, 0,z,0,0, 0,0,z,0, 0,0,0,1).finished();
  26. view = (view * S.inverse()).eval();
  27. const Eigen::Matrix4f T0 = T;
  28. T = (S * T).eval();
  29. ImGuiIO& io = ImGui::GetIO();
  30. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  31. ImGuizmo::Manipulate(
  32. view.data(),proj.data(),operation,ImGuizmo::LOCAL,T.data(),NULL,NULL);
  33. // invert scaling that was shifted onto T
  34. T = (S.inverse() * T).eval();
  35. const float diff = (T-T0).array().abs().maxCoeff();
  36. // Only call if actually changed; otherwise, triggers on all mouse events
  37. if( diff > 1e-7) { callback(T); }
  38. }
  39. }}}}