Drawable.h 14 KB

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