Drawable.cpp 9.6 KB

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