Drawable.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // Copyright (c) 2008-2015 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. /// %Geometry type.
  91. GeometryType geometryType_;
  92. };
  93. /// Base class for visible components.
  94. class URHO3D_API Drawable : public Component
  95. {
  96. URHO3D_OBJECT(Drawable, Component);
  97. friend class Octant;
  98. friend class Octree;
  99. friend void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex);
  100. public:
  101. /// Construct.
  102. Drawable(Context* context, unsigned char drawableFlags);
  103. /// Destruct.
  104. virtual ~Drawable();
  105. /// Register object attributes. Drawable must be registered first.
  106. static void RegisterObject(Context* context);
  107. /// Handle enabled/disabled state change.
  108. virtual void OnSetEnabled();
  109. /// Process octree raycast. May be called from a worker thread.
  110. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  111. /// Update before octree reinsertion. Is called from a worker thread.
  112. virtual void Update(const FrameInfo& frame);
  113. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  114. virtual void UpdateBatches(const FrameInfo& frame);
  115. /// Prepare geometry for rendering.
  116. virtual void UpdateGeometry(const FrameInfo& frame);
  117. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  118. virtual UpdateGeometryType GetUpdateGeometryType() { return UPDATE_NONE; }
  119. /// Return the geometry for a specific LOD level.
  120. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  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);
  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 shadowcaster flag.
  144. void SetCastShadows(bool enable);
  145. /// Set occlusion flag.
  146. void SetOccluder(bool enable);
  147. /// Set occludee flag.
  148. void SetOccludee(bool enable);
  149. /// Mark for update and octree reinsertion. Update is automatically queued when the drawable's scene node moves or changes scale.
  150. void MarkForUpdate();
  151. /// Return local space bounding box. May not be applicable or properly updated on all drawables.
  152. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  153. /// Return world-space 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 shadowcaster flag.
  174. bool GetCastShadows() const { return castShadows_; }
  175. /// Return occluder flag.
  176. bool IsOccluder() const { return occluder_; }
  177. /// Return occludee flag.
  178. bool IsOccludee() const { return occludee_; }
  179. /// Return whether is in view this frame from any viewport camera. Excludes shadow map cameras.
  180. bool IsInView() const;
  181. /// Return whether is in view of a specific camera this frame. Pass in a null camera to allow any camera, including shadow map cameras.
  182. bool IsInView(Camera* camera) const;
  183. /// Return draw call source data.
  184. const Vector<SourceBatch>& GetBatches() const { return batches_; }
  185. /// Set new zone. Zone assignment may optionally be temporary, meaning it needs to be re-evaluated on the next frame.
  186. void SetZone(Zone* zone, bool temporary = false);
  187. /// Set sorting value.
  188. void SetSortValue(float value);
  189. /// Set view-space depth bounds.
  190. void SetMinMaxZ(float minZ, float maxZ)
  191. {
  192. minZ_ = minZ;
  193. maxZ_ = maxZ;
  194. }
  195. /// Mark in view. Also clear the light list.
  196. void MarkInView(const FrameInfo& frame);
  197. /// Mark in view without specifying a camera. Used for shadow casters.
  198. void MarkInView(unsigned frameNumber);
  199. /// Sort and limit per-pixel lights to maximum allowed. Convert extra lights into vertex lights.
  200. void LimitLights();
  201. /// Sort and limit per-vertex lights to maximum allowed.
  202. void LimitVertexLights(bool removeConvertedLights);
  203. /// Set base pass flag for a batch.
  204. void SetBasePass(unsigned batchIndex) { basePassFlags_ |= (1 << batchIndex); }
  205. /// Return octree octant.
  206. Octant* GetOctant() const { return octant_; }
  207. /// Return current zone.
  208. Zone* GetZone() const { return zone_; }
  209. /// Return whether current zone is inconclusive or dirty due to the drawable moving.
  210. bool IsZoneDirty() const { return zoneDirty_; }
  211. /// Return distance from camera.
  212. float GetDistance() const { return distance_; }
  213. /// Return LOD scaled distance from camera.
  214. float GetLodDistance() const { return lodDistance_; }
  215. /// Return sorting value.
  216. float GetSortValue() const { return sortValue_; }
  217. /// Return whether is in view on the current frame. Called by View.
  218. bool IsInView(const FrameInfo& frame, bool anyCamera = false) const;
  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. // Add a per-pixel light affecting the object this frame.
  232. void AddLight(Light* light)
  233. {
  234. if (!firstLight_)
  235. firstLight_ = light;
  236. // Need to store into the light list only if the per-pixel lights are being limited.
  237. // Otherwise recording the first light is enough
  238. if (maxLights_)
  239. lights_.Push(light);
  240. }
  241. // Add a per-vertex light affecting the object this frame.
  242. void AddVertexLight(Light* light)
  243. {
  244. vertexLights_.Push(light);
  245. }
  246. protected:
  247. /// Handle node being assigned.
  248. virtual void OnNodeSet(Node* node);
  249. /// Handle scene being assigned.
  250. virtual void OnSceneSet(Scene* scene);
  251. /// Handle node transform being dirtied.
  252. virtual void OnMarkedDirty(Node* node);
  253. /// Recalculate the world-space bounding box.
  254. virtual void OnWorldBoundingBoxUpdate() = 0;
  255. /// Handle removal from octree.
  256. virtual void OnRemoveFromOctree() { }
  257. /// Add to octree.
  258. void AddToOctree();
  259. /// Remove from octree.
  260. void RemoveFromOctree();
  261. /// Move into another octree octant.
  262. void SetOctant(Octant* octant) { octant_ = octant; }
  263. /// World-space bounding box.
  264. BoundingBox worldBoundingBox_;
  265. /// Local-space bounding box.
  266. BoundingBox boundingBox_;
  267. /// Draw call source data.
  268. Vector<SourceBatch> batches_;
  269. /// Drawable flags.
  270. unsigned char drawableFlags_;
  271. /// Bounding box dirty flag.
  272. bool worldBoundingBoxDirty_;
  273. /// Shadowcaster flag.
  274. bool castShadows_;
  275. /// Occluder flag.
  276. bool occluder_;
  277. /// Occludee flag.
  278. bool occludee_;
  279. /// Octree update queued flag.
  280. bool updateQueued_;
  281. /// Zone inconclusive or dirtied flag.
  282. bool zoneDirty_;
  283. /// Octree octant.
  284. Octant* octant_;
  285. /// Current zone.
  286. Zone* zone_;
  287. /// View mask.
  288. unsigned viewMask_;
  289. /// Light mask.
  290. unsigned lightMask_;
  291. /// Shadow mask.
  292. unsigned shadowMask_;
  293. /// Zone mask.
  294. unsigned zoneMask_;
  295. /// Last visible frame number.
  296. unsigned viewFrameNumber_;
  297. /// Current distance to camera.
  298. float distance_;
  299. /// LOD scaled distance.
  300. float lodDistance_;
  301. /// Draw distance.
  302. float drawDistance_;
  303. /// Shadow distance.
  304. float shadowDistance_;
  305. /// Current sort value.
  306. float sortValue_;
  307. /// Current minimum view space depth.
  308. float minZ_;
  309. /// Current maximum view space depth.
  310. float maxZ_;
  311. /// LOD bias.
  312. float lodBias_;
  313. /// Base pass flags, bit per batch.
  314. unsigned basePassFlags_;
  315. /// Maximum per-pixel lights.
  316. unsigned maxLights_;
  317. /// List of cameras from which is seen on the current frame.
  318. PODVector<Camera*> viewCameras_;
  319. /// First per-pixel light added this frame.
  320. Light* firstLight_;
  321. /// Per-pixel lights affecting this drawable.
  322. PODVector<Light*> lights_;
  323. /// Per-vertex lights affecting this drawable.
  324. PODVector<Light*> vertexLights_;
  325. };
  326. inline bool CompareDrawables(Drawable* lhs, Drawable* rhs)
  327. {
  328. return lhs->GetSortValue() < rhs->GetSortValue();
  329. }
  330. URHO3D_API bool WriteDrawablesToOBJ(PODVector<Drawable*> drawables, File* outputFile, bool asZUp, bool asRightHanded, bool writeLightmapUV = false);
  331. }