primitive_meshes.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*************************************************************************/
  2. /* primitive_meshes.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef PRIMITIVE_MESHES_H
  31. #define PRIMITIVE_MESHES_H
  32. #include "scene/resources/mesh.h"
  33. ///@TODO probably should change a few integers to unsigned integers...
  34. /**
  35. @author Bastiaan Olij <[email protected]>
  36. Base class for all the classes in this file, handles a number of code functions that are shared among all meshes.
  37. This class is set appart that it assumes a single surface is always generated for our mesh.
  38. */
  39. class PrimitiveMesh : public Mesh {
  40. GDCLASS(PrimitiveMesh, Mesh);
  41. private:
  42. RID mesh;
  43. mutable Rect3 aabb;
  44. Ref<Material> material;
  45. mutable bool pending_request;
  46. void _update() const;
  47. protected:
  48. Mesh::PrimitiveType primitive_type;
  49. static void _bind_methods();
  50. virtual void _create_mesh_array(Array &p_arr) const = 0;
  51. void _request_update();
  52. public:
  53. virtual int get_surface_count() const;
  54. virtual int surface_get_array_len(int p_idx) const;
  55. virtual int surface_get_array_index_len(int p_idx) const;
  56. virtual Array surface_get_arrays(int p_surface) const;
  57. virtual uint32_t surface_get_format(int p_idx) const;
  58. virtual Mesh::PrimitiveType surface_get_primitive_type(int p_idx) const;
  59. virtual Ref<Material> surface_get_material(int p_idx) const;
  60. virtual int get_blend_shape_count() const;
  61. virtual StringName get_blend_shape_name(int p_index) const;
  62. virtual Rect3 get_aabb() const;
  63. virtual RID get_rid() const;
  64. void set_material(const Ref<Material> &p_material);
  65. Ref<Material> get_material() const;
  66. PrimitiveMesh();
  67. ~PrimitiveMesh();
  68. };
  69. /**
  70. Mesh for a simple capsule
  71. */
  72. class CapsuleMesh : public PrimitiveMesh {
  73. GDCLASS(CapsuleMesh, PrimitiveMesh);
  74. private:
  75. float radius;
  76. float mid_height;
  77. int radial_segments;
  78. int rings;
  79. protected:
  80. static void _bind_methods();
  81. virtual void _create_mesh_array(Array &p_arr) const;
  82. public:
  83. void set_radius(const float p_radius);
  84. float get_radius() const;
  85. void set_mid_height(const float p_mid_height);
  86. float get_mid_height() const;
  87. void set_radial_segments(const int p_segments);
  88. int get_radial_segments() const;
  89. void set_rings(const int p_rings);
  90. int get_rings() const;
  91. CapsuleMesh();
  92. };
  93. /**
  94. Similar to test cube but with subdivision support and different texture coordinates
  95. */
  96. class CubeMesh : public PrimitiveMesh {
  97. GDCLASS(CubeMesh, PrimitiveMesh);
  98. private:
  99. Vector3 size;
  100. int subdivide_w;
  101. int subdivide_h;
  102. int subdivide_d;
  103. protected:
  104. static void _bind_methods();
  105. virtual void _create_mesh_array(Array &p_arr) const;
  106. public:
  107. void set_size(const Vector3 &p_size);
  108. Vector3 get_size() const;
  109. void set_subdivide_width(const int p_divisions);
  110. int get_subdivide_width() const;
  111. void set_subdivide_height(const int p_divisions);
  112. int get_subdivide_height() const;
  113. void set_subdivide_depth(const int p_divisions);
  114. int get_subdivide_depth() const;
  115. CubeMesh();
  116. };
  117. /**
  118. A cylinder
  119. */
  120. class CylinderMesh : public PrimitiveMesh {
  121. GDCLASS(CylinderMesh, PrimitiveMesh);
  122. private:
  123. float top_radius;
  124. float bottom_radius;
  125. float height;
  126. int radial_segments;
  127. int rings;
  128. protected:
  129. static void _bind_methods();
  130. virtual void _create_mesh_array(Array &p_arr) const;
  131. public:
  132. void set_top_radius(const float p_radius);
  133. float get_top_radius() const;
  134. void set_bottom_radius(const float p_radius);
  135. float get_bottom_radius() const;
  136. void set_height(const float p_height);
  137. float get_height() const;
  138. void set_radial_segments(const int p_segments);
  139. int get_radial_segments() const;
  140. void set_rings(const int p_rings);
  141. int get_rings() const;
  142. CylinderMesh();
  143. };
  144. /**
  145. Similar to quadmesh but with tesselation support
  146. */
  147. class PlaneMesh : public PrimitiveMesh {
  148. GDCLASS(PlaneMesh, PrimitiveMesh);
  149. private:
  150. Size2 size;
  151. int subdivide_w;
  152. int subdivide_d;
  153. protected:
  154. static void _bind_methods();
  155. virtual void _create_mesh_array(Array &p_arr) const;
  156. public:
  157. void set_size(const Size2 &p_size);
  158. Size2 get_size() const;
  159. void set_subdivide_width(const int p_divisions);
  160. int get_subdivide_width() const;
  161. void set_subdivide_depth(const int p_divisions);
  162. int get_subdivide_depth() const;
  163. PlaneMesh();
  164. };
  165. /**
  166. A prism shapen, handy for ramps, triangles, etc.
  167. */
  168. class PrismMesh : public PrimitiveMesh {
  169. GDCLASS(PrismMesh, PrimitiveMesh);
  170. private:
  171. float left_to_right;
  172. Vector3 size;
  173. int subdivide_w;
  174. int subdivide_h;
  175. int subdivide_d;
  176. protected:
  177. static void _bind_methods();
  178. virtual void _create_mesh_array(Array &p_arr) const;
  179. public:
  180. void set_left_to_right(const float p_left_to_right);
  181. float get_left_to_right() const;
  182. void set_size(const Vector3 &p_size);
  183. Vector3 get_size() const;
  184. void set_subdivide_width(const int p_divisions);
  185. int get_subdivide_width() const;
  186. void set_subdivide_height(const int p_divisions);
  187. int get_subdivide_height() const;
  188. void set_subdivide_depth(const int p_divisions);
  189. int get_subdivide_depth() const;
  190. PrismMesh();
  191. };
  192. /**
  193. Our original quadmesh...
  194. */
  195. class QuadMesh : public PrimitiveMesh {
  196. GDCLASS(QuadMesh, PrimitiveMesh)
  197. private:
  198. // nothing? really? Maybe add size some day atleast... :)
  199. protected:
  200. static void _bind_methods();
  201. virtual void _create_mesh_array(Array &p_arr) const;
  202. public:
  203. QuadMesh();
  204. };
  205. /**
  206. A sphere..
  207. */
  208. class SphereMesh : public PrimitiveMesh {
  209. GDCLASS(SphereMesh, PrimitiveMesh);
  210. private:
  211. float radius;
  212. float height;
  213. int radial_segments;
  214. int rings;
  215. bool is_hemisphere;
  216. protected:
  217. static void _bind_methods();
  218. virtual void _create_mesh_array(Array &p_arr) const;
  219. public:
  220. void set_radius(const float p_radius);
  221. float get_radius() const;
  222. void set_height(const float p_height);
  223. float get_height() const;
  224. void set_radial_segments(const int p_radial_segments);
  225. int get_radial_segments() const;
  226. void set_rings(const int p_rings);
  227. int get_rings() const;
  228. void set_is_hemisphere(const bool p_is_hemisphere);
  229. bool get_is_hemisphere() const;
  230. SphereMesh();
  231. };
  232. #endif