aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs69
1 files changed, 39 insertions, 30 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
index dca7150..1d55ce3 100755
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
@@ -88,11 +88,11 @@ public sealed class BSTerrainMesh : BSTerrainPhys
88 // Something is very messed up and a crash is in our future. 88 // Something is very messed up and a crash is in our future.
89 return; 89 return;
90 } 90 }
91 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}",
92 ID, indicesCount, indices.Length, verticesCount, vertices.Length);
91 93
92 m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr, 94 m_terrainShape = PhysicsScene.PE.CreateMeshShape(PhysicsScene.World, indicesCount, indices, verticesCount, vertices);
93 indicesCount, indices, verticesCount, vertices), 95 if (!m_terrainShape.HasPhysicalShape)
94 BSPhysicsShapeType.SHAPE_MESH);
95 if (m_terrainShape.ptr == IntPtr.Zero)
96 { 96 {
97 // DISASTER!! 97 // DISASTER!!
98 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID); 98 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
@@ -104,8 +104,8 @@ public sealed class BSTerrainMesh : BSTerrainPhys
104 Vector3 pos = regionBase; 104 Vector3 pos = regionBase;
105 Quaternion rot = Quaternion.Identity; 105 Quaternion rot = Quaternion.Identity;
106 106
107 m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2( m_terrainShape.ptr, ID, pos, rot)); 107 m_terrainBody = PhysicsScene.PE.CreateBodyWithDefaultMotionState(m_terrainShape, ID, pos, rot);
108 if (m_terrainBody.ptr == IntPtr.Zero) 108 if (!m_terrainBody.HasPhysicalBody)
109 { 109 {
110 // DISASTER!! 110 // DISASTER!!
111 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase); 111 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
@@ -114,39 +114,38 @@ public sealed class BSTerrainMesh : BSTerrainPhys
114 } 114 }
115 115
116 // Set current terrain attributes 116 // Set current terrain attributes
117 BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction); 117 PhysicsScene.PE.SetFriction(m_terrainBody, BSParam.TerrainFriction);
118 BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction); 118 PhysicsScene.PE.SetHitFraction(m_terrainBody, BSParam.TerrainHitFraction);
119 BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution); 119 PhysicsScene.PE.SetRestitution(m_terrainBody, BSParam.TerrainRestitution);
120 BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT); 120 PhysicsScene.PE.SetCollisionFlags(m_terrainBody, CollisionFlags.CF_STATIC_OBJECT);
121 121
122 // Static objects are not very massive. 122 // Static objects are not very massive.
123 BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero); 123 PhysicsScene.PE.SetMassProps(m_terrainBody, 0f, Vector3.Zero);
124 124
125 // Return the new terrain to the world of physical objects 125 // Put the new terrain to the world of physical objects
126 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); 126 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_terrainBody);
127 127
128 // redo its bounding box now that it is in the world 128 // Redo its bounding box now that it is in the world
129 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr); 129 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_terrainBody);
130 130
131 BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr, 131 m_terrainBody.collisionType = CollisionType.Terrain;
132 (uint)CollisionFilterGroups.TerrainFilter, 132 m_terrainBody.ApplyCollisionMask(PhysicsScene);
133 (uint)CollisionFilterGroups.TerrainMask);
134 133
135 // Make it so the terrain will not move or be considered for movement. 134 // Make it so the terrain will not move or be considered for movement.
136 BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION); 135 PhysicsScene.PE.ForceActivationState(m_terrainBody, ActivationState.DISABLE_SIMULATION);
137 } 136 }
138 137
139 public override void Dispose() 138 public override void Dispose()
140 { 139 {
141 if (m_terrainBody.ptr != IntPtr.Zero) 140 if (m_terrainBody.HasPhysicalBody)
142 { 141 {
143 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr); 142 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_terrainBody);
144 // Frees both the body and the shape. 143 // Frees both the body and the shape.
145 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr); 144 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_terrainBody);
146 } 145 }
147 } 146 }
148 147
149 public override float GetHeightAtXYZ(Vector3 pos) 148 public override float GetTerrainHeightAtXYZ(Vector3 pos)
150 { 149 {
151 // For the moment use the saved heightmap to get the terrain height. 150 // For the moment use the saved heightmap to get the terrain height.
152 // TODO: raycast downward to find the true terrain below the position. 151 // TODO: raycast downward to find the true terrain below the position.
@@ -167,6 +166,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys
167 return ret; 166 return ret;
168 } 167 }
169 168
169 // The passed position is relative to the base of the region.
170 public override float GetWaterLevelAtXYZ(Vector3 pos)
171 {
172 return PhysicsScene.SimpleWaterLevel;
173 }
174
170 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). 175 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2().
171 // Return 'true' if successfully created. 176 // Return 'true' if successfully created.
172 public static bool ConvertHeightmapToMesh( 177 public static bool ConvertHeightmapToMesh(
@@ -188,6 +193,11 @@ public sealed class BSTerrainMesh : BSTerrainPhys
188 // Simple mesh creation which assumes magnification == 1. 193 // Simple mesh creation which assumes magnification == 1.
189 // TODO: do a more general solution that scales, adds new vertices and smoothes the result. 194 // TODO: do a more general solution that scales, adds new vertices and smoothes the result.
190 195
196 // Create an array of vertices that is sizeX+1 by sizeY+1 (note the loop
197 // from zero to <= sizeX). The triangle indices are then generated as two triangles
198 // per heightmap point. There are sizeX by sizeY of these squares. The extra row and
199 // column of vertices are used to complete the triangles of the last row and column
200 // of the heightmap.
191 try 201 try
192 { 202 {
193 // One vertice per heightmap value plus the vertices off the top and bottom edge. 203 // One vertice per heightmap value plus the vertices off the top and bottom edge.
@@ -200,16 +210,18 @@ public sealed class BSTerrainMesh : BSTerrainPhys
200 float magY = (float)sizeY / extentY; 210 float magY = (float)sizeY / extentY;
201 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}", 211 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}",
202 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY); 212 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY);
213 float minHeight = float.MaxValue;
203 // Note that sizeX+1 vertices are created since there is land between this and the next region. 214 // Note that sizeX+1 vertices are created since there is land between this and the next region.
204 for (int yy = 0; yy <= sizeY; yy++) 215 for (int yy = 0; yy <= sizeY; yy++)
205 { 216 {
206 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times 217 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we go around sizeX + 1 times
207 { 218 {
208 int offset = yy * sizeX + xx; 219 int offset = yy * sizeX + xx;
209 // Extend the height from the height from the last row or column 220 // Extend the height with the height from the last row or column
210 if (yy == sizeY) offset -= sizeX; 221 if (yy == sizeY) offset -= sizeX;
211 if (xx == sizeX) offset -= 1; 222 if (xx == sizeX) offset -= 1;
212 float height = heightMap[offset]; 223 float height = heightMap[offset];
224 minHeight = Math.Min(minHeight, height);
213 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; 225 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X;
214 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; 226 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y;
215 vertices[verticesCount + 2] = height + extentBase.Z; 227 vertices[verticesCount + 2] = height + extentBase.Z;
@@ -217,14 +229,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys
217 } 229 }
218 } 230 }
219 verticesCount = verticesCount / 3; 231 verticesCount = verticesCount / 3;
220 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}",
221 BSScene.DetailLogZero, verticesCount);
222 232
223 for (int yy = 0; yy < sizeY; yy++) 233 for (int yy = 0; yy < sizeY; yy++)
224 { 234 {
225 for (int xx = 0; xx < sizeX; xx++) 235 for (int xx = 0; xx < sizeX; xx++)
226 { 236 {
227 int offset = yy * sizeX + xx; 237 int offset = yy * (sizeX + 1) + xx;
228 // Each vertices is presumed to be the upper left corner of a box of two triangles 238 // Each vertices is presumed to be the upper left corner of a box of two triangles
229 indices[indicesCount + 0] = offset; 239 indices[indicesCount + 0] = offset;
230 indices[indicesCount + 1] = offset + 1; 240 indices[indicesCount + 1] = offset + 1;
@@ -235,8 +245,7 @@ public sealed class BSTerrainMesh : BSTerrainPhys
235 indicesCount += 6; 245 indicesCount += 6;
236 } 246 }
237 } 247 }
238 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG 248
239 LogHeader, indicesCount); // DEBUG
240 ret = true; 249 ret = true;
241 } 250 }
242 catch (Exception e) 251 catch (Exception e)