Drawable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 DRAWABLES_PER_WORK_ITEM = 16;
  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. /// %Object's world transform.
  82. const Matrix3x4* worldTransform_;
  83. /// Vertex shader data in floats.
  84. const float* shaderData_;
  85. /// Vertex shader data size.
  86. unsigned shaderDataSize_;
  87. /// %Geometry type.
  88. GeometryType geometryType_;
  89. /// Override view transform flag.
  90. bool overrideView_;
  91. };
  92. /// Base class for visible components.
  93. class Drawable : public Component
  94. {
  95. OBJECT(Drawable);
  96. friend class Octant;
  97. friend class Octree;
  98. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  99. public:
  100. /// Construct.
  101. Drawable(Context* context, unsigned char drawableFlags);
  102. /// Destruct.
  103. virtual ~Drawable();
  104. /// Register object attributes. Drawable must be registered first.
  105. static void RegisterObject(Context* context);
  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. Needs to be requested with MarkForUpdate().
  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 visible flag.
  141. void SetVisible(bool enable);
  142. /// Set shadowcaster flag.
  143. void SetCastShadows(bool enable);
  144. /// Set occlusion flag.
  145. void SetOccluder(bool enable);
  146. /// Set occludee flag.
  147. void SetOccludee(bool enable);
  148. /// Mark for update before octree reinsertion.
  149. void MarkForUpdate();
  150. /// Return world 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 visible flag.
  171. bool IsVisible() const { return visible_; }
  172. /// Return shadowcaster flag.
  173. bool GetCastShadows() const { return castShadows_; }
  174. /// Return occluder flag.
  175. bool IsOccluder() const { return occluder_; }
  176. /// Return occludee flag.
  177. bool IsOccludee() const { return occludee_; }
  178. /// Return draw call source data.
  179. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  180. /// Set new zone.
  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. /// Mark in view (either the main camera, or a shadow camera view) this frame.
  187. void MarkInView(const FrameInfo& frame, bool mainView = true);
  188. /// Clear lights and base pass flags for a new frame.
  189. void ClearLights();
  190. /// Add a per-pixel light.
  191. void AddLight(Light* light);
  192. /// Add a per-vertex light.
  193. void AddVertexLight(Light* light);
  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();
  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;
  204. /// Return previous zone.
  205. Zone* GetLastZone() const;
  206. /// Return if zone assignment needs re-evaluation.
  207. bool IsZoneDirty() const { return zoneDirty_; }
  208. /// Return distance from camera.
  209. float GetDistance() const { return distance_; }
  210. /// Return LOD scaled distance from camera.
  211. float GetLodDistance() const { return lodDistance_; }
  212. /// Return sorting value.
  213. float GetSortValue() const { return sortValue_; }
  214. /// Return whether is in view this frame.
  215. bool IsInView(unsigned frameNumber) const { return viewFrameNumber_ == frameNumber; }
  216. /// Return whether is visible in a specific view this frame.
  217. bool IsInView(const FrameInfo& frame, bool mainView = true) const { return viewFrameNumber_ == frame.frameNumber_ && viewFrame_ == &frame && (!mainView || viewCamera_ == frame.camera_); }
  218. /// Return whether has a base pass.
  219. bool HasBasePass(unsigned batchIndex) const { return (basePassFlags_ & (1 << batchIndex)) != 0; }
  220. /// Return per-pixel lights.
  221. const PODVector<Light*>& GetLights() const { return lights_; }
  222. /// Return per-vertex lights.
  223. const PODVector<Light*>& GetVertexLights() const { return vertexLights_; }
  224. /// Return the first added per-pixel light.
  225. Light* GetFirstLight() const { return firstLight_; }
  226. /// Return the minimum view-space depth.
  227. float GetMinZ() const { return minZ_; }
  228. /// Return the maximum view-space depth.
  229. float GetMaxZ() const { return maxZ_; }
  230. protected:
  231. /// Handle node being assigned.
  232. virtual void OnNodeSet(Node* node);
  233. /// Handle node transform being dirtied.
  234. virtual void OnMarkedDirty(Node* node);
  235. /// Recalculate the world-space bounding box.
  236. virtual void OnWorldBoundingBoxUpdate() = 0;
  237. /// Add to octree.
  238. void AddToOctree();
  239. /// Remove from octree.
  240. void RemoveFromOctree();
  241. /// Move into another octree octant.
  242. void SetOctant(Octant* octant) { octant_ = octant; }
  243. /// World bounding box.
  244. BoundingBox worldBoundingBox_;
  245. /// Draw call source data.
  246. Vector<SourceBatch> batches_;
  247. /// Drawable flags.
  248. unsigned char drawableFlags_;
  249. /// Bounding box dirty flag.
  250. bool worldBoundingBoxDirty_;
  251. /// Visible flag.
  252. bool visible_;
  253. /// Shadowcaster flag.
  254. bool castShadows_;
  255. /// Occluder flag.
  256. bool occluder_;
  257. /// Occludee flag.
  258. bool occludee_;
  259. /// Octree update queued flag.
  260. bool updateQueued_;
  261. /// Octree reinsertion queued flag.
  262. bool reinsertionQueued_;
  263. /// View mask.
  264. unsigned viewMask_;
  265. /// Light mask.
  266. unsigned lightMask_;
  267. /// Shadow mask.
  268. unsigned shadowMask_;
  269. /// Zone mask.
  270. unsigned zoneMask_;
  271. /// Last visible frame number.
  272. unsigned viewFrameNumber_;
  273. /// Current distance to camera.
  274. float distance_;
  275. /// LOD scaled distance.
  276. float lodDistance_;
  277. /// Draw distance.
  278. float drawDistance_;
  279. /// Shadow distance.
  280. float shadowDistance_;
  281. /// Current sort value.
  282. float sortValue_;
  283. /// Current minimum view space depth.
  284. float minZ_;
  285. /// Current maximum view space depth.
  286. float maxZ_;
  287. /// LOD bias.
  288. float lodBias_;
  289. /// Base pass flags.
  290. unsigned basePassFlags_;
  291. /// Maximum lights.
  292. unsigned maxLights_;
  293. /// Octree octant.
  294. Octant* octant_;
  295. /// First per-pixel light added this frame.
  296. Light* firstLight_;
  297. /// Per-pixel lights affecting this drawable.
  298. PODVector<Light*> lights_;
  299. /// Per-vertex lights affecting this drawable.
  300. PODVector<Light*> vertexLights_;
  301. /// Current zone.
  302. WeakPtr<Zone> zone_;
  303. /// Previous zone.
  304. WeakPtr<Zone> lastZone_;
  305. /// Last view's frameinfo. Not safe to dereference.
  306. const FrameInfo* viewFrame_;
  307. /// Last view's camera. Not safe to dereference.
  308. Camera* viewCamera_;
  309. /// Zone assignment dirty flag.
  310. bool zoneDirty_;
  311. };
  312. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  313. {
  314. return lhs->GetSortValue() < rhs->GetSortValue();
  315. }
  316. }