TerrainEngine.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using OpenSim.Terrain.BasicTerrain;
  5. namespace OpenSim.Terrain
  6. {
  7. public class TerrainEngine
  8. {
  9. /// <summary>
  10. /// A [normally] 256x256 heightmap
  11. /// </summary>
  12. public float[,] map;
  13. /// <summary>
  14. /// A 256x256 heightmap storing water height values
  15. /// </summary>
  16. public float[,] water;
  17. int w, h;
  18. /// <summary>
  19. /// Generate a new TerrainEngine instance and creates a new heightmap
  20. /// </summary>
  21. public TerrainEngine()
  22. {
  23. w = 256;
  24. h = 256;
  25. map = new float[w, h];
  26. water = new float[w, h];
  27. }
  28. /// <summary>
  29. /// Converts the heightmap to a 65536 value 1D floating point array
  30. /// </summary>
  31. /// <returns>A float[65536] array containing the heightmap</returns>
  32. public float[] getHeights1D()
  33. {
  34. float[] heights = new float[w * h];
  35. int i;
  36. for (i = 0; i < w * h; i++)
  37. {
  38. heights[i] = map[i / w, i % w];
  39. }
  40. return heights;
  41. }
  42. /// <summary>
  43. /// Imports a 1D floating point array into the 2D heightmap array
  44. /// </summary>
  45. /// <param name="heights">The array to import (must have 65536 members)</param>
  46. public void setHeights1D(float[] heights)
  47. {
  48. int i;
  49. for (i = 0; i < w * h; i++)
  50. {
  51. map[i / w, i % w] = heights[i];
  52. }
  53. }
  54. /// <summary>
  55. /// Loads a file consisting of 256x256 doubles and imports it as an array into the map.
  56. /// </summary>
  57. /// <param name="filename">The filename of the double array to import</param>
  58. public void loadFromFileF64(string filename)
  59. {
  60. System.IO.FileInfo file = new System.IO.FileInfo(filename);
  61. System.IO.FileStream s = file.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
  62. System.IO.BinaryReader bs = new System.IO.BinaryReader(s);
  63. int x, y;
  64. for (x = 0; x < w; x++)
  65. {
  66. for (y = 0; y < h; y++)
  67. {
  68. map[x, y] = (float)bs.ReadDouble();
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Swaps the references between the height and water buffers to allow you to edit the water heightmap. Remember to swap back when you are done.
  74. /// </summary>
  75. public void swapWaterBuffer()
  76. {
  77. float[,] temp = map;
  78. map = water;
  79. water = temp;
  80. }
  81. /// <summary>
  82. /// Raises land in a sphere around the specified coordinates
  83. /// </summary>
  84. /// <param name="rx">Center of the sphere on the X axis</param>
  85. /// <param name="ry">Center of the sphere on the Y axis</param>
  86. /// <param name="size">The radius of the sphere</param>
  87. /// <param name="amount">Scale the height of the sphere by this amount (recommended 0..2)</param>
  88. public void raise(double rx, double ry, double size, double amount)
  89. {
  90. lock (map)
  91. {
  92. RaiseLower.raiseSphere(this.map, rx, ry, size, amount);
  93. }
  94. }
  95. /// <summary>
  96. /// Lowers the land in a sphere around the specified coordinates
  97. /// </summary>
  98. /// <param name="rx">The center of the sphere at the X axis</param>
  99. /// <param name="ry">The center of the sphere at the Y axis</param>
  100. /// <param name="size">The radius of the sphere in meters</param>
  101. /// <param name="amount">Scale the height of the sphere by this amount (recommended 0..2)</param>
  102. public void lower(double rx, double ry, double size, double amount)
  103. {
  104. lock (map)
  105. {
  106. RaiseLower.lowerSphere(this.map, rx, ry, size, amount);
  107. }
  108. }
  109. /// <summary>
  110. /// Generates a simple set of hills in the shape of an island
  111. /// </summary>
  112. public void hills()
  113. {
  114. lock (map)
  115. {
  116. Hills.hillsSpheres(this.map, 1337, 200, 20, 40, false, true, false);
  117. Normalise.normalise(this.map,60);
  118. }
  119. }
  120. }
  121. }