Drawable.h 13 KB

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