Zone.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "ReplicationUtils.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_COLOR(0.0f, 0.0f, 0.0f, 0.0f);
  30. Zone::Zone(Octant* octant, const std::string& name) :
  31. VolumeNode(NODE_ZONE, octant, name),
  32. mAmbientColor(DEFAULT_COLOR),
  33. mFogColor(DEFAULT_COLOR),
  34. mFogStart(DEFAULT_FOGSTART),
  35. mFogEnd(DEFAULT_FOGEND),
  36. mPriority(0)
  37. {
  38. }
  39. Zone::~Zone()
  40. {
  41. }
  42. void Zone::save(Serializer& dest)
  43. {
  44. // Write VolumeNode properties
  45. VolumeNode::save(dest);
  46. // Write Zone properties
  47. dest.writeBoundingBox(mBoundingBox);
  48. dest.writeColor(mAmbientColor);
  49. dest.writeColor(mFogColor);
  50. dest.writeFloat(mFogStart);
  51. dest.writeFloat(mFogEnd);
  52. dest.writeInt(mPriority);
  53. }
  54. void Zone::load(Deserializer& source, ResourceCache* cache)
  55. {
  56. // Read VolumeNode properties
  57. VolumeNode::load(source, cache);
  58. // Read Zone properties
  59. mBoundingBox = source.readBoundingBox();
  60. mAmbientColor = source.readColor();
  61. mFogColor = source.readColor();
  62. mFogStart = source.readFloat();
  63. mFogEnd = source.readFloat();
  64. mPriority = source.readInt();
  65. }
  66. void Zone::saveXML(XMLElement& dest)
  67. {
  68. // Write VolumeNode properties
  69. VolumeNode::saveXML(dest);
  70. // Write Zone properties
  71. XMLElement zoneElement = dest.createChildElement("zone");
  72. zoneElement.setBoundingBox(mBoundingBox);
  73. zoneElement.setColor("ambientcolor", mAmbientColor);
  74. zoneElement.setInt("priority", mPriority);
  75. XMLElement fogElement = dest.createChildElement("fog");
  76. fogElement.setColor("color", mFogColor);
  77. fogElement.setFloat("start", mFogStart);
  78. fogElement.setFloat("end", mFogEnd);
  79. }
  80. void Zone::loadXML(const XMLElement& source, ResourceCache* cache)
  81. {
  82. // Read VolumeNode properties
  83. VolumeNode::loadXML(source, cache);
  84. // Read Zone properties
  85. XMLElement zoneElement = source.getChildElement("zone");
  86. mBoundingBox = zoneElement.getBoundingBox();
  87. mAmbientColor = zoneElement.getColor("ambientcolor");
  88. mPriority = zoneElement.getInt("priority");
  89. XMLElement fogElement = source.getChildElement("fog");
  90. mFogColor = fogElement.getColor("color");
  91. mFogStart = fogElement.getFloat("start");
  92. mFogEnd = fogElement.getFloat("end");
  93. }
  94. bool Zone::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  95. {
  96. // Write VolumeNode properties and see if there were any changes
  97. bool prevBits = VolumeNode::writeNetUpdate(dest, destRevision, baseRevision, info);
  98. // Build bitmask of changed properties
  99. unsigned char bits = 0;
  100. checkVector3(mBoundingBox.mMin, baseRevision, bits, 1);
  101. checkVector3(mBoundingBox.mMax, baseRevision, bits, 1);
  102. checkColor(mAmbientColor, DEFAULT_COLOR, baseRevision, bits, 2);
  103. checkColor(mFogColor, DEFAULT_COLOR, baseRevision, bits, 4);
  104. checkFloat(mFogStart, DEFAULT_FOGSTART, baseRevision, bits, 8);
  105. checkFloat(mFogEnd, DEFAULT_FOGEND, baseRevision, bits, 8);
  106. checkInt(mPriority, 0, baseRevision, bits, 16);
  107. // Update replication state fully, and network stream by delta
  108. dest.writeUByte(bits);
  109. writeVector3Delta(mBoundingBox.mMin, dest, destRevision, bits & 1);
  110. writeVector3Delta(mBoundingBox.mMax, dest, destRevision, bits & 1);
  111. writeColorDelta(mAmbientColor, dest, destRevision, bits & 2);
  112. writeColorDelta(mFogColor, dest, destRevision, bits & 4);
  113. writeFloatDelta(mFogStart, dest, destRevision, bits & 8);
  114. writeFloatDelta(mFogEnd, dest, destRevision, bits & 8);
  115. writeIntDelta(mPriority, dest, destRevision, bits & 16);
  116. return prevBits || (bits != 0);
  117. }
  118. void Zone::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  119. {
  120. // Read VolumeNode properties
  121. VolumeNode::readNetUpdate(source, cache, info);
  122. unsigned char bits = source.readUByte();
  123. if (bits & 1)
  124. mBoundingBox = source.readBoundingBox();
  125. readColorDelta(mAmbientColor, source, bits & 2);
  126. readColorDelta(mFogColor, source, bits & 4);
  127. readFloatDelta(mFogStart, source, bits & 8);
  128. readFloatDelta(mFogEnd, source, bits & 8);
  129. readIntDelta(mPriority, source, bits & 16);
  130. }
  131. void Zone::setBoundingBox(const BoundingBox& box)
  132. {
  133. mBoundingBox = box;
  134. markDirty();
  135. }
  136. void Zone::setAmbientColor(const Color& color)
  137. {
  138. // Ensure that alpha is zero, because custom rendering may depend on it
  139. mAmbientColor = Color(color, 0.0f);
  140. }
  141. void Zone::setFogColor(const Color& color)
  142. {
  143. // Ensure that alpha is zero, because custom rendering may depend on it
  144. mFogColor = Color(color, 0.0f);
  145. }
  146. void Zone::setFogStart(float start)
  147. {
  148. if (start < 0.0f)
  149. start = 0.0f;
  150. mFogStart = start;
  151. }
  152. void Zone::setFogEnd(float end)
  153. {
  154. if (end < 0.0f)
  155. end = 0.0f;
  156. mFogEnd = end;
  157. }
  158. void Zone::setPriority(int priority)
  159. {
  160. mPriority = priority;
  161. }
  162. bool Zone::isInside(const Vector3& point)
  163. {
  164. // Use an oriented bounding box test
  165. Matrix4x3 inverse = getWorldTransform().getInverse();
  166. Vector3 localpoint = inverse * point;
  167. return mBoundingBox.isInside(point) != OUTSIDE;
  168. }
  169. void Zone::onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox)
  170. {
  171. worldBoundingBox = mBoundingBox.getTransformed(getWorldTransform());
  172. }