GeometryNode.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "Geometry.h"
  25. #include "GeometryNode.h"
  26. #include "Light.h"
  27. #include "ReplicationUtils.h"
  28. #include "XMLElement.h"
  29. #include <algorithm>
  30. #include "DebugNew.h"
  31. GeometryNode::GeometryNode(unsigned flags, Octant* octant, const std::string& name) :
  32. VolumeNode(flags | NODE_GEOMETRY, octant, name),
  33. mLodBias(1.0f),
  34. mLodDistance(0.0f),
  35. mLodLevelsDirty(true)
  36. {
  37. }
  38. void GeometryNode::save(Serializer& dest)
  39. {
  40. // Write VolumeNode properties
  41. VolumeNode::save(dest);
  42. // Write GeometryNode properties
  43. dest.writeFloat(mLodBias);
  44. }
  45. void GeometryNode::load(Deserializer& source, ResourceCache* cache)
  46. {
  47. // Read VolumeNode properties
  48. VolumeNode::load(source, cache);
  49. // Read GeometryNode properties
  50. mLodBias = source.readFloat();
  51. }
  52. void GeometryNode::saveXML(XMLElement& dest)
  53. {
  54. // Write VolumeNode properties
  55. VolumeNode::saveXML(dest);
  56. // Write GeometryNode properties
  57. XMLElement lodElem = dest.getChildElement("lod");
  58. lodElem.setFloat("lodbias", mLodBias);
  59. }
  60. void GeometryNode::loadXML(const XMLElement& source, ResourceCache* cache)
  61. {
  62. // Read VolumeNode properties
  63. VolumeNode::loadXML(source, cache);
  64. // Read GeometryNode properties
  65. XMLElement lodElem = source.getChildElement("lod");
  66. mLodBias = lodElem.getFloat("lodbias");
  67. }
  68. bool GeometryNode::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  69. {
  70. // Use possibly modified transform in case the parent component is not to be replicated
  71. Vector3 position;
  72. Quaternion rotation;
  73. Vector3 scale;
  74. ComponentRef parentRef;
  75. getNetTransform(position, rotation, scale, parentRef, info);
  76. // Build bitmask of changed properties
  77. unsigned char bits = 0;
  78. checkVector3(position, Vector3::sZero, baseRevision, bits, 1);
  79. checkQuaternion(rotation, Quaternion::sIdentity, baseRevision, bits, 2);
  80. checkVector3(scale, Vector3::sUnity, baseRevision, bits, 4);
  81. checkComponentRef(parentRef, ComponentRef(), baseRevision, bits, 8);
  82. checkBool(mCastShadows, false, baseRevision, bits, 16);
  83. checkBool(mOccluder, false, baseRevision, bits, 16);
  84. checkBool(mVisible, true, baseRevision, bits, 16);
  85. checkFloat(mDrawDistance, 0.0f, baseRevision, bits, 32);
  86. checkFloat(mShadowDistance, 0.0f, baseRevision, bits, 32);
  87. checkUInt(mViewMask, VIEW_ALL, baseRevision, bits, 64);
  88. checkUInt(mLightMask, VIEW_ALL, baseRevision, bits, 64);
  89. checkFloat(mLodBias, 1.0f, baseRevision, bits, 128);
  90. // Update replication state fully, and network stream by delta
  91. dest.writeUByte(bits);
  92. writeVector3Delta(position, dest, destRevision, bits & 1);
  93. writePackedQuaternionDelta(rotation, dest, destRevision, bits & 2);
  94. writeVector3Delta(scale, dest, destRevision, bits & 4);
  95. writeComponentRefDelta(parentRef, dest, destRevision, bits & 8);
  96. writeBoolDelta(mCastShadows, dest, destRevision, bits & 16);
  97. writeBoolDelta(mOccluder, dest, destRevision, bits & 16);
  98. writeBoolDelta(mVisible, dest, destRevision, bits & 16);
  99. writeFloatDelta(mDrawDistance, dest, destRevision, bits & 32);
  100. writeFloatDelta(mShadowDistance, dest, destRevision, bits & 32);
  101. writeUIntDelta(mViewMask, dest, destRevision, bits & 64);
  102. writeUIntDelta(mLightMask, dest, destRevision, bits & 64);
  103. writeFloatDelta(mLodBias, dest, destRevision, bits & 128);
  104. return bits != 0;
  105. }
  106. void GeometryNode::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  107. {
  108. unsigned char bits = source.readUByte();
  109. // Interpolate if just position/rotation/scale changes, snap if parent changes
  110. bool interpolate = (bits & 7) && (!(bits & 8));
  111. if (!interpolate)
  112. {
  113. if (bits & 1)
  114. setPosition(source.readVector3());
  115. if (bits & 2)
  116. setRotation(source.readPackedQuaternion());
  117. if (bits & 4)
  118. setScale(source.readVector3());
  119. }
  120. else
  121. {
  122. if (bits & 1)
  123. {
  124. mInterpolationPosition = source.readVector3();
  125. mInterpolationFlags |= INTERP_POS;
  126. }
  127. if (bits & 2)
  128. {
  129. mInterpolationRotation = source.readPackedQuaternion();
  130. mInterpolationFlags |= INTERP_ROT;
  131. }
  132. if (bits & 4)
  133. {
  134. mInterpolationScale = source.readVector3();
  135. mInterpolationFlags |= INTERP_SCALE;
  136. }
  137. }
  138. readComponentRefDelta(mParentRef, source, bits & 8);
  139. readBoolDelta(mCastShadows, source, bits & 16);
  140. readBoolDelta(mOccluder, source, bits & 16);
  141. readBoolDelta(mVisible, source, bits & 16);
  142. readFloatDelta(mDrawDistance, source, bits & 32);
  143. readFloatDelta(mShadowDistance, source, bits & 32);
  144. readUIntDelta(mViewMask, source, bits & 64);
  145. readUIntDelta(mLightMask, source, bits & 64);
  146. readFloatDelta(mLodBias, source, bits & 128);
  147. }
  148. void GeometryNode::updateDistance(const FrameInfo& frame)
  149. {
  150. mDistance = frame.mCamera->getDistance(getWorldPosition());
  151. static const Vector3 dotScale(1 / 3.0f, 1 / 3.0f, 1 / 3.0f);
  152. float scale = getWorldBoundingBox().getSize().dotProduct(dotScale);
  153. float newLodDistance = frame.mCamera->getLodDistance(mDistance, scale, mLodBias);
  154. if (newLodDistance != mLodDistance)
  155. {
  156. mLodDistance = newLodDistance;
  157. mLodLevelsDirty = true;
  158. }
  159. }
  160. bool GeometryNode::getVertexShaderParameter(unsigned batchIndex, VSParameter parameter, const float** data, unsigned* count)
  161. {
  162. return false;
  163. }
  164. bool GeometryNode::drawOcclusion(OcclusionBuffer* buffer)
  165. {
  166. return true;
  167. }
  168. void GeometryNode::setLodBias(float bias)
  169. {
  170. mLodBias = max(bias, M_EPSILON);
  171. }