Drawable.cpp 11 KB

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