Drawable.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "DebugNew.h"
  33. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  34. OBJECTTYPESTATIC(Drawable);
  35. Drawable::Drawable(Context* context) :
  36. Component(context),
  37. octant_(0),
  38. drawDistance_(0.0f),
  39. shadowDistance_(0.0f),
  40. lodBias_(1.0f),
  41. viewMask_(DEFAULT_VIEWMASK),
  42. lightMask_(DEFAULT_LIGHTMASK),
  43. maxLights_(0),
  44. drawableFlags_(0),
  45. visible_(true),
  46. castShadows_(false),
  47. occluder_(false),
  48. distance_(0.0f),
  49. lodDistance_(0.0f),
  50. sortValue_(0.0f),
  51. viewFrameNumber_(0),
  52. basePassFlags_(0),
  53. viewCamera_(0),
  54. worldBoundingBoxDirty_(true),
  55. lodLevelsDirty_(true)
  56. {
  57. }
  58. Drawable::~Drawable()
  59. {
  60. RemoveFromOctree();
  61. }
  62. void Drawable::RegisterObject(Context* context)
  63. {
  64. ATTRIBUTE(Drawable, VAR_BOOL, "Is Visible", visible_, true);
  65. ATTRIBUTE(Drawable, VAR_BOOL, "Is Occluder", occluder_, false);
  66. ATTRIBUTE(Drawable, VAR_BOOL, "Cast Shadows", castShadows_, false);
  67. ATTRIBUTE(Drawable, VAR_FLOAT, "Draw Distance", drawDistance_, 0.0f);
  68. ATTRIBUTE(Drawable, VAR_FLOAT, "Shadow Distance", shadowDistance_, 0.0f);
  69. ATTRIBUTE(Drawable, VAR_FLOAT, "LOD Bias", lodBias_, 1.0f);
  70. ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK);
  71. ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK);
  72. ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0);
  73. }
  74. void Drawable::ProcessRayQuery(RayOctreeQuery& query, float initialDistance)
  75. {
  76. // By default just store the result from the bounding box raycast
  77. RayQueryResult result;
  78. result.drawable_ = this;
  79. result.node_ = GetNode();
  80. result.distance_ = initialDistance;
  81. query.result_.Push(result);
  82. }
  83. void Drawable::UpdateDistance(const FrameInfo& frame)
  84. {
  85. distance_ = frame.camera_->GetDistance(GetWorldPosition());
  86. float scale = GetWorldBoundingBox().GetSize().DotProduct(dotScale);
  87. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  88. if (newLodDistance != lodDistance_)
  89. {
  90. lodDistance_ = newLodDistance;
  91. lodLevelsDirty_ = true;
  92. }
  93. }
  94. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  95. {
  96. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  97. }
  98. void Drawable::SetDrawDistance(float distance)
  99. {
  100. drawDistance_ = distance;
  101. }
  102. void Drawable::SetShadowDistance(float distance)
  103. {
  104. shadowDistance_ = distance;
  105. }
  106. void Drawable::SetLodBias(float bias)
  107. {
  108. lodBias_ = Max(bias, M_EPSILON);
  109. }
  110. void Drawable::SetViewMask(unsigned mask)
  111. {
  112. viewMask_ = mask;
  113. }
  114. void Drawable::SetLightMask(unsigned mask)
  115. {
  116. lightMask_ = mask;
  117. }
  118. void Drawable::SetMaxLights(unsigned num)
  119. {
  120. maxLights_ = num;
  121. }
  122. void Drawable::SetVisible(bool enable)
  123. {
  124. visible_ = enable;
  125. }
  126. void Drawable::SetCastShadows(bool enable)
  127. {
  128. castShadows_ = enable;
  129. }
  130. void Drawable::SetOccluder(bool enable)
  131. {
  132. occluder_ = enable;
  133. }
  134. void Drawable::MarkForUpdate()
  135. {
  136. if (octant_)
  137. octant_->GetRoot()->QueueUpdate(this);
  138. }
  139. const BoundingBox& Drawable::GetWorldBoundingBox()
  140. {
  141. if (worldBoundingBoxDirty_)
  142. {
  143. OnWorldBoundingBoxUpdate();
  144. worldBoundingBoxDirty_ = false;
  145. }
  146. return worldBoundingBox_;
  147. }
  148. void Drawable::MarkInView(const FrameInfo& frame)
  149. {
  150. viewFrameNumber_ = frame.frameNumber_;
  151. viewCamera_ = frame.camera_;
  152. }
  153. void Drawable::MarkInShadowView(const FrameInfo& frame)
  154. {
  155. if (viewFrameNumber_ != frame.frameNumber_)
  156. {
  157. viewFrameNumber_ = frame.frameNumber_;
  158. viewCamera_ = 0;
  159. }
  160. }
  161. void Drawable::SetSortValue(float value)
  162. {
  163. sortValue_ = value;
  164. }
  165. void Drawable::ClearBasePass()
  166. {
  167. basePassFlags_ = 0;
  168. lights_.Clear();
  169. }
  170. void Drawable::SetBasePass(unsigned batchIndex)
  171. {
  172. basePassFlags_ |= (1 << batchIndex);
  173. }
  174. void Drawable::AddLight(Light* light)
  175. {
  176. lights_.Push(light);
  177. }
  178. void Drawable::LimitLights()
  179. {
  180. Set<Light*> uniqueLights;
  181. const Vector3& worldPos = GetWorldPosition();
  182. for (unsigned i = 0; i < lights_.Size(); ++i)
  183. lights_[i]->SetIntensitySortValue(worldPos);
  184. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  185. for (unsigned i = 0; i < lights_.Size(); ++i)
  186. {
  187. // For split lights, take into account the original light instead, so that we do not get "partial" lighting
  188. Light* originalLight = lights_[i]->GetOriginalLight();
  189. if (originalLight)
  190. uniqueLights.Insert(originalLight);
  191. else
  192. uniqueLights.Insert(lights_[i]);
  193. // Stop when allowed light count exceeded
  194. if (uniqueLights.Size() > maxLights_)
  195. {
  196. lights_.Resize(i);
  197. return;
  198. }
  199. }
  200. }
  201. bool Drawable::IsInView(unsigned frameNumber) const
  202. {
  203. return viewFrameNumber_ == frameNumber;
  204. }
  205. bool Drawable::IsInView(const FrameInfo& frame) const
  206. {
  207. return (viewFrameNumber_ == frame.frameNumber_) && (viewCamera_ == frame.camera_);
  208. }
  209. void Drawable::OnNodeSet(Node* node)
  210. {
  211. // Insert initially into the root octant
  212. if (node)
  213. {
  214. AddToOctree();
  215. node->AddListener(this);
  216. }
  217. else
  218. RemoveFromOctree();
  219. }
  220. void Drawable::OnMarkedDirty(Node* node)
  221. {
  222. if (node == node_)
  223. {
  224. worldBoundingBoxDirty_ = true;
  225. if (octant_)
  226. octant_->GetRoot()->QueueReinsertion(this);
  227. }
  228. }
  229. void Drawable::AddToOctree()
  230. {
  231. Scene* scene = node_->GetScene();
  232. if (scene)
  233. {
  234. Octree* octree = scene->GetComponent<Octree>();
  235. if (octree)
  236. octree->AddDrawable(this);
  237. }
  238. }
  239. void Drawable::RemoveFromOctree()
  240. {
  241. if (octant_)
  242. {
  243. Octree* octree = octant_->GetRoot();
  244. octree->CancelUpdate(this);
  245. octree->CancelReinsertion(this);
  246. octant_->RemoveDrawable(this);
  247. }
  248. }