ImGuizmoPlugin.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "ImGuizmoPlugin.h"
  2. #include <imgui/imgui.h>
  3. #include <imgui_impl_glfw.h>
  4. #include <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 ImGuizmoPlugin::init(igl::opengl::glfw::Viewer *_viewer)
  9. {
  10. ImGuiMenu::init(_viewer);
  11. }
  12. IGL_INLINE bool ImGuizmoPlugin::pre_draw()
  13. {
  14. ImGuiMenu::pre_draw();
  15. ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
  16. ImGuizmo::BeginFrame();
  17. ImGui::PopStyleVar();
  18. return false;
  19. }
  20. IGL_INLINE bool ImGuizmoPlugin::post_draw()
  21. {
  22. // Don't draw the Viewer's default menu: draw just the ImGuizmo
  23. Eigen::Matrix4f view = (viewer->core().view / viewer->core().camera_zoom);
  24. Eigen::Matrix4f proj = viewer->core().proj;
  25. if(viewer->core().orthographic){ view(2,3) -= 1000;/* Hack depth */ }
  26. // ImGuizmo doesn't like a lot of scaling in view, shift it to T
  27. const float z = viewer->core().camera_base_zoom;
  28. const Eigen::Matrix4f S =
  29. (Eigen::Matrix4f()<< z,0,0,0, 0,z,0,0, 0,0,z,0, 0,0,0,1).finished();
  30. view = (view * S.inverse()).eval();
  31. const Eigen::Matrix4f T0 = T;
  32. T = (S * T).eval();
  33. ImGuiIO& io = ImGui::GetIO();
  34. ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
  35. ImGuizmo::Manipulate(
  36. view.data(),proj.data(),operation,ImGuizmo::LOCAL,T.data(),NULL,NULL);
  37. // invert scaling that was shifted onto T
  38. T = (S.inverse() * T).eval();
  39. const float diff = (T-T0).array().abs().maxCoeff();
  40. // Only call if actually changed; otherwise, triggers on all mouse events
  41. if( diff > 1e-7) { callback(T); }
  42. ImGui::Render();
  43. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  44. return false;
  45. }
  46. }}}}