Drawable.cpp 8.6 KB

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