diff options
Diffstat (limited to 'OpenSim/Region/Physics')
7 files changed, 213 insertions, 20 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs b/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs index 3a27d2c..77ea3ed 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSAPIUnman.cs | |||
@@ -286,7 +286,7 @@ public override void SetShapeCollisionMargin(BulletShape shape, float margin) | |||
286 | { | 286 | { |
287 | BulletShapeUnman shapeu = shape as BulletShapeUnman; | 287 | BulletShapeUnman shapeu = shape as BulletShapeUnman; |
288 | if (shapeu != null && shapeu.HasPhysicalShape) | 288 | if (shapeu != null && shapeu.HasPhysicalShape) |
289 | BSAPICPP.SetShapeCollisionMargin2(shapeu.ptr, margin); | 289 | BSAPICPP.SetShapeCollisionMargin(shapeu.ptr, margin); |
290 | } | 290 | } |
291 | 291 | ||
292 | public override BulletShape BuildCapsuleShape(BulletWorld world, float radius, float height, Vector3 scale) | 292 | public override BulletShape BuildCapsuleShape(BulletWorld world, float radius, float height, Vector3 scale) |
@@ -1420,7 +1420,7 @@ public static extern IntPtr BuildNativeShape2(IntPtr world, ShapeData shapeData) | |||
1420 | public static extern bool IsNativeShape2(IntPtr shape); | 1420 | public static extern bool IsNativeShape2(IntPtr shape); |
1421 | 1421 | ||
1422 | [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] | 1422 | [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
1423 | public static extern void SetShapeCollisionMargin2(IntPtr shape, float margin); | 1423 | public static extern void SetShapeCollisionMargin(IntPtr shape, float margin); |
1424 | 1424 | ||
1425 | [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] | 1425 | [DllImport("BulletSim", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
1426 | public static extern IntPtr BuildCapsuleShape2(IntPtr world, float radius, float height, Vector3 scale); | 1426 | public static extern IntPtr BuildCapsuleShape2(IntPtr world, float radius, float height, Vector3 scale); |
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs index 38596fa..5549984 100644 --- a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs | |||
@@ -1201,8 +1201,9 @@ namespace OpenSim.Region.Physics.BulletSPlugin | |||
1201 | 1201 | ||
1202 | VehicleAddForce(appliedGravity); | 1202 | VehicleAddForce(appliedGravity); |
1203 | 1203 | ||
1204 | VDetailLog("{0}, MoveLinear,applyGravity,vehGrav={1},collid={2},appliedForce={3}", | 1204 | VDetailLog("{0}, MoveLinear,applyGravity,vehGrav={1},collid={2},fudge={3},mass={4},appliedForce={3}", |
1205 | Prim.LocalID, m_VehicleGravity, Prim.IsColliding, appliedGravity); | 1205 | Prim.LocalID, m_VehicleGravity, |
1206 | Prim.IsColliding, BSParam.VehicleGroundGravityFudge, m_vehicleMass, appliedGravity); | ||
1206 | } | 1207 | } |
1207 | 1208 | ||
1208 | // ======================================================================= | 1209 | // ======================================================================= |
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs index 77bdacb..4d89a88 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs | |||
@@ -39,6 +39,20 @@ public static class BSParam | |||
39 | { | 39 | { |
40 | private static string LogHeader = "[BULLETSIM PARAMETERS]"; | 40 | private static string LogHeader = "[BULLETSIM PARAMETERS]"; |
41 | 41 | ||
42 | // Tuning notes: | ||
43 | // From: http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=6575 | ||
44 | // Contact points can be added even if the distance is positive. The constraint solver can deal with | ||
45 | // contacts with positive distances as well as negative (penetration). Contact points are discarded | ||
46 | // if the distance exceeds a certain threshold. | ||
47 | // Bullet has a contact processing threshold and a contact breaking threshold. | ||
48 | // If the distance is larger than the contact breaking threshold, it will be removed after one frame. | ||
49 | // If the distance is larger than the contact processing threshold, the constraint solver will ignore it. | ||
50 | |||
51 | // This is separate/independent from the collision margin. The collision margin increases the object a bit | ||
52 | // to improve collision detection performance and accuracy. | ||
53 | // =================== | ||
54 | // From: | ||
55 | |||
42 | // Level of Detail values kept as float because that's what the Meshmerizer wants | 56 | // Level of Detail values kept as float because that's what the Meshmerizer wants |
43 | public static float MeshLOD { get; private set; } | 57 | public static float MeshLOD { get; private set; } |
44 | public static float MeshCircularLOD { get; private set; } | 58 | public static float MeshCircularLOD { get; private set; } |
@@ -74,9 +88,11 @@ public static class BSParam | |||
74 | public static bool ShouldRemoveZeroWidthTriangles { get; private set; } | 88 | public static bool ShouldRemoveZeroWidthTriangles { get; private set; } |
75 | 89 | ||
76 | public static float TerrainImplementation { get; private set; } | 90 | public static float TerrainImplementation { get; private set; } |
91 | public static int TerrainMeshMagnification { get; private set; } | ||
77 | public static float TerrainFriction { get; private set; } | 92 | public static float TerrainFriction { get; private set; } |
78 | public static float TerrainHitFraction { get; private set; } | 93 | public static float TerrainHitFraction { get; private set; } |
79 | public static float TerrainRestitution { get; private set; } | 94 | public static float TerrainRestitution { get; private set; } |
95 | public static float TerrainContactProcessingThreshold { get; private set; } | ||
80 | public static float TerrainCollisionMargin { get; private set; } | 96 | public static float TerrainCollisionMargin { get; private set; } |
81 | 97 | ||
82 | public static float DefaultFriction { get; private set; } | 98 | public static float DefaultFriction { get; private set; } |
@@ -446,6 +462,10 @@ public static class BSParam | |||
446 | (float)BSTerrainPhys.TerrainImplementation.Mesh, | 462 | (float)BSTerrainPhys.TerrainImplementation.Mesh, |
447 | (s) => { return TerrainImplementation; }, | 463 | (s) => { return TerrainImplementation; }, |
448 | (s,v) => { TerrainImplementation = v; } ), | 464 | (s,v) => { TerrainImplementation = v; } ), |
465 | new ParameterDefn<int>("TerrainMeshMagnification", "Number of times the 256x256 heightmap is multiplied to create the terrain mesh" , | ||
466 | 3, | ||
467 | (s) => { return TerrainMeshMagnification; }, | ||
468 | (s,v) => { TerrainMeshMagnification = v; } ), | ||
449 | new ParameterDefn<float>("TerrainFriction", "Factor to reduce movement against terrain surface" , | 469 | new ParameterDefn<float>("TerrainFriction", "Factor to reduce movement against terrain surface" , |
450 | 0.3f, | 470 | 0.3f, |
451 | (s) => { return TerrainFriction; }, | 471 | (s) => { return TerrainFriction; }, |
@@ -458,6 +478,10 @@ public static class BSParam | |||
458 | 0f, | 478 | 0f, |
459 | (s) => { return TerrainRestitution; }, | 479 | (s) => { return TerrainRestitution; }, |
460 | (s,v) => { TerrainRestitution = v; /* TODO: set on real terrain */ } ), | 480 | (s,v) => { TerrainRestitution = v; /* TODO: set on real terrain */ } ), |
481 | new ParameterDefn<float>("TerrainContactProcessingThreshold", "Distance from terrain to stop processing collisions" , | ||
482 | 0.0f, | ||
483 | (s) => { return TerrainContactProcessingThreshold; }, | ||
484 | (s,v) => { TerrainContactProcessingThreshold = v; /* TODO: set on real terrain */ } ), | ||
461 | new ParameterDefn<float>("TerrainCollisionMargin", "Margin where collision checking starts" , | 485 | new ParameterDefn<float>("TerrainCollisionMargin", "Margin where collision checking starts" , |
462 | 0.08f, | 486 | 0.08f, |
463 | (s) => { return TerrainCollisionMargin; }, | 487 | (s) => { return TerrainCollisionMargin; }, |
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs index a465613..2cbbe9a 100644 --- a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | |||
@@ -947,9 +947,9 @@ public class BSPrim : BSPhysObject | |||
947 | ZeroMotion(true); | 947 | ZeroMotion(true); |
948 | 948 | ||
949 | // Set various physical properties so other object interact properly | 949 | // Set various physical properties so other object interact properly |
950 | MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false); | ||
951 | PhysicsScene.PE.SetFriction(PhysBody, Friction); | 950 | PhysicsScene.PE.SetFriction(PhysBody, Friction); |
952 | PhysicsScene.PE.SetRestitution(PhysBody, Restitution); | 951 | PhysicsScene.PE.SetRestitution(PhysBody, Restitution); |
952 | PhysicsScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold); | ||
953 | 953 | ||
954 | // Mass is zero which disables a bunch of physics stuff in Bullet | 954 | // Mass is zero which disables a bunch of physics stuff in Bullet |
955 | UpdatePhysicalMassProperties(0f, false); | 955 | UpdatePhysicalMassProperties(0f, false); |
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs b/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs index 2e54a93..05c147d 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSShapeCollection.cs | |||
@@ -687,7 +687,7 @@ public sealed class BSShapeCollection : IDisposable | |||
687 | } | 687 | } |
688 | else | 688 | else |
689 | { | 689 | { |
690 | PhysicsScene.Logger.ErrorFormat("{0} All mesh triangles degenerate. Prim {1} at {2} in {3}", | 690 | PhysicsScene.Logger.DebugFormat("{0} All mesh triangles degenerate. Prim {1} at {2} in {3}", |
691 | LogHeader, prim.PhysObjectName, prim.RawPosition, PhysicsScene.Name); | 691 | LogHeader, prim.PhysObjectName, prim.RawPosition, PhysicsScene.Name); |
692 | } | 692 | } |
693 | } | 693 | } |
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs index e8040d8..a60946d 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs | |||
@@ -263,6 +263,7 @@ public sealed class BSTerrainManager : IDisposable | |||
263 | 263 | ||
264 | if (MegaRegionParentPhysicsScene == null) | 264 | if (MegaRegionParentPhysicsScene == null) |
265 | { | 265 | { |
266 | // This terrain is not part of the mega-region scheme. Create vanilla terrain. | ||
266 | BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords); | 267 | BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords); |
267 | m_terrains.Add(terrainRegionBase, newTerrainPhys); | 268 | m_terrains.Add(terrainRegionBase, newTerrainPhys); |
268 | 269 | ||
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs index 57a5ff2..a9cd8a1 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs | |||
@@ -76,11 +76,26 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
76 | m_sizeX = (int)(maxCoords.X - minCoords.X); | 76 | m_sizeX = (int)(maxCoords.X - minCoords.X); |
77 | m_sizeY = (int)(maxCoords.Y - minCoords.Y); | 77 | m_sizeY = (int)(maxCoords.Y - minCoords.Y); |
78 | 78 | ||
79 | if (!BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, initialMap, | 79 | bool meshCreationSuccess = false; |
80 | m_sizeX, m_sizeY, | 80 | if (BSParam.TerrainMeshMagnification == 1) |
81 | (float)m_sizeX, (float)m_sizeY, | 81 | { |
82 | Vector3.Zero, 1.0f, | 82 | // If a magnification of one, use the old routine that is tried and true. |
83 | out indicesCount, out indices, out verticesCount, out vertices)) | 83 | meshCreationSuccess = BSTerrainMesh.ConvertHeightmapToMesh(PhysicsScene, |
84 | initialMap, m_sizeX, m_sizeY, // input size | ||
85 | Vector3.Zero, // base for mesh | ||
86 | out indicesCount, out indices, out verticesCount, out vertices); | ||
87 | } | ||
88 | else | ||
89 | { | ||
90 | // Other magnifications use the newer routine | ||
91 | meshCreationSuccess = BSTerrainMesh.ConvertHeightmapToMesh2(PhysicsScene, | ||
92 | initialMap, m_sizeX, m_sizeY, // input size | ||
93 | BSParam.TerrainMeshMagnification, | ||
94 | physicsScene.TerrainManager.DefaultRegionSize, | ||
95 | Vector3.Zero, // base for mesh | ||
96 | out indicesCount, out indices, out verticesCount, out vertices); | ||
97 | } | ||
98 | if (!meshCreationSuccess) | ||
84 | { | 99 | { |
85 | // DISASTER!! | 100 | // DISASTER!! |
86 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID); | 101 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID); |
@@ -88,6 +103,7 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
88 | // Something is very messed up and a crash is in our future. | 103 | // Something is very messed up and a crash is in our future. |
89 | return; | 104 | return; |
90 | } | 105 | } |
106 | |||
91 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}", | 107 | PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}", |
92 | ID, indicesCount, indices.Length, verticesCount, vertices.Length); | 108 | ID, indicesCount, indices.Length, verticesCount, vertices.Length); |
93 | 109 | ||
@@ -112,11 +128,13 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
112 | // Something is very messed up and a crash is in our future. | 128 | // Something is very messed up and a crash is in our future. |
113 | return; | 129 | return; |
114 | } | 130 | } |
131 | physicsScene.PE.SetShapeCollisionMargin(m_terrainShape, BSParam.TerrainCollisionMargin); | ||
115 | 132 | ||
116 | // Set current terrain attributes | 133 | // Set current terrain attributes |
117 | PhysicsScene.PE.SetFriction(m_terrainBody, BSParam.TerrainFriction); | 134 | PhysicsScene.PE.SetFriction(m_terrainBody, BSParam.TerrainFriction); |
118 | PhysicsScene.PE.SetHitFraction(m_terrainBody, BSParam.TerrainHitFraction); | 135 | PhysicsScene.PE.SetHitFraction(m_terrainBody, BSParam.TerrainHitFraction); |
119 | PhysicsScene.PE.SetRestitution(m_terrainBody, BSParam.TerrainRestitution); | 136 | PhysicsScene.PE.SetRestitution(m_terrainBody, BSParam.TerrainRestitution); |
137 | PhysicsScene.PE.SetContactProcessingThreshold(m_terrainBody, BSParam.TerrainContactProcessingThreshold); | ||
120 | PhysicsScene.PE.SetCollisionFlags(m_terrainBody, CollisionFlags.CF_STATIC_OBJECT); | 138 | PhysicsScene.PE.SetCollisionFlags(m_terrainBody, CollisionFlags.CF_STATIC_OBJECT); |
121 | 139 | ||
122 | // Static objects are not very massive. | 140 | // Static objects are not very massive. |
@@ -184,9 +202,7 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
184 | // Return 'true' if successfully created. | 202 | // Return 'true' if successfully created. |
185 | public static bool ConvertHeightmapToMesh( BSScene physicsScene, | 203 | public static bool ConvertHeightmapToMesh( BSScene physicsScene, |
186 | float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap | 204 | float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap |
187 | float extentX, float extentY, // zero based range for output vertices | ||
188 | Vector3 extentBase, // base to be added to all vertices | 205 | Vector3 extentBase, // base to be added to all vertices |
189 | float magnification, // number of vertices to create between heightMap coords | ||
190 | out int indicesCountO, out int[] indicesO, | 206 | out int indicesCountO, out int[] indicesO, |
191 | out int verticesCountO, out float[] verticesO) | 207 | out int verticesCountO, out float[] verticesO) |
192 | { | 208 | { |
@@ -207,17 +223,15 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
207 | // of the heightmap. | 223 | // of the heightmap. |
208 | try | 224 | try |
209 | { | 225 | { |
210 | // One vertice per heightmap value plus the vertices off the top and bottom edge. | 226 | // One vertice per heightmap value plus the vertices off the side and bottom edge. |
211 | int totalVertices = (sizeX + 1) * (sizeY + 1); | 227 | int totalVertices = (sizeX + 1) * (sizeY + 1); |
212 | vertices = new float[totalVertices * 3]; | 228 | vertices = new float[totalVertices * 3]; |
213 | int totalIndices = sizeX * sizeY * 6; | 229 | int totalIndices = sizeX * sizeY * 6; |
214 | indices = new int[totalIndices]; | 230 | indices = new int[totalIndices]; |
215 | 231 | ||
216 | float magX = (float)sizeX / extentX; | ||
217 | float magY = (float)sizeY / extentY; | ||
218 | if (physicsScene != null) | 232 | if (physicsScene != null) |
219 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}", | 233 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3}", |
220 | BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY); | 234 | BSScene.DetailLogZero, totalVertices, totalIndices, extentBase); |
221 | float minHeight = float.MaxValue; | 235 | float minHeight = float.MaxValue; |
222 | // Note that sizeX+1 vertices are created since there is land between this and the next region. | 236 | // Note that sizeX+1 vertices are created since there is land between this and the next region. |
223 | for (int yy = 0; yy <= sizeY; yy++) | 237 | for (int yy = 0; yy <= sizeY; yy++) |
@@ -230,8 +244,8 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
230 | if (xx == sizeX) offset -= 1; | 244 | if (xx == sizeX) offset -= 1; |
231 | float height = heightMap[offset]; | 245 | float height = heightMap[offset]; |
232 | minHeight = Math.Min(minHeight, height); | 246 | minHeight = Math.Min(minHeight, height); |
233 | vertices[verticesCount + 0] = (float)xx * magX + extentBase.X; | 247 | vertices[verticesCount + 0] = (float)xx + extentBase.X; |
234 | vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y; | 248 | vertices[verticesCount + 1] = (float)yy + extentBase.Y; |
235 | vertices[verticesCount + 2] = height + extentBase.Z; | 249 | vertices[verticesCount + 2] = height + extentBase.Z; |
236 | verticesCount += 3; | 250 | verticesCount += 3; |
237 | } | 251 | } |
@@ -270,5 +284,158 @@ public sealed class BSTerrainMesh : BSTerrainPhys | |||
270 | 284 | ||
271 | return ret; | 285 | return ret; |
272 | } | 286 | } |
287 | |||
288 | private class HeightMapGetter | ||
289 | { | ||
290 | private float[] m_heightMap; | ||
291 | private int m_sizeX; | ||
292 | private int m_sizeY; | ||
293 | public HeightMapGetter(float[] pHeightMap, int pSizeX, int pSizeY) | ||
294 | { | ||
295 | m_heightMap = pHeightMap; | ||
296 | m_sizeX = pSizeX; | ||
297 | m_sizeY = pSizeY; | ||
298 | } | ||
299 | // The heightmap is extended as an infinite plane at the last height | ||
300 | public float GetHeight(int xx, int yy) | ||
301 | { | ||
302 | int offset = 0; | ||
303 | // Extend the height with the height from the last row or column | ||
304 | if (yy >= m_sizeY) | ||
305 | if (xx >= m_sizeX) | ||
306 | offset = (m_sizeY - 1) * m_sizeX + (m_sizeX - 1); | ||
307 | else | ||
308 | offset = (m_sizeY - 1) * m_sizeX + xx; | ||
309 | else | ||
310 | if (xx >= m_sizeX) | ||
311 | offset = yy * m_sizeX + (m_sizeX - 1); | ||
312 | else | ||
313 | offset = yy * m_sizeX + xx; | ||
314 | |||
315 | return m_heightMap[offset]; | ||
316 | } | ||
317 | } | ||
318 | |||
319 | // Convert the passed heightmap to mesh information suitable for CreateMeshShape2(). | ||
320 | // Version that handles magnification. | ||
321 | // Return 'true' if successfully created. | ||
322 | public static bool ConvertHeightmapToMesh2( BSScene physicsScene, | ||
323 | float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap | ||
324 | int magnification, // number of vertices per heighmap step | ||
325 | Vector3 extent, // dimensions of the output mesh | ||
326 | Vector3 extentBase, // base to be added to all vertices | ||
327 | out int indicesCountO, out int[] indicesO, | ||
328 | out int verticesCountO, out float[] verticesO) | ||
329 | { | ||
330 | bool ret = false; | ||
331 | |||
332 | int indicesCount = 0; | ||
333 | int verticesCount = 0; | ||
334 | int[] indices = new int[0]; | ||
335 | float[] vertices = new float[0]; | ||
336 | |||
337 | HeightMapGetter hmap = new HeightMapGetter(heightMap, sizeX, sizeY); | ||
338 | |||
339 | // The vertices dimension of the output mesh | ||
340 | int meshX = sizeX * magnification; | ||
341 | int meshY = sizeY * magnification; | ||
342 | // The output size of one mesh step | ||
343 | float meshXStep = extent.X / meshX; | ||
344 | float meshYStep = extent.Y / meshY; | ||
345 | |||
346 | // Create an array of vertices that is meshX+1 by meshY+1 (note the loop | ||
347 | // from zero to <= meshX). The triangle indices are then generated as two triangles | ||
348 | // per heightmap point. There are meshX by meshY of these squares. The extra row and | ||
349 | // column of vertices are used to complete the triangles of the last row and column | ||
350 | // of the heightmap. | ||
351 | try | ||
352 | { | ||
353 | // Vertices for the output heightmap plus one on the side and bottom to complete triangles | ||
354 | int totalVertices = (meshX + 1) * (meshY + 1); | ||
355 | vertices = new float[totalVertices * 3]; | ||
356 | int totalIndices = meshX * meshY * 6; | ||
357 | indices = new int[totalIndices]; | ||
358 | |||
359 | if (physicsScene != null) | ||
360 | physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh2,inSize={1},outSize={2},totVert={3},totInd={4},extentBase={5}", | ||
361 | BSScene.DetailLogZero, new Vector2(sizeX, sizeY), new Vector2(meshX, meshY), | ||
362 | totalVertices, totalIndices, extentBase); | ||
363 | |||
364 | float minHeight = float.MaxValue; | ||
365 | // Note that sizeX+1 vertices are created since there is land between this and the next region. | ||
366 | // Loop through the output vertices and compute the mediun height in between the input vertices | ||
367 | for (int yy = 0; yy <= meshY; yy++) | ||
368 | { | ||
369 | for (int xx = 0; xx <= meshX; xx++) // Hint: the "<=" means we go around sizeX + 1 times | ||
370 | { | ||
371 | float offsetY = (float)yy * (float)sizeY / (float)meshY; // The Y that is closest to the mesh point | ||
372 | int stepY = (int)offsetY; | ||
373 | float fractionalY = offsetY - (float)stepY; | ||
374 | float offsetX = (float)xx * (float)sizeX / (float)meshX; // The X that is closest to the mesh point | ||
375 | int stepX = (int)offsetX; | ||
376 | float fractionalX = offsetX - (float)stepX; | ||
377 | |||
378 | // physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh2,xx={1},yy={2},offX={3},stepX={4},fractX={5},offY={6},stepY={7},fractY={8}", | ||
379 | // BSScene.DetailLogZero, xx, yy, offsetX, stepX, fractionalX, offsetY, stepY, fractionalY); | ||
380 | |||
381 | // get the four corners of the heightmap square the mesh point is in | ||
382 | float heightUL = hmap.GetHeight(stepX , stepY ); | ||
383 | float heightUR = hmap.GetHeight(stepX + 1, stepY ); | ||
384 | float heightLL = hmap.GetHeight(stepX , stepY + 1); | ||
385 | float heightLR = hmap.GetHeight(stepX + 1, stepY + 1); | ||
386 | |||
387 | // bilinear interplolation | ||
388 | float height = heightUL * (1 - fractionalX) * (1 - fractionalY) | ||
389 | + heightUR * fractionalX * (1 - fractionalY) | ||
390 | + heightLL * (1 - fractionalX) * fractionalY | ||
391 | + heightLR * fractionalX * fractionalY; | ||
392 | |||
393 | // physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh2,heightUL={1},heightUR={2},heightLL={3},heightLR={4},heightMap={5}", | ||
394 | // BSScene.DetailLogZero, heightUL, heightUR, heightLL, heightLR, height); | ||
395 | |||
396 | minHeight = Math.Min(minHeight, height); | ||
397 | |||
398 | vertices[verticesCount + 0] = (float)xx * meshXStep + extentBase.X; | ||
399 | vertices[verticesCount + 1] = (float)yy * meshYStep + extentBase.Y; | ||
400 | vertices[verticesCount + 2] = height + extentBase.Z; | ||
401 | verticesCount += 3; | ||
402 | } | ||
403 | } | ||
404 | // The number of vertices generated | ||
405 | verticesCount /= 3; | ||
406 | |||
407 | // Loop through all the heightmap squares and create indices for the two triangles for that square | ||
408 | for (int yy = 0; yy < meshY; yy++) | ||
409 | { | ||
410 | for (int xx = 0; xx < meshX; xx++) | ||
411 | { | ||
412 | int offset = yy * (meshX + 1) + xx; | ||
413 | // Each vertices is presumed to be the upper left corner of a box of two triangles | ||
414 | indices[indicesCount + 0] = offset; | ||
415 | indices[indicesCount + 1] = offset + 1; | ||
416 | indices[indicesCount + 2] = offset + meshX + 1; // accounting for the extra column | ||
417 | indices[indicesCount + 3] = offset + 1; | ||
418 | indices[indicesCount + 4] = offset + meshX + 2; | ||
419 | indices[indicesCount + 5] = offset + meshX + 1; | ||
420 | indicesCount += 6; | ||
421 | } | ||
422 | } | ||
423 | |||
424 | ret = true; | ||
425 | } | ||
426 | catch (Exception e) | ||
427 | { | ||
428 | if (physicsScene != null) | ||
429 | physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. For={1}/{2}, e={3}", | ||
430 | LogHeader, physicsScene.RegionName, extentBase, e); | ||
431 | } | ||
432 | |||
433 | indicesCountO = indicesCount; | ||
434 | indicesO = indices; | ||
435 | verticesCountO = verticesCount; | ||
436 | verticesO = vertices; | ||
437 | |||
438 | return ret; | ||
439 | } | ||
273 | } | 440 | } |
274 | } | 441 | } |