Drawable.cpp 10 KB

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