BsHandleDrawManager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsGpuParams.h"
  4. namespace BansheeEngine
  5. {
  6. class HandleDrawManagerCore;
  7. /**
  8. * @brief Allows you to easily draw various kinds of simple shapes, primarily
  9. * used for drawing handles in the scene view.
  10. *
  11. * Drawn elements only persist for a single draw call and need to be re-queued
  12. * after.
  13. */
  14. class BS_ED_EXPORT HandleDrawManager
  15. {
  16. public:
  17. HandleDrawManager();
  18. ~HandleDrawManager();
  19. /**
  20. * @brief Sets the color of all the following draw* calls.
  21. */
  22. void setColor(const Color& color);
  23. /**
  24. * @brief Sets the transform matrix that will be applied to all
  25. * following draw* calls.
  26. */
  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. * @brief Draws a solid cuboid.
  35. *
  36. * @param position Center of the cuboid.
  37. * @param extents Radius of the cuboid in all directions.
  38. * @param size Uniform scale of the object.
  39. */
  40. void drawCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  41. /**
  42. * @brief Draws a solid sphere.
  43. *
  44. * @param position Center of the sphere.
  45. * @param radius Radius of the sphere.
  46. * @param size Uniform scale of the object.
  47. */
  48. void drawSphere(const Vector3& position, float radius, float size = 1.0f);
  49. /**
  50. * @brief Draws a wireframe cuboid.
  51. *
  52. * @param position Center of the cuboid.
  53. * @param extents Radius of the cuboid in all directions.
  54. * @param size Uniform scale of the object.
  55. */
  56. void drawWireCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  57. /**
  58. * @brief Draws a wireframe sphere.
  59. *
  60. * @param position Center of the sphere.
  61. * @param radius Radius of the sphere.
  62. * @param size Uniform scale of the object.
  63. */
  64. void drawWireSphere(const Vector3& position, float radius, float size = 1.0f);
  65. /**
  66. * @brief Draws a solid cone.
  67. *
  68. * @param base Position of the center of the base of the cone.
  69. * @param normal Orientation of the cone, pointing from center base to the tip of the cone.
  70. * @param height Height of the cone (along the normal).
  71. * @param radius Radius of the base of the cone.
  72. * @param 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. * @brief Draws a line.
  77. *
  78. * @param start Starting point for the line.
  79. * @param end Ending point for the line.
  80. * @param size Uniform scale of the object.
  81. */
  82. void drawLine(const Vector3& start, const Vector3& end, float size = 1.0f);
  83. /**
  84. * @brief Draws a double-sided solid disc.
  85. *
  86. * @param position Center of the disc.
  87. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  88. * @param radius Radius of the disc.
  89. * @param size Uniform scale of the object.
  90. */
  91. void drawDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  92. /**
  93. * @brief Draws a wireframe disc.
  94. *
  95. * @param position Center of the disc.
  96. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  97. * @param radius Radius of the disc.
  98. * @param size Uniform scale of the object.
  99. */
  100. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  101. /**
  102. * @brief Draws a double-sided solid arc.
  103. *
  104. * @param position Center of the arc.
  105. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  106. * @param radius Radius of the arc.
  107. * @param startAngle Angle at which to start the arc.
  108. * @param amountAngle Length of the arc.
  109. * @param 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. * @brief Draws a wireframe arc.
  114. *
  115. * @param position Center of the arc.
  116. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  117. * @param radius Radius of the arc.
  118. * @param startAngle Angle at which to start the arc.
  119. * @param amountAngle Length of the arc.
  120. * @param 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. * @brief Draws a double-sided solid rectangle.
  125. *
  126. * @param area Position and size of the rectangle.
  127. * @param size Uniform scale of the object.
  128. */
  129. void drawRect(const Rect3& area, float size = 1.0f);
  130. /**
  131. * @brief Queues all the handle draw commands executed so far for rendering. All commands
  132. * are cleared and will need to be called again to draw them again.
  133. */
  134. void draw(const CameraPtr& camera);
  135. private:
  136. friend class HandleDrawManagerCore;
  137. /**
  138. * @brief Initializes the core thread portion of the draw manager.
  139. *
  140. * @param wireMat Material used for drawing the wireframe objects.
  141. * @param solidMat Material used for drawing the solid objects.
  142. */
  143. void initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat);
  144. /**
  145. * @brief Destroys the core thread portion of the draw manager.
  146. */
  147. void destroyCore(HandleDrawManagerCore* core);
  148. static const UINT32 SPHERE_QUALITY;
  149. static const UINT32 WIRE_SPHERE_QUALITY;
  150. static const UINT32 ARC_QUALITY;
  151. Matrix4 mTransform;
  152. std::atomic<HandleDrawManagerCore*> mCore;
  153. DrawHelper* mDrawHelper;
  154. };
  155. /**
  156. * @brief Core thread specific portion of the HandleDrawManager that
  157. * handles actual rendering.
  158. */
  159. class BS_ED_EXPORT HandleDrawManagerCore
  160. {
  161. /**
  162. * @brief Contains information about the material used for
  163. * drawing solid objects and its parameters.
  164. */
  165. struct SolidMaterialData
  166. {
  167. SPtr<MaterialCore> mat;
  168. GpuParamMat4Core mViewProj;
  169. GpuParamVec4Core mViewDir;
  170. };
  171. /**
  172. * @brief Contains information about the material used for
  173. * drawing wireframe objects and its parameters.
  174. */
  175. struct WireMaterialData
  176. {
  177. SPtr<MaterialCore> mat;
  178. GpuParamMat4Core mViewProj;
  179. };
  180. /**
  181. * @brief Type of mesh that can be drawn.
  182. */
  183. enum class MeshType
  184. {
  185. Solid, Wire
  186. };
  187. /**
  188. * @brief Contains data about a render mesh.
  189. */
  190. struct MeshData
  191. {
  192. MeshData(const SPtr<MeshCoreBase>& mesh, MeshType type)
  193. :mesh(mesh), type(type)
  194. { }
  195. SPtr<MeshCoreBase> mesh;
  196. MeshType type;
  197. };
  198. struct PrivatelyConstruct { };
  199. public:
  200. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  201. ~HandleDrawManagerCore();
  202. private:
  203. friend class HandleDrawManager;
  204. /**
  205. * @brief Initializes the object. Must be called right after construction.
  206. *
  207. * @param wireMat Material used for drawing the wireframe objects.
  208. * @param solidMat Material used for drawing the solid objects.
  209. */
  210. void initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat);
  211. /**
  212. * @brief Updates the data that will be used for rendering the new frame.
  213. *
  214. * @param camera Camera to render to.
  215. * @param meshes Meshes to render.
  216. */
  217. void updateData(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes);
  218. /**
  219. * @brief Callback triggered by the renderer. Draws the stored meshes.
  220. */
  221. void render();
  222. SPtr<CameraCore> mCamera;
  223. Vector<MeshData> mMeshes;
  224. // Immutable
  225. SolidMaterialData mSolidMaterial;
  226. WireMaterialData mWireMaterial;
  227. };
  228. }