diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs b/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs new file mode 100755 index 0000000..96cd55e --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs | |||
@@ -0,0 +1,208 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Linq; | ||
31 | using System.Text; | ||
32 | |||
33 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
34 | { | ||
35 | public abstract class BSShape | ||
36 | { | ||
37 | public IntPtr ptr { get; set; } | ||
38 | public BSPhysicsShapeType type { get; set; } | ||
39 | public System.UInt64 key { get; set; } | ||
40 | public int referenceCount { get; set; } | ||
41 | public DateTime lastReferenced { get; set; } | ||
42 | |||
43 | public BSShape() | ||
44 | { | ||
45 | ptr = IntPtr.Zero; | ||
46 | type = BSPhysicsShapeType.SHAPE_UNKNOWN; | ||
47 | key = 0; | ||
48 | referenceCount = 0; | ||
49 | lastReferenced = DateTime.Now; | ||
50 | } | ||
51 | |||
52 | // Get a reference to a physical shape. Create if it doesn't exist | ||
53 | public static BSShape GetShapeReference(BSScene physicsScene, bool forceRebuild, BSPhysObject prim) | ||
54 | { | ||
55 | BSShape ret = null; | ||
56 | |||
57 | if (prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_CAPSULE) | ||
58 | { | ||
59 | // an avatar capsule is close to a native shape (it is not shared) | ||
60 | ret = BSShapeNative.GetReference(physicsScene, prim, BSPhysicsShapeType.SHAPE_CAPSULE, | ||
61 | FixedShapeKey.KEY_CAPSULE); | ||
62 | physicsScene.DetailLog("{0},BSShape.GetShapeReference,avatarCapsule,shape={1}", prim.LocalID, ret); | ||
63 | } | ||
64 | |||
65 | // Compound shapes are handled special as they are rebuilt from scratch. | ||
66 | // This isn't too great a hardship since most of the child shapes will already been created. | ||
67 | if (ret == null && prim.PreferredPhysicalShape == BSPhysicsShapeType.SHAPE_COMPOUND) | ||
68 | { | ||
69 | // Getting a reference to a compound shape gets you the compound shape with the root prim shape added | ||
70 | ret = BSShapeCompound.GetReference(prim); | ||
71 | physicsScene.DetailLog("{0},BSShapeCollection.CreateGeom,compoundShape,shape={1}", prim.LocalID, ret); | ||
72 | } | ||
73 | |||
74 | if (ret == null) | ||
75 | ret = GetShapeReferenceNonSpecial(physicsScene, forceRebuild, prim); | ||
76 | |||
77 | return ret; | ||
78 | } | ||
79 | public static BSShape GetShapeReferenceNonSpecial(BSScene physicsScene, bool forceRebuild, BSPhysObject prim) | ||
80 | { | ||
81 | return null; | ||
82 | } | ||
83 | public static BSShape GetShapeReferenceNonNative(BSScene physicsScene, bool forceRebuild, BSPhysObject prim) | ||
84 | { | ||
85 | return null; | ||
86 | } | ||
87 | |||
88 | // Release the use of a physical shape. | ||
89 | public abstract void Dereference(BSScene physicsScene); | ||
90 | |||
91 | // All shapes have a static call to get a reference to the physical shape | ||
92 | // protected abstract static BSShape GetReference(); | ||
93 | |||
94 | public override string ToString() | ||
95 | { | ||
96 | StringBuilder buff = new StringBuilder(); | ||
97 | buff.Append("<p="); | ||
98 | buff.Append(ptr.ToString("X")); | ||
99 | buff.Append(",s="); | ||
100 | buff.Append(type.ToString()); | ||
101 | buff.Append(",k="); | ||
102 | buff.Append(key.ToString("X")); | ||
103 | buff.Append(",c="); | ||
104 | buff.Append(referenceCount.ToString()); | ||
105 | buff.Append(">"); | ||
106 | return buff.ToString(); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | public class BSShapeNull : BSShape | ||
111 | { | ||
112 | public BSShapeNull() : base() | ||
113 | { | ||
114 | } | ||
115 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
116 | public override void Dereference(BSScene physicsScene) { /* The magic of garbage collection will make this go away */ } | ||
117 | } | ||
118 | |||
119 | public class BSShapeNative : BSShape | ||
120 | { | ||
121 | private static string LogHeader = "[BULLETSIM SHAPE NATIVE]"; | ||
122 | public BSShapeNative() : base() | ||
123 | { | ||
124 | } | ||
125 | public static BSShape GetReference(BSScene physicsScene, BSPhysObject prim, | ||
126 | BSPhysicsShapeType shapeType, FixedShapeKey shapeKey) | ||
127 | { | ||
128 | // Native shapes are not shared and are always built anew. | ||
129 | return new BSShapeNative(physicsScene, prim, shapeType, shapeKey); | ||
130 | } | ||
131 | |||
132 | private BSShapeNative(BSScene physicsScene, BSPhysObject prim, | ||
133 | BSPhysicsShapeType shapeType, FixedShapeKey shapeKey) | ||
134 | { | ||
135 | ShapeData nativeShapeData = new ShapeData(); | ||
136 | nativeShapeData.Type = shapeType; | ||
137 | nativeShapeData.ID = prim.LocalID; | ||
138 | nativeShapeData.Scale = prim.Scale; | ||
139 | nativeShapeData.Size = prim.Scale; | ||
140 | nativeShapeData.MeshKey = (ulong)shapeKey; | ||
141 | nativeShapeData.HullKey = (ulong)shapeKey; | ||
142 | |||
143 | |||
144 | if (shapeType == BSPhysicsShapeType.SHAPE_CAPSULE) | ||
145 | { | ||
146 | ptr = BulletSimAPI.BuildCapsuleShape2(physicsScene.World.ptr, 1f, 1f, prim.Scale); | ||
147 | physicsScene.DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale); | ||
148 | } | ||
149 | else | ||
150 | { | ||
151 | ptr = BulletSimAPI.BuildNativeShape2(physicsScene.World.ptr, nativeShapeData); | ||
152 | } | ||
153 | if (ptr == IntPtr.Zero) | ||
154 | { | ||
155 | physicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}", | ||
156 | LogHeader, prim.LocalID, shapeType); | ||
157 | } | ||
158 | type = shapeType; | ||
159 | key = (UInt64)shapeKey; | ||
160 | } | ||
161 | // Make this reference to the physical shape go away since native shapes are not shared. | ||
162 | public override void Dereference(BSScene physicsScene) | ||
163 | { | ||
164 | // Native shapes are not tracked and are released immediately | ||
165 | physicsScene.DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this); | ||
166 | BulletSimAPI.DeleteCollisionShape2(physicsScene.World.ptr, ptr); | ||
167 | ptr = IntPtr.Zero; | ||
168 | // Garbage collection will free up this instance. | ||
169 | } | ||
170 | } | ||
171 | |||
172 | public class BSShapeMesh : BSShape | ||
173 | { | ||
174 | private static string LogHeader = "[BULLETSIM SHAPE MESH]"; | ||
175 | private static Dictionary<System.UInt64, BSShapeMesh> Meshes = new Dictionary<System.UInt64, BSShapeMesh>(); | ||
176 | |||
177 | public BSShapeMesh() : base() | ||
178 | { | ||
179 | } | ||
180 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
181 | public override void Dereference(BSScene physicsScene) { } | ||
182 | } | ||
183 | |||
184 | public class BSShapeHull : BSShape | ||
185 | { | ||
186 | private static string LogHeader = "[BULLETSIM SHAPE HULL]"; | ||
187 | private static Dictionary<System.UInt64, BSShapeHull> Hulls = new Dictionary<System.UInt64, BSShapeHull>(); | ||
188 | |||
189 | public BSShapeHull() : base() | ||
190 | { | ||
191 | } | ||
192 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
193 | public override void Dereference(BSScene physicsScene) { } | ||
194 | } | ||
195 | |||
196 | public class BSShapeCompound : BSShape | ||
197 | { | ||
198 | private static string LogHeader = "[BULLETSIM SHAPE COMPOUND]"; | ||
199 | public BSShapeCompound() : base() | ||
200 | { | ||
201 | } | ||
202 | public static BSShape GetReference(BSPhysObject prim) | ||
203 | { | ||
204 | return new BSShapeNull(); | ||
205 | } | ||
206 | public override void Dereference(BSScene physicsScene) { } | ||
207 | } | ||
208 | } | ||