BsHandleDrawManager.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "BsGpuParams.h"
  6. #include "BsDrawHelper.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Handles
  10. * @{
  11. */
  12. class HandleDrawManagerCore;
  13. /**
  14. * Allows you to easily draw various kinds of simple shapes, primarily used for drawing handles in the scene view.
  15. *
  16. * Drawn elements only persist for a single draw() call and need to be re-queued after.
  17. */
  18. class BS_ED_EXPORT HandleDrawManager
  19. {
  20. public:
  21. HandleDrawManager();
  22. ~HandleDrawManager();
  23. /** Sets the color of all the following draw* calls. */
  24. void setColor(const Color& color);
  25. /** Sets the transform matrix that will be applied to all following draw* calls. */
  26. void setTransform(const Matrix4& transform);
  27. /**
  28. * Sets the layer bitfield that controls whether a handle is considered visible in a specific camera. Handle layer
  29. * must match camera layer in order for the camera to render it
  30. */
  31. void setLayer(UINT64 layer);
  32. /**
  33. * Draws a solid cuboid.
  34. *
  35. * @param[in] position Center of the cuboid.
  36. * @param[in] extents Radius of the cuboid in all directions.
  37. * @param[in] size Uniform scale of the object.
  38. */
  39. void drawCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  40. /**
  41. * Draws a solid sphere.
  42. *
  43. * @param[in] position Center of the sphere.
  44. * @param[in] radius Radius of the sphere.
  45. * @param[in] size Uniform scale of the object.
  46. */
  47. void drawSphere(const Vector3& position, float radius, float size = 1.0f);
  48. /**
  49. * Draws a wireframe cuboid.
  50. *
  51. * @param[in] position Center of the cuboid.
  52. * @param[in] extents Radius of the cuboid in all directions.
  53. * @param[in] size Uniform scale of the object.
  54. */
  55. void drawWireCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  56. /**
  57. * Draws a wireframe sphere.
  58. *
  59. * @param[in] position Center of the sphere.
  60. * @param[in] radius Radius of the sphere.
  61. * @param[in] size Uniform scale of the object.
  62. */
  63. void drawWireSphere(const Vector3& position, float radius, float size = 1.0f);
  64. /**
  65. * Draws a solid cone.
  66. *
  67. * @param[in] base Position of the center of the base of the cone.
  68. * @param[in] normal Orientation of the cone, pointing from center base to the tip of the cone.
  69. * @param[in] height Height of the cone (along the normal).
  70. * @param[in] radius Radius of the base of the cone.
  71. * @param[in] size Uniform scale of the object.
  72. */
  73. void drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size = 1.0f);
  74. /**
  75. * Draws a line.
  76. *
  77. * @param[in] start Starting point for the line.
  78. * @param[in] end Ending point for the line.
  79. * @param[in] size Uniform scale of the object.
  80. */
  81. void drawLine(const Vector3& start, const Vector3& end, float size = 1.0f);
  82. /**
  83. * Draws a double-sided solid disc.
  84. *
  85. * @param[in] position Center of the disc.
  86. * @param[in] normal Orientation of the disc, pointing in the direction the disc is visible in.
  87. * @param[in] radius Radius of the disc.
  88. * @param[in] size Uniform scale of the object.
  89. */
  90. void drawDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  91. /**
  92. * Draws a wireframe disc.
  93. *
  94. * @param[in] position Center of the disc.
  95. * @param[in] normal Orientation of the disc, pointing in the direction the disc is visible in.
  96. * @param[in] radius Radius of the disc.
  97. * @param[in] size Uniform scale of the object.
  98. */
  99. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  100. /**
  101. * Draws a double-sided solid arc.
  102. *
  103. * @param[in] position Center of the arc.
  104. * @param[in] normal Orientation of the arc, pointing in the direction the arc is visible in.
  105. * @param[in] radius Radius of the arc.
  106. * @param[in] startAngle Angle at which to start the arc.
  107. * @param[in] amountAngle Length of the arc.
  108. * @param[in] size Uniform scale of the object.
  109. */
  110. void drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  111. /**
  112. * Draws a wireframe arc.
  113. *
  114. * @param[in] position Center of the arc.
  115. * @param[in] normal Orientation of the arc, pointing in the direction the arc is visible in.
  116. * @param[in] radius Radius of the arc.
  117. * @param[in] startAngle Angle at which to start the arc.
  118. * @param[in] amountAngle Length of the arc.
  119. * @param[in] size Uniform scale of the object.
  120. */
  121. void drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  122. /**
  123. * Draws a double-sided solid rectangle.
  124. *
  125. * @param[in] area Position and size of the rectangle.
  126. * @param[in] size Uniform scale of the object.
  127. */
  128. void drawRect(const Rect3& area, float size = 1.0f);
  129. /**
  130. * Draws a mesh representing 2D text with the specified properties.
  131. *
  132. * @param[in] position Position to render the text at. Text will be centered around this point.
  133. * @param[in] text Text to draw.
  134. * @param[in] font Font to use for rendering the text's characters.
  135. * @param[in] fontSize Size of the characters, in points.
  136. * @param[in] size Uniform scale of the rendered mesh.
  137. */
  138. void drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize = 16, float size = 1.0f);
  139. /** Queues all the handle draw commands queued since the last call to clear() for rendering. */
  140. void draw(const CameraPtr& camera);
  141. /** Clears all handle draw commands. */
  142. void clear();
  143. private:
  144. friend class HandleDrawManagerCore;
  145. /**
  146. * Initializes the core thread portion of the draw manager.
  147. *
  148. * @param[in] lineMat Material used for drawing the line objects.
  149. * @param[in] solidMat Material used for drawing the solid objects.
  150. * @param[in] textMat Material used for drawing the text.
  151. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  152. */
  153. void initializeCore(const SPtr<MaterialCore>& lineMat, const SPtr<MaterialCore>& solidMat,
  154. const SPtr<MaterialCore>& textMat, const SPtr<MaterialCore>& clearMat);
  155. /** Destroys the core thread portion of the draw manager. */
  156. void destroyCore(HandleDrawManagerCore* core);
  157. /** Destroys all meshes allocated since the last call to this method. */
  158. void clearMeshes();
  159. static const UINT32 SPHERE_QUALITY;
  160. static const UINT32 WIRE_SPHERE_QUALITY;
  161. static const UINT32 ARC_QUALITY;
  162. Vector<Vector<DrawHelper::ShapeMeshData>> mActiveMeshes;
  163. UINT64 mLastFrameIdx;
  164. Matrix4 mTransform;
  165. std::atomic<HandleDrawManagerCore*> mCore;
  166. DrawHelper* mDrawHelper;
  167. };
  168. /** @cond INTERNAL */
  169. /** Core thread specific portion of the HandleDrawManager that handles actual rendering. */
  170. class BS_ED_EXPORT HandleDrawManagerCore
  171. {
  172. /** Contains information about the material used for drawing solid objects and its parameters. */
  173. struct SolidMaterialData
  174. {
  175. SPtr<MaterialCore> mat;
  176. GpuParamMat4Core viewProj;
  177. GpuParamVec4Core viewDir;
  178. };
  179. /** Contains information about the material used for drawing line objects and its parameters. */
  180. struct LineMaterialData
  181. {
  182. SPtr<MaterialCore> mat;
  183. GpuParamMat4Core viewProj;
  184. };
  185. /** Contains information about the material used for drawing text and its parameters. */
  186. struct TextMaterialData
  187. {
  188. SPtr<MaterialCore> mat;
  189. GpuParamMat4Core viewProj;
  190. GpuParamTextureCore texture;
  191. };
  192. /** Contains information about the material used for clearing the alpha channel in the empty areas. */
  193. struct ClearAlphaMaterialData
  194. {
  195. SPtr<MaterialCore> mat;
  196. };
  197. /** Type of mesh that can be drawn. */
  198. enum class MeshType
  199. {
  200. Solid, Line, Text
  201. };
  202. /** Data about a mesh rendered by the draw manager. */
  203. struct MeshData
  204. {
  205. MeshData(const SPtr<MeshCoreBase>& mesh, SPtr<TextureCore> texture, MeshType type)
  206. :mesh(mesh), texture(texture), type(type)
  207. { }
  208. SPtr<MeshCoreBase> mesh;
  209. SPtr<TextureCore> texture;
  210. MeshType type;
  211. };
  212. /** Data about a camera and the meshes that are queued for rendering on it */
  213. struct QueuedData
  214. {
  215. SPtr<CameraCore> camera;
  216. Vector<MeshData> meshes;
  217. };
  218. struct PrivatelyConstruct { };
  219. public:
  220. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  221. ~HandleDrawManagerCore();
  222. private:
  223. friend class HandleDrawManager;
  224. /**
  225. * Initializes the object. Must be called right after construction.
  226. *
  227. * @param[in] lineMat Material used for drawing the line objects.
  228. * @param[in] solidMat Material used for drawing the solid objects.
  229. * @param[in] textMat Material used for drawing the text.
  230. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  231. */
  232. void initialize(const SPtr<MaterialCore>& lineMat, const SPtr<MaterialCore>& solidMat,
  233. const SPtr<MaterialCore>& textMat, const SPtr<MaterialCore>& clearMat);
  234. /**
  235. * Queues new data for rendering.
  236. *
  237. * @param[in] camera Camera to render to.
  238. * @param[in] meshes Meshes to render.
  239. */
  240. void queueForDraw(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes);
  241. /** Deletes any meshes queued for rendering. */
  242. void clearQueued();
  243. /** Callback triggered by the renderer. Draws all queued meshes. */
  244. void render(UINT32 queuedDataIdx);
  245. Vector<QueuedData> mQueuedData;
  246. // Immutable
  247. SolidMaterialData mSolidMaterial;
  248. LineMaterialData mLineMaterial;
  249. TextMaterialData mTextMaterial;
  250. ClearAlphaMaterialData mClearMaterial;
  251. };
  252. /** @endcond */
  253. /** @} */
  254. }