diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs index dca7150..8244f02 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,40 @@ 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); |
145 | m_terrainBody.Clear(); | ||
146 | m_terrainShape.Clear(); | ||
146 | } | 147 | } |
147 | } | 148 | } |
148 | 149 | ||
149 | public override float GetHeightAtXYZ(Vector3 pos) | 150 | public override float GetTerrainHeightAtXYZ(Vector3 pos) |
150 | { | 151 | { |
151 | // For the moment use the saved heightmap to get the terrain height. | 152 | // For the moment use the saved heightmap to get the terrain height. |
152 | // TODO: raycast downward to find the true terrain below the position. | 153 | // TODO: raycast downward to find the true terrain below the position. |
@@ -167,6 +168,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
167 | return ret; | 168 | return ret; |
168 | } | 169 | } |
169 | 170 | ||
171 | // The passed position is relative to the base of the region. | ||
172 | public override float GetWaterLevelAtXYZ(Vector3 pos) | ||
173 | { | ||
174 | return PhysicsScene.SimpleWaterLevel; | ||
175 | } | ||
176 | |||
170 | // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). | 177 | // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). |
171 | // Return 'true' if successfully created. | 178 | // Return 'true' if successfully created. |
172 | public static bool ConvertHeightmapToMesh( | 179 | public static bool ConvertHeightmapToMesh( |
@@ -188,6 +195,11 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
188 | // Simple mesh creation which assumes magnification == 1. | 195 | // Simple mesh creation which assumes magnification == 1. |
189 | // TODO: do a more general solution that scales, adds new vertices and smoothes the result. | 196 | // TODO: do a more general solution that scales, adds new vertices and smoothes the result. |
190 | 197 | ||
198 | // Create an array of vertices that is sizeX+1 by sizeY+1 (note the loop | ||
199 | // from zero to <= sizeX). The triangle indices are then generated as two triangles | ||
200 | // per heightmap point. There are sizeX by sizeY of these squares. The extra row and | ||
201 | // column of vertices are used to complete the triangles of the last row and column | ||
202 | // of the heightmap. | ||
191 | try | 203 | try |
192 | { | 204 | { |
193 | // One vertice per heightmap value plus the vertices off the top and bottom edge. | 205 | // One vertice per heightmap value plus the vertices off the top and bottom edge. |
@@ -200,16 +212,18 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
200 | float magY = (float)sizeY / extentY; | 212 | float magY = (float)sizeY / extentY; |
201 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}", | 213 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}", |
202 | BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY); | 214 | BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY); |
215 | float minHeight = float.MaxValue; | ||
203 | // Note that sizeX+1 vertices are created since there is land between this and the next region. | 216 | // 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++) | 217 | for (int yy = 0; yy <= sizeY; yy++) |
205 | { | 218 | { |
206 | for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we got through sizeX + 1 times | 219 | for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we go around sizeX + 1 times |
207 | { | 220 | { |
208 | int offset = yy * sizeX + xx; | 221 | int offset = yy * sizeX + xx; |
209 | // Extend the height from the height from the last row or column | 222 | // Extend the height with the height from the last row or column |
210 | if (yy == sizeY) offset -= sizeX; | 223 | if (yy == sizeY) offset -= sizeX; |
211 | if (xx == sizeX) offset -= 1; | 224 | if (xx == sizeX) offset -= 1; |
212 | float height = heightMap[offset]; | 225 | float height = heightMap[offset]; |
226 | minHeight = Math.Min(minHeight, height); | ||
213 | vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; | 227 | vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; |
214 | vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; | 228 | vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; |
215 | vertices[verticesCount + 2] = height + extentBase.Z; | 229 | vertices[verticesCount + 2] = height + extentBase.Z; |
@@ -217,14 +231,12 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
217 | } | 231 | } |
218 | } | 232 | } |
219 | verticesCount = verticesCount / 3; | 233 | verticesCount = verticesCount / 3; |
220 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeVerts,verCount={1}", | ||
221 | BSScene.DetailLogZero, verticesCount); | ||
222 | 234 | ||
223 | for (int yy = 0; yy < sizeY; yy++) | 235 | for (int yy = 0; yy < sizeY; yy++) |
224 | { | 236 | { |
225 | for (int xx = 0; xx < sizeX; xx++) | 237 | for (int xx = 0; xx < sizeX; xx++) |
226 | { | 238 | { |
227 | int offset = yy * sizeX + xx; | 239 | int offset = yy * (sizeX + 1) + xx; |
228 | // Each vertices is presumed to be the upper left corner of a box of two triangles | 240 | // Each vertices is presumed to be the upper left corner of a box of two triangles |
229 | indices[indicesCount + 0] = offset; | 241 | indices[indicesCount + 0] = offset; |
230 | indices[indicesCount + 1] = offset + 1; | 242 | indices[indicesCount + 1] = offset + 1; |
@@ -235,8 +247,7 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
235 | indicesCount += 6; | 247 | indicesCount += 6; |
236 | } | 248 | } |
237 | } | 249 | } |
238 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,completeIndices,indCount={1}", // DEEBUG DEBUG DEBUG | 250 | |
239 | LogHeader, indicesCount); // DEBUG | ||
240 | ret = true; | 251 | ret = true; |
241 | } | 252 | } |
242 | catch (Exception e) | 253 | catch (Exception e) |