aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs')
-rw-r--r--OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs130
1 files changed, 4 insertions, 126 deletions
diff --git a/OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs b/OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs
index 77c10b8..4719ba3 100644
--- a/OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs
+++ b/OpenSim/Region/CoreModules/World/Warp3DMap/TerrainSplat.cs
@@ -79,13 +79,10 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
79 /// <remarks>Based on the algorithm described at http://opensimulator.org/wiki/Terrain_Splatting 79 /// <remarks>Based on the algorithm described at http://opensimulator.org/wiki/Terrain_Splatting
80 /// Note we create a 256x256 dimension texture even if the actual terrain is larger. 80 /// Note we create a 256x256 dimension texture even if the actual terrain is larger.
81 /// </remarks> 81 /// </remarks>
82<<<<<<< HEAD 82
83 public static Bitmap Splat(ITerrainChannel terrain, 83 public static Bitmap Splat(ITerrainChannel terrain,
84 UUID[] textureIDs, float[] startHeights, float[] heightRanges, 84 UUID[] textureIDs, float[] startHeights, float[] heightRanges,
85 Vector3d regionPosition, IAssetService assetService, bool textureTerrain) 85 Vector3d regionPosition, IAssetService assetService, bool textureTerrain)
86=======
87 public static Bitmap Splat(ITerrainChannel terrain, UUID[] textureIDs, float[] startHeights, float[] heightRanges, Vector3d regionPosition, IAssetService assetService, bool textureTerrain)
88>>>>>>> avn/ubitvar
89 { 86 {
90 Debug.Assert(textureIDs.Length == 4); 87 Debug.Assert(textureIDs.Length == 4);
91 Debug.Assert(startHeights.Length == 4); 88 Debug.Assert(startHeights.Length == 4);
@@ -133,8 +130,8 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
133 asset = assetService.Get(textureIDs[i].ToString()); 130 asset = assetService.Get(textureIDs[i].ToString());
134 if (asset != null) 131 if (asset != null)
135 { 132 {
136// m_log.DebugFormat( 133 // m_log.DebugFormat(
137// "[TERRAIN SPLAT]: Got cached original JPEG2000 terrain texture {0} {1}", i, asset.ID); 134 // "[TERRAIN SPLAT]: Got cached original JPEG2000 terrain texture {0} {1}", i, asset.ID);
138 135
139 try { detailTexture[i] = (Bitmap)CSJ2K.J2kImage.FromBytes(asset.Data); } 136 try { detailTexture[i] = (Bitmap)CSJ2K.J2kImage.FromBytes(asset.Data); }
140 catch (Exception ex) 137 catch (Exception ex)
@@ -144,7 +141,7 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
144 } 141 }
145 142
146 if (detailTexture[i] != null) 143 if (detailTexture[i] != null)
147 { 144 {
148 // Make sure this texture is the correct size, otherwise resize 145 // Make sure this texture is the correct size, otherwise resize
149 if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256) 146 if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256)
150 { 147 {
@@ -199,74 +196,12 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
199 using (SolidBrush brush = new SolidBrush(DEFAULT_TERRAIN_COLOR[i])) 196 using (SolidBrush brush = new SolidBrush(DEFAULT_TERRAIN_COLOR[i]))
200 gfx.FillRectangle(brush, 0, 0, 256, 256); 197 gfx.FillRectangle(brush, 0, 0, 256, 256);
201 } 198 }
202 else
203 {
204 if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256)
205 {
206 detailTexture[i] = ResizeBitmap(detailTexture[i], 256, 256);
207 }
208 }
209 } 199 }
210<<<<<<< HEAD
211 else 200 else
212=======
213
214 #region Layer Map
215
216 float[,] layermap = new float[256 , 256];
217
218 int xFactor = terrain.Width / 256;
219 int yFactor = terrain.Height / 256;
220
221 for (int y = 0; y < 256; y++)
222>>>>>>> avn/ubitvar
223 { 201 {
224 if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256) 202 if (detailTexture[i].Width != 256 || detailTexture[i].Height != 256)
225 { 203 {
226<<<<<<< HEAD
227 detailTexture[i] = ResizeBitmap(detailTexture[i], 256, 256); 204 detailTexture[i] = ResizeBitmap(detailTexture[i], 256, 256);
228=======
229 float height = (float)terrain[x * xFactor, y * yFactor];
230
231 float pctX = (float)x / 255f;
232 float pctY = (float)y / 255f;
233
234 // Use bilinear interpolation between the four corners of start height and
235 // height range to select the current values at this position
236 float startHeight = ImageUtils.Bilinear(
237 startHeights[0],
238 startHeights[2],
239 startHeights[1],
240 startHeights[3],
241 pctX, pctY);
242 startHeight = Utils.Clamp(startHeight, 0f, 255f);
243
244 float heightRange = ImageUtils.Bilinear(
245 heightRanges[0],
246 heightRanges[2],
247 heightRanges[1],
248 heightRanges[3],
249 pctX, pctY);
250 heightRange = Utils.Clamp(heightRange, 0f, 255f);
251
252 // Generate two frequencies of perlin noise based on our global position
253 // The magic values were taken from http://opensimulator.org/wiki/Terrain_Splatting
254 Vector3 vec = new Vector3
255 (
256 ((float)regionPosition.X + (x * xFactor)) * 0.20319f,
257 ((float)regionPosition.Y + (y * yFactor)) * 0.20319f,
258 height * 0.25f
259 );
260
261 float lowFreq = Perlin.noise2(vec.X * 0.222222f, vec.Y * 0.222222f) * 6.5f;
262 float highFreq = Perlin.turbulence2(vec.X, vec.Y, 2f) * 2.25f;
263 float noise = (lowFreq + highFreq) * 2f;
264
265 // Combine the current height, generated noise, start height, and height range parameters, then scale all of it
266 float layer = ((height + noise - startHeight) / heightRange) * 4f;
267 if (Single.IsNaN(layer)) layer = 0f;
268 layermap[x,y] = Utils.Clamp(layer, 0f, 3f);
269>>>>>>> avn/ubitvar
270 } 205 }
271 } 206 }
272 } 207 }
@@ -286,7 +221,6 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
286 { 221 {
287 for (int x = 0; x < 256; x++) 222 for (int x = 0; x < 256; x++)
288 { 223 {
289<<<<<<< HEAD
290 float height = (float)terrain[x * xFactor, y * yFactor]; 224 float height = (float)terrain[x * xFactor, y * yFactor];
291 225
292 float pctX = (float)x / 255f; 226 float pctX = (float)x / 255f;
@@ -328,58 +262,6 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
328 if (Single.IsNaN(layer)) 262 if (Single.IsNaN(layer))
329 layer = 0f; 263 layer = 0f;
330 layermap[x, y] = Utils.Clamp(layer, 0f, 3f); 264 layermap[x, y] = Utils.Clamp(layer, 0f, 3f);
331=======
332 // Get handles to all of the texture data arrays
333 BitmapData[] datas = new BitmapData[]
334 {
335 detailTexture[0].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[0].PixelFormat),
336 detailTexture[1].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[1].PixelFormat),
337 detailTexture[2].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[2].PixelFormat),
338 detailTexture[3].LockBits(new Rectangle(0, 0, 256, 256), ImageLockMode.ReadOnly, detailTexture[3].PixelFormat)
339 };
340
341 int[] comps = new int[]
342 {
343 (datas[0].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
344 (datas[1].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
345 (datas[2].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3,
346 (datas[3].PixelFormat == PixelFormat.Format32bppArgb) ? 4 : 3
347 };
348
349 for (int y = 0; y < 256; y++)
350 {
351 for (int x = 0; x < 256; x++)
352 {
353 float layer = layermap[x, y];
354
355 // Select two textures
356 int l0 = (int)Math.Floor(layer);
357 int l1 = Math.Min(l0 + 1, 3);
358
359 byte* ptrA = (byte*)datas[l0].Scan0 + y * datas[l0].Stride + x * comps[l0];
360 byte* ptrB = (byte*)datas[l1].Scan0 + y * datas[l1].Stride + x * comps[l1];
361 byte* ptrO = (byte*)outputData.Scan0 + y * outputData.Stride + x * 3;
362
363 float aB = *(ptrA + 0);
364 float aG = *(ptrA + 1);
365 float aR = *(ptrA + 2);
366
367 float bB = *(ptrB + 0);
368 float bG = *(ptrB + 1);
369 float bR = *(ptrB + 2);
370
371 float layerDiff = layer - l0;
372
373 // Interpolate between the two selected textures
374 *(ptrO + 0) = (byte)Math.Floor(aB + layerDiff * (bB - aB));
375 *(ptrO + 1) = (byte)Math.Floor(aG + layerDiff * (bG - aG));
376 *(ptrO + 2) = (byte)Math.Floor(aR + layerDiff * (bR - aR));
377 }
378 }
379
380 for (int i = 0; i < 4; i++)
381 detailTexture[i].UnlockBits(datas[i]);
382>>>>>>> avn/ubitvar
383 } 265 }
384 } 266 }
385 267
@@ -471,10 +353,6 @@ namespace OpenSim.Region.CoreModules.World.Warp3DMap
471 b.Dispose(); 353 b.Dispose();
472 return result; 354 return result;
473 } 355 }
474<<<<<<< HEAD
475
476=======
477>>>>>>> avn/ubitvar
478 public static Bitmap SplatSimple(float[] heightmap) 356 public static Bitmap SplatSimple(float[] heightmap)
479 { 357 {
480 const float BASE_HSV_H = 93f / 360f; 358 const float BASE_HSV_H = 93f / 360f;