Drawable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include "Precompiled.h"
  23. #include "../Graphics/Camera.h"
  24. #include "../Core/Context.h"
  25. #include "../Graphics/DebugRenderer.h"
  26. #include "../IO/Log.h"
  27. #include "../Graphics/Material.h"
  28. #include "../Graphics/Octree.h"
  29. #include "../Graphics/Renderer.h"
  30. #include "../Scene/Scene.h"
  31. #include "../Container/Sort.h"
  32. #include "../Graphics/Zone.h"
  33. #include "../DebugNew.h"
  34. namespace Atomic
  35. {
  36. const char* GEOMETRY_CATEGORY = "Geometry";
  37. SourceBatch::SourceBatch() :
  38. distance_(0.0f),
  39. geometry_(0),
  40. worldTransform_(&Matrix3x4::IDENTITY),
  41. numWorldTransforms_(1),
  42. geometryType_(GEOM_STATIC),
  43. lightmapTilingOffset_(0)
  44. {
  45. }
  46. SourceBatch::~SourceBatch()
  47. {
  48. }
  49. Drawable::Drawable(Context* context, unsigned char drawableFlags) :
  50. Component(context),
  51. drawableFlags_(drawableFlags),
  52. worldBoundingBoxDirty_(true),
  53. castShadows_(false),
  54. occluder_(false),
  55. occludee_(true),
  56. updateQueued_(false),
  57. zoneDirty_(false),
  58. octant_(0),
  59. zone_(0),
  60. viewMask_(DEFAULT_VIEWMASK),
  61. lightMask_(DEFAULT_LIGHTMASK),
  62. shadowMask_(DEFAULT_SHADOWMASK),
  63. zoneMask_(DEFAULT_ZONEMASK),
  64. viewFrameNumber_(0),
  65. distance_(0.0f),
  66. lodDistance_(0.0f),
  67. drawDistance_(0.0f),
  68. shadowDistance_(0.0f),
  69. sortValue_(0.0f),
  70. minZ_(0.0f),
  71. maxZ_(0.0f),
  72. lodBias_(1.0f),
  73. basePassFlags_(0),
  74. maxLights_(0),
  75. firstLight_(0)
  76. {
  77. }
  78. Drawable::~Drawable()
  79. {
  80. RemoveFromOctree();
  81. }
  82. void Drawable::RegisterObject(Context* context)
  83. {
  84. ATTRIBUTE("Max Lights", int, maxLights_, 0, AM_DEFAULT);
  85. ATTRIBUTE("View Mask", int, viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  86. ATTRIBUTE("Light Mask", int, lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  87. ATTRIBUTE("Shadow Mask", int, shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  88. ACCESSOR_ATTRIBUTE("Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  89. }
  90. void Drawable::OnSetEnabled()
  91. {
  92. bool enabled = IsEnabledEffective();
  93. if (enabled && !octant_)
  94. AddToOctree();
  95. else if (!enabled && octant_)
  96. RemoveFromOctree();
  97. }
  98. void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  99. {
  100. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  101. if (distance < query.maxDistance_)
  102. {
  103. RayQueryResult result;
  104. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  105. result.normal_ = -query.ray_.direction_;
  106. result.distance_ = distance;
  107. result.drawable_ = this;
  108. result.node_ = GetNode();
  109. result.subObject_ = M_MAX_UNSIGNED;
  110. results.Push(result);
  111. }
  112. }
  113. void Drawable::Update(const FrameInfo& frame)
  114. {
  115. }
  116. void Drawable::UpdateBatches(const FrameInfo& frame)
  117. {
  118. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  119. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  120. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  121. for (unsigned i = 0; i < batches_.Size(); ++i)
  122. {
  123. batches_[i].distance_ = distance_;
  124. batches_[i].worldTransform_ = &worldTransform;
  125. }
  126. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  127. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  128. if (newLodDistance != lodDistance_)
  129. lodDistance_ = newLodDistance;
  130. }
  131. void Drawable::UpdateGeometry(const FrameInfo& frame)
  132. {
  133. }
  134. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  135. {
  136. // By default return the visible batch geometry
  137. if (batchIndex < batches_.Size())
  138. return batches_[batchIndex].geometry_;
  139. else
  140. return 0;
  141. }
  142. bool Drawable::DrawOcclusion(OcclusionBuffer* buffer)
  143. {
  144. return true;
  145. }
  146. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  147. {
  148. if (debug && IsEnabledEffective())
  149. debug->AddBoundingBox(GetWorldBoundingBox(), Color::GREEN, depthTest);
  150. }
  151. void Drawable::SetDrawDistance(float distance)
  152. {
  153. drawDistance_ = distance;
  154. MarkNetworkUpdate();
  155. }
  156. void Drawable::SetShadowDistance(float distance)
  157. {
  158. shadowDistance_ = distance;
  159. MarkNetworkUpdate();
  160. }
  161. void Drawable::SetLodBias(float bias)
  162. {
  163. lodBias_ = Max(bias, M_EPSILON);
  164. MarkNetworkUpdate();
  165. }
  166. void Drawable::SetViewMask(unsigned mask)
  167. {
  168. viewMask_ = mask;
  169. MarkNetworkUpdate();
  170. }
  171. void Drawable::SetLightMask(unsigned mask)
  172. {
  173. lightMask_ = mask;
  174. MarkNetworkUpdate();
  175. }
  176. void Drawable::SetShadowMask(unsigned mask)
  177. {
  178. shadowMask_ = mask;
  179. MarkNetworkUpdate();
  180. }
  181. void Drawable::SetZoneMask(unsigned mask)
  182. {
  183. zoneMask_ = mask;
  184. // Mark dirty to reset cached zone
  185. OnMarkedDirty(node_);
  186. MarkNetworkUpdate();
  187. }
  188. void Drawable::SetMaxLights(unsigned num)
  189. {
  190. maxLights_ = num;
  191. MarkNetworkUpdate();
  192. }
  193. void Drawable::SetCastShadows(bool enable)
  194. {
  195. castShadows_ = enable;
  196. MarkNetworkUpdate();
  197. }
  198. void Drawable::SetOccluder(bool enable)
  199. {
  200. occluder_ = enable;
  201. MarkNetworkUpdate();
  202. }
  203. void Drawable::SetOccludee(bool enable)
  204. {
  205. if (enable != occludee_)
  206. {
  207. occludee_ = enable;
  208. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  209. if (octant_ && !updateQueued_)
  210. octant_->GetRoot()->QueueUpdate(this);
  211. MarkNetworkUpdate();
  212. }
  213. }
  214. void Drawable::MarkForUpdate()
  215. {
  216. if (!updateQueued_ && octant_)
  217. octant_->GetRoot()->QueueUpdate(this);
  218. }
  219. const BoundingBox& Drawable::GetWorldBoundingBox()
  220. {
  221. if (worldBoundingBoxDirty_)
  222. {
  223. OnWorldBoundingBoxUpdate();
  224. worldBoundingBoxDirty_ = false;
  225. }
  226. return worldBoundingBox_;
  227. }
  228. bool Drawable::IsInView() const
  229. {
  230. // Note: in headless mode there is no renderer subsystem and no view frustum tests are performed, so return
  231. // always false in that case
  232. Renderer* renderer = GetSubsystem<Renderer>();
  233. return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && !viewCameras_.Empty();
  234. }
  235. bool Drawable::IsInView(Camera* camera) const
  236. {
  237. Renderer* renderer = GetSubsystem<Renderer>();
  238. return renderer && viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && (!camera || viewCameras_.Contains(camera));
  239. }
  240. bool Drawable::IsInView(const FrameInfo& frame, bool anyCamera) const
  241. {
  242. return viewFrameNumber_ == frame.frameNumber_ && (anyCamera || viewCameras_.Contains(frame.camera_));
  243. }
  244. void Drawable::SetZone(Zone* zone, bool temporary)
  245. {
  246. zone_ = zone;
  247. // If the zone assignment was temporary (inconclusive) set the dirty flag so that it will be re-evaluated on the next frame
  248. zoneDirty_ = temporary;
  249. }
  250. void Drawable::SetSortValue(float value)
  251. {
  252. sortValue_ = value;
  253. }
  254. void Drawable::MarkInView(const FrameInfo& frame)
  255. {
  256. if (frame.frameNumber_ != viewFrameNumber_)
  257. {
  258. viewFrameNumber_ = frame.frameNumber_;
  259. viewCameras_.Resize(1);
  260. viewCameras_[0] = frame.camera_;
  261. }
  262. else
  263. viewCameras_.Push(frame.camera_);
  264. basePassFlags_ = 0;
  265. firstLight_ = 0;
  266. lights_.Clear();
  267. vertexLights_.Clear();
  268. }
  269. void Drawable::MarkInView(unsigned frameNumber)
  270. {
  271. if (frameNumber != viewFrameNumber_)
  272. {
  273. viewFrameNumber_ = frameNumber;
  274. viewCameras_.Clear();
  275. }
  276. }
  277. void Drawable::LimitLights()
  278. {
  279. // Maximum lights value 0 means unlimited
  280. if (!maxLights_ || lights_.Size() <= maxLights_)
  281. return;
  282. // If more lights than allowed, move to vertex lights and cut the list
  283. const BoundingBox& box = GetWorldBoundingBox();
  284. for (unsigned i = 0; i < lights_.Size(); ++i)
  285. lights_[i]->SetIntensitySortValue(box);
  286. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  287. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  288. lights_.Resize(maxLights_);
  289. }
  290. void Drawable::LimitVertexLights(bool removeConvertedLights)
  291. {
  292. if (removeConvertedLights)
  293. {
  294. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  295. {
  296. if (!vertexLights_[i]->GetPerVertex())
  297. vertexLights_.Erase(i);
  298. }
  299. }
  300. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  301. return;
  302. const BoundingBox& box = GetWorldBoundingBox();
  303. for (unsigned i = 0; i < vertexLights_.Size(); ++i)
  304. vertexLights_[i]->SetIntensitySortValue(box);
  305. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  306. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  307. }
  308. void Drawable::OnNodeSet(Node* node)
  309. {
  310. if (node)
  311. {
  312. AddToOctree();
  313. node->AddListener(this);
  314. }
  315. else
  316. RemoveFromOctree();
  317. }
  318. void Drawable::OnMarkedDirty(Node* node)
  319. {
  320. worldBoundingBoxDirty_ = true;
  321. if (!updateQueued_ && octant_)
  322. octant_->GetRoot()->QueueUpdate(this);
  323. // Mark zone assignment dirty when transform changes
  324. if (node == node_)
  325. zoneDirty_ = true;
  326. }
  327. void Drawable::AddToOctree()
  328. {
  329. // Do not add to octree when disabled
  330. if (!IsEnabledEffective())
  331. return;
  332. Scene* scene = GetScene();
  333. if (scene)
  334. {
  335. Octree* octree = scene->GetComponent<Octree>();
  336. if (octree)
  337. octree->InsertDrawable(this);
  338. else
  339. LOGERROR("No Octree component in scene, drawable will not render");
  340. }
  341. else
  342. {
  343. // We have a mechanism for adding detached nodes to an octree manually, so do not log this error
  344. //LOGERROR("Node is detached from scene, drawable will not render");
  345. }
  346. }
  347. void Drawable::RemoveFromOctree()
  348. {
  349. if (octant_)
  350. {
  351. Octree* octree = octant_->GetRoot();
  352. if (updateQueued_)
  353. octree->CancelUpdate(this);
  354. // Perform subclass specific deinitialization if necessary
  355. OnRemoveFromOctree();
  356. octant_->RemoveDrawable(this);
  357. }
  358. }
  359. }