aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
diff options
context:
space:
mode:
authorDiva Canto2012-11-27 14:43:01 -0800
committerDiva Canto2012-11-27 14:43:01 -0800
commita82f699f4348ea1ab139ab338973c9cee04df712 (patch)
treed1bf4948670e1d207fe4f6c60f6b7771e1625839 /OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
parentPrevent the core Groups module from being enabled when its name doesn't match... (diff)
parentBulletSim: reorganize linear movement routine into separate subroutines enabl... (diff)
downloadopensim-SC_OLD-a82f699f4348ea1ab139ab338973c9cee04df712.zip
opensim-SC_OLD-a82f699f4348ea1ab139ab338973c9cee04df712.tar.gz
opensim-SC_OLD-a82f699f4348ea1ab139ab338973c9cee04df712.tar.bz2
opensim-SC_OLD-a82f699f4348ea1ab139ab338973c9cee04df712.tar.xz
Merge branch 'master' of ssh://opensimulator.org/var/git/opensim
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs262
1 files changed, 262 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
new file mode 100755
index 0000000..5f6675d
--- /dev/null
+++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
@@ -0,0 +1,262 @@
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 */
27using System;
28using System.Collections.Generic;
29using System.Text;
30
31using OpenSim.Framework;
32using OpenSim.Region.Framework;
33using OpenSim.Region.CoreModules;
34using OpenSim.Region.Physics.Manager;
35
36using Nini.Config;
37using log4net;
38
39using OpenMetaverse;
40
41namespace OpenSim.Region.Physics.BulletSPlugin
42{
43public sealed class BSTerrainMesh : BSTerrainPhys
44{
45 static string LogHeader = "[BULLETSIM TERRAIN MESH]";
46
47 private float[] m_savedHeightMap;
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)
56 {
57 }
58
59 public BSTerrainMesh(BSScene physicsScene, Vector3 regionBase, uint id /* parameters for making mesh */)
60 : base(physicsScene, regionBase, id)
61 {
62 }
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,
80 m_sizeX, m_sizeY,
81 (float)m_sizeX, (float)m_sizeY,
82 Vector3.Zero, 1.0f,
83 out indicesCount, out indices, out verticesCount, out vertices))
84 {
85 // DISASTER!!
86 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedConversionOfHeightmap", ID);
87 PhysicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh! base={1}", LogHeader, TerrainBase);
88 // Something is very messed up and a crash is in our future.
89 return;
90 }
91 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,meshed,indices={1},indSz={2},vertices={3},vertSz={4}",
92 ID, indicesCount, indices.Length, verticesCount, vertices.Length);
93
94 m_terrainShape = new BulletShape(BulletSimAPI.CreateMeshShape2(PhysicsScene.World.ptr,
95 indicesCount, indices, verticesCount, vertices),
96 BSPhysicsShapeType.SHAPE_MESH);
97 if (m_terrainShape.ptr == IntPtr.Zero)
98 {
99 // DISASTER!!
100 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
101 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase);
102 // Something is very messed up and a crash is in our future.
103 return;
104 }
105
106 Vector3 pos = regionBase;
107 Quaternion rot = Quaternion.Identity;
108
109 m_terrainBody = new BulletBody(id, BulletSimAPI.CreateBodyWithDefaultMotionState2( m_terrainShape.ptr, ID, pos, rot));
110 if (m_terrainBody.ptr == IntPtr.Zero)
111 {
112 // DISASTER!!
113 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
114 // Something is very messed up and a crash is in our future.
115 return;
116 }
117
118 // Set current terrain attributes
119 BulletSimAPI.SetFriction2(m_terrainBody.ptr, PhysicsScene.Params.terrainFriction);
120 BulletSimAPI.SetHitFraction2(m_terrainBody.ptr, PhysicsScene.Params.terrainHitFraction);
121 BulletSimAPI.SetRestitution2(m_terrainBody.ptr, PhysicsScene.Params.terrainRestitution);
122 BulletSimAPI.SetCollisionFlags2(m_terrainBody.ptr, CollisionFlags.CF_STATIC_OBJECT);
123
124 // Static objects are not very massive.
125 BulletSimAPI.SetMassProps2(m_terrainBody.ptr, 0f, Vector3.Zero);
126
127 // Put the new terrain to the world of physical objects
128 BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr);
129
130 // Redo its bounding box now that it is in the world
131 BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_terrainBody.ptr);
132
133 BulletSimAPI.SetCollisionFilterMask2(m_terrainBody.ptr,
134 (uint)CollisionFilterGroups.TerrainFilter,
135 (uint)CollisionFilterGroups.TerrainMask);
136
137 // Make it so the terrain will not move or be considered for movement.
138 BulletSimAPI.ForceActivationState2(m_terrainBody.ptr, ActivationState.DISABLE_SIMULATION);
139 }
140
141 public override void Dispose()
142 {
143 if (m_terrainBody.ptr != IntPtr.Zero)
144 {
145 BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_terrainBody.ptr);
146 // Frees both the body and the shape.
147 BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_terrainBody.ptr);
148 }
149 }
150
151 public override float GetHeightAtXYZ(Vector3 pos)
152 {
153 // For the moment use the saved heightmap to get the terrain height.
154 // TODO: raycast downward to find the true terrain below the position.
155 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
156
157 int mapIndex = (int)pos.Y * m_sizeY + (int)pos.X;
158 try
159 {
160 ret = m_savedHeightMap[mapIndex];
161 }
162 catch
163 {
164 // Sometimes they give us wonky values of X and Y. Give a warning and return something.
165 PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
166 LogHeader, TerrainBase, pos);
167 ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
168 }
169 return ret;
170 }
171
172 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2().
173 // Return 'true' if successfully created.
174 public static bool ConvertHeightmapToMesh(
175 BSScene physicsScene,
176 float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap
177 float extentX, float extentY, // zero based range for output vertices
178 Vector3 extentBase, // base to be added to all vertices
179 float magnification, // number of vertices to create between heightMap coords
180 out int indicesCountO, out int[] indicesO,
181 out int verticesCountO, out float[] verticesO)
182 {
183 bool ret = false;
184
185 int indicesCount = 0;
186 int verticesCount = 0;
187 int[] indices = new int[0];
188 float[] vertices = new float[0];
189
190 // Simple mesh creation which assumes magnification == 1.
191 // TODO: do a more general solution that scales, adds new vertices and smoothes the result.
192
193 // Create an array of vertices that is sizeX+1 by sizeY+1 (note the loop
194 // from zero to <= sizeX). The triangle indices are then generated as two triangles
195 // per heightmap point. There are sizeX by sizeY of these squares. The extra row and
196 // column of vertices are used to complete the triangles of the last row and column
197 // of the heightmap.
198 try
199 {
200 // One vertice per heightmap value plus the vertices off the top and bottom edge.
201 int totalVertices = (sizeX + 1) * (sizeY + 1);
202 vertices = new float[totalVertices * 3];
203 int totalIndices = sizeX * sizeY * 6;
204 indices = new int[totalIndices];
205
206 float magX = (float)sizeX / extentX;
207 float magY = (float)sizeY / extentY;
208 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}",
209 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY);
210 float minHeight = float.MaxValue;
211 // Note that sizeX+1 vertices are created since there is land between this and the next region.
212 for (int yy = 0; yy <= sizeY; yy++)
213 {
214 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we go around sizeX + 1 times
215 {
216 int offset = yy * sizeX + xx;
217 // Extend the height with the height from the last row or column
218 if (yy == sizeY) offset -= sizeX;
219 if (xx == sizeX) offset -= 1;
220 float height = heightMap[offset];
221 minHeight = Math.Min(minHeight, height);
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 verticesCount += 3;
226 }
227 }
228 verticesCount = verticesCount / 3;
229
230 for (int yy = 0; yy < sizeY; yy++)
231 {
232 for (int xx = 0; xx < sizeX; xx++)
233 {
234 int offset = yy * (sizeX + 1) + xx;
235 // Each vertices is presumed to be the upper left corner of a box of two triangles
236 indices[indicesCount + 0] = offset;
237 indices[indicesCount + 1] = offset + 1;
238 indices[indicesCount + 2] = offset + sizeX + 1; // accounting for the extra column
239 indices[indicesCount + 3] = offset + 1;
240 indices[indicesCount + 4] = offset + sizeX + 2;
241 indices[indicesCount + 5] = offset + sizeX + 1;
242 indicesCount += 6;
243 }
244 }
245
246 ret = true;
247 }
248 catch (Exception e)
249 {
250 physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. For={1}/{2}, e={3}",
251 LogHeader, physicsScene.RegionName, extentBase, e);
252 }
253
254 indicesCountO = indicesCount;
255 indicesO = indices;
256 verticesCountO = verticesCount;
257 verticesO = vertices;
258
259 return ret;
260 }
261}
262}