aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
diff options
context:
space:
mode:
authorDavid Walter Seikel2016-11-03 21:44:39 +1000
committerDavid Walter Seikel2016-11-03 21:44:39 +1000
commit134f86e8d5c414409631b25b8c6f0ee45fbd8631 (patch)
tree216b89d3fb89acfb81be1e440c25c41ab09fa96d /OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
parentMore changing to production grid. Double oops. (diff)
downloadopensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.zip
opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.gz
opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.bz2
opensim-SC_OLD-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.xz
Initial update to OpenSim 0.8.2.1 source code.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs265
1 files changed, 0 insertions, 265 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
deleted file mode 100755
index 1d55ce3..0000000
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainMesh.cs
+++ /dev/null
@@ -1,265 +0,0 @@
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 = PhysicsScene.PE.CreateMeshShape(PhysicsScene.World, indicesCount, indices, verticesCount, vertices);
95 if (!m_terrainShape.HasPhysicalShape)
96 {
97 // DISASTER!!
98 PhysicsScene.DetailLog("{0},BSTerrainMesh.create,failedCreationOfShape", ID);
99 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain mesh! base={1}", LogHeader, TerrainBase);
100 // Something is very messed up and a crash is in our future.
101 return;
102 }
103
104 Vector3 pos = regionBase;
105 Quaternion rot = Quaternion.Identity;
106
107 m_terrainBody = PhysicsScene.PE.CreateBodyWithDefaultMotionState(m_terrainShape, ID, pos, rot);
108 if (!m_terrainBody.HasPhysicalBody)
109 {
110 // DISASTER!!
111 physicsScene.Logger.ErrorFormat("{0} Failed creation of terrain body! base={1}", LogHeader, TerrainBase);
112 // Something is very messed up and a crash is in our future.
113 return;
114 }
115
116 // Set current terrain attributes
117 PhysicsScene.PE.SetFriction(m_terrainBody, BSParam.TerrainFriction);
118 PhysicsScene.PE.SetHitFraction(m_terrainBody, BSParam.TerrainHitFraction);
119 PhysicsScene.PE.SetRestitution(m_terrainBody, BSParam.TerrainRestitution);
120 PhysicsScene.PE.SetCollisionFlags(m_terrainBody, CollisionFlags.CF_STATIC_OBJECT);
121
122 // Static objects are not very massive.
123 PhysicsScene.PE.SetMassProps(m_terrainBody, 0f, Vector3.Zero);
124
125 // Put the new terrain to the world of physical objects
126 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_terrainBody);
127
128 // Redo its bounding box now that it is in the world
129 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_terrainBody);
130
131 m_terrainBody.collisionType = CollisionType.Terrain;
132 m_terrainBody.ApplyCollisionMask(PhysicsScene);
133
134 // Make it so the terrain will not move or be considered for movement.
135 PhysicsScene.PE.ForceActivationState(m_terrainBody, ActivationState.DISABLE_SIMULATION);
136 }
137
138 public override void Dispose()
139 {
140 if (m_terrainBody.HasPhysicalBody)
141 {
142 PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_terrainBody);
143 // Frees both the body and the shape.
144 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_terrainBody);
145 }
146 }
147
148 public override float GetTerrainHeightAtXYZ(Vector3 pos)
149 {
150 // For the moment use the saved heightmap to get the terrain height.
151 // TODO: raycast downward to find the true terrain below the position.
152 float ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
153
154 int mapIndex = (int)pos.Y * m_sizeY + (int)pos.X;
155 try
156 {
157 ret = m_savedHeightMap[mapIndex];
158 }
159 catch
160 {
161 // Sometimes they give us wonky values of X and Y. Give a warning and return something.
162 PhysicsScene.Logger.WarnFormat("{0} Bad request for terrain height. terrainBase={1}, pos={2}",
163 LogHeader, TerrainBase, pos);
164 ret = BSTerrainManager.HEIGHT_GETHEIGHT_RET;
165 }
166 return ret;
167 }
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
175 // Convert the passed heightmap to mesh information suitable for CreateMeshShape2().
176 // Return 'true' if successfully created.
177 public static bool ConvertHeightmapToMesh(
178 BSScene physicsScene,
179 float[] heightMap, int sizeX, int sizeY, // parameters of incoming heightmap
180 float extentX, float extentY, // zero based range for output vertices
181 Vector3 extentBase, // base to be added to all vertices
182 float magnification, // number of vertices to create between heightMap coords
183 out int indicesCountO, out int[] indicesO,
184 out int verticesCountO, out float[] verticesO)
185 {
186 bool ret = false;
187
188 int indicesCount = 0;
189 int verticesCount = 0;
190 int[] indices = new int[0];
191 float[] vertices = new float[0];
192
193 // Simple mesh creation which assumes magnification == 1.
194 // TODO: do a more general solution that scales, adds new vertices and smoothes the result.
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.
201 try
202 {
203 // One vertice per heightmap value plus the vertices off the top and bottom edge.
204 int totalVertices = (sizeX + 1) * (sizeY + 1);
205 vertices = new float[totalVertices * 3];
206 int totalIndices = sizeX * sizeY * 6;
207 indices = new int[totalIndices];
208
209 float magX = (float)sizeX / extentX;
210 float magY = (float)sizeY / extentY;
211 physicsScene.DetailLog("{0},BSTerrainMesh.ConvertHeightMapToMesh,totVert={1},totInd={2},extentBase={3},magX={4},magY={5}",
212 BSScene.DetailLogZero, totalVertices, totalIndices, extentBase, magX, magY);
213 float minHeight = float.MaxValue;
214 // Note that sizeX+1 vertices are created since there is land between this and the next region.
215 for (int yy = 0; yy <= sizeY; yy++)
216 {
217 for (int xx = 0; xx <= sizeX; xx++) // Hint: the "<=" means we go around sizeX + 1 times
218 {
219 int offset = yy * sizeX + xx;
220 // Extend the height with the height from the last row or column
221 if (yy == sizeY) offset -= sizeX;
222 if (xx == sizeX) offset -= 1;
223 float height = heightMap[offset];
224 minHeight = Math.Min(minHeight, height);
225 vertices[verticesCount + 0] = (float)xx * magX + extentBase.X;
226 vertices[verticesCount + 1] = (float)yy * magY + extentBase.Y;
227 vertices[verticesCount + 2] = height + extentBase.Z;
228 verticesCount += 3;
229 }
230 }
231 verticesCount = verticesCount / 3;
232
233 for (int yy = 0; yy < sizeY; yy++)
234 {
235 for (int xx = 0; xx < sizeX; xx++)
236 {
237 int offset = yy * (sizeX + 1) + xx;
238 // Each vertices is presumed to be the upper left corner of a box of two triangles
239 indices[indicesCount + 0] = offset;
240 indices[indicesCount + 1] = offset + 1;
241 indices[indicesCount + 2] = offset + sizeX + 1; // accounting for the extra column
242 indices[indicesCount + 3] = offset + 1;
243 indices[indicesCount + 4] = offset + sizeX + 2;
244 indices[indicesCount + 5] = offset + sizeX + 1;
245 indicesCount += 6;
246 }
247 }
248
249 ret = true;
250 }
251 catch (Exception e)
252 {
253 physicsScene.Logger.ErrorFormat("{0} Failed conversion of heightmap to mesh. For={1}/{2}, e={3}",
254 LogHeader, physicsScene.RegionName, extentBase, e);
255 }
256
257 indicesCountO = indicesCount;
258 indicesO = indices;
259 verticesCountO = verticesCount;
260 verticesO = vertices;
261
262 return ret;
263 }
264}
265}