Drawable.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 DRAWABLE_ANY = 0xff;
  32. static const unsigned DEFAULT_VIEWMASK = M_MAX_UNSIGNED;
  33. static const unsigned DEFAULT_LIGHTMASK = M_MAX_UNSIGNED;
  34. static const unsigned DEFAULT_ZONEMASK = M_MAX_UNSIGNED;
  35. static const int DRAWABLES_PER_WORK_ITEM = 16;
  36. struct Batch;
  37. class Camera;
  38. class DebugRenderer;
  39. class Geometry;
  40. class Light;
  41. class Material;
  42. class OcclusionBuffer;
  43. class Octant;
  44. class RayOctreeQuery;
  45. class Zone;
  46. struct WorkItem;
  47. /// Geometry update type.
  48. enum UpdateGeometryType
  49. {
  50. UPDATE_NONE = 0,
  51. UPDATE_MAIN_THREAD,
  52. UPDATE_WORKER_THREAD
  53. };
  54. /// Rendering frame update parameters.
  55. struct FrameInfo
  56. {
  57. /// Frame number.
  58. unsigned frameNumber_;
  59. /// Time elapsed since last frame.
  60. float timeStep_;
  61. /// Viewport size.
  62. IntVector2 viewSize_;
  63. /// Camera being used.
  64. Camera* camera_;
  65. };
  66. /// Base class for visible components.
  67. class Drawable : public Component
  68. {
  69. OBJECT(Drawable);
  70. friend class Octant;
  71. friend class Octree;
  72. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  73. public:
  74. /// Construct.
  75. Drawable(Context* context);
  76. /// Destruct.
  77. virtual ~Drawable();
  78. /// Register object attributes. Drawable must be registered first.
  79. static void RegisterObject(Context* context);
  80. /// Process octree raycast.
  81. virtual void ProcessRayQuery(RayOctreeQuery& query, float initialDistance);
  82. /// Update before octree reinsertion. Is called from a worker thread. Needs to be requested with MarkForUpdate().
  83. virtual void Update(const FrameInfo& frame) {}
  84. /// Calculate distance and LOD level for rendering. May be called from worker thread(s), possibly re-entrantly.
  85. virtual void UpdateDistance(const FrameInfo& frame);
  86. /// Prepare geometry for rendering.
  87. virtual void UpdateGeometry(const FrameInfo& frame) {}
  88. /// Return whether a geometry update is necessary, and if it should happen in a worker thread.
  89. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  90. /// Return number of rendering batches.
  91. virtual unsigned GetNumBatches() { return 0; }
  92. /// Fill rendering batch with distance, geometry, material and world transform.
  93. virtual void GetBatch(Batch& batch, const FrameInfo& frame, unsigned batchIndex) {}
  94. /// Return number of occlusion geometry triangles.
  95. virtual unsigned GetNumOccluderTriangles() { return 0; }
  96. /// Draw to occlusion buffer.
  97. virtual bool DrawOcclusion(OcclusionBuffer* buffer) { return true; }
  98. /// Draw debug geometry.
  99. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  100. /// %Set draw distance.
  101. void SetDrawDistance(float distance);
  102. /// %Set shadow draw distance.
  103. void SetShadowDistance(float distance);
  104. /// %Set LOD bias.
  105. void SetLodBias(float bias);
  106. /// %Set view mask. Will be and'ed with camera's view mask to see if the object should be rendered.
  107. void SetViewMask(unsigned mask);
  108. /// %Set light mask. Will be and'ed with light's and zone's light mask to see if the object should be lit.
  109. void SetLightMask(unsigned mask);
  110. /// %Set zone mask. Will be and'ed with zone's zone mask to see if the object should belong to the zone.
  111. void SetZoneMask(unsigned mask);
  112. /// %Set maximum number of lights. Default 0 is unlimited.
  113. void SetMaxLights(unsigned num);
  114. /// %Set visible flag.
  115. void SetVisible(bool enable);
  116. /// %Set shadowcaster flag.
  117. void SetCastShadows(bool enable);
  118. /// %Set occlusion flag.
  119. void SetOccluder(bool enable);
  120. /// Mark for update before octree reinsertion.
  121. void MarkForUpdate();
  122. /// Return world bounding box.
  123. const BoundingBox& GetWorldBoundingBox();
  124. /// Return drawable flags.
  125. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  126. /// Return draw distance.
  127. float GetDrawDistance() const { return drawDistance_; }
  128. /// Return shadow draw distance.
  129. float GetShadowDistance() const { return shadowDistance_; }
  130. /// Return LOD bias.
  131. float GetLodBias() const { return lodBias_; }
  132. /// Return view mask.
  133. unsigned GetViewMask() const { return viewMask_; }
  134. /// Return light mask.
  135. unsigned GetLightMask() const { return lightMask_; }
  136. /// Return zone mask.
  137. unsigned GetZoneMask() const { return zoneMask_; }
  138. /// Return maximum number of lights.
  139. unsigned GetMaxLights() const { return maxLights_; }
  140. /// Return visible flag.
  141. bool IsVisible() const { return visible_; }
  142. /// Return shadowcaster flag.
  143. bool GetCastShadows() const { return castShadows_; }
  144. /// Return occlusion flag.
  145. bool IsOccluder() const { return occluder_; }
  146. /// %Set new zone.
  147. void SetZone(Zone* zone);
  148. /// %Set sorting value.
  149. void SetSortValue(float value);
  150. /// Mark in view (either the main camera, or a shadow camera view) this frame.
  151. void MarkInView(const FrameInfo& frame, bool mainView = true);
  152. /// Clear lights and base pass flags for a new frame.
  153. void ClearLights();
  154. /// Add a light.
  155. void AddLight(Light* light);
  156. /// Sort and limit lights to maximum allowed.
  157. void LimitLights();
  158. /// %Set base pass flag for a batch.
  159. void SetBasePass(unsigned batchIndex);
  160. /// Return octree octant.
  161. Octant* GetOctant() const { return octant_; }
  162. /// Return current zone.
  163. Zone* GetZone() const;
  164. /// Return previous zone.
  165. Zone* GetLastZone() const;
  166. /// Return distance from camera.
  167. float GetDistance() const { return distance_; }
  168. /// Return LOD scaled distance from camera.
  169. float GetLodDistance() const { return lodDistance_; }
  170. /// Return sorting value.
  171. float GetSortValue() const { return sortValue_; }
  172. /// Return whether is in view this frame.
  173. bool IsInView(unsigned frameNumber) const { return viewFrameNumber_ == frameNumber; }
  174. /// Return whether is visible in a specific view this frame.
  175. bool IsInView(const FrameInfo& frame, bool mainView = true) const { return viewFrameNumber_ == frame.frameNumber_ && viewFrame_ == &frame && (!mainView || viewCamera_ == frame.camera_); }
  176. /// Return whether has a base pass.
  177. bool HasBasePass(unsigned batchIndex) const;
  178. /// Return lights.
  179. const PODVector<Light*>& GetLights() const { return lights_; }
  180. /// Return the first added light.
  181. Light* GetFirstLight() const { return firstLight_; }
  182. protected:
  183. /// Handle node being assigned.
  184. virtual void OnNodeSet(Node* node);
  185. /// Handle node transform being dirtied.
  186. virtual void OnMarkedDirty(Node* node);
  187. /// Recalculate the world-space bounding box.
  188. virtual void OnWorldBoundingBoxUpdate() = 0;
  189. /// Add to octree.
  190. void AddToOctree();
  191. /// Remove from octree.
  192. void RemoveFromOctree();
  193. /// Move into another octree octant.
  194. void SetOctant(Octant* octant) { octant_ = octant; }
  195. /// Octree octant.
  196. Octant* octant_;
  197. /// World bounding box.
  198. BoundingBox worldBoundingBox_;
  199. /// Draw distance.
  200. float drawDistance_;
  201. /// Shadow distance.
  202. float shadowDistance_;
  203. /// LOD bias.
  204. float lodBias_;
  205. /// View mask.
  206. unsigned viewMask_;
  207. /// Light mask.
  208. unsigned lightMask_;
  209. /// Zone mask.
  210. unsigned zoneMask_;
  211. /// Maximum lights.
  212. unsigned maxLights_;
  213. /// Current distance to camera.
  214. float distance_;
  215. /// LOD scaled distance.
  216. float lodDistance_;
  217. /// Current sort value.
  218. float sortValue_;
  219. /// Last visible frame number.
  220. unsigned viewFrameNumber_;
  221. /// Base pass flags per batch index.
  222. PODVector<unsigned> basePassFlags_;
  223. /// Last view's frameinfo. Not safe to dereference.
  224. const FrameInfo* viewFrame_;
  225. /// Last view's camera. Not safe to dereference.
  226. Camera* viewCamera_;
  227. /// Lights affecting this drawable.
  228. PODVector<Light*> lights_;
  229. /// First light added this frame.
  230. Light* firstLight_;
  231. /// Current zone.
  232. WeakPtr<Zone> zone_;
  233. /// Previous zone.
  234. WeakPtr<Zone> lastZone_;
  235. /// Drawable flags.
  236. unsigned char drawableFlags_;
  237. /// Visible flag.
  238. bool visible_;
  239. /// Shadowcaster flag.
  240. bool castShadows_;
  241. /// Occluder flag.
  242. bool occluder_;
  243. /// Bounding box dirty flag.
  244. bool worldBoundingBoxDirty_;
  245. /// Octree update queued flag.
  246. bool updateQueued_;
  247. /// Octree reinsertion queued flag.
  248. bool reinsertionQueued_;
  249. };
  250. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  251. {
  252. return lhs->GetSortValue() < rhs->GetSortValue();
  253. }