BsHandleDrawManager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * @brief Draws a solid cuboid.
  30. *
  31. * @param position Center of the cuboid.
  32. * @param extents Radius of the cuboid in all directions.
  33. * @param size Uniform scale of the object.
  34. */
  35. void drawCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  36. /**
  37. * @brief Draws a solid sphere.
  38. *
  39. * @param position Center of the sphere.
  40. * @param radius Radius of the sphere.
  41. * @param size Uniform scale of the object.
  42. */
  43. void drawSphere(const Vector3& position, float radius, float size = 1.0f);
  44. /**
  45. * @brief Draws a wireframe cuboid.
  46. *
  47. * @param position Center of the cuboid.
  48. * @param extents Radius of the cuboid in all directions.
  49. * @param size Uniform scale of the object.
  50. */
  51. void drawWireCube(const Vector3& position, const Vector3& extents, float size = 1.0f);
  52. /**
  53. * @brief Draws a wireframe sphere.
  54. *
  55. * @param position Center of the sphere.
  56. * @param radius Radius of the sphere.
  57. * @param size Uniform scale of the object.
  58. */
  59. void drawWireSphere(const Vector3& position, float radius, float size = 1.0f);
  60. /**
  61. * @brief Draws a solid cone.
  62. *
  63. * @param base Position of the center of the base of the cone.
  64. * @param normal Orientation of the cone, pointing from center base to the tip of the cone.
  65. * @param height Height of the cone (along the normal).
  66. * @param radius Radius of the base of the cone.
  67. * @param size Uniform scale of the object.
  68. */
  69. void drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size = 1.0f);
  70. /**
  71. * @brief Draws a line.
  72. *
  73. * @param start Starting point for the line.
  74. * @param end Ending point for the line.
  75. * @param size Uniform scale of the object.
  76. */
  77. void drawLine(const Vector3& start, const Vector3& end, float size = 1.0f);
  78. /**
  79. * @brief Draws a single-sided solid disc.
  80. *
  81. * @param position Center of the disc.
  82. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  83. * @param radius Radius of the disc.
  84. * @param size Uniform scale of the object.
  85. */
  86. void drawDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  87. /**
  88. * @brief Draws a single-sided wireframe disc.
  89. *
  90. * @param position Center of the disc.
  91. * @param normal Orientation of the disc, pointing in the direction the disc is visible in.
  92. * @param radius Radius of the disc.
  93. * @param size Uniform scale of the object.
  94. */
  95. void drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size = 1.0f);
  96. /**
  97. * @brief Draws a single-sided solid arc.
  98. *
  99. * @param position Center of the arc.
  100. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  101. * @param radius Radius of the arc.
  102. * @param startAngle Angle at which to start the arc.
  103. * @param amountAngle Length of the arc.
  104. * @param size Uniform scale of the object.
  105. */
  106. void drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  107. /**
  108. * @brief Draws a single-sided wireframe arc.
  109. *
  110. * @param position Center of the arc.
  111. * @param normal Orientation of the arc, pointing in the direction the arc is visible in.
  112. * @param radius Radius of the arc.
  113. * @param startAngle Angle at which to start the arc.
  114. * @param amountAngle Length of the arc.
  115. * @param size Uniform scale of the object.
  116. */
  117. void drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size = 1.0f);
  118. /**
  119. * @brief Draws a single-sided solid rectangle.
  120. *
  121. * @param area Position and size of the rectangle.
  122. * @param size Uniform scale of the object.
  123. */
  124. void drawRect(const Rect3& area, float size = 1.0f);
  125. /**
  126. * @brief Queues all the handle draw commands executed so far for rendering. All commands
  127. * are cleared and will need to be called again to draw them again.
  128. */
  129. void draw(const CameraHandlerPtr& camera);
  130. private:
  131. friend class HandleDrawManagerCore;
  132. /**
  133. * @brief Initializes the core thread portion of the draw manager.
  134. *
  135. * @param wireMat Material used for drawing the wireframe objects.
  136. * @param solidMat Material used for drawing the solid objects.
  137. */
  138. void initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat);
  139. /**
  140. * @brief Destroys the core thread portion of the draw manager.
  141. */
  142. void destroyCore(HandleDrawManagerCore* core);
  143. static const UINT32 SPHERE_QUALITY;
  144. static const UINT32 WIRE_SPHERE_QUALITY;
  145. static const UINT32 ARC_QUALITY;
  146. Matrix4 mTransform;
  147. std::atomic<HandleDrawManagerCore*> mCore;
  148. DrawHelper* mDrawHelper;
  149. };
  150. /**
  151. * @brief Core thread specific portion of the HandleDrawManager that
  152. * handles actual rendering.
  153. */
  154. class BS_ED_EXPORT HandleDrawManagerCore
  155. {
  156. /**
  157. * @brief Contains information about the material used for
  158. * drawing solid objects and its parameters.
  159. */
  160. struct SolidMaterialData
  161. {
  162. SPtr<MaterialCore> mat;
  163. GpuParamMat4Core mViewProj;
  164. GpuParamVec4Core mViewDir;
  165. };
  166. /**
  167. * @brief Contains information about the material used for
  168. * drawing wireframe objects and its parameters.
  169. */
  170. struct WireMaterialData
  171. {
  172. SPtr<MaterialCore> mat;
  173. GpuParamMat4Core mViewProj;
  174. };
  175. /**
  176. * @brief Type of mesh that can be drawn.
  177. */
  178. enum class MeshType
  179. {
  180. Solid, Wire
  181. };
  182. /**
  183. * @brief Contains data about a render mesh.
  184. */
  185. struct MeshData
  186. {
  187. MeshData(const SPtr<MeshCoreBase>& mesh, MeshType type)
  188. :mesh(mesh), type(type)
  189. { }
  190. SPtr<MeshCoreBase> mesh;
  191. MeshType type;
  192. };
  193. struct PrivatelyConstruct { };
  194. public:
  195. HandleDrawManagerCore(const PrivatelyConstruct& dummy) { }
  196. private:
  197. friend class HandleDrawManager;
  198. /**
  199. * @brief Initializes the object. Must be called right after construction.
  200. *
  201. * @param wireMat Material used for drawing the wireframe objects.
  202. * @param solidMat Material used for drawing the solid objects.
  203. */
  204. void initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat);
  205. /**
  206. * @brief Updates the data that will be used for rendering the new frame.
  207. *
  208. * @param camera Camera to render to.
  209. * @param meshes Meshes to render.
  210. */
  211. void updateData(const SPtr<CameraHandlerCore>& camera, const Vector<MeshData>& meshes);
  212. /**
  213. * @brief Callback triggered by the renderer. Draws the stored meshes.
  214. */
  215. void render();
  216. SPtr<CameraHandlerCore> mCamera;
  217. Vector<MeshData> mMeshes;
  218. // Immutable
  219. SolidMaterialData mSolidMaterial;
  220. WireMaterialData mWireMaterial;
  221. };
  222. }