BsRenderableHandler.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsCoreObject.h"
  5. #include "BsIResourceListener.h"
  6. #include "BsBounds.h"
  7. #include "BsAABox.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Signals which portion of a RenderableHandler is dirty.
  12. */
  13. enum class RenderableDirtyFlag
  14. {
  15. Transform = 0x01,
  16. Everything = 0x02
  17. };
  18. /**
  19. * @brief Renderable represents any visible object in the scene. It has a mesh,
  20. * bounds and a set of materials. Renderer will render any Renderable objects
  21. * visible by the camera.
  22. */
  23. template<bool Core>
  24. class BS_EXPORT TRenderableHandler
  25. {
  26. template<bool Core> struct TMeshType {};
  27. template<> struct TMeshType < false > { typedef HMesh Type; };
  28. template<> struct TMeshType < true > { typedef SPtr<MeshCore> Type; };
  29. template<bool Core> struct TMaterialType {};
  30. template<> struct TMaterialType < false > { typedef HMaterial Type; };
  31. template<> struct TMaterialType < true > { typedef SPtr<MaterialCore> Type; };
  32. typedef typename TMeshType<Core>::Type MeshType;
  33. typedef typename TMaterialType<Core>::Type MaterialType;
  34. public:
  35. TRenderableHandler();
  36. virtual ~TRenderableHandler();
  37. /**
  38. * @brief Sets the mesh to render. All sub-meshes of the mesh will be rendered,
  39. * and you may set individual materials for each sub-mesh.
  40. */
  41. void setMesh(const MeshType& mesh);
  42. /**
  43. * @brief Sets a material that will be used for rendering a sub-mesh with
  44. * the specified index. If a sub-mesh doesn't have a specific material set
  45. * then the primary material will be used.
  46. */
  47. void setMaterial(UINT32 idx, const MaterialType& material);
  48. /**
  49. * @brief Sets the primary material to use for rendering. Any sub-mesh that
  50. * doesn't have an explicit material set will use this material.
  51. *
  52. * @note This is equivalent to calling setMaterial(0, material).
  53. */
  54. void setMaterial(const MaterialType& material);
  55. /**
  56. * @brief Sets the layer bitfield. Renderable layer must match camera layer
  57. * in order for the camera to render the component.
  58. */
  59. void setLayer(UINT64 layer);
  60. /**
  61. * @brief Sets the transform matrix that is applied to the object
  62. * when its being rendered.
  63. */
  64. void setTransform(const Matrix4& transform);
  65. /**
  66. * @brief Sets the world position of the renderable.
  67. */
  68. void setPosition(const Vector3& position);
  69. /**
  70. * @brief Sets whether the object should be rendered or not.
  71. */
  72. void setIsActive(bool active);
  73. /**
  74. * @brief Gets the layer bitfield. Renderable layer must match camera layer
  75. * in order for the camera to render the component.
  76. */
  77. UINT64 getLayer() const { return mLayer; }
  78. /**
  79. * @brief Returns the mesh used for rendering.
  80. */
  81. MeshType getMesh() const { return mMesh; }
  82. /**
  83. * @brief Returns the material used for rendering a sub-mesh with
  84. * the specified index.
  85. */
  86. MaterialType getMaterial(UINT32 idx) const { return mMaterials[idx]; }
  87. /**
  88. * @brief Returns the transform matrix that is applied to the object
  89. * when its being rendered.
  90. */
  91. Matrix4 getTransform() const { return mTransform; }
  92. /**
  93. * @brief Gets whether the object should be rendered or not.
  94. */
  95. bool getIsActive() const { return mIsActive; }
  96. /**
  97. * @brief Retrieves the world position of the renderable.
  98. */
  99. Vector3 getPosition() const { return mPosition; }
  100. protected:
  101. /**
  102. * @copydoc CoreObject::markCoreDirty
  103. */
  104. virtual void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything) { }
  105. /**
  106. * @copydoc IResourceListener::markResourcesDirty
  107. */
  108. virtual void _markResourcesDirty() { }
  109. MeshType mMesh;
  110. Vector<MaterialType> mMaterials;
  111. UINT64 mLayer;
  112. Vector<AABox> mWorldBounds;
  113. Vector3 mPosition;
  114. Matrix4 mTransform;
  115. bool mIsActive;
  116. };
  117. /**
  118. * @copydoc TRenderableHandler
  119. */
  120. class BS_EXPORT RenderableHandlerCore : public CoreObjectCore, public TRenderableHandler<true>
  121. {
  122. public:
  123. ~RenderableHandlerCore();
  124. /**
  125. * @brief Gets world bounds of the mesh rendered by this object.
  126. */
  127. Bounds getBounds() const;
  128. /**
  129. * @brief Returns the type that controls how is this object rendered.
  130. */
  131. RenderableType getRenderableType() const { return RenType_LitTextured; }
  132. /**
  133. * @brief Sets an ID that can be used for uniquely identifying this handler by the renderer.
  134. */
  135. void setRendererId(UINT32 id) { mRendererId = id; }
  136. /**
  137. * @brief Retrieves an ID that can be used for uniquely identifying this handler by the renderer.
  138. */
  139. UINT32 getRendererId() const { return mRendererId; }
  140. protected:
  141. friend class RenderableHandler;
  142. RenderableHandlerCore();
  143. /**
  144. * @copydoc RenderableHandlerCore::initialize
  145. */
  146. void initialize();
  147. /**
  148. * @copydoc CoreObject::syncToCore
  149. */
  150. void syncToCore(const CoreSyncData& data);
  151. UINT32 mRendererId;
  152. };
  153. /**
  154. * @copydoc TRenderableHandler
  155. */
  156. class BS_EXPORT RenderableHandler : public IReflectable, public CoreObject, public TRenderableHandler<false>, public IResourceListener
  157. {
  158. public:
  159. /**
  160. * @brief Gets world bounds of the mesh rendered by this object.
  161. */
  162. Bounds getBounds() const;
  163. /**
  164. * @brief Retrieves an implementation of a renderable handler usable only from the
  165. * core thread.
  166. */
  167. SPtr<RenderableHandlerCore> getCore() const;
  168. /**
  169. * @brief Creates a new renderable handler instance.
  170. */
  171. static RenderableHandlerPtr create();
  172. protected:
  173. RenderableHandler() { }
  174. /**
  175. * @copydoc CoreObject::createCore
  176. */
  177. SPtr<CoreObjectCore> createCore() const;
  178. /**
  179. * @copydoc CoreObject::markCoreDirty
  180. */
  181. void _markCoreDirty(RenderableDirtyFlag flag = RenderableDirtyFlag::Everything);
  182. /**
  183. * @copydoc IResourceListener::markResourcesDirty
  184. */
  185. void _markResourcesDirty();
  186. /**
  187. * @copydoc CoreObject::syncToCore
  188. */
  189. CoreSyncData syncToCore(FrameAlloc* allocator);
  190. /**
  191. * @copydoc CoreObject::getCoreDependencies
  192. */
  193. void getCoreDependencies(Vector<SPtr<CoreObject>>& dependencies);
  194. /**
  195. * @copydoc IResourceListener::getResourceDependencies
  196. */
  197. void getResourceDependencies(Vector<HResource>& resources);
  198. /**
  199. * @copydoc IResourceListener::notifyResourceLoaded
  200. */
  201. void notifyResourceLoaded(const HResource& resource) { markCoreDirty(); }
  202. /**
  203. * @copydoc IResourceListener::notifyResourceDestroyed
  204. */
  205. void notifyResourceDestroyed(const HResource& resource) { markCoreDirty(); }
  206. /**
  207. * @copydoc IResourceListener::notifyResourceChanged
  208. */
  209. void notifyResourceChanged(const HResource& resource) { markCoreDirty(); }
  210. /**
  211. * @brief Creates a new renderable handler instance without initializing it.
  212. */
  213. static RenderableHandlerPtr createEmpty();
  214. /************************************************************************/
  215. /* RTTI */
  216. /************************************************************************/
  217. public:
  218. friend class RenderableHandlerRTTI;
  219. static RTTITypeBase* getRTTIStatic();
  220. virtual RTTITypeBase* getRTTI() const;
  221. };
  222. }