MeshGL.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_OPENGL_MESHGL_H
  9. #define IGL_OPENGL_MESHGL_H
  10. // Coverts mesh data inside a igl::ViewerData class in an OpenGL
  11. // compatible format The class includes a shader and the opengl calls to plot
  12. // the data
  13. #include <igl/igl_inline.h>
  14. #include <Eigen/Core>
  15. namespace igl
  16. {
  17. namespace opengl
  18. {
  19. class MeshGL
  20. {
  21. public:
  22. typedef unsigned int GLuint;
  23. enum DirtyFlags
  24. {
  25. DIRTY_NONE = 0x0000,
  26. DIRTY_POSITION = 0x0001,
  27. DIRTY_UV = 0x0002,
  28. DIRTY_NORMAL = 0x0004,
  29. DIRTY_AMBIENT = 0x0008,
  30. DIRTY_DIFFUSE = 0x0010,
  31. DIRTY_SPECULAR = 0x0020,
  32. DIRTY_TEXTURE = 0x0040,
  33. DIRTY_FACE = 0x0080,
  34. DIRTY_MESH = 0x00FF,
  35. DIRTY_OVERLAY_LINES = 0x0100,
  36. DIRTY_OVERLAY_POINTS = 0x0200,
  37. DIRTY_ALL = 0x03FF
  38. };
  39. bool is_initialized = false;
  40. GLuint vao_mesh;
  41. GLuint vao_overlay_lines;
  42. GLuint vao_overlay_points;
  43. GLuint shader_mesh;
  44. GLuint shader_overlay_lines;
  45. GLuint shader_overlay_points;
  46. GLuint vbo_V; // Vertices of the current mesh (#V x 3)
  47. GLuint vbo_V_uv; // UV coordinates for the current mesh (#V x 2)
  48. GLuint vbo_V_normals; // Vertices of the current mesh (#V x 3)
  49. GLuint vbo_V_ambient; // Ambient material (#V x 3)
  50. GLuint vbo_V_diffuse; // Diffuse material (#V x 3)
  51. GLuint vbo_V_specular; // Specular material (#V x 3)
  52. GLuint vbo_F; // Faces of the mesh (#F x 3)
  53. GLuint vbo_tex; // Texture
  54. GLuint vbo_lines_F; // Indices of the line overlay
  55. GLuint vbo_lines_V; // Vertices of the line overlay
  56. GLuint vbo_lines_V_colors; // Color values of the line overlay
  57. GLuint vbo_points_F; // Indices of the point overlay
  58. GLuint vbo_points_V; // Vertices of the point overlay
  59. GLuint vbo_points_V_colors; // Color values of the point overlay
  60. // Temporary copy of the content of each VBO
  61. typedef Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMatrixXf;
  62. RowMatrixXf V_vbo;
  63. RowMatrixXf V_normals_vbo;
  64. RowMatrixXf V_ambient_vbo;
  65. RowMatrixXf V_diffuse_vbo;
  66. RowMatrixXf V_specular_vbo;
  67. RowMatrixXf V_uv_vbo;
  68. RowMatrixXf lines_V_vbo;
  69. RowMatrixXf lines_V_colors_vbo;
  70. RowMatrixXf points_V_vbo;
  71. RowMatrixXf points_V_colors_vbo;
  72. int tex_u;
  73. int tex_v;
  74. Eigen::Matrix<char,Eigen::Dynamic,1> tex;
  75. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> F_vbo;
  76. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> lines_F_vbo;
  77. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> points_F_vbo;
  78. // Marks dirty buffers that need to be uploaded to OpenGL
  79. uint32_t dirty;
  80. // Initialize shaders and buffers
  81. IGL_INLINE void init();
  82. // Release all resources
  83. IGL_INLINE void free();
  84. // Create a new set of OpenGL buffer objects
  85. IGL_INLINE void init_buffers();
  86. // Bind the underlying OpenGL buffer objects for subsequent mesh draw calls
  87. IGL_INLINE void bind_mesh();
  88. /// Draw the currently buffered mesh (either solid or wireframe)
  89. IGL_INLINE void draw_mesh(bool solid);
  90. // Bind the underlying OpenGL buffer objects for subsequent line overlay draw calls
  91. IGL_INLINE void bind_overlay_lines();
  92. /// Draw the currently buffered line overlay
  93. IGL_INLINE void draw_overlay_lines();
  94. // Bind the underlying OpenGL buffer objects for subsequent point overlay draw calls
  95. IGL_INLINE void bind_overlay_points();
  96. /// Draw the currently buffered point overlay
  97. IGL_INLINE void draw_overlay_points();
  98. // Release the OpenGL buffer objects
  99. IGL_INLINE void free_buffers();
  100. };
  101. }
  102. }
  103. #ifndef IGL_STATIC_LIBRARY
  104. # include "MeshGL.cpp"
  105. #endif
  106. #endif