BsRenderable.h 8.3 KB

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