Drawable.cpp 8.7 KB

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