Drawable.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. visible_(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. 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::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  93. {
  94. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  95. if (distance <= query.maxDistance_)
  96. {
  97. RayQueryResult result;
  98. result.drawable_ = this;
  99. result.node_ = GetNode();
  100. result.distance_ = distance;
  101. result.subObject_ = M_MAX_UNSIGNED;
  102. results.Push(result);
  103. }
  104. }
  105. void Drawable::UpdateBatches(const FrameInfo& frame)
  106. {
  107. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  108. distance_ = frame.camera_->GetDistance(node_->GetWorldPosition());
  109. for (unsigned i = 0; i < batches_.Size(); ++i)
  110. {
  111. batches_[i].distance_ = distance_;
  112. batches_[i].worldTransform_ = &worldTransform;
  113. }
  114. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  115. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  116. if (newLodDistance != lodDistance_)
  117. lodDistance_ = newLodDistance;
  118. }
  119. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  120. {
  121. // By default return the visible batch geometry
  122. if (batchIndex < batches_.Size())
  123. return batches_[batchIndex].geometry_;
  124. else
  125. return 0;
  126. }
  127. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  128. {
  129. if (debug)
  130. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  131. }
  132. void Drawable::SetDrawDistance(float distance)
  133. {
  134. drawDistance_ = distance;
  135. MarkNetworkUpdate();
  136. }
  137. void Drawable::SetShadowDistance(float distance)
  138. {
  139. shadowDistance_ = distance;
  140. MarkNetworkUpdate();
  141. }
  142. void Drawable::SetLodBias(float bias)
  143. {
  144. lodBias_ = Max(bias, M_EPSILON);
  145. MarkNetworkUpdate();
  146. }
  147. void Drawable::SetViewMask(unsigned mask)
  148. {
  149. viewMask_ = mask;
  150. MarkNetworkUpdate();
  151. }
  152. void Drawable::SetLightMask(unsigned mask)
  153. {
  154. lightMask_ = mask;
  155. MarkNetworkUpdate();
  156. }
  157. void Drawable::SetShadowMask(unsigned mask)
  158. {
  159. shadowMask_ = mask;
  160. MarkNetworkUpdate();
  161. }
  162. void Drawable::SetZoneMask(unsigned mask)
  163. {
  164. zoneMask_ = mask;
  165. // Mark dirty to reset cached zone
  166. OnMarkedDirty(node_);
  167. MarkNetworkUpdate();
  168. }
  169. void Drawable::SetMaxLights(unsigned num)
  170. {
  171. maxLights_ = num;
  172. MarkNetworkUpdate();
  173. }
  174. void Drawable::SetVisible(bool enable)
  175. {
  176. visible_ = enable;
  177. MarkNetworkUpdate();
  178. }
  179. void Drawable::SetCastShadows(bool enable)
  180. {
  181. castShadows_ = enable;
  182. MarkNetworkUpdate();
  183. }
  184. void Drawable::SetOccluder(bool enable)
  185. {
  186. occluder_ = enable;
  187. MarkNetworkUpdate();
  188. }
  189. void Drawable::SetOccludee(bool enable)
  190. {
  191. if (enable != occludee_)
  192. {
  193. occludee_ = enable;
  194. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  195. if (octant_ && !reinsertionQueued_)
  196. octant_->GetRoot()->QueueReinsertion(this);
  197. MarkNetworkUpdate();
  198. }
  199. }
  200. void Drawable::MarkForUpdate()
  201. {
  202. if (!updateQueued_ && octant_)
  203. octant_->GetRoot()->QueueUpdate(this);
  204. }
  205. const BoundingBox& Drawable::GetWorldBoundingBox()
  206. {
  207. if (worldBoundingBoxDirty_)
  208. {
  209. OnWorldBoundingBoxUpdate();
  210. worldBoundingBoxDirty_ = false;
  211. }
  212. return worldBoundingBox_;
  213. }
  214. void Drawable::SetZone(Zone* zone, bool temporary)
  215. {
  216. zone_ = zone;
  217. lastZone_ = zone;
  218. // If the zone assignment was temporary (inconclusive) set the dirty flag so that it will be re-evaluated on the next frame
  219. zoneDirty_ = temporary;
  220. }
  221. void Drawable::SetSortValue(float value)
  222. {
  223. sortValue_ = value;
  224. }
  225. void Drawable::SetMinMaxZ(float minZ, float maxZ)
  226. {
  227. minZ_ = minZ;
  228. maxZ_ = maxZ;
  229. }
  230. void Drawable::MarkInView(const FrameInfo& frame, bool mainView)
  231. {
  232. if (mainView)
  233. {
  234. viewFrameNumber_ = frame.frameNumber_;
  235. viewFrame_ = &frame;
  236. viewCamera_ = frame.camera_;
  237. }
  238. else
  239. {
  240. if (viewFrameNumber_ != frame.frameNumber_ || viewFrame_ != &frame)
  241. {
  242. viewFrameNumber_ = frame.frameNumber_;
  243. viewFrame_ = &frame;
  244. viewCamera_ = 0;
  245. }
  246. }
  247. }
  248. void Drawable::ClearLights()
  249. {
  250. basePassFlags_ = 0;
  251. firstLight_ = 0;
  252. lights_.Clear();
  253. vertexLights_.Clear();
  254. }
  255. void Drawable::AddLight(Light* light)
  256. {
  257. if (lights_.Empty())
  258. firstLight_ = light;
  259. lights_.Push(light);
  260. }
  261. void Drawable::AddVertexLight(Light* light)
  262. {
  263. vertexLights_.Push(light);
  264. }
  265. void Drawable::LimitLights()
  266. {
  267. // Maximum lights value 0 means unlimited
  268. if (!maxLights_ || lights_.Size() <= maxLights_)
  269. return;
  270. // If more lights than allowed, move to vertex lights and cut the list
  271. const BoundingBox& box = GetWorldBoundingBox();
  272. for (unsigned i = 0; i < lights_.Size(); ++i)
  273. lights_[i]->SetIntensitySortValue(box);
  274. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  275. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  276. lights_.Resize(maxLights_);
  277. }
  278. void Drawable::LimitVertexLights()
  279. {
  280. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  281. return;
  282. const BoundingBox& box = GetWorldBoundingBox();
  283. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  284. vertexLights_[i]->SetIntensitySortValue(box);
  285. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  286. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  287. }
  288. Zone* Drawable::GetZone() const
  289. {
  290. return zone_;
  291. }
  292. Zone* Drawable::GetLastZone() const
  293. {
  294. return lastZone_;
  295. }
  296. void Drawable::OnNodeSet(Node* node)
  297. {
  298. if (node)
  299. {
  300. AddToOctree();
  301. node->AddListener(this);
  302. }
  303. else
  304. RemoveFromOctree();
  305. }
  306. void Drawable::OnMarkedDirty(Node* node)
  307. {
  308. worldBoundingBoxDirty_ = true;
  309. if (!reinsertionQueued_ && octant_)
  310. octant_->GetRoot()->QueueReinsertion(this);
  311. // Mark zone assignment dirty. Due to possibly being called from a worker thread, it is unsafe to manipulate the Zone weak
  312. // pointer here
  313. if (node == node_)
  314. zoneDirty_ = true;
  315. }
  316. void Drawable::AddToOctree()
  317. {
  318. Scene* scene = GetScene();
  319. if (scene)
  320. {
  321. Octree* octree = scene->GetComponent<Octree>();
  322. if (octree)
  323. {
  324. const BoundingBox& box = GetWorldBoundingBox();
  325. octree->InsertDrawable(this, box.Center(), box.Size());
  326. }
  327. }
  328. }
  329. void Drawable::RemoveFromOctree()
  330. {
  331. if (octant_)
  332. octant_->RemoveDrawable(this);
  333. }
  334. }