Drawable.cpp 8.2 KB

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