ShadedMapTileRenderer.cs 12 KB

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