Drawable.cpp 7.6 KB

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