Drawable.h 14 KB

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