Drawable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Component.h"
  26. #include "GraphicsDefs.h"
  27. #include "Material.h"
  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 = 6;
  38. class Camera;
  39. class Geometry;
  40. class Light;
  41. class OcclusionBuffer;
  42. class Octant;
  43. class RayOctreeQuery;
  44. class Zone;
  45. struct RayQueryResult;
  46. struct WorkItem;
  47. /// Geometry update type.
  48. enum UpdateGeometryType
  49. {
  50. UPDATE_NONE = 0,
  51. UPDATE_MAIN_THREAD,
  52. UPDATE_WORKER_THREAD
  53. };
  54. /// Rendering frame update parameters.
  55. struct FrameInfo
  56. {
  57. /// Frame number.
  58. unsigned frameNumber_;
  59. /// Time elapsed since last frame.
  60. float timeStep_;
  61. /// Viewport size.
  62. IntVector2 viewSize_;
  63. /// Camera being used.
  64. Camera* camera_;
  65. };
  66. /// Source data for a 3D geometry draw call.
  67. struct SourceBatch
  68. {
  69. /// Construct with defaults.
  70. SourceBatch() :
  71. distance_(0.0f),
  72. geometry_(0),
  73. worldTransform_(&Matrix3x4::IDENTITY),
  74. shaderData_(0),
  75. shaderDataSize_(0),
  76. geometryType_(GEOM_STATIC),
  77. overrideView_(false)
  78. {
  79. }
  80. /// Distance from camera.
  81. float distance_;
  82. /// Geometry.
  83. Geometry* geometry_;
  84. /// Material.
  85. SharedPtr<Material> material_;
  86. /// %Object's world transform.
  87. const Matrix3x4* worldTransform_;
  88. /// Vertex shader data.
  89. const float* shaderData_;
  90. /// Vertex shader data size in floats.
  91. unsigned shaderDataSize_;
  92. /// %Geometry type.
  93. GeometryType geometryType_;
  94. /// Override view transform flag.
  95. bool overrideView_;
  96. };
  97. /// Base class for visible components.
  98. class Drawable : public Component
  99. {
  100. OBJECT(Drawable);
  101. friend class Octant;
  102. friend class Octree;
  103. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  104. public:
  105. /// Construct.
  106. Drawable(Context* context);
  107. /// Destruct.
  108. virtual ~Drawable();
  109. /// Register object attributes. Drawable must be registered first.
  110. static void RegisterObject(Context* context);
  111. /// Process octree raycast. May be called from a worker thread.
  112. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  113. /// Update before octree reinsertion. Is called from a worker thread. Needs to be requested with MarkForUpdate().
  114. virtual void Update(const FrameInfo& frame) {}
  115. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  116. virtual void UpdateBatches(const FrameInfo& frame);
  117. /// Prepare geometry for rendering.
  118. virtual void UpdateGeometry(const FrameInfo& frame) {}
  119. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  120. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  121. /// Return number of occlusion geometry triangles.
  122. virtual unsigned GetNumOccluderTriangles() { return 0; }
  123. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  124. virtual bool DrawOcclusion(OcclusionBuffer* buffer) { return true; }
  125. /// Visualize the component as debug geometry.
  126. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  127. /// %Set draw distance.
  128. void SetDrawDistance(float distance);
  129. /// %Set shadow draw distance.
  130. void SetShadowDistance(float distance);
  131. /// %Set LOD bias.
  132. void SetLodBias(float bias);
  133. /// %Set view mask. Is and'ed with camera's view mask to see if the object should be rendered.
  134. void SetViewMask(unsigned mask);
  135. /// %Set light mask. Is and'ed with light's and zone's light mask to see if the object should be lit.
  136. void SetLightMask(unsigned mask);
  137. /// %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.
  138. void SetShadowMask(unsigned mask);
  139. /// %Set zone mask. Is and'ed with zone's zone mask to see if the object should belong to the zone.
  140. void SetZoneMask(unsigned mask);
  141. /// %Set maximum number of per-pixel lights. Default 0 is unlimited.
  142. void SetMaxLights(unsigned num);
  143. /// %Set visible flag.
  144. void SetVisible(bool enable);
  145. /// %Set shadowcaster flag.
  146. void SetCastShadows(bool enable);
  147. /// %Set occlusion flag.
  148. void SetOccluder(bool enable);
  149. /// %Set occludee flag.
  150. void SetOccludee(bool enable);
  151. /// Mark for update before octree reinsertion.
  152. void MarkForUpdate();
  153. /// Return world bounding box.
  154. const BoundingBox& GetWorldBoundingBox();
  155. /// Return drawable flags.
  156. unsigned char GetDrawableFlags() const { return drawableFlags_; }
  157. /// Return draw distance.
  158. float GetDrawDistance() const { return drawDistance_; }
  159. /// Return shadow draw distance.
  160. float GetShadowDistance() const { return shadowDistance_; }
  161. /// Return LOD bias.
  162. float GetLodBias() const { return lodBias_; }
  163. /// Return view mask.
  164. unsigned GetViewMask() const { return viewMask_; }
  165. /// Return light mask.
  166. unsigned GetLightMask() const { return lightMask_; }
  167. /// Return shadow mask.
  168. unsigned GetShadowMask() const { return shadowMask_; }
  169. /// Return zone mask.
  170. unsigned GetZoneMask() const { return zoneMask_; }
  171. /// Return maximum number of per-pixel lights.
  172. unsigned GetMaxLights() const { return maxLights_; }
  173. /// Return visible flag.
  174. bool IsVisible() const { return visible_; }
  175. /// Return shadowcaster flag.
  176. bool GetCastShadows() const { return castShadows_; }
  177. /// Return occluder flag.
  178. bool IsOccluder() const { return occluder_; }
  179. /// Return occludee flag.
  180. bool IsOccludee() const { return occludee_; }
  181. /// Return draw call source data.
  182. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  183. /// %Set new zone.
  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 (either the main camera, or a shadow camera view) this frame.
  190. void MarkInView(const FrameInfo& frame, bool mainView = true);
  191. /// Clear lights and base pass flags for a new frame.
  192. void ClearLights();
  193. /// Add a per-pixel light.
  194. void AddLight(Light* light);
  195. /// Add a per-vertex light.
  196. void AddVertexLight(Light* light);
  197. /// Sort and limit per-pixel lights to maximum allowed. Convert extra lights into vertex lights.
  198. void LimitLights();
  199. /// Sort and limit per-vertex lights to maximum allowed.
  200. void LimitVertexLights();
  201. /// %Set base pass flag for a batch.
  202. void SetBasePass(unsigned batchIndex) { basePassFlags_ |= (1 << batchIndex); }
  203. /// Return octree octant.
  204. Octant* GetOctant() const { return octant_; }
  205. /// Return current zone.
  206. Zone* GetZone() const;
  207. /// Return previous zone.
  208. Zone* GetLastZone() const;
  209. /// Return distance from camera.
  210. float GetDistance() const { return distance_; }
  211. /// Return LOD scaled distance from camera.
  212. float GetLodDistance() const { return lodDistance_; }
  213. /// Return sorting value.
  214. float GetSortValue() const { return sortValue_; }
  215. /// Return whether is in view this frame.
  216. bool IsInView(unsigned frameNumber) const { return viewFrameNumber_ == frameNumber; }
  217. /// Return whether is visible in a specific view this frame.
  218. bool IsInView(const FrameInfo& frame, bool mainView = true) const { return viewFrameNumber_ == frame.frameNumber_ && viewFrame_ == &frame && (!mainView || viewCamera_ == frame.camera_); }
  219. /// Return whether has a base pass.
  220. bool HasBasePass(unsigned batchIndex) const { return (basePassFlags_ & (1 << batchIndex)) != 0; }
  221. /// Return per-pixel lights.
  222. const PODVector<Light*>& GetLights() const { return lights_; }
  223. /// Return per-vertex lights.
  224. const PODVector<Light*>& GetVertexLights() const { return vertexLights_; }
  225. /// Return the first added per-pixel light.
  226. Light* GetFirstLight() const { return firstLight_; }
  227. /// Return the minimum view-space depth.
  228. float GetMinZ() const { return minZ_; }
  229. /// Return the maximum view-space depth.
  230. float GetMaxZ() const { return maxZ_; }
  231. protected:
  232. /// Handle node being assigned.
  233. virtual void OnNodeSet(Node* node);
  234. /// Handle node transform being dirtied.
  235. virtual void OnMarkedDirty(Node* node);
  236. /// Recalculate the world-space bounding box.
  237. virtual void OnWorldBoundingBoxUpdate() = 0;
  238. /// Add to octree.
  239. void AddToOctree();
  240. /// Remove from octree.
  241. void RemoveFromOctree();
  242. /// Move into another octree octant.
  243. void SetOctant(Octant* octant) { octant_ = octant; }
  244. /// World bounding box.
  245. BoundingBox worldBoundingBox_;
  246. /// Draw call source data.
  247. Vector<SourceBatch> batches_;
  248. /// Drawable flags.
  249. unsigned char drawableFlags_;
  250. /// Bounding box dirty flag.
  251. bool worldBoundingBoxDirty_;
  252. /// Visible flag.
  253. bool visible_;
  254. /// Shadowcaster flag.
  255. bool castShadows_;
  256. /// Occluder flag.
  257. bool occluder_;
  258. /// Occludee flag.
  259. bool occludee_;
  260. /// Octree update queued flag.
  261. bool updateQueued_;
  262. /// Octree reinsertion queued flag.
  263. bool reinsertionQueued_;
  264. /// View mask.
  265. unsigned viewMask_;
  266. /// Light mask.
  267. unsigned lightMask_;
  268. /// Shadow mask.
  269. unsigned shadowMask_;
  270. /// Zone mask.
  271. unsigned zoneMask_;
  272. /// Last visible frame number.
  273. unsigned viewFrameNumber_;
  274. /// Current distance to camera.
  275. float distance_;
  276. /// LOD scaled distance.
  277. float lodDistance_;
  278. /// Draw distance.
  279. float drawDistance_;
  280. /// Shadow distance.
  281. float shadowDistance_;
  282. /// Current sort value.
  283. float sortValue_;
  284. /// Current minimum view space depth.
  285. float minZ_;
  286. /// Current maximum view space depth.
  287. float maxZ_;
  288. /// LOD bias.
  289. float lodBias_;
  290. /// Base pass flags.
  291. unsigned basePassFlags_;
  292. /// Maximum lights.
  293. unsigned maxLights_;
  294. /// Octree octant.
  295. Octant* octant_;
  296. /// First per-pixel light added this frame.
  297. Light* firstLight_;
  298. /// Per-pixel lights affecting this drawable.
  299. PODVector<Light*> lights_;
  300. /// Per-vertex lights affecting this drawable.
  301. PODVector<Light*> vertexLights_;
  302. /// Current zone.
  303. WeakPtr<Zone> zone_;
  304. /// Previous zone.
  305. WeakPtr<Zone> lastZone_;
  306. /// Last view's frameinfo. Not safe to dereference.
  307. const FrameInfo* viewFrame_;
  308. /// Last view's camera. Not safe to dereference.
  309. Camera* viewCamera_;
  310. /// Zone assignment temporary flag.
  311. bool temporaryZone_;
  312. };
  313. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  314. {
  315. return lhs->GetSortValue() < rhs->GetSortValue();
  316. }