BsDrawHelper.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsMatrix4.h"
  6. #include "BsVector3.h"
  7. #include "BsVector2.h"
  8. #include "BsColor.h"
  9. #include "BsRect3.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Utility-Engine
  13. * @{
  14. */
  15. /** Helper class for immediate drawing of common geometric shapes. */
  16. class BS_EXPORT DrawHelper
  17. {
  18. public:
  19. /** Controls in what order will elements be rendered, depending on some reference point. */
  20. enum class SortType
  21. {
  22. BackToFront,
  23. FrontToBack,
  24. None
  25. };
  26. /** Type of meshes that are output by DrawHelper. */
  27. enum class MeshType
  28. {
  29. Solid, Wire, Line, Text
  30. };
  31. /** Container for mesh of a specific type output by the DrawHelper. */
  32. struct ShapeMeshData
  33. {
  34. SPtr<TransientMesh> mesh;
  35. MeshType type;
  36. HTexture texture;
  37. };
  38. DrawHelper();
  39. ~DrawHelper();
  40. /** Sets a color that will be used for any shapes recorded after this call. */
  41. void setColor(const Color& color);
  42. /** Sets a transform matrix that will be used for any shapes recorded after this call. */
  43. void setTransform(const Matrix4& transform);
  44. /** Sets the layer bitfield that can be used for filtering which objects are output into the final mesh. */
  45. void setLayer(UINT64 layer);
  46. /** Records a solid cuboid with the specified properties in the internal draw queue. */
  47. void cube(const Vector3& position, const Vector3& extents);
  48. /** Records a solid sphere with the specified properties in the internal draw queue. */
  49. void sphere(const Vector3& position, float radius, UINT32 quality = 1);
  50. /** Records a wireframe cube with the specified properties in the internal draw queue. */
  51. void wireCube(const Vector3& position, const Vector3& extents);
  52. /** Records a wireframe sphere with the specified properties in the internal draw queue. */
  53. void wireSphere(const Vector3& position, float radius, UINT32 quality = 10);
  54. /** Records a line with the specified properties in the internal draw queue. */
  55. void line(const Vector3& start, const Vector3& end);
  56. /**
  57. * Records a list of lines in the internal draw queue. The list must contain lines as pair of vertices, starting
  58. * point followed by an end point, and so on.
  59. */
  60. void lineList(const Vector<Vector3>& lines);
  61. /** Records a wireframe frustum with the specified properties in the internal draw queue. */
  62. void frustum(const Vector3& position, float aspect, Degree FOV, float near, float far);
  63. /** Records a solid cone with the specified properties in the internal draw queue. */
  64. void cone(const Vector3& base, const Vector3& normal, float height, float radius,
  65. const Vector2& scale = Vector2::ONE, UINT32 quality = 10);
  66. /** Records a wire cone with the specified properties in the internal draw queue. */
  67. void wireCone(const Vector3& base, const Vector3& normal, float height, float radius,
  68. const Vector2& scale = Vector2::ONE, UINT32 quality = 10);
  69. /** Records a solid disc with the specified properties in the internal draw queue. */
  70. void disc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality = 10);
  71. /** Records a wireframe disc with the specified properties in the internal draw queue. */
  72. void wireDisc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality = 10);
  73. /** Records a solid arc with the specified properties in the internal draw queue. */
  74. void arc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle,
  75. UINT32 quality = 10);
  76. /** Records a wireframe arc with the specified properties in the internal draw queue. */
  77. void wireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle,
  78. UINT32 quality = 10);
  79. /** Records a 3D mesh to be drawn as wireframe in the internal draw queue. */
  80. void wireMesh(const SPtr<MeshData>& meshData);
  81. /** Records a solid rectangle with the specified properties in the internal draw queue. */
  82. void rectangle(const Rect3& area);
  83. /**
  84. * Records a mesh representing 2D text with the specified properties in the internal draw queue.
  85. *
  86. * @param[in] position Position to render the text at. Text will be centered around this point.
  87. * @param[in] text Text to draw.
  88. * @param[in] font Font to use for rendering the text's characters.
  89. * @param[in] size Size of the characters, in points.
  90. */
  91. void text(const Vector3& position, const WString& text, const HFont& font, UINT32 size = 10);
  92. /** Clears all recorded shapes. */
  93. void clear();
  94. /**
  95. * Generates a set of meshes from all the recorded solid and wireframe shapes. The meshes can be accessed via
  96. * getMeshes() and released via clearMeshes().
  97. *
  98. * @param sorting (optional) Determines how (and if) should elements be sorted
  99. * based on their distance from the reference point.
  100. * @param reference (optional) Reference point to use for determining distance when
  101. * sorting.
  102. * @param layers (optional) Layers bitfield that can be used for controlling which shapes will be included
  103. * in the mesh. This bitfield will be ANDed with the layer specified when recording the shape.
  104. *
  105. * @note You must call clearMeshes() when done.
  106. */
  107. void buildMeshes(SortType sorting = SortType::None, const Vector3& reference = Vector3::ZERO,
  108. UINT64 layers = 0xFFFFFFFFFFFFFFFF);
  109. /** Returns a set of meshes that were built using the last call to buildMeshes(). */
  110. const Vector<ShapeMeshData>& getMeshes() const { return mMeshes; }
  111. /** Deallocates meshes previously built with buildMeshes(). */
  112. void clearMeshes(const Vector<ShapeMeshData>& meshes);
  113. private:
  114. struct CommonData
  115. {
  116. Color color;
  117. Matrix4 transform;
  118. Vector3 center;
  119. UINT64 layer;
  120. };
  121. struct CubeData : CommonData
  122. {
  123. Vector3 position;
  124. Vector3 extents;
  125. };
  126. struct SphereData : CommonData
  127. {
  128. Vector3 position;
  129. float radius;
  130. UINT32 quality;
  131. };
  132. struct LineData : CommonData
  133. {
  134. Vector3 start;
  135. Vector3 end;
  136. };
  137. struct LineListData : CommonData
  138. {
  139. Vector<Vector3> lines;
  140. };
  141. struct Rect3Data : CommonData
  142. {
  143. Rect3 area;
  144. };
  145. struct FrustumData : CommonData
  146. {
  147. Vector3 position;
  148. float aspect;
  149. Degree FOV;
  150. float near;
  151. float far;
  152. };
  153. struct ConeData : CommonData
  154. {
  155. Vector3 base;
  156. Vector3 normal;
  157. float height;
  158. float radius;
  159. Vector2 scale;
  160. UINT32 quality;
  161. };
  162. struct DiscData : CommonData
  163. {
  164. Vector3 position;
  165. Vector3 normal;
  166. float radius;
  167. UINT32 quality;
  168. };
  169. struct ArcData : CommonData
  170. {
  171. Vector3 position;
  172. Vector3 normal;
  173. float radius;
  174. Degree startAngle;
  175. Degree amountAngle;
  176. UINT32 quality;
  177. };
  178. struct Text2DData : CommonData
  179. {
  180. Vector3 position;
  181. WString text;
  182. HFont font;
  183. UINT32 size;
  184. };
  185. struct WireMeshData : CommonData
  186. {
  187. SPtr<MeshData> meshData;
  188. };
  189. static const UINT32 VERTEX_BUFFER_GROWTH;
  190. static const UINT32 INDEX_BUFFER_GROWTH;
  191. Color mColor;
  192. Matrix4 mTransform;
  193. UINT64 mLayer;
  194. Vector<CubeData> mSolidCubeData;
  195. Vector<CubeData> mWireCubeData;
  196. Vector<SphereData> mSolidSphereData;
  197. Vector<SphereData> mWireSphereData;
  198. Vector<LineData> mLineData;
  199. Vector<LineListData> mLineListData;
  200. Vector<Rect3Data> mRect3Data;
  201. Vector<FrustumData> mFrustumData;
  202. Vector<ConeData> mConeData;
  203. Vector<ConeData> mWireConeData;
  204. Vector<DiscData> mDiscData;
  205. Vector<DiscData> mWireDiscData;
  206. Vector<ArcData> mArcData;
  207. Vector<ArcData> mWireArcData;
  208. Vector<Text2DData> mText2DData;
  209. Vector<WireMeshData> mWireMeshData;
  210. Vector<ShapeMeshData> mMeshes;
  211. UINT32 mNumActiveMeshes;
  212. SPtr<MeshHeap> mSolidMeshHeap;
  213. SPtr<MeshHeap> mWireMeshHeap;
  214. SPtr<MeshHeap> mLineMeshHeap;
  215. SPtr<MeshHeap> mTextMeshHeap;
  216. SPtr<VertexDataDesc> mSolidVertexDesc;
  217. SPtr<VertexDataDesc> mWireVertexDesc;
  218. SPtr<VertexDataDesc> mLineVertexDesc;
  219. SPtr<VertexDataDesc> mTextVertexDesc;
  220. };
  221. /** @} */
  222. }