main.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <igl/read_triangle_mesh.h>
  2. #include <igl/opengl/glfw/Viewer.h>
  3. #include <igl/opengl/glfw/imgui/ImGuizmoPlugin.h>
  4. #include <GLFW/glfw3.h>
  5. #include <imgui/imgui.h>
  6. #include <imgui/imgui_internal.h>
  7. #include <imgui_impl_glfw.h>
  8. #include <imgui_impl_opengl3.h>
  9. #include <imguizmo/ImGuizmo.h>
  10. int main(int argc, char *argv[])
  11. {
  12. // Load a mesh from file
  13. Eigen::MatrixXd V;
  14. Eigen::MatrixXi F;
  15. igl::read_triangle_mesh(argc>1?argv[1]: TUTORIAL_SHARED_PATH "/cow.off",V,F);
  16. // Set up viewer
  17. igl::opengl::glfw::Viewer vr;
  18. vr.data().set_mesh(V,F);
  19. // Custom menu
  20. igl::opengl::glfw::imgui::ImGuizmoPlugin plugin;
  21. vr.plugins.push_back(&plugin);
  22. // Initialize ImGuizmo at mesh centroid
  23. plugin.T.block(0,3,3,1) =
  24. 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff()).transpose().cast<float>();
  25. // Update can be applied relative to this remembered initial transform
  26. const Eigen::Matrix4f T0 = plugin.T;
  27. // Attach callback to apply imguizmo's transform to mesh
  28. plugin.callback = [&](const Eigen::Matrix4f & T)
  29. {
  30. const Eigen::Matrix4d TT = (T*T0.inverse()).cast<double>().transpose();
  31. vr.data().set_vertices(
  32. (V.rowwise().homogeneous()*TT).rowwise().hnormalized());
  33. vr.data().compute_normals();
  34. };
  35. // Maya-style keyboard shortcuts for operation
  36. vr.callback_key_pressed = [&](decltype(vr) &,unsigned int key, int mod)
  37. {
  38. switch(key)
  39. {
  40. case 'W': case 'w': plugin.operation = ImGuizmo::TRANSLATE; return true;
  41. case 'E': case 'e': plugin.operation = ImGuizmo::ROTATE; return true;
  42. case 'R': case 'r': plugin.operation = ImGuizmo::SCALE; return true;
  43. }
  44. return false;
  45. };
  46. std::cout<<R"(
  47. W,w Switch to translate operation
  48. E,e Switch to rotate operation
  49. R,r Switch to scale operation
  50. )";
  51. vr.launch();
  52. }