MeshGL.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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. #include "../igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <cstdint>
  13. namespace igl
  14. {
  15. namespace opengl
  16. {
  17. /// Coverts mesh data inside a igl::ViewerData class in an OpenGL compatible
  18. /// format The class includes a shader and the opengl calls to plot the data
  19. class MeshGL
  20. {
  21. public:
  22. typedef unsigned int GLuint;
  23. typedef unsigned int GLint;
  24. /// Bitmask flags for keeping track of what needs to be (re)-uploaded to the
  25. /// GPU
  26. enum DirtyFlags
  27. {
  28. DIRTY_NONE = 0x0000,
  29. DIRTY_POSITION = 0x0001,
  30. DIRTY_UV = 0x0002,
  31. DIRTY_NORMAL = 0x0004,
  32. DIRTY_AMBIENT = 0x0008,
  33. DIRTY_DIFFUSE = 0x0010,
  34. DIRTY_SPECULAR = 0x0020,
  35. DIRTY_TEXTURE = 0x0040,
  36. DIRTY_FACE = 0x0080,
  37. DIRTY_MESH = 0x00FF,
  38. DIRTY_OVERLAY_LINES = 0x0100,
  39. DIRTY_OVERLAY_POINTS = 0x0200,
  40. DIRTY_VERTEX_LABELS = 0x0400,
  41. DIRTY_FACE_LABELS = 0x0800,
  42. DIRTY_CUSTOM_LABELS = 0x1000,
  43. DIRTY_ALL = 0xFFFF
  44. };
  45. bool is_initialized = false;
  46. GLuint vao_mesh;
  47. GLuint vao_overlay_lines;
  48. GLuint vao_overlay_points;
  49. GLuint shader_mesh;
  50. GLuint shader_overlay_lines;
  51. GLuint shader_overlay_points;
  52. GLuint shader_text;
  53. /// Vertices of the current mesh (#V x 3)
  54. GLuint vbo_V;
  55. /// UV coordinates for the current mesh (#V x 2)
  56. GLuint vbo_V_uv;
  57. /// Vertices of the current mesh (#V x 3)
  58. GLuint vbo_V_normals;
  59. /// Ambient material (#V x 3)
  60. GLuint vbo_V_ambient;
  61. /// Diffuse material (#V x 3)
  62. GLuint vbo_V_diffuse;
  63. /// Specular material (#V x 3)
  64. GLuint vbo_V_specular;
  65. /// Faces of the mesh (#F x 3)
  66. GLuint vbo_F;
  67. /// Texture
  68. GLuint vbo_tex;
  69. /// Indices of the line overlay
  70. GLuint vbo_lines_F;
  71. /// Vertices of the line overlay
  72. GLuint vbo_lines_V;
  73. /// Color values of the line overlay
  74. GLuint vbo_lines_V_colors;
  75. /// Indices of the point overlay
  76. GLuint vbo_points_F;
  77. /// Vertices of the point overlay
  78. GLuint vbo_points_V;
  79. /// Color values of the point overlay
  80. GLuint vbo_points_V_colors;
  81. // Temporary copy of the content of each VBO
  82. typedef Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMatrixXf;
  83. RowMatrixXf V_vbo;
  84. RowMatrixXf V_normals_vbo;
  85. RowMatrixXf V_ambient_vbo;
  86. RowMatrixXf V_diffuse_vbo;
  87. RowMatrixXf V_specular_vbo;
  88. RowMatrixXf V_uv_vbo;
  89. RowMatrixXf lines_V_vbo;
  90. RowMatrixXf lines_V_colors_vbo;
  91. RowMatrixXf points_V_vbo;
  92. RowMatrixXf points_V_colors_vbo;
  93. // Text Rendering
  94. struct TextGL
  95. {
  96. std::uint32_t dirty_flag;
  97. GLuint vao_labels;
  98. GLuint vbo_labels_pos;
  99. GLuint vbo_labels_characters;
  100. GLuint vbo_labels_offset;
  101. GLuint vbo_labels_indices;
  102. RowMatrixXf label_pos_vbo;
  103. RowMatrixXf label_char_vbo;
  104. RowMatrixXf label_offset_vbo;
  105. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> label_indices_vbo;
  106. void init_buffers();
  107. void free_buffers();
  108. };
  109. TextGL vertex_labels;
  110. TextGL face_labels;
  111. TextGL custom_labels;
  112. GLuint font_atlas;
  113. int tex_u;
  114. int tex_v;
  115. GLint tex_filter;
  116. GLint tex_wrap;
  117. Eigen::Matrix<char,Eigen::Dynamic,1> tex;
  118. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> F_vbo;
  119. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> lines_F_vbo;
  120. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> points_F_vbo;
  121. /// Marks dirty buffers that need to be uploaded to OpenGL
  122. std::uint32_t dirty;
  123. IGL_INLINE MeshGL();
  124. /// Initialize shaders and buffers
  125. IGL_INLINE void init();
  126. /// Release all resources
  127. IGL_INLINE void free();
  128. /// Create a new set of OpenGL buffer objects
  129. IGL_INLINE void init_buffers();
  130. /// Bind the underlying OpenGL buffer objects for subsequent mesh draw calls
  131. IGL_INLINE void bind_mesh();
  132. /// Draw the currently buffered mesh (either solid or wireframe)
  133. ///
  134. /// @param[in] solid Whether to draw the mesh as a solid or wireframe
  135. IGL_INLINE void draw_mesh(bool solid);
  136. /// Bind the underlying OpenGL buffer objects for subsequent line overlay draw calls
  137. IGL_INLINE void bind_overlay_lines();
  138. /// Draw the currently buffered line overlay
  139. IGL_INLINE void draw_overlay_lines();
  140. /// Bind the underlying OpenGL buffer objects for subsequent point overlay draw calls
  141. IGL_INLINE void bind_overlay_points();
  142. /// Draw the currently buffered point overlay
  143. IGL_INLINE void draw_overlay_points();
  144. /// Text Binding and Draw functions
  145. IGL_INLINE void init_text_rendering();
  146. /// Bind the underlying OpenGL buffer objects for subsequent text draw calls
  147. IGL_INLINE void bind_labels(const TextGL& labels);
  148. /// Draw the currently buffered text
  149. IGL_INLINE void draw_labels(const TextGL& labels);
  150. /// Release the OpenGL buffer objects
  151. IGL_INLINE void free_buffers();
  152. };
  153. }
  154. }
  155. #ifndef IGL_STATIC_LIBRARY
  156. # include "MeshGL.cpp"
  157. #endif
  158. #endif