Drawable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_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 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. /// Mark for update before octree reinsertion.
  126. void MarkForUpdate();
  127. /// Return world bounding box.
  128. const BoundingBox& GetWorldBoundingBox();
  129. /// Return drawable flags.
  130. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  131. /// Return draw distance.
  132. float GetDrawDistance() const { return drawDistance_; }
  133. /// Return shadow draw distance.
  134. float GetShadowDistance() const { return shadowDistance_; }
  135. /// Return LOD bias.
  136. float GetLodBias() const { return lodBias_; }
  137. /// Return view mask.
  138. unsigned GetViewMask() const { return viewMask_; }
  139. /// Return light mask.
  140. unsigned GetLightMask() const { return lightMask_; }
  141. /// Return shadow mask.
  142. unsigned GetShadowMask() const { return shadowMask_; }
  143. /// Return zone mask.
  144. unsigned GetZoneMask() const { return zoneMask_; }
  145. /// Return maximum number of lights.
  146. unsigned GetMaxLights() const { return maxLights_; }
  147. /// Return visible flag.
  148. bool IsVisible() const { return visible_; }
  149. /// Return shadowcaster flag.
  150. bool GetCastShadows() const { return castShadows_; }
  151. /// Return occlusion flag.
  152. bool IsOccluder() const { return occluder_; }
  153. /// %Set new zone.
  154. void SetZone(Zone* zone);
  155. /// %Set sorting value.
  156. void SetSortValue(float value);
  157. /// Mark in view (either the main camera, or a shadow camera view) this frame.
  158. void MarkInView(const FrameInfo& frame, bool mainView = true);
  159. /// Clear lights and base pass flags for a new frame.
  160. void ClearLights();
  161. /// Add a per-pixel light.
  162. void AddLight(Light* light);
  163. /// Add a per-vertex light.
  164. void AddVertexLight(Light* light);
  165. /// Sort and limit per-pixel lights to maximum allowed.
  166. void LimitLights();
  167. /// Sort and limit per-vertex lights to maximum allowed.
  168. void LimitVertexLights();
  169. /// %Set base pass flag for a batch.
  170. void SetBasePass(unsigned batchIndex);
  171. /// Return octree octant.
  172. Octant* GetOctant() const { return octant_; }
  173. /// Return current zone.
  174. Zone* GetZone() const;
  175. /// Return previous zone.
  176. Zone* GetLastZone() const;
  177. /// Return distance from camera.
  178. float GetDistance() const { return distance_; }
  179. /// Return LOD scaled distance from camera.
  180. float GetLodDistance() const { return lodDistance_; }
  181. /// Return sorting value.
  182. float GetSortValue() const { return sortValue_; }
  183. /// Return whether is in view this frame.
  184. bool IsInView(unsigned frameNumber) const { return viewFrameNumber_ == frameNumber; }
  185. /// Return whether is visible in a specific view this frame.
  186. bool IsInView(const FrameInfo& frame, bool mainView = true) const { return viewFrameNumber_ == frame.frameNumber_ && viewFrame_ == &frame && (!mainView || viewCamera_ == frame.camera_); }
  187. /// Return whether has a base pass.
  188. bool HasBasePass(unsigned batchIndex) const;
  189. /// Return per-pixel lights.
  190. const PODVector<Light*>& GetLights() const { return lights_; }
  191. /// Return per-vertex lights.
  192. const PODVector<Light*>& GetVertexLights() const { return vertexLights_; }
  193. /// Return the first added per-pixel light.
  194. Light* GetFirstLight() const { return firstLight_; }
  195. protected:
  196. /// Handle node being assigned.
  197. virtual void OnNodeSet(Node* node);
  198. /// Handle node transform being dirtied.
  199. virtual void OnMarkedDirty(Node* node);
  200. /// Recalculate the world-space bounding box.
  201. virtual void OnWorldBoundingBoxUpdate() = 0;
  202. /// Add to octree.
  203. void AddToOctree();
  204. /// Remove from octree.
  205. void RemoveFromOctree();
  206. /// Move into another octree octant.
  207. void SetOctant(Octant* octant) { octant_ = octant; }
  208. /// Octree octant.
  209. Octant* octant_;
  210. /// World bounding box.
  211. BoundingBox worldBoundingBox_;
  212. /// Base pass flags per batch index.
  213. PODVector<unsigned> basePassFlags_;
  214. /// Last view's frameinfo. Not safe to dereference.
  215. const FrameInfo* viewFrame_;
  216. /// Last view's camera. Not safe to dereference.
  217. Camera* viewCamera_;
  218. /// Per-pixel lights affecting this drawable.
  219. PODVector<Light*> lights_;
  220. /// Per-vertex lights affecting this drawable.
  221. PODVector<Light*> vertexLights_;
  222. /// First per-pixel light added this frame.
  223. Light* firstLight_;
  224. /// Current zone.
  225. WeakPtr<Zone> zone_;
  226. /// Previous zone.
  227. WeakPtr<Zone> lastZone_;
  228. /// Draw distance.
  229. float drawDistance_;
  230. /// Shadow distance.
  231. float shadowDistance_;
  232. /// LOD bias.
  233. float lodBias_;
  234. /// View mask.
  235. unsigned viewMask_;
  236. /// Light mask.
  237. unsigned lightMask_;
  238. /// Shadow mask.
  239. unsigned shadowMask_;
  240. /// Zone mask.
  241. unsigned zoneMask_;
  242. /// Maximum lights.
  243. unsigned maxLights_;
  244. /// Current distance to camera.
  245. float distance_;
  246. /// LOD scaled distance.
  247. float lodDistance_;
  248. /// Current sort value.
  249. float sortValue_;
  250. /// Last visible frame number.
  251. unsigned viewFrameNumber_;
  252. /// Drawable flags.
  253. unsigned char drawableFlags_;
  254. /// Visible flag.
  255. bool visible_;
  256. /// Shadowcaster flag.
  257. bool castShadows_;
  258. /// Occluder flag.
  259. bool occluder_;
  260. /// Bounding box dirty flag.
  261. bool worldBoundingBoxDirty_;
  262. /// Octree update queued flag.
  263. bool updateQueued_;
  264. /// Octree reinsertion queued flag.
  265. bool reinsertionQueued_;
  266. };
  267. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  268. {
  269. return lhs->GetSortValue() < rhs->GetSortValue();
  270. }