Drawable.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "Material.h"
  28. #include "Octree.h"
  29. #include "Scene.h"
  30. #include "Sort.h"
  31. #include "Zone.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. SourceBatch::SourceBatch() :
  36. distance_(0.0f),
  37. geometry_(0),
  38. worldTransform_(&Matrix3x4::IDENTITY),
  39. shaderData_(0),
  40. shaderDataSize_(0),
  41. geometryType_(GEOM_STATIC),
  42. overrideView_(false)
  43. {
  44. }
  45. SourceBatch::~SourceBatch()
  46. {
  47. }
  48. OBJECTTYPESTATIC(Drawable);
  49. Drawable::Drawable(Context* context) :
  50. Component(context),
  51. drawableFlags_(0),
  52. worldBoundingBoxDirty_(true),
  53. visible_(true),
  54. castShadows_(false),
  55. occluder_(false),
  56. occludee_(true),
  57. updateQueued_(false),
  58. reinsertionQueued_(false),
  59. viewMask_(DEFAULT_VIEWMASK),
  60. lightMask_(DEFAULT_LIGHTMASK),
  61. shadowMask_(DEFAULT_SHADOWMASK),
  62. zoneMask_(DEFAULT_ZONEMASK),
  63. viewFrameNumber_(0),
  64. distance_(0.0f),
  65. lodDistance_(0.0f),
  66. drawDistance_(0.0f),
  67. shadowDistance_(0.0f),
  68. sortValue_(0.0f),
  69. minZ_(0.0f),
  70. maxZ_(0.0f),
  71. lodBias_(1.0f),
  72. basePassFlags_(0),
  73. maxLights_(0),
  74. octant_(0),
  75. firstLight_(0),
  76. viewFrame_(0),
  77. viewCamera_(0),
  78. temporaryZone_(false)
  79. {
  80. }
  81. Drawable::~Drawable()
  82. {
  83. RemoveFromOctree();
  84. }
  85. void Drawable::RegisterObject(Context* context)
  86. {
  87. ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0, AM_DEFAULT);
  88. ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  89. ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  90. ATTRIBUTE(Drawable, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  91. ACCESSOR_ATTRIBUTE(Drawable, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  92. }
  93. void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  94. {
  95. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  96. if (distance <= query.maxDistance_)
  97. {
  98. RayQueryResult result;
  99. result.drawable_ = this;
  100. result.node_ = GetNode();
  101. result.distance_ = distance;
  102. result.subObject_ = M_MAX_UNSIGNED;
  103. results.Push(result);
  104. }
  105. }
  106. void Drawable::UpdateBatches(const FrameInfo& frame)
  107. {
  108. distance_ = frame.camera_->GetDistance(node_->GetWorldPosition());
  109. float scale = GetWorldBoundingBox().Size().DotProduct(DOT_SCALE);
  110. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  111. if (newLodDistance != lodDistance_)
  112. lodDistance_ = newLodDistance;
  113. }
  114. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  115. {
  116. // By default return the visible batch geometry
  117. if (batchIndex < batches_.Size())
  118. return batches_[batchIndex].geometry_;
  119. else
  120. return 0;
  121. }
  122. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  123. {
  124. if (debug)
  125. debug->AddBoundingBox(GetWorldBoundingBox(), Color(0.0f, 1.0f, 0.0f), depthTest);
  126. }
  127. void Drawable::SetDrawDistance(float distance)
  128. {
  129. drawDistance_ = distance;
  130. MarkNetworkUpdate();
  131. }
  132. void Drawable::SetShadowDistance(float distance)
  133. {
  134. shadowDistance_ = distance;
  135. MarkNetworkUpdate();
  136. }
  137. void Drawable::SetLodBias(float bias)
  138. {
  139. lodBias_ = Max(bias, M_EPSILON);
  140. MarkNetworkUpdate();
  141. }
  142. void Drawable::SetViewMask(unsigned mask)
  143. {
  144. viewMask_ = mask;
  145. MarkNetworkUpdate();
  146. }
  147. void Drawable::SetLightMask(unsigned mask)
  148. {
  149. lightMask_ = mask;
  150. MarkNetworkUpdate();
  151. }
  152. void Drawable::SetShadowMask(unsigned mask)
  153. {
  154. shadowMask_ = mask;
  155. MarkNetworkUpdate();
  156. }
  157. void Drawable::SetZoneMask(unsigned mask)
  158. {
  159. zoneMask_ = mask;
  160. // Mark dirty to reset cached zone
  161. OnMarkedDirty(node_);
  162. MarkNetworkUpdate();
  163. }
  164. void Drawable::SetMaxLights(unsigned num)
  165. {
  166. maxLights_ = num;
  167. MarkNetworkUpdate();
  168. }
  169. void Drawable::SetVisible(bool enable)
  170. {
  171. visible_ = enable;
  172. MarkNetworkUpdate();
  173. }
  174. void Drawable::SetCastShadows(bool enable)
  175. {
  176. castShadows_ = enable;
  177. MarkNetworkUpdate();
  178. }
  179. void Drawable::SetOccluder(bool enable)
  180. {
  181. occluder_ = enable;
  182. MarkNetworkUpdate();
  183. }
  184. void Drawable::SetOccludee(bool enable)
  185. {
  186. if (enable != occludee_)
  187. {
  188. occludee_ = enable;
  189. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  190. if (octant_ && !reinsertionQueued_)
  191. octant_->GetRoot()->QueueReinsertion(this);
  192. MarkNetworkUpdate();
  193. }
  194. }
  195. void Drawable::MarkForUpdate()
  196. {
  197. if (octant_ && !updateQueued_)
  198. octant_->GetRoot()->QueueUpdate(this);
  199. }
  200. const BoundingBox& Drawable::GetWorldBoundingBox()
  201. {
  202. if (worldBoundingBoxDirty_)
  203. {
  204. OnWorldBoundingBoxUpdate();
  205. worldBoundingBoxDirty_ = false;
  206. }
  207. return worldBoundingBox_;
  208. }
  209. void Drawable::SetZone(Zone* zone, bool temporary)
  210. {
  211. zone_ = zone;
  212. lastZone_ = zone;
  213. temporaryZone_ = temporary;
  214. }
  215. void Drawable::SetSortValue(float value)
  216. {
  217. sortValue_ = value;
  218. }
  219. void Drawable::SetMinMaxZ(float minZ, float maxZ)
  220. {
  221. minZ_ = minZ;
  222. maxZ_ = maxZ;
  223. }
  224. void Drawable::MarkInView(const FrameInfo& frame, bool mainView)
  225. {
  226. if (mainView)
  227. {
  228. // Reset zone assignment now if was temporary
  229. if (temporaryZone_)
  230. {
  231. zone_.Reset();
  232. temporaryZone_ = false;
  233. }
  234. viewFrameNumber_ = frame.frameNumber_;
  235. viewFrame_ = &frame;
  236. viewCamera_ = frame.camera_;
  237. }
  238. else
  239. {
  240. if (viewFrameNumber_ != frame.frameNumber_ || viewFrame_ != &frame)
  241. {
  242. viewFrameNumber_ = frame.frameNumber_;
  243. viewFrame_ = &frame;
  244. viewCamera_ = 0;
  245. }
  246. }
  247. }
  248. void Drawable::ClearLights()
  249. {
  250. basePassFlags_ = 0;
  251. firstLight_ = 0;
  252. lights_.Clear();
  253. vertexLights_.Clear();
  254. }
  255. void Drawable::AddLight(Light* light)
  256. {
  257. if (lights_.Empty())
  258. firstLight_ = light;
  259. lights_.Push(light);
  260. }
  261. void Drawable::AddVertexLight(Light* light)
  262. {
  263. vertexLights_.Push(light);
  264. }
  265. void Drawable::LimitLights()
  266. {
  267. // Maximum lights value 0 means unlimited
  268. if (!maxLights_ || lights_.Size() <= maxLights_)
  269. return;
  270. // If more lights than allowed, move to vertex lights and cut the list
  271. const BoundingBox& box = GetWorldBoundingBox();
  272. for (unsigned i = 0; i < lights_.Size(); ++i)
  273. lights_[i]->SetIntensitySortValue(box);
  274. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  275. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  276. lights_.Resize(maxLights_);
  277. }
  278. void Drawable::LimitVertexLights()
  279. {
  280. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  281. return;
  282. const BoundingBox& box = GetWorldBoundingBox();
  283. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  284. vertexLights_[i]->SetIntensitySortValue(box);
  285. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  286. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  287. }
  288. Zone* Drawable::GetZone() const
  289. {
  290. return zone_;
  291. }
  292. Zone* Drawable::GetLastZone() const
  293. {
  294. return lastZone_;
  295. }
  296. void Drawable::OnNodeSet(Node* node)
  297. {
  298. if (node)
  299. {
  300. AddToOctree();
  301. node->AddListener(this);
  302. }
  303. else
  304. RemoveFromOctree();
  305. }
  306. void Drawable::OnMarkedDirty(Node* node)
  307. {
  308. worldBoundingBoxDirty_ = true;
  309. if (octant_ && !reinsertionQueued_)
  310. octant_->GetRoot()->QueueReinsertion(this);
  311. if (node == node_)
  312. zone_.Reset();
  313. }
  314. void Drawable::AddToOctree()
  315. {
  316. Scene* scene = GetScene();
  317. if (scene)
  318. {
  319. Octree* octree = scene->GetComponent<Octree>();
  320. if (octree)
  321. {
  322. const BoundingBox& box = GetWorldBoundingBox();
  323. octree->InsertDrawable(this, box.Center(), box.Size());
  324. }
  325. }
  326. }
  327. void Drawable::RemoveFromOctree()
  328. {
  329. if (octant_)
  330. octant_->RemoveDrawable(this);
  331. }
  332. }