Drawable.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Camera.h"
  25. #include "Context.h"
  26. #include "DebugRenderer.h"
  27. #include "Light.h"
  28. #include "Octree.h"
  29. #include "Scene.h"
  30. #include "Sort.h"
  31. #include "Zone.h"
  32. #include "DebugNew.h"
  33. static const Vector3 DOT_SCALE(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  34. OBJECTTYPESTATIC(Drawable);
  35. Drawable::Drawable(Context* context) :
  36. Component(context),
  37. drawableFlags_(0),
  38. worldBoundingBoxDirty_(true),
  39. visible_(true),
  40. castShadows_(false),
  41. occluder_(false),
  42. occludee_(true),
  43. updateQueued_(false),
  44. reinsertionQueued_(false),
  45. viewMask_(DEFAULT_VIEWMASK),
  46. lightMask_(DEFAULT_LIGHTMASK),
  47. shadowMask_(DEFAULT_SHADOWMASK),
  48. zoneMask_(DEFAULT_ZONEMASK),
  49. viewFrameNumber_(0),
  50. distance_(0.0f),
  51. lodDistance_(0.0f),
  52. drawDistance_(0.0f),
  53. shadowDistance_(0.0f),
  54. sortValue_(0.0f),
  55. minZ_(0.0f),
  56. maxZ_(0.0f),
  57. lodBias_(1.0f),
  58. basePassFlags_(0),
  59. maxLights_(0),
  60. octant_(0),
  61. firstLight_(0),
  62. viewFrame_(0),
  63. viewCamera_(0),
  64. temporaryZone_(false)
  65. {
  66. }
  67. Drawable::~Drawable()
  68. {
  69. RemoveFromOctree();
  70. }
  71. void Drawable::RegisterObject(Context* context)
  72. {
  73. ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0, AM_DEFAULT);
  74. ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  75. ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  76. ATTRIBUTE(Drawable, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  77. ACCESSOR_ATTRIBUTE(Drawable, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  78. }
  79. void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  80. {
  81. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  82. if (distance <= query.maxDistance_)
  83. {
  84. RayQueryResult result;
  85. result.drawable_ = this;
  86. result.node_ = GetNode();
  87. result.distance_ = distance;
  88. result.subObject_ = M_MAX_UNSIGNED;
  89. results.Push(result);
  90. }
  91. }
  92. void Drawable::UpdateBatches(const FrameInfo& frame)
  93. {
  94. distance_ = frame.camera_->GetDistance(node_->GetWorldPosition());
  95. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  96. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  97. if (newLodDistance != lodDistance_)
  98. lodDistance_ = newLodDistance;
  99. }
  100. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  101. {
  102. // By default return just the batch geometry
  103. if (batchIndex < batches_.Size())
  104. return batches_[batchIndex].geometry_;
  105. else
  106. return 0;
  107. }
  108. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  109. {
  110. if (debug)
  111. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  112. }
  113. void Drawable::SetDrawDistance(float distance)
  114. {
  115. drawDistance_ = distance;
  116. MarkNetworkUpdate();
  117. }
  118. void Drawable::SetShadowDistance(float distance)
  119. {
  120. shadowDistance_ = distance;
  121. MarkNetworkUpdate();
  122. }
  123. void Drawable::SetLodBias(float bias)
  124. {
  125. lodBias_ = Max(bias, M_EPSILON);
  126. MarkNetworkUpdate();
  127. }
  128. void Drawable::SetViewMask(unsigned mask)
  129. {
  130. viewMask_ = mask;
  131. MarkNetworkUpdate();
  132. }
  133. void Drawable::SetLightMask(unsigned mask)
  134. {
  135. lightMask_ = mask;
  136. MarkNetworkUpdate();
  137. }
  138. void Drawable::SetShadowMask(unsigned mask)
  139. {
  140. shadowMask_ = mask;
  141. MarkNetworkUpdate();
  142. }
  143. void Drawable::SetZoneMask(unsigned mask)
  144. {
  145. zoneMask_ = mask;
  146. // Mark dirty to reset cached zone
  147. OnMarkedDirty(node_);
  148. MarkNetworkUpdate();
  149. }
  150. void Drawable::SetMaxLights(unsigned num)
  151. {
  152. maxLights_ = num;
  153. MarkNetworkUpdate();
  154. }
  155. void Drawable::SetVisible(bool enable)
  156. {
  157. visible_ = enable;
  158. MarkNetworkUpdate();
  159. }
  160. void Drawable::SetCastShadows(bool enable)
  161. {
  162. castShadows_ = enable;
  163. MarkNetworkUpdate();
  164. }
  165. void Drawable::SetOccluder(bool enable)
  166. {
  167. occluder_ = enable;
  168. MarkNetworkUpdate();
  169. }
  170. void Drawable::SetOccludee(bool enable)
  171. {
  172. if (enable != occludee_)
  173. {
  174. occludee_ = enable;
  175. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  176. if (octant_ && !reinsertionQueued_)
  177. octant_->GetRoot()->QueueReinsertion(this);
  178. MarkNetworkUpdate();
  179. }
  180. }
  181. void Drawable::MarkForUpdate()
  182. {
  183. if (octant_ && !updateQueued_)
  184. octant_->GetRoot()->QueueUpdate(this);
  185. }
  186. const BoundingBox& Drawable::GetWorldBoundingBox()
  187. {
  188. if (worldBoundingBoxDirty_)
  189. {
  190. OnWorldBoundingBoxUpdate();
  191. worldBoundingBoxDirty_ = false;
  192. }
  193. return worldBoundingBox_;
  194. }
  195. void Drawable::SetZone(Zone* zone, bool temporary)
  196. {
  197. zone_ = zone;
  198. lastZone_ = zone;
  199. temporaryZone_ = temporary;
  200. }
  201. void Drawable::SetSortValue(float value)
  202. {
  203. sortValue_ = value;
  204. }
  205. void Drawable::SetMinMaxZ(float minZ, float maxZ)
  206. {
  207. minZ_ = minZ;
  208. maxZ_ = maxZ;
  209. }
  210. void Drawable::MarkInView(const FrameInfo& frame, bool mainView)
  211. {
  212. if (mainView)
  213. {
  214. // Reset zone assignment now if was temporary
  215. if (temporaryZone_)
  216. {
  217. zone_.Reset();
  218. temporaryZone_ = false;
  219. }
  220. viewFrameNumber_ = frame.frameNumber_;
  221. viewFrame_ = &frame;
  222. viewCamera_ = frame.camera_;
  223. }
  224. else
  225. {
  226. if (viewFrameNumber_ != frame.frameNumber_ || viewFrame_ != &frame)
  227. {
  228. viewFrameNumber_ = frame.frameNumber_;
  229. viewFrame_ = &frame;
  230. viewCamera_ = 0;
  231. }
  232. }
  233. }
  234. void Drawable::ClearLights()
  235. {
  236. basePassFlags_ = 0;
  237. firstLight_ = 0;
  238. lights_.Clear();
  239. vertexLights_.Clear();
  240. }
  241. void Drawable::AddLight(Light* light)
  242. {
  243. if (lights_.Empty())
  244. firstLight_ = light;
  245. lights_.Push(light);
  246. }
  247. void Drawable::AddVertexLight(Light* light)
  248. {
  249. vertexLights_.Push(light);
  250. }
  251. void Drawable::LimitLights()
  252. {
  253. // Maximum lights value 0 means unlimited
  254. if (!maxLights_ || lights_.Size() <= maxLights_)
  255. return;
  256. // If more lights than allowed, move to vertex lights and cut the list
  257. const BoundingBox& box = GetWorldBoundingBox();
  258. for (unsigned i = 0; i < lights_.Size(); ++i)
  259. lights_[i]->SetIntensitySortValue(box);
  260. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  261. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  262. lights_.Resize(maxLights_);
  263. }
  264. void Drawable::LimitVertexLights()
  265. {
  266. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  267. return;
  268. const BoundingBox& box = GetWorldBoundingBox();
  269. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  270. vertexLights_[i]->SetIntensitySortValue(box);
  271. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  272. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  273. }
  274. Zone* Drawable::GetZone() const
  275. {
  276. return zone_;
  277. }
  278. Zone* Drawable::GetLastZone() const
  279. {
  280. return lastZone_;
  281. }
  282. void Drawable::OnNodeSet(Node* node)
  283. {
  284. if (node)
  285. {
  286. AddToOctree();
  287. node->AddListener(this);
  288. }
  289. else
  290. RemoveFromOctree();
  291. }
  292. void Drawable::OnMarkedDirty(Node* node)
  293. {
  294. worldBoundingBoxDirty_ = true;
  295. if (octant_ && !reinsertionQueued_)
  296. octant_->GetRoot()->QueueReinsertion(this);
  297. if (node == node_)
  298. zone_.Reset();
  299. }
  300. void Drawable::AddToOctree()
  301. {
  302. Scene* scene = GetScene();
  303. if (scene)
  304. {
  305. Octree* octree = scene->GetComponent<Octree>();
  306. if (octree)
  307. {
  308. const BoundingBox& box = GetWorldBoundingBox();
  309. octree->InsertDrawable(this, box.Center(), box.Size());
  310. }
  311. }
  312. }
  313. void Drawable::RemoveFromOctree()
  314. {
  315. if (octant_)
  316. octant_->RemoveDrawable(this);
  317. }