Drawable.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Component.h"
  26. #include "GraphicsDefs.h"
  27. #include "Node.h"
  28. static const unsigned DRAWABLE_GEOMETRY = 0x1;
  29. static const unsigned DRAWABLE_LIGHT = 0x2;
  30. static const unsigned DRAWABLE_ZONE = 0x4;
  31. static const unsigned DEFAULT_VIEWMASK = M_MAX_UNSIGNED;
  32. static const unsigned DEFAULT_LIGHTMASK = M_MAX_UNSIGNED;
  33. struct Batch;
  34. class Camera;
  35. class DebugRenderer;
  36. class Geometry;
  37. class Light;
  38. class Material;
  39. class OcclusionBuffer;
  40. class Octant;
  41. class RayOctreeQuery;
  42. /// Rendering frame update parameters
  43. struct FrameInfo
  44. {
  45. /// Frame number
  46. unsigned frameNumber_;
  47. /// Time elapsed since last frame
  48. float timeStep_;
  49. /// Viewport size
  50. IntVector2 viewSize_;
  51. /// Camera being used
  52. Camera* camera_;
  53. };
  54. /// Base class for visible components
  55. class Drawable : public Component
  56. {
  57. OBJECT(Drawable);
  58. friend class Octant;
  59. public:
  60. /// Construct
  61. Drawable(Context* context);
  62. /// Destruct
  63. virtual ~Drawable();
  64. /// Register object attributes
  65. static void RegisterObject(Context* context);
  66. /// Process octree raycast
  67. virtual void ProcessRayQuery(RayOctreeQuery& query, float initialDistance);
  68. /// Update before octree reinsertion. Needs to be requested with MarkForUpdate()
  69. virtual void Update(const FrameInfo& frame) {}
  70. /// Calculate distance for rendering
  71. virtual void UpdateDistance(const FrameInfo& frame);
  72. /// Prepare geometry for rendering
  73. virtual void UpdateGeometry(const FrameInfo& frame) {}
  74. /// Return number of rendering batches
  75. virtual unsigned GetNumBatches() { return 0; }
  76. /// Return rendering batch
  77. virtual void GetBatch(const FrameInfo& frame, unsigned batchIndex, Batch& batch) {}
  78. /// Draw to occlusion buffer
  79. virtual bool DrawOcclusion(OcclusionBuffer* buffer) { return true; }
  80. /// Draw debug geometry
  81. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  82. /// Set draw distance
  83. void SetDrawDistance(float distance);
  84. /// Set shadow draw distance
  85. void SetShadowDistance(float distance);
  86. /// Set LOD bias
  87. void SetLodBias(float bias);
  88. /// Set view mask. Will be and'ed with camera's view mask to see if the object should be rendered
  89. void SetViewMask(unsigned mask);
  90. /// Set light mask. Will be and'ed with light's light mask to see if the object should be lit
  91. void SetLightMask(unsigned mask);
  92. /// Set maximum number of lights (forward lighting only). Default 0 is unlimited
  93. void SetMaxLights(unsigned num);
  94. /// Set visible flag
  95. void SetVisible(bool enable);
  96. /// Set shadowcaster flag
  97. void SetCastShadows(bool enable);
  98. /// Set occlusion flag
  99. void SetOccluder(bool enable);
  100. /// Mark for update before octree reinsertion
  101. void MarkForUpdate();
  102. /// Return world bounding box
  103. const BoundingBox& GetWorldBoundingBox();
  104. /// Return drawable flags
  105. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  106. /// Return draw distance
  107. float GetDrawDistance() const { return drawDistance_; }
  108. /// Return shadow draw distance
  109. float GetShadowDistance() const { return shadowDistance_; }
  110. /// Return LOD bias
  111. float GetLodBias() const { return lodBias_; }
  112. /// Return view mask
  113. unsigned GetViewMask() const { return viewMask_; }
  114. /// Return light mask
  115. unsigned GetLightMask() const { return lightMask_; }
  116. /// Return maximum number of lights
  117. unsigned GetMaxLights() const { return maxLights_; }
  118. /// Return visible flag
  119. bool IsVisible() const { return visible_; }
  120. /// Return shadowcaster flag
  121. bool GetCastShadows() const { return castShadows_; }
  122. /// Return occlusion flag
  123. bool IsOccluder() const { return occluder_; }
  124. /// Return octree octant
  125. Octant* GetOctant() const { return octant_; }
  126. /// Set sorting value. Called by View
  127. void SetSortValue(float value);
  128. /// Mark in view this frame. Called by View
  129. void MarkInView(const FrameInfo& frame);
  130. /// Mark in a shadow camera view this frame. If an actual view is already set, does not override it. Called by View
  131. void MarkInShadowView(const FrameInfo& frame);
  132. /// Clear base pass flags. Also resets light vector
  133. void ClearBasePass();
  134. /// Set base pass flag for a batch
  135. void SetBasePass(unsigned batchIndex);
  136. /// Add a light, for drawables that limit the maximum light count
  137. void AddLight(Light* light);
  138. /// Sort and limit lights to maximum allowed
  139. void LimitLights();
  140. /// Return distance from camera
  141. float GetDistance() const { return distance_; }
  142. /// Return LOD scaled distance from camera
  143. float GetLodDistance() const { return lodDistance_; }
  144. /// Return sorting value
  145. float GetSortValue() const { return sortValue_; }
  146. /// Return whether is in view this frame
  147. bool IsInView(unsigned frameNumber) const;
  148. /// Return whether is visible in a specific view this frame
  149. bool IsInView(const FrameInfo& frame) const;
  150. /// Return whether has a base pass
  151. bool HasBasePass(unsigned batchIndex) const { return (basePassFlags_ & (1 << batchIndex)) != 0; }
  152. /// Return lights
  153. const PODVector<Light*>& GetLights() const { return lights_; }
  154. protected:
  155. /// Handle node being assigned
  156. virtual void OnNodeSet(Node* node);
  157. /// Handle node transform being dirtied
  158. virtual void OnMarkedDirty(Node* node);
  159. /// Recalculate world bounding box
  160. virtual void OnWorldBoundingBoxUpdate() = 0;
  161. /// Add to octree
  162. void AddToOctree();
  163. /// Remove from octree
  164. void RemoveFromOctree();
  165. /// Move into another octree octant
  166. void SetOctant(Octant* octant) { octant_ = octant; }
  167. /// Octree octant
  168. Octant* octant_;
  169. /// World bounding box
  170. BoundingBox worldBoundingBox_;
  171. /// Draw distance
  172. float drawDistance_;
  173. /// Shadow distance
  174. float shadowDistance_;
  175. /// LOD bias
  176. float lodBias_;
  177. /// View mask
  178. unsigned viewMask_;
  179. /// Light mask
  180. unsigned lightMask_;
  181. /// Maximum lights
  182. unsigned maxLights_;
  183. /// Drawable flags
  184. unsigned char drawableFlags_;
  185. /// Visible flag
  186. bool visible_;
  187. /// Shadowcaster flag
  188. bool castShadows_;
  189. /// Occluder flag
  190. bool occluder_;
  191. /// Current distance to camera
  192. float distance_;
  193. /// LOD scaled distance
  194. float lodDistance_;
  195. /// Current sort value
  196. float sortValue_;
  197. /// Last visible frame number
  198. unsigned viewFrameNumber_;
  199. /// Base pass flags per batch index
  200. unsigned basePassFlags_;
  201. /// Last camera rendered from. Not safe to dereference
  202. Camera* viewCamera_;
  203. /// Lights affecting this drawable, when light count is limited
  204. PODVector<Light*> lights_;
  205. /// Bounding box dirty flag
  206. bool worldBoundingBoxDirty_;
  207. /// Lod levels dirty flag
  208. bool lodLevelsDirty_;
  209. };
  210. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  211. {
  212. return lhs->GetSortValue() < rhs->GetSortValue();
  213. }