Drawable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/GraphicsDefs.h"
  24. #include "../Math/BoundingBox.h"
  25. #include "../Scene/Component.h"
  26. namespace Atomic
  27. {
  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_GEOMETRY2D = 0x8;
  32. static const unsigned DRAWABLE_ANY = 0xff;
  33. static const unsigned DEFAULT_VIEWMASK = M_MAX_UNSIGNED;
  34. static const unsigned DEFAULT_LIGHTMASK = M_MAX_UNSIGNED;
  35. static const unsigned DEFAULT_SHADOWMASK = M_MAX_UNSIGNED;
  36. static const unsigned DEFAULT_ZONEMASK = M_MAX_UNSIGNED;
  37. static const int MAX_VERTEX_LIGHTS = 4;
  38. static const float ANIMATION_LOD_BASESCALE = 2500.0f;
  39. class Camera;
  40. class Geometry;
  41. class Light;
  42. class Material;
  43. class OcclusionBuffer;
  44. class Octant;
  45. class RayOctreeQuery;
  46. class Zone;
  47. struct RayQueryResult;
  48. struct WorkItem;
  49. /// Geometry update type.
  50. enum UpdateGeometryType
  51. {
  52. UPDATE_NONE = 0,
  53. UPDATE_MAIN_THREAD,
  54. UPDATE_WORKER_THREAD
  55. };
  56. /// Rendering frame update parameters.
  57. struct FrameInfo
  58. {
  59. /// Frame number.
  60. unsigned frameNumber_;
  61. /// Time elapsed since last frame.
  62. float timeStep_;
  63. /// Viewport size.
  64. IntVector2 viewSize_;
  65. /// Camera being used.
  66. Camera* camera_;
  67. };
  68. /// Source data for a 3D geometry draw call.
  69. struct SourceBatch
  70. {
  71. /// Construct with defaults.
  72. SourceBatch();
  73. /// Destruct.
  74. ~SourceBatch();
  75. /// Distance from camera.
  76. float distance_;
  77. /// Geometry.
  78. Geometry* geometry_;
  79. /// Material.
  80. SharedPtr<Material> material_;
  81. /// World transform(s). For a skinned model, these are the bone transforms.
  82. const Matrix3x4* worldTransform_;
  83. /// Number of world transforms.
  84. unsigned numWorldTransforms_;
  85. /// %Geometry type.
  86. GeometryType geometryType_;
  87. };
  88. /// Base class for visible components.
  89. class ATOMIC_API Drawable : public Component
  90. {
  91. OBJECT(Drawable);
  92. friend class Octant;
  93. friend class Octree;
  94. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  95. public:
  96. /// Construct.
  97. Drawable(Context* context, unsigned char drawableFlags = 0);
  98. /// Destruct.
  99. virtual ~Drawable();
  100. /// Register object attributes. Drawable must be registered first.
  101. static void RegisterObject(Context* context);
  102. /// Handle enabled/disabled state change.
  103. virtual void OnSetEnabled();
  104. /// Process octree raycast. May be called from a worker thread.
  105. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  106. /// Update before octree reinsertion. Is called from a worker thread.
  107. virtual void Update(const FrameInfo& frame);
  108. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  109. virtual void UpdateBatches(const FrameInfo& frame);
  110. /// Prepare geometry for rendering.
  111. virtual void UpdateGeometry(const FrameInfo& frame);
  112. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  113. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  114. /// Return the geometry for a specific LOD level.
  115. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  116. /// Return number of occlusion geometry triangles.
  117. virtual unsigned GetNumOccluderTriangles() { return 0; }
  118. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  119. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  120. /// Visualize the component as debug geometry.
  121. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  122. /// Set draw distance.
  123. void SetDrawDistance(float distance);
  124. /// Set shadow draw distance.
  125. void SetShadowDistance(float distance);
  126. /// Set LOD bias.
  127. void SetLodBias(float bias);
  128. /// Set view mask. Is and'ed with camera's view mask to see if the object should be rendered.
  129. void SetViewMask(unsigned mask);
  130. /// Set light mask. Is and'ed with light's and zone's light mask to see if the object should be lit.
  131. void SetLightMask(unsigned mask);
  132. /// 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.
  133. void SetShadowMask(unsigned mask);
  134. /// Set zone mask. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  135. void SetZoneMask(unsigned mask);
  136. /// Set maximum number of per-pixel lights. Default 0 is unlimited.
  137. void SetMaxLights(unsigned num);
  138. /// Set shadowcaster flag.
  139. void SetCastShadows(bool enable);
  140. /// Set occlusion flag.
  141. void SetOccluder(bool enable);
  142. /// Set occludee flag.
  143. void SetOccludee(bool enable);
  144. /// Mark for update and octree reinsertion. Update is automatically queued when the drawable's scene node moves or changes scale.
  145. void MarkForUpdate();
  146. /// Return local space bounding box. May not be applicable or properly updated on all drawables.
  147. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  148. /// Return world-space bounding box.
  149. const BoundingBox& GetWorldBoundingBox();
  150. /// Return drawable flags.
  151. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  152. /// Return draw distance.
  153. float GetDrawDistance() const { return drawDistance_; }
  154. /// Return shadow draw distance.
  155. float GetShadowDistance() const { return shadowDistance_; }
  156. /// Return LOD bias.
  157. float GetLodBias() const { return lodBias_; }
  158. /// Return view mask.
  159. unsigned GetViewMask() const { return viewMask_; }
  160. /// Return light mask.
  161. unsigned GetLightMask() const { return lightMask_; }
  162. /// Return shadow mask.
  163. unsigned GetShadowMask() const { return shadowMask_; }
  164. /// Return zone mask.
  165. unsigned GetZoneMask() const { return zoneMask_; }
  166. /// Return maximum number of per-pixel lights.
  167. unsigned GetMaxLights() const { return maxLights_; }
  168. /// Return shadowcaster flag.
  169. bool GetCastShadows() const { return castShadows_; }
  170. /// Return occluder flag.
  171. bool IsOccluder() const { return occluder_; }
  172. /// Return occludee flag.
  173. bool IsOccludee() const { return occludee_; }
  174. /// Return whether is in view this frame from any viewport camera. Excludes shadow map cameras.
  175. bool IsInView() const;
  176. /// Return whether is in view of a specific camera this frame. Pass in a null camera to allow any camera, including shadow map cameras.
  177. bool IsInView(Camera* camera) const;
  178. /// Return draw call source data.
  179. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  180. /// Set new zone. Zone assignment may optionally be temporary, meaning it needs to be re-evaluated on the next frame.
  181. void SetZone(Zone* zone, bool temporary = false);
  182. /// Set sorting value.
  183. void SetSortValue(float value);
  184. /// Set view-space depth bounds.
  185. void SetMinMaxZ(float minZ, float maxZ)
  186. {
  187. minZ_ = minZ;
  188. maxZ_ = maxZ;
  189. }
  190. /// Mark in view. Also clear the light list.
  191. void MarkInView(const FrameInfo& frame);
  192. /// Mark in view without specifying a camera. Used for shadow casters.
  193. void MarkInView(unsigned frameNumber);
  194. /// Sort and limit per-pixel lights to maximum allowed. Convert extra lights into vertex lights.
  195. void LimitLights();
  196. /// Sort and limit per-vertex lights to maximum allowed.
  197. void LimitVertexLights(bool removeConvertedLights);
  198. /// Set base pass flag for a batch.
  199. void SetBasePass(unsigned batchIndex) { basePassFlags_ |= (1 << batchIndex); }
  200. /// Return octree octant.
  201. Octant* GetOctant() const { return octant_; }
  202. /// Return current zone.
  203. Zone* GetZone() const { return zone_; }
  204. /// Return whether current zone is inconclusive or dirty due to the drawable moving.
  205. bool IsZoneDirty() const { return zoneDirty_; }
  206. /// Return distance from camera.
  207. float GetDistance() const { return distance_; }
  208. /// Return LOD scaled distance from camera.
  209. float GetLodDistance() const { return lodDistance_; }
  210. /// Return sorting value.
  211. float GetSortValue() const { return sortValue_; }
  212. /// Return whether is in view on the current frame. Called by View.
  213. bool IsInView(const FrameInfo& frame, bool anyCamera = false) const;
  214. /// Return whether has a base pass.
  215. bool HasBasePass(unsigned batchIndex) const { return (basePassFlags_ & (1 << batchIndex)) != 0; }
  216. /// Return per-pixel lights.
  217. const PODVector<Light*>& GetLights() const { return lights_; }
  218. /// Return per-vertex lights.
  219. const PODVector<Light*>& GetVertexLights() const { return vertexLights_; }
  220. /// Return the first added per-pixel light.
  221. Light* GetFirstLight() const { return firstLight_; }
  222. /// Return the minimum view-space depth.
  223. float GetMinZ() const { return minZ_; }
  224. /// Return the maximum view-space depth.
  225. float GetMaxZ() const { return maxZ_; }
  226. // Add a per-pixel light affecting the object this frame.
  227. void AddLight(Light* light)
  228. {
  229. if (!firstLight_)
  230. firstLight_ = light;
  231. // Need to store into the light list only if the per-pixel lights are being limited.
  232. // Otherwise recording the first light is enough
  233. if (maxLights_)
  234. lights_.Push(light);
  235. }
  236. // Add a per-vertex light affecting the object this frame.
  237. void AddVertexLight(Light* light)
  238. {
  239. vertexLights_.Push(light);
  240. }
  241. protected:
  242. /// Handle node being assigned.
  243. virtual void OnNodeSet(Node* node);
  244. /// Handle scene being assigned.
  245. virtual void OnSceneSet(Scene* scene);
  246. /// Handle node transform being dirtied.
  247. virtual void OnMarkedDirty(Node* node);
  248. /// Recalculate the world-space bounding box.
  249. virtual void OnWorldBoundingBoxUpdate() = 0;
  250. /// Handle removal from octree.
  251. virtual void OnRemoveFromOctree() { }
  252. /// Add to octree.
  253. void AddToOctree();
  254. /// Remove from octree.
  255. void RemoveFromOctree();
  256. /// Move into another octree octant.
  257. void SetOctant(Octant* octant) { octant_ = octant; }
  258. /// World-space bounding box.
  259. BoundingBox worldBoundingBox_;
  260. /// Local-space bounding box.
  261. BoundingBox boundingBox_;
  262. /// Draw call source data.
  263. Vector<SourceBatch> batches_;
  264. /// Drawable flags.
  265. unsigned char drawableFlags_;
  266. /// Bounding box dirty flag.
  267. bool worldBoundingBoxDirty_;
  268. /// Shadowcaster flag.
  269. bool castShadows_;
  270. /// Occluder flag.
  271. bool occluder_;
  272. /// Occludee flag.
  273. bool occludee_;
  274. /// Octree update queued flag.
  275. bool updateQueued_;
  276. /// Zone inconclusive or dirtied flag.
  277. bool zoneDirty_;
  278. /// Octree octant.
  279. Octant* octant_;
  280. /// Current zone.
  281. Zone* zone_;
  282. /// View mask.
  283. unsigned viewMask_;
  284. /// Light mask.
  285. unsigned lightMask_;
  286. /// Shadow mask.
  287. unsigned shadowMask_;
  288. /// Zone mask.
  289. unsigned zoneMask_;
  290. /// Last visible frame number.
  291. unsigned viewFrameNumber_;
  292. /// Current distance to camera.
  293. float distance_;
  294. /// LOD scaled distance.
  295. float lodDistance_;
  296. /// Draw distance.
  297. float drawDistance_;
  298. /// Shadow distance.
  299. float shadowDistance_;
  300. /// Current sort value.
  301. float sortValue_;
  302. /// Current minimum view space depth.
  303. float minZ_;
  304. /// Current maximum view space depth.
  305. float maxZ_;
  306. /// LOD bias.
  307. float lodBias_;
  308. /// Base pass flags, bit per batch.
  309. unsigned basePassFlags_;
  310. /// Maximum per-pixel lights.
  311. unsigned maxLights_;
  312. /// List of cameras from which is seen on the current frame.
  313. PODVector<Camera*> viewCameras_;
  314. /// First per-pixel light added this frame.
  315. Light* firstLight_;
  316. /// Per-pixel lights affecting this drawable.
  317. PODVector<Light*> lights_;
  318. /// Per-vertex lights affecting this drawable.
  319. PODVector<Light*> vertexLights_;
  320. };
  321. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  322. {
  323. return lhs->GetSortValue() < rhs->GetSortValue();
  324. }
  325. }