Terragen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.IO;
  29. using System.Text;
  30. using OpenSim.Region.Framework.Interfaces;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
  34. {
  35. /// <summary>
  36. /// Terragen File Format Loader
  37. /// Built from specification at
  38. /// http://www.planetside.co.uk/terragen/dev/tgterrain.html
  39. /// </summary>
  40. internal class Terragen : ITerrainLoader
  41. {
  42. #region ITerrainLoader Members
  43. public ITerrainChannel LoadFile(string filename)
  44. {
  45. FileInfo file = new FileInfo(filename);
  46. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  47. ITerrainChannel retval = LoadStream(s);
  48. s.Close();
  49. return retval;
  50. }
  51. public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
  52. {
  53. TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
  54. FileInfo file = new FileInfo(filename);
  55. FileStream s = file.Open(FileMode.Open, FileAccess.Read);
  56. BinaryReader bs = new BinaryReader(s);
  57. bool eof = false;
  58. int fileXPoints = 0;
  59. int fileYPoints = 0;
  60. // Terragen file
  61. while (eof == false)
  62. {
  63. string tmp = Encoding.ASCII.GetString(bs.ReadBytes(4));
  64. switch (tmp)
  65. {
  66. case "SIZE":
  67. fileXPoints = bs.ReadInt16() + 1;
  68. fileYPoints = fileXPoints;
  69. bs.ReadInt16();
  70. break;
  71. case "XPTS":
  72. fileXPoints = bs.ReadInt16();
  73. bs.ReadInt16();
  74. break;
  75. case "YPTS":
  76. fileYPoints = bs.ReadInt16();
  77. bs.ReadInt16();
  78. break;
  79. case "ALTW":
  80. eof = true;
  81. Int16 heightScale = bs.ReadInt16();
  82. Int16 baseHeight = bs.ReadInt16();
  83. int currFileYOffset = 0;
  84. // if our region isn't on the first X section of the areas to be landscaped, then
  85. // advance to our section of the file
  86. while (currFileYOffset < offsetY)
  87. {
  88. // read a whole strip of regions
  89. int heightsToRead = sectionHeight * fileXPoints;
  90. bs.ReadBytes(heightsToRead * 2); // because the shorts are 2 bytes in the file
  91. currFileYOffset++;
  92. }
  93. for (int y = 0; y < sectionHeight; y++)
  94. {
  95. int currFileXOffset = 0;
  96. // if our region isn't the first X section of the areas to be landscaped, then
  97. // advance the stream to the X start pos of our section in the file
  98. // i.e. eat X upto where we start
  99. while (currFileXOffset < offsetX)
  100. {
  101. bs.ReadBytes(sectionWidth * 2); // 2 bytes = short
  102. currFileXOffset++;
  103. }
  104. // got to our X offset, so write our regions X line
  105. for (int x = 0; x < sectionWidth; x++)
  106. {
  107. // Read a strip and continue
  108. retval[x, y] = baseHeight + bs.ReadInt16() * (float)heightScale / 65536.0f;
  109. }
  110. // record that we wrote it
  111. currFileXOffset++;
  112. // if our region isn't the last X section of the areas to be landscaped, then
  113. // advance the stream to the end of this Y column
  114. while (currFileXOffset < fileWidth)
  115. {
  116. // eat the next regions x line
  117. bs.ReadBytes(sectionWidth * 2); // 2 bytes = short
  118. currFileXOffset++;
  119. }
  120. //eat the last additional point
  121. bs.ReadInt16();
  122. }
  123. break;
  124. default:
  125. bs.ReadInt32();
  126. break;
  127. }
  128. }
  129. bs.Close();
  130. s.Close();
  131. return retval;
  132. }
  133. public ITerrainChannel LoadStream(Stream s)
  134. {
  135. // Set to default size
  136. int w = (int)Constants.RegionSize;
  137. int h = (int)Constants.RegionSize;
  138. // create a dummy channel (in case data is bad)
  139. TerrainChannel retval = new TerrainChannel(w, h);
  140. BinaryReader bs = new BinaryReader(s);
  141. bool eof = false;
  142. if (Encoding.ASCII.GetString(bs.ReadBytes(16)) == "TERRAGENTERRAIN ")
  143. {
  144. // Terragen file
  145. while (eof == false)
  146. {
  147. string tmp = Encoding.ASCII.GetString(bs.ReadBytes(4));
  148. switch (tmp)
  149. {
  150. case "SIZE":
  151. w = bs.ReadInt16() + 1;
  152. h = w;
  153. bs.ReadInt16();
  154. break;
  155. case "XPTS":
  156. w = bs.ReadInt16();
  157. bs.ReadInt16();
  158. break;
  159. case "YPTS":
  160. h = bs.ReadInt16();
  161. bs.ReadInt16();
  162. break;
  163. case "ALTW":
  164. eof = true;
  165. // create new channel of proper size (now that we know it)
  166. retval = new TerrainChannel(w, h);
  167. float heightScale = bs.ReadInt16() / 65536.0f;
  168. float baseHeight = bs.ReadInt16();
  169. for (int y = 0; y < h; y++)
  170. {
  171. for (int x = 0; x < w; x++)
  172. {
  173. retval[x, y] = baseHeight + bs.ReadInt16() * heightScale;
  174. }
  175. }
  176. break;
  177. default:
  178. bs.ReadInt32();
  179. break;
  180. }
  181. }
  182. }
  183. bs.Close();
  184. return retval;
  185. }
  186. public void SaveFile(string filename, ITerrainChannel map)
  187. {
  188. FileInfo file = new FileInfo(filename);
  189. FileStream s = file.Open(FileMode.Create, FileAccess.Write);
  190. SaveStream(s, map);
  191. s.Close();
  192. }
  193. public void SaveStream(Stream stream, ITerrainChannel map)
  194. {
  195. BinaryWriter bs = new BinaryWriter(stream);
  196. //find the max and min heights on the map
  197. float heightMax = map[0,0];
  198. float heightMin = map[0,0];
  199. for (int y = 0; y < map.Height; y++)
  200. {
  201. for (int x = 0; x < map.Width; x++)
  202. {
  203. float current = map[x,y];
  204. if (heightMax < current)
  205. heightMax = current;
  206. if (heightMin > current)
  207. heightMin = current;
  208. }
  209. }
  210. float baseHeight = (float)Math.Floor(0.5f * (heightMax + heightMin));
  211. float horizontalScale = (float) Math.Ceiling((heightMax - heightMin));
  212. // if we are completely flat add 1cm range to avoid NaN divisions
  213. if (horizontalScale < 0.01f)
  214. horizontalScale = 0.01f;
  215. Encoding enc = Encoding.ASCII;
  216. bs.Write(enc.GetBytes("TERRAGENTERRAIN "));
  217. bs.Write(enc.GetBytes("SIZE"));
  218. bs.Write(Convert.ToInt16(map.Width));
  219. bs.Write(Convert.ToInt16(0)); // necessary padding
  220. //The XPTS and YPTS chunks are not needed for square regions
  221. //but L3DT won't load the terrain file properly without them.
  222. bs.Write(enc.GetBytes("XPTS"));
  223. bs.Write(Convert.ToInt16(map.Width));
  224. bs.Write(Convert.ToInt16(0)); // necessary padding
  225. bs.Write(enc.GetBytes("YPTS"));
  226. bs.Write(Convert.ToInt16(map.Height));
  227. bs.Write(Convert.ToInt16(0)); // necessary padding
  228. bs.Write(enc.GetBytes("SCAL"));
  229. bs.Write(ToLittleEndian(1f)); //we're going to say that 1 terrain unit is 1 metre
  230. bs.Write(ToLittleEndian(1f));
  231. bs.Write(ToLittleEndian(1f));
  232. // as we are square and not projected on a sphere then the other
  233. // header blocks are not required
  234. // now write the elevation data
  235. bs.Write(enc.GetBytes("ALTW"));
  236. bs.Write(Convert.ToInt16(horizontalScale)); // range between max and min
  237. bs.Write(Convert.ToInt16(baseHeight)); // base height or mid point
  238. double factor = 65536.0 / horizontalScale; // avoid computing this on each iteration
  239. for (int y = 0; y < map.Height; y++)
  240. {
  241. for (int x = 0; x < map.Width; x++)
  242. {
  243. float elevation = (float)((map[x,y] - baseHeight) * factor); // see LoadStream for inverse
  244. // clamp rounding issues
  245. if (elevation > Int16.MaxValue)
  246. elevation = Int16.MaxValue;
  247. else if (elevation < Int16.MinValue)
  248. elevation = Int16.MinValue;
  249. bs.Write(Convert.ToInt16(elevation));
  250. }
  251. }
  252. //This is necessary for older versions of Terragen.
  253. bs.Write(enc.GetBytes("EOF "));
  254. bs.Close();
  255. }
  256. public string FileExtension
  257. {
  258. get { return ".ter"; }
  259. }
  260. public virtual void SaveFile(ITerrainChannel m_channel, string filename,
  261. int offsetX, int offsetY,
  262. int fileWidth, int fileHeight,
  263. int regionSizeX, int regionSizeY)
  264. {
  265. throw new System.Exception("Not Implemented");
  266. }
  267. #endregion
  268. public override string ToString()
  269. {
  270. return "Terragen";
  271. }
  272. //Returns true if this extension is supported for terrain save-tile
  273. public bool SupportsTileSave()
  274. {
  275. return false;
  276. }
  277. /// <summary>
  278. /// terragen SCAL floats need to be written intel ordered regardless of
  279. /// big or little endian system
  280. /// </summary>
  281. /// <param name="number"></param>
  282. /// <returns></returns>
  283. private byte[] ToLittleEndian( float number)
  284. {
  285. byte[] retVal = BitConverter.GetBytes(number);
  286. if (BitConverter.IsLittleEndian == false)
  287. {
  288. byte[] tmp = new byte[4];
  289. for (int i = 3; i >= 0; i--)
  290. {
  291. tmp[i] = retVal[3 - i];
  292. }
  293. retVal = tmp;
  294. }
  295. return retVal ;
  296. }
  297. }
  298. }