ShadedMapTileRenderer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 OpenSim 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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. using System.Drawing.Imaging;
  33. using System.Reflection;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Imaging;
  36. using Nini.Config;
  37. using log4net;
  38. using OpenSim.Region.Environment.Interfaces;
  39. using OpenSim.Region.Environment.Scenes;
  40. namespace OpenSim.Region.Environment.Modules.World.WorldMap
  41. {
  42. public class ShadedMapTileRenderer : IMapTileTerrainRenderer
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private Scene m_scene;
  47. //private IConfigSource m_config; // not used currently
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. m_scene = scene;
  51. // m_config = config; // not used currently
  52. }
  53. public void TerrainToBitmap(Bitmap mapbmp)
  54. {
  55. int tc = System.Environment.TickCount;
  56. m_log.Info("[MAPTILE]: Generating Maptile Step 1: Terrain");
  57. double[,] hm = m_scene.Heightmap.GetDoubles();
  58. bool ShadowDebugContinue = true;
  59. bool terraincorruptedwarningsaid = false;
  60. float low = 255;
  61. float high = 0;
  62. for (int x = 0; x < 256; x++)
  63. {
  64. for (int y = 0; y < 256; y++)
  65. {
  66. float hmval = (float)hm[x, y];
  67. if (hmval < low)
  68. low = hmval;
  69. if (hmval > high)
  70. high = hmval;
  71. }
  72. }
  73. float waterHeight = (float)m_scene.RegionInfo.RegionSettings.WaterHeight;
  74. for (int x = 0; x < 256; x++)
  75. {
  76. for (int y = 0; y < 256; y++)
  77. {
  78. // Y flip the cordinates for the bitmap: hf origin is lower left, bm origin is upper left
  79. int yr = 255 - y;
  80. float heightvalue = (float)hm[x, y];
  81. if (heightvalue > waterHeight)
  82. {
  83. // scale height value
  84. // No, that doesn't scale it:
  85. // heightvalue = low + mid * (heightvalue - low) / mid; => low + (heightvalue - low) * mid / mid = low + (heightvalue - low) * 1 = low + heightvalue - low = heightvalue
  86. if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
  87. heightvalue = 0;
  88. else if (heightvalue > 255f)
  89. heightvalue = 255f;
  90. else if (heightvalue < 0f)
  91. heightvalue = 0f;
  92. Color color = Color.FromArgb((int)heightvalue, 100, (int)heightvalue);
  93. mapbmp.SetPixel(x, yr, color);
  94. try
  95. {
  96. //X
  97. // .
  98. //
  99. // Shade the terrain for shadows
  100. if (x < 255 && yr < 255)
  101. {
  102. float hfvalue = (float)hm[x, y];
  103. float hfvaluecompare = 0f;
  104. if ((x + 1 < 256) && (y + 1 < 256))
  105. {
  106. hfvaluecompare = (float)hm[x + 1, y + 1]; // light from north-east => look at land height there
  107. }
  108. if (Single.IsInfinity(hfvalue) || Single.IsNaN(hfvalue))
  109. hfvalue = 0f;
  110. if (Single.IsInfinity(hfvaluecompare) || Single.IsNaN(hfvaluecompare))
  111. hfvaluecompare = 0f;
  112. float hfdiff = hfvalue - hfvaluecompare; // => positive if NE is lower, negative if here is lower
  113. int hfdiffi = 0;
  114. int hfdiffihighlight = 0;
  115. float highlightfactor = 0.18f;
  116. try
  117. {
  118. // hfdiffi = Math.Abs((int)((hfdiff * 4) + (hfdiff * 0.5))) + 1;
  119. hfdiffi = Math.Abs((int)(hfdiff * 4.5f)) + 1;
  120. if (hfdiff % 1f != 0)
  121. {
  122. // hfdiffi = hfdiffi + Math.Abs((int)(((hfdiff % 1) * 0.5f) * 10f) - 1);
  123. hfdiffi = hfdiffi + Math.Abs((int)((hfdiff % 1f) * 5f) - 1);
  124. }
  125. hfdiffihighlight = Math.Abs((int)((hfdiff * highlightfactor) * 4.5f)) + 1;
  126. if (hfdiff % 1f != 0)
  127. {
  128. // hfdiffi = hfdiffi + Math.Abs((int)(((hfdiff % 1) * 0.5f) * 10f) - 1);
  129. hfdiffihighlight = hfdiffihighlight + Math.Abs((int)(((hfdiff * highlightfactor) % 1f) * 5f) - 1);
  130. }
  131. }
  132. catch (System.OverflowException)
  133. {
  134. m_log.Debug("[MAPTILE]: Shadow failed at value: " + hfdiff.ToString());
  135. ShadowDebugContinue = false;
  136. }
  137. if (hfdiff > 0.3f)
  138. {
  139. // NE is lower than here
  140. // We have to desaturate and lighten the land at the same time
  141. // we use floats, colors use bytes, so shrink are space down to
  142. // 0-255
  143. if (ShadowDebugContinue)
  144. {
  145. int r = color.R;
  146. int g = color.G;
  147. int b = color.B;
  148. color = Color.FromArgb((r + hfdiffihighlight < 255) ? r + hfdiffihighlight : 255,
  149. (g + hfdiffihighlight < 255) ? g + hfdiffihighlight : 255,
  150. (b + hfdiffihighlight < 255) ? b + hfdiffihighlight : 255);
  151. }
  152. }
  153. else if (hfdiff < -0.3f)
  154. {
  155. // here is lower than NE:
  156. // We have to desaturate and blacken the land at the same time
  157. // we use floats, colors use bytes, so shrink are space down to
  158. // 0-255
  159. if (ShadowDebugContinue)
  160. {
  161. if ((x - 1 > 0) && (yr + 1 < 256))
  162. {
  163. color = mapbmp.GetPixel(x - 1, yr + 1);
  164. int r = color.R;
  165. int g = color.G;
  166. int b = color.B;
  167. color = Color.FromArgb((r - hfdiffi > 0) ? r - hfdiffi : 0,
  168. (g - hfdiffi > 0) ? g - hfdiffi : 0,
  169. (b - hfdiffi > 0) ? b - hfdiffi : 0);
  170. mapbmp.SetPixel(x-1, yr+1, color);
  171. }
  172. }
  173. }
  174. }
  175. }
  176. catch (System.ArgumentException)
  177. {
  178. if (!terraincorruptedwarningsaid)
  179. {
  180. m_log.WarnFormat("[MAPIMAGE]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", m_scene.RegionInfo.RegionName);
  181. terraincorruptedwarningsaid = true;
  182. }
  183. color = Color.Black;
  184. mapbmp.SetPixel(x, yr, color);
  185. }
  186. }
  187. else
  188. {
  189. // We're under the water level with the terrain, so paint water instead of land
  190. // Y flip the cordinates
  191. heightvalue = waterHeight - heightvalue;
  192. if (Single.IsInfinity(heightvalue) || Single.IsNaN(heightvalue))
  193. heightvalue = 0f;
  194. else if (heightvalue > 19f)
  195. heightvalue = 19f;
  196. else if (heightvalue < 0f)
  197. heightvalue = 0f;
  198. heightvalue = 100f - (heightvalue * 100f) / 19f;
  199. try
  200. {
  201. Color water = Color.FromArgb((int)heightvalue, (int)heightvalue, 255);
  202. mapbmp.SetPixel(x, yr, water);
  203. }
  204. catch (System.ArgumentException)
  205. {
  206. if (!terraincorruptedwarningsaid)
  207. {
  208. m_log.WarnFormat("[MAPIMAGE]: Your terrain is corrupted in region {0}, it might take a few minutes to generate the map image depending on the corruption level", m_scene.RegionInfo.RegionName);
  209. terraincorruptedwarningsaid = true;
  210. }
  211. Color black = Color.Black;
  212. mapbmp.SetPixel(x, (256 - y) - 1, black);
  213. }
  214. }
  215. }
  216. }
  217. m_log.Info("[MAPTILE]: Generating Maptile Step 1: Done in " + (System.Environment.TickCount - tc) + " ms");
  218. }
  219. }
  220. }