aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
diff options
context:
space:
mode:
authorRobert Adams2012-11-20 20:36:49 -0800
committerRobert Adams2012-11-21 16:43:21 -0800
commit34cbc738a84b7946a87e8bfd3bb04869519b3dee (patch)
tree1e0c0891022162f4aac9793ef5937c254bbaa2fa /OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
parentBulletSim: pull heightmap implementation out of the terrain manager so a mesh... (diff)
downloadopensim-SC_OLD-34cbc738a84b7946a87e8bfd3bb04869519b3dee.zip
opensim-SC_OLD-34cbc738a84b7946a87e8bfd3bb04869519b3dee.tar.gz
opensim-SC_OLD-34cbc738a84b7946a87e8bfd3bb04869519b3dee.tar.bz2
opensim-SC_OLD-34cbc738a84b7946a87e8bfd3bb04869519b3dee.tar.xz
BulletSim: enablement and debugging of mesh terrain.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs229
1 files changed, 220 insertions, 9 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
index 387c78b..bbb014a 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
@@ -44,30 +44,241 @@ public sealed class BSTerrainMesh : BSTerrainPhys
44{ 44{
45 static string LogHeader = "[BULLETSIM TERRAIN MESH]"; 45 static string LogHeader = "[BULLETSIM TERRAIN MESH]";
46 46
47 public BSTerrainMesh(BSScene physicsScene, uint id, Vector3 regionSize) 47 private float[] m_savedHeightMap;
48 : base(physicsScene) 48 int m_sizeX;
49 int m_sizeY;
50
51 BulletShape m_terrainShape;
52 BulletBody m_terrainBody;
53
54 public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, Vector3 regionSize)
55 : base(physicsScene, regionBase, id)
49 { 56 {
50 } 57 }
51 58
52 public BSTerrainMesh(BSScene physicsScene /* parameters for making mesh */) 59 public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id /* parameters for making mesh */)
53 : base(physicsScene) 60 : base(physicsScene, regionBase, id)
54 { 61 {
55 } 62 }
56 63
64 // Create terrain mesh from a heightmap.
65 public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id, float[] initialMap,
66 Vector3 minCoords, Vector3 maxCoords)
67 : base(physicsScene, regionBase, id)
68 {
69 int indicesCount;
70 int[] indices;
71 int verticesCount;
72 float[] vertices;
73
74 m_savedHeightMap = initialMap;
75
76 m_sizeX = (int)(maxCoords.X - minCoords.X);
77 m_sizeY = (int)(maxCoords.Y - minCoords.Y);
78
79 if (!BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, initialMap, m_sizeX, m_sizeY,
80 (float)m_sizeX, (float)m_sizeY,
81 Vector3.Zero, 1.0f,
82 out indicesCount, out indices, out verticesCount, out vertices))
83 {
84 // DISASTER!!
85 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID);
86 PhysicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh! base={1}", LogHeader, TerrainBase);
87 // Something is very messed up and a crash is in our future.
88 return;
89 }
90 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterConvertHeightmapToMesh,ver={1},ind={2}",
91 ID, verticesCount, indicesCount);
92
93 m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr,
94 indicesCount, indices, verticesCount, vertices),
95 PhysicsShapeType.SHAPE_MESH);
96 if (m_terrainShape.ptr == IntPtr.Zero)
97 {
98 // DISASTER!!
99 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
100 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase);
101 // Something is very messed up and a crash is in our future.
102 return;
103 }
104 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateShape,shape={1}", ID, m_terrainShape);
105
106 // The terrain object initial position is at the center of the object
107 Vector3 centerPos;
108 centerPos.X = minCoords.X + (m_sizeX / 2f);
109 centerPos.Y = minCoords.Y + (m_sizeY / 2f);
110 centerPos.Z = minCoords.Z + ((maxCoords.Z - minCoords.Z) / 2f);
111 Quaternion rot = Quaternion.Identity;
112
113 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,creatingBody,centerPos={1},rot={2}", ID, centerPos, rot);
114 m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2(
115 m_terrainShape.ptr, ID, centerPos, rot));
116 if (m_terrainBody.ptr == IntPtr.Zero)
117 {
118 // DISASTER!!
119 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
120 // Something is very messed up and a crash is in our future.
121 return;
122 }
123 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,afterCreateBody,body={1}", ID, m_terrainBody);
124
125 // Set current terrain attributes
126 BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction);
127 BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
128 BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution);
129 BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
130
131 // Static objects are not very massive.
132 BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero);
133
134 // Return the new terrain to the world of physical objects
135 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr);
136
137 // redo its bounding box now that it is in the world
138 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr);
139
140 BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr,
141 (uint)CollisionFilterGroups.TerrainFilter,
142 (uint)CollisionFilterGroups.TerrainMask);
143
144 // Make it so the terrain will not move or be considered for movement.
145 BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION);
146 }
147
57 public override void Dispose() 148 public override void Dispose()
58 { 149 {
59 return; 150 if (m_terrainBody.ptr != IntPtr.Zero)
151 {
152 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr);
153 // Frees both the body and the shape.
154 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr);
155 }
60 } 156 }
61 157
62 public override float GetHeightAtXYZ(Vector3 pos) 158 public override float GetHeightAtXYZ(Vector3 pos)
63 { 159 {
64 return 12345f; 160 // For the moment use the saved heightmap to get the terrain height.
161 // TODO: raycast downward to find the true terrain below the position.
162 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
163
164 int mapIndex = (int)pos.Y * m_sizeY + (int)pos.X;
165 try
166 {
167 ret = m_savedHeightMap[mapIndex];
168 }
169 catch
170 {
171 // Sometimes they give us wonky values of X and Y. Give a warning and return something.
172 PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
173 LogHeader, TerrainBase, pos);
174 ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
175 }
176 return ret;
65 } 177 }
66 178
67 public override Vector3 TerrainBase 179 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2().
180 // Return 'true' if successfully created.
181 public static bool ConvertHeightmapToMesh(
182 BSScene physicsScene,
183 float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap
184 float extentX, float extentY, // zero based range for output vertices
185 Vector3 extentBase, // base to be added to all vertices
186 float magnification, // number of vertices to create between heightMap coords
187 out int indicesCountO, out int[] indicesO,
188 out int verticesCountO, out float[] verticesO)
68 { 189 {
69 get { return Vector3.Zero; } 190 bool ret = false;
70 } 191
192 int indicesCount = 0;
193 int verticesCount = 0;
194 int[] indices = new int[0];
195 float[] vertices = new float[0];
71 196
197 // Simple mesh creation which assumes magnification == 1, sizeX == extentX and sizeY == extentY.
198 // TODO: do a more general solution that scales, adds new vertices and smoothes the result.
199
200 try
201 {
202 // One vertice per heightmap value plus the vertices off the top and bottom edge.
203 int totalVertices = (sizeX + 1) * (sizeY + 1);
204 vertices = new float[totalVertices * 3];
205 int totalIndices = sizeX * sizeY * 6;
206 indices = new int[totalIndices];
207
208 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2}",
209 BSScene.DetailLogZero, totalVertices, totalIndices);
210 float magX = (float)sizeX / extentX;
211 float magY = (float)sizeY / extentY;
212 // Note that sizeX+1 vertices are created since there is land between this and the next region.
213 for (int yy = 0; yy <= sizeY; yy++)
214 {
215 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times
216 {
217 int offset = yy * sizeX + xx;
218 // Extend the height from the height from the last row or column
219 if (yy == sizeY) offset -= sizeX;
220 if (xx == sizeX) offset -= 1;
221 float height = heightMap[offset];
222 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X;
223 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y;
224 vertices[verticesCount + 2] = height + extentBase.Z;
225 if (physicsScene.PhysicsLogging.Enabled && verticesCount < 900) // DEBUG DEBUG DEBUG
226 {
227 Vector3 genVertex = new Vector3(
228 vertices[verticesCount + 0],
229 vertices[verticesCount + 1],
230 vertices[verticesCount + 2]);
231 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,ii={1},vertex={2}",
232 BSScene.DetailLogZero, verticesCount/3, genVertex);
233 }
234 verticesCount += 3;
235 }
236 }
237 verticesCount = verticesCount / 3;
238 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}",
239 BSScene.DetailLogZero, verticesCount);
240
241 for (int yy = 0; yy < sizeY; yy++)
242 {
243 for (int xx = 0; xx < sizeX; xx++)
244 {
245 int offset = yy * sizeX + xx;
246 // Each vertices is presumed to be the upper left corner of a box of two triangles
247 indices[indicesCount + 0] = offset;
248 indices[indicesCount + 1] = offset + 1;
249 indices[indicesCount + 2] = offset + sizeX + 1; // accounting for the extra column
250 indices[indicesCount + 3] = offset + 1;
251 indices[indicesCount + 4] = offset + sizeX + 2;
252 indices[indicesCount + 5] = offset + sizeX + 1;
253 if (indicesCount < (300 * 6)) // DEBUG DEBUG DEBUG
254 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,i0={1},i1={2},i2={3},i3={4},i4={5},i5={6}", // DEEBUG DEBUG DEBUG
255 BSScene.DetailLogZero,
256 indices[indicesCount + 0],
257 indices[indicesCount + 1],
258 indices[indicesCount + 2],
259 indices[indicesCount + 3],
260 indices[indicesCount + 4],
261 indices[indicesCount + 5]
262 );
263 indicesCount += 6;
264 }
265 }
266 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG
267 LogHeader, indicesCount); // DEBUG
268 ret = true;
269 }
270 catch (Exception e)
271 {
272 physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. Base={1}, e={2}",
273 LogHeader, extentBase, e);
274 }
275
276 indicesCountO = indicesCount;
277 indicesO = indices;
278 verticesCountO = verticesCount;
279 verticesO = vertices;
280
281 return ret;
282 }
72} 283}
73} 284}