Drawable.h 14 KB

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