Drawable.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "OctreeQuery.h"
  30. #include "Scene.h"
  31. #include "Zone.h"
  32. #include "Sort.h"
  33. #include "DebugNew.h"
  34. static const Vector3 DOT_SCALE(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  35. OBJECTTYPESTATIC(Drawable);
  36. Drawable::Drawable(Context* context) :
  37. Component(context),
  38. octant_(0),
  39. drawDistance_(0.0f),
  40. shadowDistance_(0.0f),
  41. lodBias_(1.0f),
  42. viewMask_(DEFAULT_VIEWMASK),
  43. lightMask_(DEFAULT_LIGHTMASK),
  44. zoneMask_(DEFAULT_ZONEMASK),
  45. maxLights_(0),
  46. drawableFlags_(0),
  47. visible_(true),
  48. castShadows_(false),
  49. occluder_(false),
  50. distance_(0.0f),
  51. lodDistance_(0.0f),
  52. sortValue_(0.0f),
  53. viewFrameNumber_(0),
  54. viewCamera_(0),
  55. firstLight_(0),
  56. worldBoundingBoxDirty_(true),
  57. lodLevelsDirty_(true)
  58. {
  59. }
  60. Drawable::~Drawable()
  61. {
  62. RemoveFromOctree();
  63. }
  64. void Drawable::RegisterObject(Context* context)
  65. {
  66. ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0, AM_DEFAULT);
  67. ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  68. ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  69. ACCESSOR_ATTRIBUTE(Drawable, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  70. }
  71. void Drawable::ProcessRayQuery(RayOctreeQuery& query, float initialDistance)
  72. {
  73. // By default just store the result from the bounding box raycast
  74. RayQueryResult result;
  75. result.drawable_ = this;
  76. result.node_ = GetNode();
  77. result.distance_ = initialDistance;
  78. query.result_.Push(result);
  79. }
  80. void Drawable::UpdateDistance(const FrameInfo& frame)
  81. {
  82. distance_ = frame.camera_->GetDistance(GetWorldPosition());
  83. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  84. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  85. if (newLodDistance != lodDistance_)
  86. {
  87. lodDistance_ = newLodDistance;
  88. lodLevelsDirty_ = true;
  89. }
  90. }
  91. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  92. {
  93. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  94. }
  95. void Drawable::SetDrawDistance(float distance)
  96. {
  97. drawDistance_ = distance;
  98. }
  99. void Drawable::SetShadowDistance(float distance)
  100. {
  101. shadowDistance_ = distance;
  102. }
  103. void Drawable::SetLodBias(float bias)
  104. {
  105. lodBias_ = Max(bias, M_EPSILON);
  106. }
  107. void Drawable::SetViewMask(unsigned mask)
  108. {
  109. viewMask_ = mask;
  110. }
  111. void Drawable::SetLightMask(unsigned mask)
  112. {
  113. lightMask_ = mask;
  114. }
  115. void Drawable::SetZoneMask(unsigned mask)
  116. {
  117. zoneMask_ = mask;
  118. // Mark dirty to reset cached zone
  119. OnMarkedDirty(node_);
  120. }
  121. void Drawable::SetMaxLights(unsigned num)
  122. {
  123. maxLights_ = num;
  124. }
  125. void Drawable::SetVisible(bool enable)
  126. {
  127. visible_ = enable;
  128. }
  129. void Drawable::SetCastShadows(bool enable)
  130. {
  131. castShadows_ = enable;
  132. }
  133. void Drawable::SetOccluder(bool enable)
  134. {
  135. occluder_ = enable;
  136. }
  137. void Drawable::MarkForUpdate()
  138. {
  139. if (octant_)
  140. octant_->GetRoot()->QueueUpdate(this);
  141. }
  142. const BoundingBox& Drawable::GetWorldBoundingBox()
  143. {
  144. if (worldBoundingBoxDirty_)
  145. {
  146. OnWorldBoundingBoxUpdate();
  147. worldBoundingBoxDirty_ = false;
  148. }
  149. return worldBoundingBox_;
  150. }
  151. void Drawable::SetZone(Zone* zone)
  152. {
  153. zone_ = zone;
  154. lastZone_ = zone;
  155. }
  156. void Drawable::SetSortValue(float value)
  157. {
  158. sortValue_ = value;
  159. }
  160. void Drawable::MarkInView(const FrameInfo& frame)
  161. {
  162. viewFrameNumber_ = frame.frameNumber_;
  163. viewCamera_ = frame.camera_;
  164. }
  165. void Drawable::MarkInShadowView(const FrameInfo& frame)
  166. {
  167. if (viewFrameNumber_ != frame.frameNumber_)
  168. {
  169. viewFrameNumber_ = frame.frameNumber_;
  170. viewCamera_ = 0;
  171. }
  172. }
  173. void Drawable::ClearLights()
  174. {
  175. for (unsigned i = 0; i < basePassFlags_.Size(); ++i)
  176. basePassFlags_[i] = 0;
  177. firstLight_ = 0;
  178. lights_.Clear();
  179. }
  180. void Drawable::AddLight(Light* light)
  181. {
  182. if (lights_.Empty())
  183. firstLight_ = light;
  184. lights_.Push(light);
  185. }
  186. void Drawable::LimitLights()
  187. {
  188. // Maximum lights value 0 means unlimited
  189. if (!maxLights_)
  190. return;
  191. for (unsigned i = 0; i < lights_.Size(); ++i)
  192. lights_[i]->SetIntensitySortValue(GetWorldBoundingBox());
  193. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  194. // If more lights than allowed, cut the list
  195. if (lights_.Size() > maxLights_)
  196. lights_.Resize(maxLights_);
  197. }
  198. void Drawable::SetBasePass(unsigned batchIndex)
  199. {
  200. unsigned index = batchIndex << 5;
  201. if (basePassFlags_.Size() <= index)
  202. {
  203. unsigned oldSize = basePassFlags_.Size();
  204. basePassFlags_.Resize(index + 1);
  205. for (unsigned i = oldSize; i <= index; ++i)
  206. basePassFlags_[i] = 0;
  207. }
  208. basePassFlags_[index] |= (1 << (batchIndex & 31));
  209. }
  210. Zone* Drawable::GetZone() const
  211. {
  212. return zone_;
  213. }
  214. Zone* Drawable::GetLastZone() const
  215. {
  216. return lastZone_;
  217. }
  218. bool Drawable::IsInView(unsigned frameNumber) const
  219. {
  220. return viewFrameNumber_ == frameNumber;
  221. }
  222. bool Drawable::IsInView(const FrameInfo& frame) const
  223. {
  224. return viewFrameNumber_ == frame.frameNumber_ && viewCamera_ == frame.camera_;
  225. }
  226. bool Drawable::HasBasePass(unsigned batchIndex) const
  227. {
  228. unsigned index = batchIndex >> 5;
  229. if (index < basePassFlags_.Size())
  230. return (basePassFlags_[index] & (1 << (batchIndex & 31))) != 0;
  231. else
  232. return false;
  233. }
  234. void Drawable::OnNodeSet(Node* node)
  235. {
  236. // Insert initially into the root octant
  237. if (node)
  238. {
  239. AddToOctree();
  240. node->AddListener(this);
  241. }
  242. else
  243. RemoveFromOctree();
  244. }
  245. void Drawable::OnMarkedDirty(Node* node)
  246. {
  247. if (node == node_)
  248. {
  249. worldBoundingBoxDirty_ = true;
  250. zone_.Reset();
  251. if (octant_)
  252. octant_->GetRoot()->QueueReinsertion(this);
  253. }
  254. }
  255. void Drawable::AddToOctree()
  256. {
  257. Scene* scene = node_->GetScene();
  258. if (scene)
  259. {
  260. Octree* octree = scene->GetComponent<Octree>();
  261. if (octree)
  262. octree->AddDrawable(this);
  263. }
  264. }
  265. void Drawable::RemoveFromOctree()
  266. {
  267. if (octant_)
  268. {
  269. Octree* octree = octant_->GetRoot();
  270. octree->CancelUpdate(this);
  271. octree->CancelReinsertion(this);
  272. octant_->RemoveDrawable(this);
  273. }
  274. }