Drawable.cpp 9.7 KB

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