BsHandleDrawManager.h 9.4 KB

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