Drawable.h 12 KB

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