BsHandleDrawManager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 SPtr<Camera>& 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. /** @} */
  169. /** @addtogroup Handles-Internal
  170. * @{
  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. SPtr<GpuParamsSetCore> params;
  180. GpuParamMat4Core viewProj;
  181. GpuParamVec4Core viewDir;
  182. };
  183. /** Contains information about the material used for drawing line objects and its parameters. */
  184. struct LineMaterialData
  185. {
  186. SPtr<MaterialCore> mat;
  187. SPtr<GpuParamsSetCore> params;
  188. GpuParamMat4Core viewProj;
  189. };
  190. /** Contains information about the material used for drawing text and its parameters. */
  191. struct TextMaterialData
  192. {
  193. SPtr<MaterialCore> mat;
  194. SPtr<GpuParamsSetCore> params;
  195. GpuParamMat4Core viewProj;
  196. GpuParamTextureCore texture;
  197. };
  198. /** Contains information about the material used for clearing the alpha channel in the empty areas. */
  199. struct ClearAlphaMaterialData
  200. {
  201. SPtr<MaterialCore> mat;
  202. };
  203. /** Type of mesh that can be drawn. */
  204. enum class MeshType
  205. {
  206. Solid, Line, Text
  207. };
  208. /** Data about a mesh rendered by the draw manager. */
  209. struct MeshData
  210. {
  211. MeshData(const SPtr<MeshCoreBase>& mesh, SPtr<TextureCore> texture, MeshType type)
  212. :mesh(mesh), texture(texture), type(type)
  213. { }
  214. SPtr<MeshCoreBase> mesh;
  215. SPtr<TextureCore> texture;
  216. MeshType type;
  217. };
  218. /** Data about a camera and the meshes that are queued for rendering on it */
  219. struct QueuedData
  220. {
  221. SPtr<CameraCore> camera;
  222. Vector<MeshData> meshes;
  223. };
  224. struct PrivatelyConstruct { };
  225. public:
  226. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  227. ~HandleDrawManagerCore();
  228. private:
  229. friend class HandleDrawManager;
  230. /**
  231. * Initializes the object. Must be called right after construction.
  232. *
  233. * @param[in] lineMat Material used for drawing the line objects.
  234. * @param[in] solidMat Material used for drawing the solid objects.
  235. * @param[in] textMat Material used for drawing the text.
  236. * @param[in] clearMat Material used for clearing the alpha channel in the empty areas.
  237. */
  238. void initialize(const SPtr<MaterialCore>& lineMat, const SPtr<MaterialCore>& solidMat,
  239. const SPtr<MaterialCore>& textMat, const SPtr<MaterialCore>& clearMat);
  240. /**
  241. * Queues new data for rendering.
  242. *
  243. * @param[in] camera Camera to render to.
  244. * @param[in] meshes Meshes to render.
  245. */
  246. void queueForDraw(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes);
  247. /** Deletes any meshes queued for rendering. */
  248. void clearQueued();
  249. /** Callback triggered by the renderer. Draws all queued meshes. */
  250. void render(UINT32 queuedDataIdx);
  251. Vector<QueuedData> mQueuedData;
  252. // Immutable
  253. SolidMaterialData mSolidMaterial;
  254. LineMaterialData mLineMaterial;
  255. TextMaterialData mTextMaterial;
  256. ClearAlphaMaterialData mClearMaterial;
  257. };
  258. /** @} */
  259. }