diff options
author | Robert Adams | 2012-11-07 15:29:51 -0800 |
---|---|---|
committer | Robert Adams | 2012-11-07 15:29:51 -0800 |
commit | 366651831952558b03b8f6ec442a0867b257b808 (patch) | |
tree | 1a7794f357fce1340c8f992a7fa7386dc3ef40f9 /OpenSim/Region/Physics/BulletSPlugin | |
parent | BulletSim: add classes for physics motors. Eventually these will replace the ... (diff) | |
download | opensim-SC-366651831952558b03b8f6ec442a0867b257b808.zip opensim-SC-366651831952558b03b8f6ec442a0867b257b808.tar.gz opensim-SC-366651831952558b03b8f6ec442a0867b257b808.tar.bz2 opensim-SC-366651831952558b03b8f6ec442a0867b257b808.tar.xz |
BulletSim: add skeleton classes for shape objectification. This will eventually replace all the if's and switches in ShapeCollection with polymorphism.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs | 213 |
1 files changed, 213 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..5e2c4a8 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BSShapes.cs | |||
@@ -0,0 +1,213 @@ | |||
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 ShapeData.PhysicsShapeType type { get; set; } | ||
39 | public System.UInt64 key { get; set; } | ||
40 | public int referenceCount { get; set; } | ||
41 | public DateTime lastReferenced { get; set; } | ||
42 | |||
43 | protected void Initialize() | ||
44 | { | ||
45 | ptr = IntPtr.Zero; | ||
46 | type = ShapeData.PhysicsShapeType.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 == ShapeData.PhysicsShapeType.SHAPE_AVATAR) | ||
58 | { | ||
59 | // an avatar capsule is close to a native shape (it is not shared) | ||
60 | ret = BSShapeNative.GetReference(physicsScene, prim, ShapeData.PhysicsShapeType.SHAPE_AVATAR, | ||
61 | ShapeData.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 == ShapeData.PhysicsShapeType.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() | ||
113 | { | ||
114 | base.Initialize(); | ||
115 | } | ||
116 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
117 | public override void Dereference(BSScene physicsScene) { /* The magic of garbage collection will make this go away */ } | ||
118 | } | ||
119 | |||
120 | public class BSShapeNative : BSShape | ||
121 | { | ||
122 | private static string LogHeader = "[BULLETSIM SHAPE NATIVE]"; | ||
123 | public BSShapeNative() | ||
124 | { | ||
125 | base.Initialize(); | ||
126 | } | ||
127 | public static BSShape GetReference(BSScene physicsScene, BSPhysObject prim, | ||
128 | ShapeData.PhysicsShapeType shapeType, ShapeData.FixedShapeKey shapeKey) | ||
129 | { | ||
130 | // Native shapes are not shared and are always built anew. | ||
131 | return new BSShapeNative(physicsScene, prim, shapeType, shapeKey); | ||
132 | } | ||
133 | |||
134 | private BSShapeNative(BSScene physicsScene, BSPhysObject prim, | ||
135 | ShapeData.PhysicsShapeType shapeType, ShapeData.FixedShapeKey shapeKey) | ||
136 | { | ||
137 | ShapeData nativeShapeData = new ShapeData(); | ||
138 | nativeShapeData.Type = shapeType; | ||
139 | nativeShapeData.ID = prim.LocalID; | ||
140 | nativeShapeData.Scale = prim.Scale; | ||
141 | nativeShapeData.Size = prim.Scale; | ||
142 | nativeShapeData.MeshKey = (ulong)shapeKey; | ||
143 | nativeShapeData.HullKey = (ulong)shapeKey; | ||
144 | |||
145 | |||
146 | if (shapeType == ShapeData.PhysicsShapeType.SHAPE_AVATAR) | ||
147 | { | ||
148 | ptr = BulletSimAPI.BuildCapsuleShape2(physicsScene.World.ptr, 1f, 1f, prim.Scale); | ||
149 | physicsScene.DetailLog("{0},BSShapeCollection.BuiletPhysicalNativeShape,capsule,scale={1}", prim.LocalID, prim.Scale); | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | ptr = BulletSimAPI.BuildNativeShape2(physicsScene.World.ptr, nativeShapeData); | ||
154 | } | ||
155 | if (ptr == IntPtr.Zero) | ||
156 | { | ||
157 | physicsScene.Logger.ErrorFormat("{0} BuildPhysicalNativeShape failed. ID={1}, shape={2}", | ||
158 | LogHeader, prim.LocalID, shapeType); | ||
159 | } | ||
160 | type = shapeType; | ||
161 | key = (UInt64)shapeKey; | ||
162 | } | ||
163 | // Make this reference to the physical shape go away since native shapes are not shared. | ||
164 | public override void Dereference(BSScene physicsScene) | ||
165 | { | ||
166 | // Native shapes are not tracked and are released immediately | ||
167 | physicsScene.DetailLog("{0},BSShapeCollection.DereferenceShape,deleteNativeShape,shape={1}", BSScene.DetailLogZero, this); | ||
168 | BulletSimAPI.DeleteCollisionShape2(physicsScene.World.ptr, ptr); | ||
169 | ptr = IntPtr.Zero; | ||
170 | // Garbage collection will free up this instance. | ||
171 | } | ||
172 | } | ||
173 | |||
174 | public class BSShapeMesh : BSShape | ||
175 | { | ||
176 | private static string LogHeader = "[BULLETSIM SHAPE MESH]"; | ||
177 | private static Dictionary<System.UInt64, BSShapeMesh> Meshes = new Dictionary<System.UInt64, BSShapeMesh>(); | ||
178 | |||
179 | public BSShapeMesh() | ||
180 | { | ||
181 | base.Initialize(); | ||
182 | } | ||
183 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
184 | public override void Dereference(BSScene physicsScene) { } | ||
185 | } | ||
186 | |||
187 | public class BSShapeHull : BSShape | ||
188 | { | ||
189 | private static string LogHeader = "[BULLETSIM SHAPE HULL]"; | ||
190 | private static Dictionary<System.UInt64, BSShapeHull> Hulls = new Dictionary<System.UInt64, BSShapeHull>(); | ||
191 | |||
192 | public BSShapeHull() | ||
193 | { | ||
194 | base.Initialize(); | ||
195 | } | ||
196 | public static BSShape GetReference() { return new BSShapeNull(); } | ||
197 | public override void Dereference(BSScene physicsScene) { } | ||
198 | } | ||
199 | |||
200 | public class BSShapeCompound : BSShape | ||
201 | { | ||
202 | private static string LogHeader = "[BULLETSIM SHAPE COMPOUND]"; | ||
203 | public BSShapeCompound() | ||
204 | { | ||
205 | base.Initialize(); | ||
206 | } | ||
207 | public static BSShape GetReference(BSPhysObject prim) | ||
208 | { | ||
209 | return new BSShapeNull(); | ||
210 | } | ||
211 | public override void Dereference(BSScene physicsScene) { } | ||
212 | } | ||
213 | } | ||