Zone.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Octree.h"
  27. #include "XMLElement.h"
  28. #include "Zone.h"
  29. static const Color DEFAULT_AMBIENT_COLOR(0.1f, 0.1f, 0.1f);
  30. static const Color DEFAULT_FOG_COLOR(0.0f, 0.0f, 0.0f);
  31. static const float DEFAULT_FOG_START = 250.0f;
  32. static const float DEFAULT_FOG_END = 1000.0f;
  33. OBJECTTYPESTATIC(Zone);
  34. Zone::Zone(Context* context) :
  35. Drawable(context),
  36. ambientStartColor_(DEFAULT_AMBIENT_COLOR),
  37. ambientEndColor_(DEFAULT_AMBIENT_COLOR),
  38. fogColor_(DEFAULT_FOG_COLOR),
  39. fogStart_(DEFAULT_FOG_START),
  40. fogEnd_(DEFAULT_FOG_END),
  41. priority_(0),
  42. override_(false)
  43. {
  44. drawableFlags_ = DRAWABLE_ZONE;
  45. }
  46. Zone::~Zone()
  47. {
  48. }
  49. void Zone::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<Zone>();
  52. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Min", boundingBox_.min_, Vector3::ZERO, AM_DEFAULT);
  53. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Max", boundingBox_.max_, Vector3::ZERO, AM_DEFAULT);
  54. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color (Min Z)", ambientStartColor_, DEFAULT_AMBIENT_COLOR, AM_DEFAULT);
  55. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color (Max Z)", ambientEndColor_, DEFAULT_AMBIENT_COLOR, AM_DEFAULT);
  56. ATTRIBUTE(Zone, VAR_COLOR, "Fog Color", fogColor_, DEFAULT_FOG_COLOR, AM_DEFAULT);
  57. ATTRIBUTE(Zone, VAR_FLOAT, "Fog Start", fogStart_, DEFAULT_FOG_START, AM_DEFAULT);
  58. ATTRIBUTE(Zone, VAR_FLOAT, "Fog End", fogEnd_, DEFAULT_FOG_END, AM_DEFAULT);
  59. ATTRIBUTE(Zone, VAR_BOOL, "Is Visible", visible_, true, AM_DEFAULT);
  60. ATTRIBUTE(Zone, VAR_BOOL, "Override Mode", override_, 0, AM_DEFAULT);
  61. ATTRIBUTE(Zone, VAR_INT, "Priority", priority_, 0, AM_DEFAULT);
  62. ATTRIBUTE(Zone, VAR_INT, "Light Mask", lightMask_, DEFAULT_LIGHTMASK, AM_DEFAULT);
  63. ATTRIBUTE(Zone, VAR_INT, "Shadow Mask", shadowMask_, DEFAULT_SHADOWMASK, AM_DEFAULT);
  64. ACCESSOR_ATTRIBUTE(Zone, VAR_INT, "Zone Mask", GetZoneMask, SetZoneMask, unsigned, DEFAULT_ZONEMASK, AM_DEFAULT);
  65. }
  66. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  67. {
  68. Serializable::OnSetAttribute(attr, src);
  69. // If bounding box, override mode or priority changes, dirty the drawable as applicable
  70. switch (attr.offset_)
  71. {
  72. case offsetof(Zone, boundingBox_.min_):
  73. case offsetof(Zone, boundingBox_.max_):
  74. case offsetof(Zone, priority_):
  75. OnMarkedDirty(node_);
  76. break;
  77. }
  78. }
  79. void Zone::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  80. {
  81. debug->AddBoundingBox(boundingBox_, GetWorldTransform(), Color::GREEN, depthTest);
  82. }
  83. void Zone::SetBoundingBox(const BoundingBox& box)
  84. {
  85. boundingBox_ = box;
  86. OnMarkedDirty(node_);
  87. }
  88. void Zone::SetAmbientColor(const Color& color)
  89. {
  90. ambientStartColor_ = ambientEndColor_ = Color(color, 1.0f);
  91. }
  92. void Zone::SetAmbientStartColor(const Color& color)
  93. {
  94. ambientStartColor_ = Color(color, 1.0f);
  95. }
  96. void Zone::SetAmbientEndColor(const Color& color)
  97. {
  98. ambientEndColor_ = Color(color, 1.0f);
  99. }
  100. void Zone::SetFogColor(const Color& color)
  101. {
  102. fogColor_ = Color(color, 1.0f);
  103. }
  104. void Zone::SetFogStart(float start)
  105. {
  106. if (start < 0.0f)
  107. start = 0.0f;
  108. fogStart_ = start;
  109. }
  110. void Zone::SetFogEnd(float end)
  111. {
  112. if (end < 0.0f)
  113. end = 0.0f;
  114. fogEnd_ = end;
  115. }
  116. void Zone::SetPriority(int priority)
  117. {
  118. priority_ = priority;
  119. }
  120. bool Zone::IsInside(const Vector3& point)
  121. {
  122. // Use an oriented bounding box test
  123. Matrix3x4 inverse(GetWorldTransform().Inverse());
  124. Vector3 localPoint(inverse * point);
  125. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  126. }
  127. void Zone::OnMarkedDirty(Node* node)
  128. {
  129. Drawable::OnMarkedDirty(node);
  130. // When marked dirty, clear the cached zone from all drawables inside the zone bounding box
  131. if (octant_ && lastBoundingBox_.defined_)
  132. {
  133. PODVector<Drawable*> result;
  134. BoxOctreeQuery query(result, lastBoundingBox_, DRAWABLE_GEOMETRY);
  135. octant_->GetRoot()->GetDrawables(query);
  136. for (PODVector<Drawable*>::Iterator i = result.Begin(); i != result.End(); ++i)
  137. (*i)->SetZone(0);
  138. }
  139. lastBoundingBox_ = GetWorldBoundingBox();
  140. }
  141. void Zone::OnWorldBoundingBoxUpdate()
  142. {
  143. worldBoundingBox_ = boundingBox_.Transformed(GetWorldTransform());
  144. }