BSShapes.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Text;
  31. namespace OpenSim.Region.Physics.BulletSNPlugin
  32. {
  33. public abstract class BSShape
  34. {
  35. public Object ptr { get; set; }
  36. public BSPhysicsShapeType type { get; set; }
  37. public System.UInt64 key { get; set; }
  38. public int referenceCount { get; set; }
  39. public DateTime lastReferenced { get; set; }
  40. public BSShape()
  41. {
  42. ptr = null;
  43. type = BSPhysicsShapeType.SHAPE_UNKNOWN;
  44. key = 0;
  45. referenceCount = 0;
  46. lastReferenced = DateTime.Now;
  47. }
  48. // Get a reference to a physical shape. Create if it doesn't exist
  49. public static BSShape GetShapeReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  50. {
  51. BSShape ret = null;
  52. if (prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_CAPSULE)
  53. {
  54. // an avatar capsule is close to a native shape (it is not shared)
  55. ret = BSShapeNative.GetReference(physicsScene, prim, BSPhysicsShapeType.SHAPE_CAPSULE,
  56. FixedShapeKey.KEY_CAPSULE);
  57. physicsScene.DetailLog("{0},BSShape.GetShapeReference,avatarCapsule,shape={1}", prim.LocalID, ret);
  58. }
  59. // Compound shapes are handled special as they are rebuilt from scratch.
  60. // This isn't too great a hardship since most of the child shapes will already been created.
  61. if (ret == null && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND)
  62. {
  63. // Getting a reference to a compound shape gets you the compound shape with the root prim shape added
  64. ret = BSShapeCompound.GetReference(prim);
  65. physicsScene.DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, ret);
  66. }
  67. if (ret == null)
  68. ret = GetShapeReferenceNonSpecial(physicsScene, forceRebuild, prim);
  69. return ret;
  70. }
  71. public static BSShape GetShapeReferenceNonSpecial(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  72. {
  73. return null;
  74. }
  75. public static BSShape GetShapeReferenceNonNative(BSScene physicsScene, bool forceRebuild, BSPhysObject prim)
  76. {
  77. return null;
  78. }
  79. // Release the use of a physical shape.
  80. public abstract void Dereference(BSScene physicsScene);
  81. // All shapes have a static call to get a reference to the physical shape
  82. // protected abstract static BSShape GetReference();
  83. public override string ToString()
  84. {
  85. StringBuilder buff = new StringBuilder();
  86. buff.Append("<p=");
  87. buff.Append(ptr.ToString());
  88. buff.Append(",s=");
  89. buff.Append(type.ToString());
  90. buff.Append(",k=");
  91. buff.Append(key.ToString("X"));
  92. buff.Append(",c=");
  93. buff.Append(referenceCount.ToString());
  94. buff.Append(">");
  95. return buff.ToString();
  96. }
  97. }
  98. public class BSShapeNull : BSShape
  99. {
  100. public BSShapeNull() : base()
  101. {
  102. }
  103. public static BSShape GetReference() { return new BSShapeNull(); }
  104. public override void Dereference(BSScene physicsScene) { /* The magic of garbage collection will make this go away */ }
  105. }
  106. public class BSShapeNative : BSShape
  107. {
  108. private static string LogHeader = "[BULLETSIM SHAPE NATIVE]";
  109. public BSShapeNative() : base()
  110. {
  111. }
  112. public static BSShape GetReference(BSScene physicsScene, BSPhysObject prim,
  113. BSPhysicsShapeType shapeType, FixedShapeKey shapeKey)
  114. {
  115. // Native shapes are not shared and are always built anew.
  116. return new BSShapeNative(physicsScene, prim, shapeType, shapeKey);
  117. }
  118. private BSShapeNative(BSScene physicsScene, BSPhysObject prim,
  119. BSPhysicsShapeType shapeType, FixedShapeKey shapeKey)
  120. {
  121. ShapeData nativeShapeData = new ShapeData();
  122. nativeShapeData.Type = shapeType;
  123. nativeShapeData.ID = prim.LocalID;
  124. nativeShapeData.Scale = prim.Scale;
  125. nativeShapeData.Size = prim.Scale;
  126. nativeShapeData.MeshKey = (ulong)shapeKey;
  127. nativeShapeData.HullKey = (ulong)shapeKey;
  128. if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE)
  129. {
  130. ptr = BulletSimAPI.BuildCapsuleShape2(physicsScene.World.ptr, 1f, 1f, prim.Scale);
  131. physicsScene.DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale);
  132. }
  133. else
  134. {
  135. ptr = BulletSimAPI.BuildNativeShape2(physicsScene.World.ptr, nativeShapeData);
  136. }
  137. if (ptr == null)
  138. {
  139. physicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}",
  140. LogHeader, prim.LocalID, shapeType);
  141. }
  142. type = shapeType;
  143. key = (UInt64)shapeKey;
  144. }
  145. // Make this reference to the physical shape go away since native shapes are not shared.
  146. public override void Dereference(BSScene physicsScene)
  147. {
  148. // Native shapes are not tracked and are released immediately
  149. physicsScene.DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this);
  150. BulletSimAPI.DeleteCollisionShape2(physicsScene.World.ptr, ptr);
  151. ptr = null;
  152. // Garbage collection will free up this instance.
  153. }
  154. }
  155. public class BSShapeMesh : BSShape
  156. {
  157. private static string LogHeader = "[BULLETSIM SHAPE MESH]";
  158. private static Dictionary<System.UInt64, BSShapeMesh> Meshes = new Dictionary<System.UInt64, BSShapeMesh>();
  159. public BSShapeMesh() : base()
  160. {
  161. }
  162. public static BSShape GetReference() { return new BSShapeNull(); }
  163. public override void Dereference(BSScene physicsScene) { }
  164. }
  165. public class BSShapeHull : BSShape
  166. {
  167. private static string LogHeader = "[BULLETSIM SHAPE HULL]";
  168. private static Dictionary<System.UInt64, BSShapeHull> Hulls = new Dictionary<System.UInt64, BSShapeHull>();
  169. public BSShapeHull() : base()
  170. {
  171. }
  172. public static BSShape GetReference() { return new BSShapeNull(); }
  173. public override void Dereference(BSScene physicsScene) { }
  174. }
  175. public class BSShapeCompound : BSShape
  176. {
  177. private static string LogHeader = "[BULLETSIM SHAPE COMPOUND]";
  178. public BSShapeCompound() : base()
  179. {
  180. }
  181. public static BSShape GetReference(BSPhysObject prim)
  182. {
  183. return new BSShapeNull();
  184. }
  185. public override void Dereference(BSScene physicsScene) { }
  186. }
  187. }