BsHandleDrawManager.h 9.8 KB

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