Drawable.h 14 KB

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