aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs')
-rwxr-xr-xOpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs458
1 files changed, 0 insertions, 458 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
deleted file mode 100755
index 2e9db39..0000000
--- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs
+++ /dev/null
@@ -1,458 +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{
43
44// The physical implementation of the terrain is wrapped in this class.
45public abstract class BSTerrainPhys : IDisposable
46{
47 public enum TerrainImplementation
48 {
49 Heightmap = 0,
50 Mesh = 1
51 }
52
53 public BSScene PhysicsScene { get; private set; }
54 // Base of the region in world coordinates. Coordinates inside the region are relative to this.
55 public Vector3 TerrainBase { get; private set; }
56 public uint ID { get; private set; }
57
58 public BSTerrainPhys(BSScene physicsScene, Vector3 regionBase, uint id)
59 {
60 PhysicsScene = physicsScene;
61 TerrainBase = regionBase;
62 ID = id;
63 }
64 public abstract void Dispose();
65 public abstract float GetTerrainHeightAtXYZ(Vector3 pos);
66 public abstract float GetWaterLevelAtXYZ(Vector3 pos);
67}
68
69// ==========================================================================================
70public sealed class BSTerrainManager : IDisposable
71{
72 static string LogHeader = "[BULLETSIM TERRAIN MANAGER]";
73
74 // These height values are fractional so the odd values will be
75 // noticable when debugging.
76 public const float HEIGHT_INITIALIZATION = 24.987f;
77 public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f;
78 public const float HEIGHT_GETHEIGHT_RET = 24.765f;
79 public const float WATER_HEIGHT_GETHEIGHT_RET = 19.998f;
80
81 // If the min and max height are equal, we reduce the min by this
82 // amount to make sure that a bounding box is built for the terrain.
83 public const float HEIGHT_EQUAL_FUDGE = 0.2f;
84
85 // Until the whole simulator is changed to pass us the region size, we rely on constants.
86 public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
87
88 // The scene that I am part of
89 private BSScene PhysicsScene { get; set; }
90
91 // The ground plane created to keep thing from falling to infinity.
92 private BulletBody m_groundPlane;
93
94 // If doing mega-regions, if we're region zero we will be managing multiple
95 // region terrains since region zero does the physics for the whole mega-region.
96 private Dictionary<Vector3, BSTerrainPhys> m_terrains;
97
98 // Flags used to know when to recalculate the height.
99 private bool m_terrainModified = false;
100
101 // If we are doing mega-regions, terrains are added from TERRAIN_ID to m_terrainCount.
102 // This is incremented before assigning to new region so it is the last ID allocated.
103 private uint m_terrainCount = BSScene.CHILDTERRAIN_ID - 1;
104 public uint HighestTerrainID { get {return m_terrainCount; } }
105
106 // If doing mega-regions, this holds our offset from region zero of
107 // the mega-regions. "parentScene" points to the PhysicsScene of region zero.
108 private Vector3 m_worldOffset;
109 // If the parent region (region 0), this is the extent of the combined regions
110 // relative to the origin of region zero
111 private Vector3 m_worldMax;
112 private PhysicsScene MegaRegionParentPhysicsScene { get; set; }
113
114 public BSTerrainManager(BSScene physicsScene)
115 {
116 PhysicsScene = physicsScene;
117 m_terrains = new Dictionary<Vector3,BSTerrainPhys>();
118
119 // Assume one region of default size
120 m_worldOffset = Vector3.Zero;
121 m_worldMax = new Vector3(DefaultRegionSize);
122 MegaRegionParentPhysicsScene = null;
123 }
124
125 public void Dispose()
126 {
127 ReleaseGroundPlaneAndTerrain();
128 }
129
130 // Create the initial instance of terrain and the underlying ground plane.
131 // This is called from the initialization routine so we presume it is
132 // safe to call Bullet in real time. We hope no one is moving prims around yet.
133 public void CreateInitialGroundPlaneAndTerrain()
134 {
135 // The ground plane is here to catch things that are trying to drop to negative infinity
136 BulletShape groundPlaneShape = PhysicsScene.PE.CreateGroundPlaneShape(BSScene.GROUNDPLANE_ID, 1f, BSParam.TerrainCollisionMargin);
137 m_groundPlane = PhysicsScene.PE.CreateBodyWithDefaultMotionState(groundPlaneShape,
138 BSScene.GROUNDPLANE_ID, Vector3.Zero, Quaternion.Identity);
139
140 PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_groundPlane);
141 PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_groundPlane);
142 // Ground plane does not move
143 PhysicsScene.PE.ForceActivationState(m_groundPlane, ActivationState.DISABLE_SIMULATION);
144 // Everything collides with the ground plane.
145 m_groundPlane.collisionType = CollisionType.Groundplane;
146 m_groundPlane.ApplyCollisionMask(PhysicsScene);
147
148 // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain.
149 BSTerrainPhys initialTerrain = new BSTerrainHeightmap(PhysicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize);
150 m_terrains.Add(Vector3.Zero, initialTerrain);
151 }
152
153 // Release all the terrain structures we might have allocated
154 public void ReleaseGroundPlaneAndTerrain()
155 {
156 if (m_groundPlane.HasPhysicalBody)
157 {
158 if (PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_groundPlane))
159 {
160 PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_groundPlane);
161 }
162 m_groundPlane.Clear();
163 }
164
165 ReleaseTerrain();
166 }
167
168 // Release all the terrain we have allocated
169 public void ReleaseTerrain()
170 {
171 lock (m_terrains)
172 {
173 foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains)
174 {
175 kvp.Value.Dispose();
176 }
177 m_terrains.Clear();
178 }
179 }
180
181 // The simulator wants to set a new heightmap for the terrain.
182 public void SetTerrain(float[] heightMap) {
183 float[] localHeightMap = heightMap;
184 // If there are multiple requests for changes to the same terrain between ticks,
185 // only do that last one.
186 PhysicsScene.PostTaintObject("TerrainManager.SetTerrain-"+ m_worldOffset.ToString(), 0, delegate()
187 {
188 if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null)
189 {
190 // If a child of a mega-region, we shouldn't have any terrain allocated for us
191 ReleaseGroundPlaneAndTerrain();
192 // If doing the mega-prim stuff and we are the child of the zero region,
193 // the terrain is added to our parent
194 if (MegaRegionParentPhysicsScene is BSScene)
195 {
196 DetailLog("{0},SetTerrain.ToParent,offset={1},worldMax={2}",
197 BSScene.DetailLogZero, m_worldOffset, m_worldMax);
198 ((BSScene)MegaRegionParentPhysicsScene).TerrainManager.UpdateTerrain(
199 BSScene.CHILDTERRAIN_ID, localHeightMap,
200 m_worldOffset, m_worldOffset + DefaultRegionSize, true);
201 }
202 }
203 else
204 {
205 // If not doing the mega-prim thing, just change the terrain
206 DetailLog("{0},SetTerrain.Existing", BSScene.DetailLogZero);
207
208 UpdateTerrain(BSScene.TERRAIN_ID, localHeightMap,
209 m_worldOffset, m_worldOffset + DefaultRegionSize, true);
210 }
211 });
212 }
213
214 // If called with no mapInfo for the terrain, this will create a new mapInfo and terrain
215 // based on the passed information. The 'id' should be either the terrain id or
216 // BSScene.CHILDTERRAIN_ID. If the latter, a new child terrain ID will be allocated and used.
217 // The latter feature is for creating child terrains for mega-regions.
218 // If called with a mapInfo in m_heightMaps and there is an existing terrain body, a new
219 // terrain shape is created and added to the body.
220 // This call is most often used to update the heightMap and parameters of the terrain.
221 // (The above does suggest that some simplification/refactoring is in order.)
222 // Called during taint-time.
223 private void UpdateTerrain(uint id, float[] heightMap,
224 Vector3 minCoords, Vector3 maxCoords, bool inTaintTime)
225 {
226 DetailLog("{0},BSTerrainManager.UpdateTerrain,call,minC={1},maxC={2},inTaintTime={3}",
227 BSScene.DetailLogZero, minCoords, maxCoords, inTaintTime);
228
229 // Find high and low points of passed heightmap.
230 // The min and max passed in is usually the area objects can be in (maximum
231 // object height, for instance). The terrain wants the bounding box for the
232 // terrain so replace passed min and max Z with the actual terrain min/max Z.
233 float minZ = float.MaxValue;
234 float maxZ = float.MinValue;
235 foreach (float height in heightMap)
236 {
237 if (height < minZ) minZ = height;
238 if (height > maxZ) maxZ = height;
239 }
240 if (minZ == maxZ)
241 {
242 // If min and max are the same, reduce min a little bit so a good bounding box is created.
243 minZ -= BSTerrainManager.HEIGHT_EQUAL_FUDGE;
244 }
245 minCoords.Z = minZ;
246 maxCoords.Z = maxZ;
247
248 Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f);
249
250 lock (m_terrains)
251 {
252 BSTerrainPhys terrainPhys;
253 if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys))
254 {
255 // There is already a terrain in this spot. Free the old and build the new.
256 DetailLog("{0},UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}",
257 BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords);
258
259 // Remove old terrain from the collection
260 m_terrains.Remove(terrainRegionBase);
261 // Release any physical memory it may be using.
262 terrainPhys.Dispose();
263
264 if (MegaRegionParentPhysicsScene == null)
265 {
266 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords);
267 m_terrains.Add(terrainRegionBase, newTerrainPhys);
268
269 m_terrainModified = true;
270 }
271 else
272 {
273 // It's possible that Combine() was called after this code was queued.
274 // If we are a child of combined regions, we don't create any terrain for us.
275 DetailLog("{0},BSTerrainManager.UpdateTerrain:AmACombineChild,taint", BSScene.DetailLogZero);
276
277 // Get rid of any terrain that may have been allocated for us.
278 ReleaseGroundPlaneAndTerrain();
279
280 // I hate doing this, but just bail
281 return;
282 }
283 }
284 else
285 {
286 // We don't know about this terrain so either we are creating a new terrain or
287 // our mega-prim child is giving us a new terrain to add to the phys world
288
289 // if this is a child terrain, calculate a unique terrain id
290 uint newTerrainID = id;
291 if (newTerrainID >= BSScene.CHILDTERRAIN_ID)
292 newTerrainID = ++m_terrainCount;
293
294 DetailLog("{0},UpdateTerrain:NewTerrain,taint,newID={1},minCoord={2},maxCoord={3}",
295 BSScene.DetailLogZero, newTerrainID, minCoords, minCoords);
296 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords);
297 m_terrains.Add(terrainRegionBase, newTerrainPhys);
298
299 m_terrainModified = true;
300 }
301 }
302 }
303
304 // TODO: redo terrain implementation selection to allow other base types than heightMap.
305 private BSTerrainPhys BuildPhysicalTerrain(Vector3 terrainRegionBase, uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
306 {
307 PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}",
308 LogHeader, PhysicsScene.RegionName, terrainRegionBase,
309 (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation);
310 BSTerrainPhys newTerrainPhys = null;
311 switch ((int)BSParam.TerrainImplementation)
312 {
313 case (int)BSTerrainPhys.TerrainImplementation.Heightmap:
314 newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id,
315 heightMap, minCoords, maxCoords);
316 break;
317 case (int)BSTerrainPhys.TerrainImplementation.Mesh:
318 newTerrainPhys = new BSTerrainMesh(PhysicsScene, terrainRegionBase, id,
319 heightMap, minCoords, maxCoords);
320 break;
321 default:
322 PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}",
323 LogHeader,
324 (int)BSParam.TerrainImplementation,
325 BSParam.TerrainImplementation,
326 PhysicsScene.RegionName, terrainRegionBase);
327 break;
328 }
329 return newTerrainPhys;
330 }
331
332 // Return 'true' of this position is somewhere in known physical terrain space
333 public bool IsWithinKnownTerrain(Vector3 pos)
334 {
335 Vector3 terrainBaseXYZ;
336 BSTerrainPhys physTerrain;
337 return GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ);
338 }
339
340 // Given an X and Y, find the height of the terrain.
341 // Since we could be handling multiple terrains for a mega-region,
342 // the base of the region is calcuated assuming all regions are
343 // the same size and that is the default.
344 // Once the heightMapInfo is found, we have all the information to
345 // compute the offset into the array.
346 private float lastHeightTX = 999999f;
347 private float lastHeightTY = 999999f;
348 private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT;
349 public float GetTerrainHeightAtXYZ(Vector3 pos)
350 {
351 float tX = pos.X;
352 float tY = pos.Y;
353 // You'd be surprized at the number of times this routine is called
354 // with the same parameters as last time.
355 if (!m_terrainModified && (lastHeightTX == tX) && (lastHeightTY == tY))
356 return lastHeight;
357 m_terrainModified = false;
358
359 lastHeightTX = tX;
360 lastHeightTY = tY;
361 float ret = HEIGHT_GETHEIGHT_RET;
362
363 Vector3 terrainBaseXYZ;
364 BSTerrainPhys physTerrain;
365 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
366 {
367 ret = physTerrain.GetTerrainHeightAtXYZ(pos - terrainBaseXYZ);
368 }
369 else
370 {
371 PhysicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}",
372 LogHeader, PhysicsScene.RegionName, tX, tY);
373 DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,terrainNotFound,pos={1},base={2}",
374 BSScene.DetailLogZero, pos, terrainBaseXYZ);
375 }
376
377 lastHeight = ret;
378 return ret;
379 }
380
381 public float GetWaterLevelAtXYZ(Vector3 pos)
382 {
383 float ret = WATER_HEIGHT_GETHEIGHT_RET;
384
385 Vector3 terrainBaseXYZ;
386 BSTerrainPhys physTerrain;
387 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
388 {
389 ret = physTerrain.GetWaterLevelAtXYZ(pos);
390 }
391 else
392 {
393 PhysicsScene.Logger.ErrorFormat("{0} GetWaterHeightAtXY: terrain not found: pos={1}, terrainBase={2}, height={3}",
394 LogHeader, PhysicsScene.RegionName, pos, terrainBaseXYZ, ret);
395 }
396 return ret;
397 }
398
399 // Given an address, return 'true' of there is a description of that terrain and output
400 // the descriptor class and the 'base' fo the addresses therein.
401 private bool GetTerrainPhysicalAtXYZ(Vector3 pos, out BSTerrainPhys outPhysTerrain, out Vector3 outTerrainBase)
402 {
403 int offsetX = ((int)(pos.X / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X;
404 int offsetY = ((int)(pos.Y / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y;
405 Vector3 terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f);
406
407 BSTerrainPhys physTerrain = null;
408 lock (m_terrains)
409 {
410 m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain);
411 }
412 outTerrainBase = terrainBaseXYZ;
413 outPhysTerrain = physTerrain;
414 return (physTerrain != null);
415 }
416
417 // Although no one seems to check this, I do support combining.
418 public bool SupportsCombining()
419 {
420 return true;
421 }
422
423 // This routine is called two ways:
424 // One with 'offset' and 'pScene' zero and null but 'extents' giving the maximum
425 // extent of the combined regions. This is to inform the parent of the size
426 // of the combined regions.
427 // and one with 'offset' as the offset of the child region to the base region,
428 // 'pScene' pointing to the parent and 'extents' of zero. This informs the
429 // child of its relative base and new parent.
430 public void Combine(PhysicsScene pScene, Vector3 offset, Vector3 extents)
431 {
432 m_worldOffset = offset;
433 m_worldMax = extents;
434 MegaRegionParentPhysicsScene = pScene;
435 if (pScene != null)
436 {
437 // We are a child.
438 // We want m_worldMax to be the highest coordinate of our piece of terrain.
439 m_worldMax = offset + DefaultRegionSize;
440 }
441 DetailLog("{0},BSTerrainManager.Combine,offset={1},extents={2},wOffset={3},wMax={4}",
442 BSScene.DetailLogZero, offset, extents, m_worldOffset, m_worldMax);
443 }
444
445 // Unhook all the combining that I know about.
446 public void UnCombine(PhysicsScene pScene)
447 {
448 // Just like ODE, we don't do anything yet.
449 DetailLog("{0},BSTerrainManager.UnCombine", BSScene.DetailLogZero);
450 }
451
452
453 private void DetailLog(string msg, params Object[] args)
454 {
455 PhysicsScene.PhysicsLogging.Write(msg, args);
456 }
457}
458}