ShadedMapTileRenderer.cs 12 KB

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