Drawable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Camera.h"
  24. #include "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Log.h"
  27. #include "Material.h"
  28. #include "Octree.h"
  29. #include "Renderer.h"
  30. #include "Scene.h"
  31. #include "Sort.h"
  32. #include "Zone.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. const char* GEOMETRY_CATEGORY = "Geometry";
  37. SourceBatch::SourceBatch() :
  38. distance_(0.0f),
  39. geometry_(0),
  40. worldTransform_(&Matrix3x4::IDENTITY),
  41. numWorldTransforms_(1),
  42. geometryType_(GEOM_STATIC),
  43. overrideView_(false)
  44. {
  45. }
  46. SourceBatch::~SourceBatch()
  47. {
  48. }
  49. Drawable::Drawable(Context* context, unsigned char drawableFlags) :
  50. Component(context),
  51. drawableFlags_(drawableFlags),
  52. worldBoundingBoxDirty_(true),
  53. castShadows_(false),
  54. occluder_(false),
  55. occludee_(true),
  56. updateQueued_(false),
  57. viewMask_(DEFAULT_VIEWMASK),
  58. lightMask_(DEFAULT_LIGHTMASK),
  59. shadowMask_(DEFAULT_SHADOWMASK),
  60. zoneMask_(DEFAULT_ZONEMASK),
  61. viewFrameNumber_(0),
  62. distance_(0.0f),
  63. lodDistance_(0.0f),
  64. drawDistance_(0.0f),
  65. shadowDistance_(0.0f),
  66. sortValue_(0.0f),
  67. minZ_(0.0f),
  68. maxZ_(0.0f),
  69. lodBias_(1.0f),
  70. basePassFlags_(0),
  71. maxLights_(0),
  72. octant_(0),
  73. firstLight_(0),
  74. zone_(0),
  75. lastZone_(0),
  76. zoneDirty_(false)
  77. {
  78. }
  79. Drawable::~Drawable()
  80. {
  81. RemoveFromOctree();
  82. }
  83. void Drawable::RegisterObject(Context* context)
  84. {
  85. ATTRIBUTE(Drawable, VAR_INT, "Max Lights", maxLights_, 0, AM_DEFAULT);
  86. ATTRIBUTE(Drawable, VAR_INT, "View Mask", viewMask_, DEFAULT_VIEWMASK, AM_DEFAULT);
  87. ATTRIBUTE(Drawable, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  88. ATTRIBUTE(Drawable, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  89. ACCESSOR_ATTRIBUTE(Drawable, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  90. }
  91. void Drawable::OnSetEnabled()
  92. {
  93. bool enabled = IsEnabledEffective();
  94. if (enabled && !octant_)
  95. AddToOctree();
  96. else if (!enabled && octant_)
  97. RemoveFromOctree();
  98. }
  99. void Drawable::ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results)
  100. {
  101. float distance = query.ray_.HitDistance(GetWorldBoundingBox());
  102. if (distance < query.maxDistance_)
  103. {
  104. RayQueryResult result;
  105. result.position_ = query.ray_.origin_ + distance * query.ray_.direction_;
  106. result.normal_ = -query.ray_.direction_;
  107. result.distance_ = distance;
  108. result.drawable_ = this;
  109. result.node_ = GetNode();
  110. result.subObject_ = M_MAX_UNSIGNED;
  111. results.Push(result);
  112. }
  113. }
  114. void Drawable::UpdateBatches(const FrameInfo& frame)
  115. {
  116. const BoundingBox& worldBoundingBox = GetWorldBoundingBox();
  117. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  118. distance_ = frame.camera_->GetDistance(worldBoundingBox.Center());
  119. for (unsigned i = 0; i < batches_.Size(); ++i)
  120. {
  121. batches_[i].distance_ = distance_;
  122. batches_[i].worldTransform_ = &worldTransform;
  123. }
  124. float scale = worldBoundingBox.Size().DotProduct(DOT_SCALE);
  125. float newLodDistance = frame.camera_->GetLodDistance(distance_, scale, lodBias_);
  126. if (newLodDistance != lodDistance_)
  127. lodDistance_ = newLodDistance;
  128. }
  129. Geometry* Drawable::GetLodGeometry(unsigned batchIndex, unsigned level)
  130. {
  131. // By default return the visible batch geometry
  132. if (batchIndex < batches_.Size())
  133. return batches_[batchIndex].geometry_;
  134. else
  135. return 0;
  136. }
  137. void Drawable::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  138. {
  139. if (debug && IsEnabledEffective())
  140. debug->AddBoundingBox(GetWorldBoundingBox(), Color::GREEN, depthTest);
  141. }
  142. void Drawable::SetDrawDistance(float distance)
  143. {
  144. drawDistance_ = distance;
  145. MarkNetworkUpdate();
  146. }
  147. void Drawable::SetShadowDistance(float distance)
  148. {
  149. shadowDistance_ = distance;
  150. MarkNetworkUpdate();
  151. }
  152. void Drawable::SetLodBias(float bias)
  153. {
  154. lodBias_ = Max(bias, M_EPSILON);
  155. MarkNetworkUpdate();
  156. }
  157. void Drawable::SetViewMask(unsigned mask)
  158. {
  159. viewMask_ = mask;
  160. MarkNetworkUpdate();
  161. }
  162. void Drawable::SetLightMask(unsigned mask)
  163. {
  164. lightMask_ = mask;
  165. MarkNetworkUpdate();
  166. }
  167. void Drawable::SetShadowMask(unsigned mask)
  168. {
  169. shadowMask_ = mask;
  170. MarkNetworkUpdate();
  171. }
  172. void Drawable::SetZoneMask(unsigned mask)
  173. {
  174. zoneMask_ = mask;
  175. // Mark dirty to reset cached zone
  176. OnMarkedDirty(node_);
  177. MarkNetworkUpdate();
  178. }
  179. void Drawable::SetMaxLights(unsigned num)
  180. {
  181. maxLights_ = num;
  182. MarkNetworkUpdate();
  183. }
  184. void Drawable::SetCastShadows(bool enable)
  185. {
  186. castShadows_ = enable;
  187. MarkNetworkUpdate();
  188. }
  189. void Drawable::SetOccluder(bool enable)
  190. {
  191. occluder_ = enable;
  192. MarkNetworkUpdate();
  193. }
  194. void Drawable::SetOccludee(bool enable)
  195. {
  196. if (enable != occludee_)
  197. {
  198. occludee_ = enable;
  199. // Reinsert to octree to make sure octant occlusion does not erroneously hide this drawable
  200. if (octant_ && !updateQueued_)
  201. octant_->GetRoot()->QueueUpdate(this);
  202. MarkNetworkUpdate();
  203. }
  204. }
  205. void Drawable::MarkForUpdate()
  206. {
  207. if (!updateQueued_ && octant_)
  208. octant_->GetRoot()->QueueUpdate(this);
  209. }
  210. const BoundingBox& Drawable::GetWorldBoundingBox()
  211. {
  212. if (worldBoundingBoxDirty_)
  213. {
  214. OnWorldBoundingBoxUpdate();
  215. worldBoundingBoxDirty_ = false;
  216. }
  217. return worldBoundingBox_;
  218. }
  219. bool Drawable::IsInView() const
  220. {
  221. // Note: in headless mode there is no renderer subsystem and no view frustum tests are performed, so return
  222. // always false in that case
  223. Renderer* renderer = GetSubsystem<Renderer>();
  224. if (renderer)
  225. return viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && !viewCameras_.Empty();
  226. else
  227. return false;
  228. }
  229. bool Drawable::IsInView(Camera* camera) const
  230. {
  231. Renderer* renderer = GetSubsystem<Renderer>();
  232. if (renderer)
  233. return viewFrameNumber_ == renderer->GetFrameInfo().frameNumber_ && (!camera || viewCameras_.Contains(camera));
  234. else
  235. return false;
  236. }
  237. bool Drawable::IsInView(const FrameInfo& frame, bool anyCamera) const
  238. {
  239. return viewFrameNumber_ == frame.frameNumber_ && (anyCamera || viewCameras_.Contains(frame.camera_));
  240. }
  241. void Drawable::SetZone(Zone* zone, bool temporary)
  242. {
  243. zone_ = zone;
  244. lastZone_ = zone;
  245. // If the zone assignment was temporary (inconclusive) set the dirty flag so that it will be re-evaluated on the next frame
  246. zoneDirty_ = temporary;
  247. }
  248. void Drawable::SetSortValue(float value)
  249. {
  250. sortValue_ = value;
  251. }
  252. void Drawable::SetMinMaxZ(float minZ, float maxZ)
  253. {
  254. minZ_ = minZ;
  255. maxZ_ = maxZ;
  256. }
  257. void Drawable::MarkInView(const FrameInfo& frame)
  258. {
  259. if (frame.frameNumber_ != viewFrameNumber_)
  260. {
  261. viewFrameNumber_ = frame.frameNumber_;
  262. viewCameras_.Clear();
  263. }
  264. viewCameras_.Insert(frame.camera_);
  265. }
  266. void Drawable::MarkInView(unsigned frameNumber, Camera* camera)
  267. {
  268. if (frameNumber != viewFrameNumber_)
  269. {
  270. viewFrameNumber_ = frameNumber;
  271. viewCameras_.Clear();
  272. }
  273. if (camera)
  274. viewCameras_.Insert(camera);
  275. }
  276. void Drawable::LimitLights()
  277. {
  278. // Maximum lights value 0 means unlimited
  279. if (!maxLights_ || lights_.Size() <= maxLights_)
  280. return;
  281. // If more lights than allowed, move to vertex lights and cut the list
  282. const BoundingBox& box = GetWorldBoundingBox();
  283. for (unsigned i = 0; i < lights_.Size(); ++i)
  284. lights_[i]->SetIntensitySortValue(box);
  285. Sort(lights_.Begin(), lights_.End(), CompareDrawables);
  286. vertexLights_.Insert(vertexLights_.End(), lights_.Begin() + maxLights_, lights_.End());
  287. lights_.Resize(maxLights_);
  288. }
  289. void Drawable::LimitVertexLights()
  290. {
  291. if (vertexLights_.Size() <= MAX_VERTEX_LIGHTS)
  292. return;
  293. const BoundingBox& box = GetWorldBoundingBox();
  294. for (unsigned i = vertexLights_.Size() - 1; i < vertexLights_.Size(); --i)
  295. vertexLights_[i]->SetIntensitySortValue(box);
  296. Sort(vertexLights_.Begin(), vertexLights_.End(), CompareDrawables);
  297. vertexLights_.Resize(MAX_VERTEX_LIGHTS);
  298. }
  299. void Drawable::OnNodeSet(Node* node)
  300. {
  301. if (node)
  302. {
  303. AddToOctree();
  304. node->AddListener(this);
  305. }
  306. else
  307. RemoveFromOctree();
  308. }
  309. void Drawable::OnMarkedDirty(Node* node)
  310. {
  311. worldBoundingBoxDirty_ = true;
  312. if (!updateQueued_ && octant_)
  313. octant_->GetRoot()->QueueUpdate(this);
  314. // Mark zone assignment dirty
  315. if (node == node_)
  316. zoneDirty_ = true;
  317. }
  318. void Drawable::AddToOctree()
  319. {
  320. // Do not add to octree when disabled
  321. if (!IsEnabledEffective())
  322. return;
  323. Scene* scene = GetScene();
  324. if (scene)
  325. {
  326. Octree* octree = scene->GetComponent<Octree>();
  327. if (octree)
  328. octree->InsertDrawable(this);
  329. else
  330. LOGERROR("No Octree component in scene, drawable will not render");
  331. }
  332. else
  333. {
  334. // We have a mechanism for adding detached nodes to an octree manually, so do not log this error
  335. //LOGERROR("Node is detached from scene, drawable will not render");
  336. }
  337. }
  338. void Drawable::RemoveFromOctree()
  339. {
  340. if (octant_)
  341. {
  342. Octree* octree = octant_->GetRoot();
  343. if (updateQueued_)
  344. octree->CancelUpdate(this);
  345. octant_->RemoveDrawable(this);
  346. }
  347. }
  348. }