ShadedMapTileRenderer.cs 12 KB

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