Zone.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "XMLElement.h"
  26. #include "Zone.h"
  27. static const float DEFAULT_FOGSTART = 250.0f;
  28. static const float DEFAULT_FOGEND = 1000.0f;
  29. static const Color DEFAULT_FOG_COLOR(0.0f, 0.0f, 0.0f);
  30. static const Color DEFAULT_AMBIENT_COLOR(0.1f, 0.1f, 0.1f);
  31. OBJECTTYPESTATIC(Zone);
  32. Zone::Zone(Context* context) :
  33. Drawable(context),
  34. ambientColor_(DEFAULT_AMBIENT_COLOR),
  35. fogColor_(DEFAULT_FOG_COLOR),
  36. fogStart_(DEFAULT_FOGSTART),
  37. fogEnd_(DEFAULT_FOGEND),
  38. priority_(0)
  39. {
  40. drawableFlags_ = DRAWABLE_ZONE;
  41. }
  42. Zone::~Zone()
  43. {
  44. }
  45. void Zone::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Zone>();
  48. context->CopyBaseAttributes<Drawable, Zone>();
  49. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Min", boundingBox_.min_, Vector3::ZERO);
  50. ATTRIBUTE(Zone, VAR_VECTOR3, "Bounding Box Max", boundingBox_.max_, Vector3::ZERO);
  51. ATTRIBUTE(Zone, VAR_COLOR, "Ambient Color", ambientColor_, DEFAULT_AMBIENT_COLOR);
  52. ATTRIBUTE(Zone, VAR_COLOR, "Fog Color", fogColor_, DEFAULT_FOG_COLOR);
  53. ATTRIBUTE(Zone, VAR_FLOAT, "Fog Start", fogStart_, DEFAULT_FOGSTART);
  54. ATTRIBUTE(Zone, VAR_FLOAT, "Fog End", fogEnd_, DEFAULT_FOGEND);
  55. ATTRIBUTE(Zone, VAR_INT, "Zone Priority", priority_, 0);
  56. }
  57. void Zone::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  58. {
  59. switch (attr.offset_)
  60. {
  61. case offsetof(Zone, boundingBox_.min_):
  62. case offsetof(Zone, boundingBox_.max_):
  63. Serializable::OnSetAttribute(attr, value);
  64. // If bounding box changes, dirty the drawable as applicable
  65. OnMarkedDirty(node_);
  66. break;
  67. default:
  68. Serializable::OnSetAttribute(attr, value);
  69. break;
  70. }
  71. }
  72. void Zone::SetBoundingBox(const BoundingBox& box)
  73. {
  74. boundingBox_ = box;
  75. OnMarkedDirty(node_);
  76. }
  77. void Zone::SetAmbientColor(const Color& color)
  78. {
  79. ambientColor_ = Color(color, 1.0f);
  80. }
  81. void Zone::SetFogColor(const Color& color)
  82. {
  83. fogColor_ = Color(color, 1.0f);
  84. }
  85. void Zone::SetFogStart(float start)
  86. {
  87. if (start < 0.0f)
  88. start = 0.0f;
  89. fogStart_ = start;
  90. }
  91. void Zone::SetFogEnd(float end)
  92. {
  93. if (end < 0.0f)
  94. end = 0.0f;
  95. fogEnd_ = end;
  96. }
  97. void Zone::SetPriority(int priority)
  98. {
  99. priority_ = priority;
  100. }
  101. bool Zone::IsInside(const Vector3& point)
  102. {
  103. // Use an oriented bounding box test
  104. Matrix3x4 inverse(GetWorldTransform().Inverse());
  105. Vector3 localPoint(inverse * point);
  106. return boundingBox_.IsInside(localPoint) != OUTSIDE;
  107. }
  108. void Zone::OnWorldBoundingBoxUpdate()
  109. {
  110. worldBoundingBox_ = boundingBox_.Transformed(GetWorldTransform());
  111. }