Mesh.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_GRAPHICS_OPENGL_MESH_H
  21. #define LOVE_GRAPHICS_OPENGL_MESH_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/int.h"
  25. #include "common/math.h"
  26. #include "common/StringMap.h"
  27. #include "graphics/Drawable.h"
  28. #include "Texture.h"
  29. #include "VertexBuffer.h"
  30. // C++
  31. #include <vector>
  32. namespace love
  33. {
  34. namespace graphics
  35. {
  36. namespace opengl
  37. {
  38. /**
  39. * Holds and draws arbitrary vertex geometry.
  40. * Each vertex in the Mesh has a position, texture coordinate, and color.
  41. **/
  42. class Mesh : public Drawable
  43. {
  44. public:
  45. // How the Mesh's vertices are used when drawing.
  46. // http://escience.anu.edu.au/lecture/cg/surfaceModeling/image/surfaceModeling015.png
  47. enum DrawMode
  48. {
  49. DRAW_MODE_FAN,
  50. DRAW_MODE_STRIP,
  51. DRAW_MODE_TRIANGLES,
  52. DRAW_MODE_POINTS,
  53. DRAW_MODE_MAX_ENUM
  54. };
  55. /**
  56. * Constructor.
  57. * @param verts The vertices to use in the Mesh.
  58. * @param mode The draw mode to use when drawing the Mesh.
  59. **/
  60. Mesh(const std::vector<Vertex> &verts, DrawMode mode = DRAW_MODE_FAN);
  61. /**
  62. * Constructor.
  63. * Creates a Mesh with a certain number of default-initialized (hidden)
  64. * vertices.
  65. * @param vertexcount The number of vertices to use in the Mesh.
  66. * @param mode The draw mode to use when drawing the Mesh.
  67. **/
  68. Mesh(int vertexcount, DrawMode mode = DRAW_MODE_FAN);
  69. virtual ~Mesh();
  70. /**
  71. * Replaces all the vertices in the Mesh with a new set of vertices.
  72. **/
  73. void setVertices(const std::vector<Vertex> &verts);
  74. /**
  75. * Gets all of the vertices in the Mesh as an array.
  76. **/
  77. const Vertex *getVertices() const;
  78. /**
  79. * Sets an individual vertex in the Mesh.
  80. * @param index The index into the list of vertices to use.
  81. * @param v The new vertex.
  82. **/
  83. void setVertex(size_t index, const Vertex &v);
  84. Vertex getVertex(size_t index) const;
  85. /**
  86. * Gets the total number of vertices in the Mesh.
  87. **/
  88. size_t getVertexCount() const;
  89. /**
  90. * Sets the vertex map to use when drawing the Mesh. The vertex map
  91. * determines the order in which vertices are used by the draw mode.
  92. * A 0-element vector is equivalent to the default vertex map:
  93. * {0, 1, 2, 3, 4, ...}
  94. **/
  95. void setVertexMap(const std::vector<uint32> &map);
  96. /**
  97. * Fills the uint32 vector passed into the method with the previously set
  98. * vertex map (index buffer) values.
  99. **/
  100. void getVertexMap(std::vector<uint32> &map) const;
  101. /**
  102. * Gets the total number of elements in the vertex map array.
  103. **/
  104. size_t getVertexMapCount() const;
  105. /**
  106. * Sets the texture used when drawing the Mesh.
  107. **/
  108. void setTexture(Texture *texture);
  109. /**
  110. * Disables any texture from being used when drawing the Mesh.
  111. **/
  112. void setTexture();
  113. /**
  114. * Gets the texture used when drawing the Mesh. May return null if no
  115. * texture is set.
  116. **/
  117. Texture *getTexture() const;
  118. /**
  119. * Sets the draw mode used when drawing the Mesh.
  120. **/
  121. void setDrawMode(DrawMode mode);
  122. DrawMode getDrawMode() const;
  123. void setDrawRange(int min, int max);
  124. void setDrawRange();
  125. void getDrawRange(int &min, int &max) const;
  126. /**
  127. * Sets whether per-vertex colors are enabled. If this is disabled, the
  128. * global color (love.graphics.setColor) will be used for the entire Mesh.
  129. **/
  130. void setVertexColors(bool enable);
  131. bool hasVertexColors() const;
  132. // Implements Drawable.
  133. void draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  134. static bool getConstant(const char *in, DrawMode &out);
  135. static bool getConstant(DrawMode in, const char *&out);
  136. private:
  137. GLenum getGLDrawMode(DrawMode mode) const;
  138. GLenum getGLDataTypeFromMax(size_t maxvalue) const;
  139. size_t getGLDataTypeSize(GLenum datatype) const;
  140. // Vertex buffer.
  141. VertexBuffer *vbo;
  142. size_t vertex_count;
  143. // Element (vertex index) buffer, for the vertex map.
  144. VertexBuffer *ibo;
  145. size_t element_count;
  146. GLenum element_data_type;
  147. DrawMode draw_mode;
  148. int range_min;
  149. int range_max;
  150. Object::StrongRef<Texture> texture;
  151. // Whether the per-vertex colors are used when drawing.
  152. bool colors_enabled;
  153. static StringMap<DrawMode, DRAW_MODE_MAX_ENUM>::Entry drawModeEntries[];
  154. static StringMap<DrawMode, DRAW_MODE_MAX_ENUM> drawModes;
  155. }; // Mesh
  156. } // opengl
  157. } // graphics
  158. } // love
  159. #endif // LOVE_GRAPHICS_OPENGL_MESH_H