2
0

Zone.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Octree.h"
  27. #include "XMLElement.h"
  28. #include "Zone.h"
  29. #include "DebugNew.h"
  30. static const Vector3 DEFAULT_BOUNDING_BOX_MIN(-10.0f, -10.0f, -10.0f);
  31. static const Vector3 DEFAULT_BOUNDING_BOX_MAX(10.0f, 10.0f, 10.0f);
  32. static const Color DEFAULT_AMBIENT_COLOR(0.1f, 0.1f, 0.1f);
  33. static const Color DEFAULT_FOG_COLOR(0.0f, 0.0f, 0.0f);
  34. static const float DEFAULT_FOG_START = 250.0f;
  35. static const float DEFAULT_FOG_END = 1000.0f;
  36. OBJECTTYPESTATIC(Zone);
  37. Zone::Zone(Context* context) :
  38. Drawable(context),
  39. boundingBox_(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX),
  40. ambientColor_(DEFAULT_AMBIENT_COLOR),
  41. fogColor_(DEFAULT_FOG_COLOR),
  42. fogStart_(DEFAULT_FOG_START),
  43. fogEnd_(DEFAULT_FOG_END),
  44. priority_(0),
  45. override_(false),
  46. ambientGradient_(false)
  47. {
  48. drawableFlags_ = DRAWABLE_ZONE;
  49. }
  50. Zone::~Zone()
  51. {
  52. }
  53. void Zone::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Zone>();
  56. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Min", boundingBox_.min_, DEFAULT_BOUNDING_BOX_MIN, AM_DEFAULT);
  57. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Max", boundingBox_.max_, DEFAULT_BOUNDING_BOX_MAX, AM_DEFAULT);
  58. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color", ambientColor_, DEFAULT_AMBIENT_COLOR, AM_DEFAULT);
  59. ATTRIBUTE(Zone, VAR_COLOR, "Fog Color", fogColor_, DEFAULT_FOG_COLOR, AM_DEFAULT);
  60. ATTRIBUTE(Zone, VAR_FLOAT, "Fog Start", fogStart_, DEFAULT_FOG_START, AM_DEFAULT);
  61. ATTRIBUTE(Zone, VAR_FLOAT, "Fog End", fogEnd_, DEFAULT_FOG_END, AM_DEFAULT);
  62. ATTRIBUTE(Zone, VAR_BOOL, "Is Visible", visible_, true, AM_DEFAULT);
  63. ATTRIBUTE(Zone, VAR_BOOL, "Override Mode", override_, 0, AM_DEFAULT);
  64. ATTRIBUTE(Zone, VAR_BOOL, "Ambient Gradient", ambientGradient_, 0, AM_DEFAULT);
  65. ATTRIBUTE(Zone, VAR_INT, "Priority", priority_, 0, AM_DEFAULT);
  66. ATTRIBUTE(Zone, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  67. ATTRIBUTE(Zone, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  68. ACCESSOR_ATTRIBUTE(Zone, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  69. }
  70. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  71. {
  72. Serializable::OnSetAttribute(attr, src);
  73. // If bounding box, override mode, visibility or priority changes, dirty the drawable as applicable
  74. switch (attr.offset_)
  75. {
  76. case offsetof(Zone, boundingBox_.min_):
  77. case offsetof(Zone, boundingBox_.max_):
  78. case offsetof(Zone, visible_):
  79. case offsetof(Zone, priority_):
  80. OnMarkedDirty(node_);
  81. break;
  82. }
  83. }
  84. void Zone::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  85. {
  86. if (debug)
  87. debug->AddBoundingBox(boundingBox_, GetWorldTransform(), Color::GREEN, depthTest);
  88. }
  89. void Zone::SetBoundingBox(const BoundingBox& box)
  90. {
  91. boundingBox_ = box;
  92. OnMarkedDirty(node_);
  93. }
  94. void Zone::SetAmbientColor(const Color& color)
  95. {
  96. ambientColor_ = Color(color, 1.0f);
  97. }
  98. void Zone::SetFogColor(const Color& color)
  99. {
  100. fogColor_ = Color(color, 1.0f);
  101. }
  102. void Zone::SetFogStart(float start)
  103. {
  104. if (start < 0.0f)
  105. start = 0.0f;
  106. fogStart_ = start;
  107. }
  108. void Zone::SetFogEnd(float end)
  109. {
  110. if (end < 0.0f)
  111. end = 0.0f;
  112. fogEnd_ = end;
  113. }
  114. void Zone::SetPriority(int priority)
  115. {
  116. priority_ = priority;
  117. }
  118. void Zone::SetOverride(bool enable)
  119. {
  120. override_ = enable;
  121. }
  122. void Zone::SetAmbientGradient(bool enable)
  123. {
  124. ambientGradient_ = enable;
  125. }
  126. const Color& Zone::GetAmbientStartColor()
  127. {
  128. if (!ambientGradient_)
  129. return ambientColor_;
  130. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  131. UpdateAmbientGradient();
  132. return ambientStartColor_;
  133. }
  134. const Color& Zone::GetAmbientEndColor()
  135. {
  136. if (!ambientGradient_)
  137. return ambientColor_;
  138. if (!lastAmbientStartZone_ || !lastAmbientEndZone_)
  139. UpdateAmbientGradient();
  140. return ambientEndColor_;
  141. }
  142. bool Zone::IsInside(const Vector3& point)
  143. {
  144. // Use an oriented bounding box test
  145. Matrix3x4 inverse(GetWorldTransform().Inverse());
  146. Vector3 localPoint(inverse * point);
  147. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  148. }
  149. void Zone::OnMarkedDirty(Node* node)
  150. {
  151. Drawable::OnMarkedDirty(node);
  152. // When marked dirty, clear the cached zone from all drawables inside the zone bounding box,
  153. // and mark gradient dirty in all neighbor zones
  154. if (octant_ && lastWorldBoundingBox_.defined_)
  155. {
  156. PODVector<Drawable*> result;
  157. BoxOctreeQuery query(result, lastWorldBoundingBox_, DRAWABLE_GEOMETRY | DRAWABLE_ZONE);
  158. octant_->GetRoot()->GetDrawables(query);
  159. for (PODVector<Drawable*>::Iterator i = result.Begin(); i != result.End(); ++i)
  160. {
  161. Drawable* drawable = *i;
  162. unsigned drawableFlags = drawable->GetDrawableFlags();
  163. if (drawableFlags & DRAWABLE_GEOMETRY)
  164. (*i)->SetZone(0);
  165. if (drawableFlags & DRAWABLE_ZONE)
  166. {
  167. Zone* zone = static_cast<Zone*>(drawable);
  168. zone->lastAmbientStartZone_.Reset();
  169. zone->lastAmbientEndZone_.Reset();
  170. }
  171. }
  172. }
  173. lastWorldBoundingBox_ = GetWorldBoundingBox();
  174. lastAmbientStartZone_.Reset();
  175. lastAmbientEndZone_.Reset();
  176. }
  177. void Zone::OnWorldBoundingBoxUpdate()
  178. {
  179. worldBoundingBox_ = boundingBox_.Transformed(GetWorldTransform());
  180. }
  181. void Zone::UpdateAmbientGradient()
  182. {
  183. // In case no neighbor zones are found, reset ambient start/end with own ambient color
  184. ambientStartColor_ = ambientColor_;
  185. ambientEndColor_ = ambientColor_;
  186. lastAmbientStartZone_ = this;
  187. lastAmbientEndZone_ = this;
  188. if (octant_)
  189. {
  190. const Matrix3x4& worldTransform = GetWorldTransform();
  191. Vector3 center = boundingBox_.Center();
  192. Vector3 minZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.min_.z_);
  193. Vector3 maxZPosition = worldTransform * Vector3(center.x_, center.y_, boundingBox_.max_.z_);
  194. PODVector<Zone*> result;
  195. {
  196. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), minZPosition, DRAWABLE_ZONE);
  197. octant_->GetRoot()->GetDrawables(query);
  198. }
  199. // Gradient start position: get the highest priority zone that is not this zone
  200. int bestPriority = M_MIN_INT;
  201. Zone* bestZone = 0;
  202. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  203. {
  204. Zone* zone = *i;
  205. int priority = zone->GetPriority();
  206. if (zone != this && priority > bestPriority && zone->IsInside(minZPosition))
  207. {
  208. bestZone = zone;
  209. bestPriority = priority;
  210. }
  211. }
  212. if (bestZone)
  213. {
  214. ambientStartColor_ = bestZone->GetAmbientColor();
  215. lastAmbientStartZone_ = bestZone;
  216. }
  217. // Do the same for gradient end position
  218. {
  219. PointOctreeQuery query(reinterpret_cast<PODVector<Drawable*>&>(result), maxZPosition, DRAWABLE_ZONE);
  220. octant_->GetRoot()->GetDrawables(query);
  221. }
  222. bestPriority = M_MIN_INT;
  223. bestZone = 0;
  224. for (PODVector<Zone*>::ConstIterator i = result.Begin(); i != result.End(); ++i)
  225. {
  226. Zone* zone = *i;
  227. int priority = zone->GetPriority();
  228. if (zone != this && priority > bestPriority && zone->IsInside(maxZPosition))
  229. {
  230. bestZone = zone;
  231. bestPriority = priority;
  232. }
  233. }
  234. if (bestZone)
  235. {
  236. ambientEndColor_ = bestZone->GetAmbientColor();
  237. lastAmbientEndZone_ = bestZone;
  238. }
  239. }
  240. }