Drawable.h 13 KB

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