Drawable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Copyright (c) 2008-2014 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 "BoundingBox.h"
  24. #include "Component.h"
  25. #include "GraphicsDefs.h"
  26. #include "HashSet.h"
  27. namespace Urho3D
  28. {
  29. static const unsigned DRAWABLE_GEOMETRY = 0x1;
  30. static const unsigned DRAWABLE_LIGHT = 0x2;
  31. static const unsigned DRAWABLE_ZONE = 0x4;
  32. static const unsigned DRAWABLE_RENDERER2D = 0x8;
  33. static const unsigned DRAWABLE_ANY = 0xff;
  34. static const unsigned DEFAULT_VIEWMASK = M_MAX_UNSIGNED;
  35. static const unsigned DEFAULT_LIGHTMASK = M_MAX_UNSIGNED;
  36. static const unsigned DEFAULT_SHADOWMASK = M_MAX_UNSIGNED;
  37. static const unsigned DEFAULT_ZONEMASK = M_MAX_UNSIGNED;
  38. static const int MAX_VERTEX_LIGHTS = 4;
  39. static const float ANIMATION_LOD_BASESCALE = 2500.0f;
  40. class Camera;
  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. /// Source data for a 3D geometry draw call.
  70. struct SourceBatch
  71. {
  72. /// Construct with defaults.
  73. SourceBatch();
  74. /// Destruct.
  75. ~SourceBatch();
  76. /// Distance from camera.
  77. float distance_;
  78. /// Geometry.
  79. Geometry* geometry_;
  80. /// Material.
  81. SharedPtr<Material> material_;
  82. /// World transform(s). For a skinned model, these are the bone transforms.
  83. const Matrix3x4* worldTransform_;
  84. /// Number of world transforms.
  85. unsigned numWorldTransforms_;
  86. /// %Geometry type.
  87. GeometryType geometryType_;
  88. /// Override view transform flag.
  89. bool overrideView_;
  90. };
  91. /// Base class for visible components.
  92. class URHO3D_API Drawable : public Component
  93. {
  94. OBJECT(Drawable);
  95. friend class Octant;
  96. friend class Octree;
  97. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  98. public:
  99. /// Construct.
  100. Drawable(Context* context, unsigned char drawableFlags);
  101. /// Destruct.
  102. virtual ~Drawable();
  103. /// Register object attributes. Drawable must be registered first.
  104. static void RegisterObject(Context* context);
  105. /// Handle enabled/disabled state change.
  106. virtual void OnSetEnabled();
  107. /// Process octree raycast. May be called from a worker thread.
  108. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  109. /// Update before octree reinsertion. Is called from a worker thread.
  110. virtual void Update(const FrameInfo& frame);
  111. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  112. virtual void UpdateBatches(const FrameInfo& frame);
  113. /// Prepare geometry for rendering.
  114. virtual void UpdateGeometry(const FrameInfo& frame);
  115. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  116. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  117. /// Return the geometry for a specific LOD level.
  118. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  119. /// Return number of occlusion geometry triangles.
  120. virtual unsigned GetNumOccluderTriangles() { return 0; }
  121. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  122. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  123. /// Visualize the component as debug geometry.
  124. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  125. /// Set draw distance.
  126. void SetDrawDistance(float distance);
  127. /// Set shadow draw distance.
  128. void SetShadowDistance(float distance);
  129. /// Set LOD bias.
  130. void SetLodBias(float bias);
  131. /// Set view mask. Is and'ed with camera's view mask to see if the object should be rendered.
  132. void SetViewMask(unsigned mask);
  133. /// Set light mask. Is and'ed with light's and zone's light mask to see if the object should be lit.
  134. void SetLightMask(unsigned mask);
  135. /// 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.
  136. void SetShadowMask(unsigned mask);
  137. /// Set zone mask. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  138. void SetZoneMask(unsigned mask);
  139. /// Set maximum number of per-pixel lights. Default 0 is unlimited.
  140. void SetMaxLights(unsigned num);
  141. /// Set shadowcaster flag.
  142. void SetCastShadows(bool enable);
  143. /// Set occlusion flag.
  144. void SetOccluder(bool enable);
  145. /// Set occludee flag.
  146. void SetOccludee(bool enable);
  147. /// Mark for update and octree reinsertion. Update is automatically queued when the drawable's scene node moves or changes scale.
  148. void MarkForUpdate();
  149. /// Return local space bounding box. May not be applicable or properly updated on all drawables.
  150. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  151. /// Return world-space bounding box.
  152. const BoundingBox& GetWorldBoundingBox();
  153. /// Return drawable flags.
  154. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  155. /// Return draw distance.
  156. float GetDrawDistance() const { return drawDistance_; }
  157. /// Return shadow draw distance.
  158. float GetShadowDistance() const { return shadowDistance_; }
  159. /// Return LOD bias.
  160. float GetLodBias() const { return lodBias_; }
  161. /// Return view mask.
  162. unsigned GetViewMask() const { return viewMask_; }
  163. /// Return light mask.
  164. unsigned GetLightMask() const { return lightMask_; }
  165. /// Return shadow mask.
  166. unsigned GetShadowMask() const { return shadowMask_; }
  167. /// Return zone mask.
  168. unsigned GetZoneMask() const { return zoneMask_; }
  169. /// Return maximum number of per-pixel lights.
  170. unsigned GetMaxLights() const { return maxLights_; }
  171. /// Return shadowcaster flag.
  172. bool GetCastShadows() const { return castShadows_; }
  173. /// Return occluder flag.
  174. bool IsOccluder() const { return occluder_; }
  175. /// Return occludee flag.
  176. bool IsOccludee() const { return occludee_; }
  177. /// Return whether is in view this frame from any viewport camera. Excludes shadow map cameras.
  178. bool IsInView() const;
  179. /// Return whether is in view of a specific camera this frame. Pass in a null camera to allow any camera, including shadow map cameras.
  180. bool IsInView(Camera* camera) const;
  181. /// Return draw call source data.
  182. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  183. /// Set new zone. Zone assignment may optionally be temporary, meaning it needs to be re-evaluated on the next frame.
  184. void SetZone(Zone* zone, bool temporary = false);
  185. /// Set sorting value.
  186. void SetSortValue(float value);
  187. /// Set view-space depth bounds.
  188. void SetMinMaxZ(float minZ, float maxZ);
  189. /// Mark in view.
  190. void MarkInView(const FrameInfo& frame);
  191. /// Mark in view of a specific camera. Specify null camera to update just the frame number.
  192. void MarkInView(unsigned frameNumber, Camera* camera);
  193. /// Sort and limit per-pixel lights to maximum allowed. Convert extra lights into vertex lights.
  194. void LimitLights();
  195. /// Sort and limit per-vertex lights to maximum allowed.
  196. void LimitVertexLights();
  197. /// Set base pass flag for a batch.
  198. void SetBasePass(unsigned batchIndex) { basePassFlags_ |= (1 << batchIndex); }
  199. /// Return octree octant.
  200. Octant* GetOctant() const { return octant_; }
  201. /// Return current zone.
  202. Zone* GetZone() const { return zone_; }
  203. /// Return whether current zone is inconclusive or dirty due to the drawable moving.
  204. bool IsZoneDirty() const { return zoneDirty_; }
  205. /// Return distance from camera.
  206. float GetDistance() const { return distance_; }
  207. /// Return LOD scaled distance from camera.
  208. float GetLodDistance() const { return lodDistance_; }
  209. /// Return sorting value.
  210. float GetSortValue() const { return sortValue_; }
  211. /// Return whether is in view on the current frame. Called by View.
  212. bool IsInView(const FrameInfo& frame, bool anyCamera = false) const;
  213. /// Return whether has a base pass.
  214. bool HasBasePass(unsigned batchIndex) const { return (basePassFlags_ & (1 << batchIndex)) != 0; }
  215. /// Return per-pixel lights.
  216. const PODVector<Light*>& GetLights() const { return lights_; }
  217. /// Return per-vertex lights.
  218. const PODVector<Light*>& GetVertexLights() const { return vertexLights_; }
  219. /// Return the first added per-pixel light.
  220. Light* GetFirstLight() const { return firstLight_; }
  221. /// Return the minimum view-space depth.
  222. float GetMinZ() const { return minZ_; }
  223. /// Return the maximum view-space depth.
  224. float GetMaxZ() const { return maxZ_; }
  225. // Clear the frame's light list.
  226. void ClearLights()
  227. {
  228. basePassFlags_ = 0;
  229. firstLight_ = 0;
  230. lights_.Clear();
  231. vertexLights_.Clear();
  232. }
  233. // Add a per-pixel light affecting the object this frame.
  234. void AddLight(Light* light)
  235. {
  236. if (lights_.Empty())
  237. firstLight_ = light;
  238. lights_.Push(light);
  239. }
  240. // Add a per-vertex light affecting the object this frame.
  241. void AddVertexLight(Light* light)
  242. {
  243. vertexLights_.Push(light);
  244. }
  245. protected:
  246. /// Handle node being assigned.
  247. virtual void OnNodeSet(Node* node);
  248. /// Handle node transform being dirtied.
  249. virtual void OnMarkedDirty(Node* node);
  250. /// Recalculate the world-space bounding box.
  251. virtual void OnWorldBoundingBoxUpdate() = 0;
  252. /// Handle removal from octree.
  253. virtual void OnRemoveFromOctree() {}
  254. /// Add to octree.
  255. void AddToOctree();
  256. /// Remove from octree.
  257. void RemoveFromOctree();
  258. /// Move into another octree octant.
  259. void SetOctant(Octant* octant) { octant_ = octant; }
  260. /// World-space bounding box.
  261. BoundingBox worldBoundingBox_;
  262. /// Local-space bounding box.
  263. BoundingBox boundingBox_;
  264. /// Draw call source data.
  265. Vector<SourceBatch> batches_;
  266. /// Drawable flags.
  267. unsigned char drawableFlags_;
  268. /// Bounding box dirty flag.
  269. bool worldBoundingBoxDirty_;
  270. /// Shadowcaster flag.
  271. bool castShadows_;
  272. /// Occluder flag.
  273. bool occluder_;
  274. /// Occludee flag.
  275. bool occludee_;
  276. /// Octree update queued flag.
  277. bool updateQueued_;
  278. /// View mask.
  279. unsigned viewMask_;
  280. /// Light mask.
  281. unsigned lightMask_;
  282. /// Shadow mask.
  283. unsigned shadowMask_;
  284. /// Zone mask.
  285. unsigned zoneMask_;
  286. /// Last visible frame number.
  287. unsigned viewFrameNumber_;
  288. /// Current distance to camera.
  289. float distance_;
  290. /// LOD scaled distance.
  291. float lodDistance_;
  292. /// Draw distance.
  293. float drawDistance_;
  294. /// Shadow distance.
  295. float shadowDistance_;
  296. /// Current sort value.
  297. float sortValue_;
  298. /// Current minimum view space depth.
  299. float minZ_;
  300. /// Current maximum view space depth.
  301. float maxZ_;
  302. /// LOD bias.
  303. float lodBias_;
  304. /// Base pass flags.
  305. unsigned basePassFlags_;
  306. /// Maximum lights.
  307. unsigned maxLights_;
  308. /// Octree octant.
  309. Octant* octant_;
  310. /// First per-pixel light added this frame.
  311. Light* firstLight_;
  312. /// Per-pixel lights affecting this drawable.
  313. PODVector<Light*> lights_;
  314. /// Per-vertex lights affecting this drawable.
  315. PODVector<Light*> vertexLights_;
  316. /// Current zone.
  317. Zone* zone_;
  318. /// Zone inconclusive or dirtied flag.
  319. bool zoneDirty_;
  320. /// Set of cameras from which is seen on the current frame.
  321. HashSet<Camera*> viewCameras_;
  322. };
  323. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  324. {
  325. return lhs->GetSortValue() < rhs->GetSortValue();
  326. }
  327. }