HeightMapGenHills.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. namespace OpenSim.Framework.Terrain
  30. {
  31. public class HeightmapGenHills
  32. {
  33. private Random Rand = new Random();
  34. private int NumHills;
  35. private float HillMin;
  36. private float HillMax;
  37. private bool Island;
  38. private float[] heightmap;
  39. public float[] GenerateHeightmap(int numHills, float hillMin, float hillMax, bool island)
  40. {
  41. NumHills = numHills;
  42. HillMin = hillMin;
  43. HillMax = hillMax;
  44. Island = island;
  45. heightmap = new float[256 * 256];
  46. for (int i = 0; i < numHills; i++)
  47. {
  48. AddHill();
  49. }
  50. Normalize();
  51. return heightmap;
  52. }
  53. private void AddHill()
  54. {
  55. float x, y;
  56. float radius = RandomRange(HillMin, HillMax);
  57. if (Island)
  58. {
  59. // Which direction from the center of the map the hill is placed
  60. float theta = RandomRange(0, 6.28f);
  61. // How far from the center of the map to place the hill. The radius
  62. // is subtracted from the range to prevent any part of the hill from
  63. // reaching the edge of the map
  64. float distance = RandomRange(radius / 2.0f, 128.0f - radius);
  65. x = 128.0f + (float)Math.Cos(theta) * distance;
  66. y = 128.0f + (float)Math.Sin(theta) * distance;
  67. }
  68. else
  69. {
  70. x = RandomRange(-radius, 256.0f + radius);
  71. y = RandomRange(-radius, 256.0f + radius);
  72. }
  73. float radiusSq = radius * radius;
  74. float distSq;
  75. float height;
  76. int xMin = (int)(x - radius) - 1;
  77. int xMax = (int)(x + radius) + 1;
  78. if (xMin < 0) xMin = 0;
  79. if (xMax > 255) xMax = 255;
  80. int yMin = (int)(y - radius) - 1;
  81. int yMax = (int)(y + radius) + 1;
  82. if (yMin < 0) yMin = 0;
  83. if (yMax > 255) yMax = 255;
  84. // Loop through each affected cell and determine the height at that point
  85. for (int v = yMin; v <= yMax; ++v)
  86. {
  87. float fv = (float)v;
  88. for (int h = xMin; h <= xMax; ++h)
  89. {
  90. float fh = (float)h;
  91. // Determine how far from the center of this hill this point is
  92. distSq = (x - fh) * (x - fh) + (y - fv) * (y - fv);
  93. height = radiusSq - distSq;
  94. // Don't add negative hill values
  95. if (height > 0.0f) heightmap[h + v * 256] += height;
  96. }
  97. }
  98. }
  99. private void Normalize()
  100. {
  101. float min = heightmap[0];
  102. float max = heightmap[0];
  103. for (int x = 0; x < 256; x++)
  104. {
  105. for (int y = 0; y < 256; y++)
  106. {
  107. if (heightmap[x + y * 256] < min) min = heightmap[x + y * 256];
  108. if (heightmap[x + y * 256] > max) max = heightmap[x + y * 256];
  109. }
  110. }
  111. // Avoid a rare divide by zero
  112. if (min != max)
  113. {
  114. for (int x = 0; x < 256; x++)
  115. {
  116. for (int y = 0; y < 256; y++)
  117. {
  118. heightmap[x + y * 256] = ((heightmap[x + y * 256] - min) / (max - min)) * (HillMax - HillMin);
  119. }
  120. }
  121. }
  122. }
  123. private float RandomRange(float min, float max)
  124. {
  125. return (float)Rand.NextDouble() * (max - min) + min;
  126. }
  127. }
  128. }