Drawable.cpp 9.4 KB

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