Drawable.cpp 11 KB

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