aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs
diff options
context:
space:
mode:
authorMelanie2010-10-03 16:34:17 +0100
committerMelanie2010-10-03 16:34:17 +0100
commitab4be3ffdfd8f68e5650077eeb75d155a0223f53 (patch)
treebbb275c6ecea849ee253c7debd80803d12e4dbed /OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs
parentSo, the client can have an old idea of the object properties for the object w... (diff)
downloadopensim-SC_OLD-ab4be3ffdfd8f68e5650077eeb75d155a0223f53.zip
opensim-SC_OLD-ab4be3ffdfd8f68e5650077eeb75d155a0223f53.tar.gz
opensim-SC_OLD-ab4be3ffdfd8f68e5650077eeb75d155a0223f53.tar.bz2
opensim-SC_OLD-ab4be3ffdfd8f68e5650077eeb75d155a0223f53.tar.xz
Initial port of the Warp3D map tile renderer
Diffstat (limited to 'OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs')
-rw-r--r--OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs418
1 files changed, 0 insertions, 418 deletions
diff --git a/OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs b/OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs
deleted file mode 100644
index 8b34f6e..0000000
--- a/OpenSim/Region/CoreModules/World/WorldMap/TexturedMapTileRenderer.cs
+++ /dev/null
@@ -1,418 +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 copyright
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 */
27
28using System;
29using System.Collections.Generic;
30using System.Drawing;
31using System.Reflection;
32using log4net;
33using Nini.Config;
34using OpenMetaverse;
35using OpenMetaverse.Imaging;
36using OpenSim.Framework;
37using OpenSim.Region.Framework.Scenes;
38
39namespace OpenSim.Region.CoreModules.World.WorldMap
40{
41 // Hue, Saturation, Value; used for color-interpolation
42 struct HSV {
43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44
45 public float h;
46 public float s;
47 public float v;
48
49 public HSV(float h, float s, float v)
50 {
51 this.h = h;
52 this.s = s;
53 this.v = v;
54 }
55
56 // (for info about algorithm, see http://en.wikipedia.org/wiki/HSL_and_HSV)
57 public HSV(Color c)
58 {
59 float r = c.R / 255f;
60 float g = c.G / 255f;
61 float b = c.B / 255f;
62 float max = Math.Max(Math.Max(r, g), b);
63 float min = Math.Min(Math.Min(r, g), b);
64 float diff = max - min;
65
66 if (max == min) h = 0f;
67 else if (max == r) h = (g - b) / diff * 60f;
68 else if (max == g) h = (b - r) / diff * 60f + 120f;
69 else h = (r - g) / diff * 60f + 240f;
70 if (h < 0f) h += 360f;
71
72 if (max == 0f) s = 0f;
73 else s = diff / max;
74
75 v = max;
76 }
77
78 // (for info about algorithm, see http://en.wikipedia.org/wiki/HSL_and_HSV)
79 public Color toColor()
80 {
81 if (s < 0f) m_log.Debug("S < 0: " + s);
82 else if (s > 1f) m_log.Debug("S > 1: " + s);
83 if (v < 0f) m_log.Debug("V < 0: " + v);
84 else if (v > 1f) m_log.Debug("V > 1: " + v);
85
86 float f = h / 60f;
87 int sector = (int)f % 6;
88 f = f - (int)f;
89 int pi = (int)(v * (1f - s) * 255f);
90 int qi = (int)(v * (1f - s * f) * 255f);
91 int ti = (int)(v * (1f - (1f - f) * s) * 255f);
92 int vi = (int)(v * 255f);
93
94 if (pi < 0) pi = 0;
95 if (pi > 255) pi = 255;
96 if (qi < 0) qi = 0;
97 if (qi > 255) qi = 255;
98 if (ti < 0) ti = 0;
99 if (ti > 255) ti = 255;
100 if (vi < 0) vi = 0;
101 if (vi > 255) vi = 255;
102
103 switch (sector)
104 {
105 case 0:
106 return Color.FromArgb(vi, ti, pi);
107 case 1:
108 return Color.FromArgb(qi, vi, pi);
109 case 2:
110 return Color.FromArgb(pi, vi, ti);
111 case 3:
112 return Color.FromArgb(pi, qi, vi);
113 case 4:
114 return Color.FromArgb(ti, pi, vi);
115 default:
116 return Color.FromArgb(vi, pi, qi);
117 }
118 }
119 }
120
121 public class TexturedMapTileRenderer : IMapTileTerrainRenderer
122 {
123 #region Constants
124
125 private static readonly ILog m_log =
126 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
127
128 // some hardcoded terrain UUIDs that work with SL 1.20 (the four default textures and "Blank").
129 // The color-values were choosen because they "look right" (at least to me) ;-)
130 private static readonly UUID defaultTerrainTexture1 = new UUID("0bc58228-74a0-7e83-89bc-5c23464bcec5");
131 private static readonly Color defaultColor1 = Color.FromArgb(165, 137, 118);
132 private static readonly UUID defaultTerrainTexture2 = new UUID("63338ede-0037-c4fd-855b-015d77112fc8");
133 private static readonly Color defaultColor2 = Color.FromArgb(69, 89, 49);
134 private static readonly UUID defaultTerrainTexture3 = new UUID("303cd381-8560-7579-23f1-f0a880799740");
135 private static readonly Color defaultColor3 = Color.FromArgb(162, 154, 141);
136 private static readonly UUID defaultTerrainTexture4 = new UUID("53a2f406-4895-1d13-d541-d2e3b86bc19c");
137 private static readonly Color defaultColor4 = Color.FromArgb(200, 200, 200);
138
139 private static readonly Color WATER_COLOR = Color.FromArgb(29, 71, 95);
140
141 #endregion
142
143
144 private Scene m_scene;
145 // private IConfigSource m_config; // not used currently
146
147 // mapping from texture UUIDs to averaged color. This will contain 5-9 values, in general; new values are only
148 // added when the terrain textures are changed in the estate dialog and a new map is generated (and will stay in
149 // that map until the region-server restarts. This could be considered a memory-leak, but it's a *very* small one.
150 // TODO does it make sense to use a "real" cache and regenerate missing entries on fetch?
151 private Dictionary<UUID, Color> m_mapping;
152
153
154 public void Initialise(Scene scene, IConfigSource source)
155 {
156 m_scene = scene;
157 // m_config = source; // not used currently
158 m_mapping = new Dictionary<UUID,Color>();
159 m_mapping.Add(defaultTerrainTexture1, defaultColor1);
160 m_mapping.Add(defaultTerrainTexture2, defaultColor2);
161 m_mapping.Add(defaultTerrainTexture3, defaultColor3);
162 m_mapping.Add(defaultTerrainTexture4, defaultColor4);
163 m_mapping.Add(Util.BLANK_TEXTURE_UUID, Color.White);
164 }
165
166 #region Helpers
167 // This fetches the texture from the asset server synchroneously. That should be ok, as we
168 // call map-creation only in those places:
169 // - on start: We can wait here until the asset server returns the texture
170 // TODO (- on "map" command: We are in the command-line thread, we will wait for completion anyway)
171 // TODO (- on "automatic" update after some change: We are called from the mapUpdateTimer here and
172 // will wait anyway)
173 private Bitmap fetchTexture(UUID id)
174 {
175 AssetBase asset = m_scene.AssetService.Get(id.ToString());
176 m_log.DebugFormat("Fetched texture {0}, found: {1}", id, asset != null);
177 if (asset == null) return null;
178
179 ManagedImage managedImage;
180 Image image;
181
182 try
183 {
184 if (OpenJPEG.DecodeToImage(asset.Data, out managedImage, out image))
185 return new Bitmap(image);
186 else
187 return null;
188 }
189 catch (DllNotFoundException)
190 {
191 m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg is not installed correctly on this system. Asset Data is emtpy for {0}", id);
192
193 }
194 catch (IndexOutOfRangeException)
195 {
196 m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg was unable to encode this. Asset Data is emtpy for {0}", id);
197
198 }
199 catch (Exception)
200 {
201 m_log.ErrorFormat("[TexturedMapTileRenderer]: OpenJpeg was unable to encode this. Asset Data is emtpy for {0}", id);
202
203 }
204 return null;
205
206 }
207
208 // Compute the average color of a texture.
209 private Color computeAverageColor(Bitmap bmp)
210 {
211 // we have 256 x 256 pixel, each with 256 possible color-values per
212 // color-channel, so 2^24 is the maximum value we can get, adding everything.
213 // int is be big enough for that.
214 int r = 0, g = 0, b = 0;
215 for (int y = 0; y < bmp.Height; ++y)
216 {
217 for (int x = 0; x < bmp.Width; ++x)
218 {
219 Color c = bmp.GetPixel(x, y);
220 r += (int)c.R & 0xff;
221 g += (int)c.G & 0xff;
222 b += (int)c.B & 0xff;
223 }
224 }
225
226 int pixels = bmp.Width * bmp.Height;
227 return Color.FromArgb(r / pixels, g / pixels, b / pixels);
228 }
229
230 // return either the average color of the texture, or the defaultColor if the texturID is invalid
231 // or the texture couldn't be found
232 private Color computeAverageColor(UUID textureID, Color defaultColor) {
233 if (textureID == UUID.Zero) return defaultColor; // not set
234 if (m_mapping.ContainsKey(textureID)) return m_mapping[textureID]; // one of the predefined textures
235
236 Bitmap bmp = fetchTexture(textureID);
237 Color color = bmp == null ? defaultColor : computeAverageColor(bmp);
238 // store it for future reference
239 m_mapping[textureID] = color;
240
241 return color;
242 }
243
244 // S-curve: f(x) = 3x² - 2x³:
245 // f(0) = 0, f(0.5) = 0.5, f(1) = 1,
246 // f'(x) = 0 at x = 0 and x = 1; f'(0.5) = 1.5,
247 // f''(0.5) = 0, f''(x) != 0 for x != 0.5
248 private float S(float v) {
249 return (v * v * (3f - 2f * v));
250 }
251
252 // interpolate two colors in HSV space and return the resulting color
253 private HSV interpolateHSV(ref HSV c1, ref HSV c2, float ratio) {
254 if (ratio <= 0f) return c1;
255 if (ratio >= 1f) return c2;
256
257 // make sure we are on the same side on the hue-circle for interpolation
258 // We change the hue of the parameters here, but we don't change the color
259 // represented by that value
260 if (c1.h - c2.h > 180f) c1.h -= 360f;
261 else if (c2.h - c1.h > 180f) c1.h += 360f;
262
263 return new HSV(c1.h * (1f - ratio) + c2.h * ratio,
264 c1.s * (1f - ratio) + c2.s * ratio,
265 c1.v * (1f - ratio) + c2.v * ratio);
266 }
267
268 // the heigthfield might have some jumps in values. Rendered land is smooth, though,
269 // as a slope is rendered at that place. So average 4 neighbour values to emulate that.
270 private float getHeight(double[,] hm, int x, int y) {
271 if (x < ((int)Constants.RegionSize - 1) && y < ((int)Constants.RegionSize - 1))
272 return (float)(hm[x, y] * .444 + (hm[x + 1, y] + hm[x, y + 1]) * .222 + hm[x + 1, y +1] * .112);
273 else
274 return (float)hm[x, y];
275 }
276 #endregion
277
278 public void TerrainToBitmap(Bitmap mapbmp)
279 {
280 int tc = Environment.TickCount;
281 m_log.Info("[MAPTILE]: Generating Maptile Step 1: Terrain");
282
283 // These textures should be in the AssetCache anyway, as every client conneting to this
284 // region needs them. Except on start, when the map is recreated (before anyone connected),
285 // and on change of the estate settings (textures and terrain values), when the map should
286 // be recreated.
287 RegionSettings settings = m_scene.RegionInfo.RegionSettings;
288
289 // the four terrain colors as HSVs for interpolation
290 HSV hsv1 = new HSV(computeAverageColor(settings.TerrainTexture1, defaultColor1));
291 HSV hsv2 = new HSV(computeAverageColor(settings.TerrainTexture2, defaultColor2));
292 HSV hsv3 = new HSV(computeAverageColor(settings.TerrainTexture3, defaultColor3));
293 HSV hsv4 = new HSV(computeAverageColor(settings.TerrainTexture4, defaultColor4));
294
295 float levelNElow = (float)settings.Elevation1NE;
296 float levelNEhigh = (float)settings.Elevation2NE;
297
298 float levelNWlow = (float)settings.Elevation1NW;
299 float levelNWhigh = (float)settings.Elevation2NW;
300
301 float levelSElow = (float)settings.Elevation1SE;
302 float levelSEhigh = (float)settings.Elevation2SE;
303
304 float levelSWlow = (float)settings.Elevation1SW;
305 float levelSWhigh = (float)settings.Elevation2SW;
306
307 float waterHeight = (float)settings.WaterHeight;
308
309 double[,] hm = m_scene.Heightmap.GetDoubles();
310
311 for (int x = 0; x < (int)Constants.RegionSize; x++)
312 {
313 float columnRatio = x / ((float)Constants.RegionSize - 1); // 0 - 1, for interpolation
314 for (int y = 0; y < (int)Constants.RegionSize; y++)
315 {
316 float rowRatio = y / ((float)Constants.RegionSize - 1); // 0 - 1, for interpolation
317
318 // Y flip the cordinates for the bitmap: hf origin is lower left, bm origin is upper left
319 int yr = ((int)Constants.RegionSize - 1) - y;
320
321 float heightvalue = getHeight(hm, x, y);
322 if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
323 heightvalue = 0;
324
325 if (heightvalue > waterHeight)
326 {
327 // add a bit noise for breaking up those flat colors:
328 // - a large-scale noise, for the "patches" (using an doubled s-curve for sharper contrast)
329 // - a small-scale noise, for bringing in some small scale variation
330 //float bigNoise = (float)TerrainUtil.InterpolatedNoise(x / 8.0, y / 8.0) * .5f + .5f; // map to 0.0 - 1.0
331 //float smallNoise = (float)TerrainUtil.InterpolatedNoise(x + 33, y + 43) * .5f + .5f;
332 //float hmod = heightvalue + smallNoise * 3f + S(S(bigNoise)) * 10f;
333 float hmod =
334 heightvalue +
335 (float)TerrainUtil.InterpolatedNoise(x + 33, y + 43) * 1.5f + 1.5f + // 0 - 3
336 S(S((float)TerrainUtil.InterpolatedNoise(x / 8.0, y / 8.0) * .5f + .5f)) * 10f; // 0 - 10
337
338 // find the low/high values for this point (interpolated bilinearily)
339 // (and remember, x=0,y=0 is SW)
340 float low = levelSWlow * (1f - rowRatio) * (1f - columnRatio) +
341 levelSElow * (1f - rowRatio) * columnRatio +
342 levelNWlow * rowRatio * (1f - columnRatio) +
343 levelNElow * rowRatio * columnRatio;
344 float high = levelSWhigh * (1f - rowRatio) * (1f - columnRatio) +
345 levelSEhigh * (1f - rowRatio) * columnRatio +
346 levelNWhigh * rowRatio * (1f - columnRatio) +
347 levelNEhigh * rowRatio * columnRatio;
348 if (high < low)
349 {
350 // someone tried to fool us. High value should be higher than low every time
351 float tmp = high;
352 high = low;
353 low = tmp;
354 }
355
356 HSV hsv;
357 if (hmod <= low) hsv = hsv1; // too low
358 else if (hmod >= high) hsv = hsv4; // too high
359 else
360 {
361 // HSV-interpolate along the colors
362 // first, rescale h to 0.0 - 1.0
363 hmod = (hmod - low) / (high - low);
364 // now we have to split: 0.00 => color1, 0.33 => color2, 0.67 => color3, 1.00 => color4
365 if (hmod < 1f/3f) hsv = interpolateHSV(ref hsv1, ref hsv2, hmod * 3f);
366 else if (hmod < 2f/3f) hsv = interpolateHSV(ref hsv2, ref hsv3, (hmod * 3f) - 1f);
367 else hsv = interpolateHSV(ref hsv3, ref hsv4, (hmod * 3f) - 2f);
368 }
369
370 // Shade the terrain for shadows
371 if (x < ((int)Constants.RegionSize - 1) && y < ((int)Constants.RegionSize - 1))
372 {
373 float hfvaluecompare = getHeight(hm, x + 1, y + 1); // light from north-east => look at land height there
374 if (Single.IsInfinity(hfvaluecompare) || Single.IsNaN(hfvaluecompare))
375 hfvaluecompare = 0f;
376
377 float hfdiff = heightvalue - hfvaluecompare; // => positive if NE is lower, negative if here is lower
378 hfdiff *= 0.06f; // some random factor so "it looks good"
379 if (hfdiff > 0.02f)
380 {
381 float highlightfactor = 0.18f;
382 // NE is lower than here
383 // We have to desaturate and lighten the land at the same time
384 hsv.s = (hsv.s - (hfdiff * highlightfactor) > 0f) ? hsv.s - (hfdiff * highlightfactor) : 0f;
385 hsv.v = (hsv.v + (hfdiff * highlightfactor) < 1f) ? hsv.v + (hfdiff * highlightfactor) : 1f;
386 }
387 else if (hfdiff < -0.02f)
388 {
389 // here is lower than NE:
390 // We have to desaturate and blacken the land at the same time
391 hsv.s = (hsv.s + hfdiff > 0f) ? hsv.s + hfdiff : 0f;
392 hsv.v = (hsv.v + hfdiff > 0f) ? hsv.v + hfdiff : 0f;
393 }
394 }
395 mapbmp.SetPixel(x, yr, hsv.toColor());
396 }
397 else
398 {
399 // We're under the water level with the terrain, so paint water instead of land
400
401 heightvalue = waterHeight - heightvalue;
402 if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
403 heightvalue = 0f;
404 else if (heightvalue > 19f)
405 heightvalue = 19f;
406 else if (heightvalue < 0f)
407 heightvalue = 0f;
408
409 heightvalue = 100f - (heightvalue * 100f) / 19f; // 0 - 19 => 100 - 0
410
411 mapbmp.SetPixel(x, yr, WATER_COLOR);
412 }
413 }
414 }
415 m_log.Info("[MAPTILE]: Generating Maptile Step 1: Done in " + (Environment.TickCount - tc) + " ms");
416 }
417 }
418}