aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim-Source/OpenSim.Terrain.BasicTerrain
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim-Source/OpenSim.Terrain.BasicTerrain')
-rw-r--r--OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj95
-rw-r--r--OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj.user12
-rw-r--r--OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.dll.build43
-rw-r--r--OpenSim-Source/OpenSim.Terrain.BasicTerrain/Properties/AssemblyInfo.cs35
-rw-r--r--OpenSim-Source/OpenSim.Terrain.BasicTerrain/TerrainEngine.cs453
5 files changed, 638 insertions, 0 deletions
diff --git a/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj
new file mode 100644
index 0000000..1cdfaf6
--- /dev/null
+++ b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj
@@ -0,0 +1,95 @@
1<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2 <PropertyGroup>
3 <ProjectType>Local</ProjectType>
4 <ProductVersion>8.0.50727</ProductVersion>
5 <SchemaVersion>2.0</SchemaVersion>
6 <ProjectGuid>{2270B8FE-0000-0000-0000-000000000000}</ProjectGuid>
7 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
8 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
9 <ApplicationIcon></ApplicationIcon>
10 <AssemblyKeyContainerName>
11 </AssemblyKeyContainerName>
12 <AssemblyName>OpenSim.Terrain.BasicTerrain</AssemblyName>
13 <DefaultClientScript>JScript</DefaultClientScript>
14 <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout>
15 <DefaultTargetSchema>IE50</DefaultTargetSchema>
16 <DelaySign>false</DelaySign>
17 <OutputType>Library</OutputType>
18 <AppDesignerFolder></AppDesignerFolder>
19 <RootNamespace>OpenSim.Terrain.BasicTerrain</RootNamespace>
20 <StartupObject></StartupObject>
21 <FileUpgradeFlags>
22 </FileUpgradeFlags>
23 </PropertyGroup>
24 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
25 <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
26 <BaseAddress>285212672</BaseAddress>
27 <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
28 <ConfigurationOverrideFile>
29 </ConfigurationOverrideFile>
30 <DefineConstants>TRACE;DEBUG</DefineConstants>
31 <DocumentationFile></DocumentationFile>
32 <DebugSymbols>True</DebugSymbols>
33 <FileAlignment>4096</FileAlignment>
34 <Optimize>False</Optimize>
35 <OutputPath>..\bin\</OutputPath>
36 <RegisterForComInterop>False</RegisterForComInterop>
37 <RemoveIntegerChecks>False</RemoveIntegerChecks>
38 <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
39 <WarningLevel>4</WarningLevel>
40 <NoWarn></NoWarn>
41 </PropertyGroup>
42 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
43 <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
44 <BaseAddress>285212672</BaseAddress>
45 <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
46 <ConfigurationOverrideFile>
47 </ConfigurationOverrideFile>
48 <DefineConstants>TRACE</DefineConstants>
49 <DocumentationFile></DocumentationFile>
50 <DebugSymbols>False</DebugSymbols>
51 <FileAlignment>4096</FileAlignment>
52 <Optimize>True</Optimize>
53 <OutputPath>..\bin\</OutputPath>
54 <RegisterForComInterop>False</RegisterForComInterop>
55 <RemoveIntegerChecks>False</RemoveIntegerChecks>
56 <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
57 <WarningLevel>4</WarningLevel>
58 <NoWarn></NoWarn>
59 </PropertyGroup>
60 <ItemGroup>
61 <Reference Include="libTerrain-BSD.dll" >
62 <HintPath>..\bin\libTerrain-BSD.dll</HintPath>
63 <Private>False</Private>
64 </Reference>
65 <Reference Include="System" >
66 <HintPath>System.dll</HintPath>
67 <Private>False</Private>
68 </Reference>
69 <Reference Include="System.Data" >
70 <HintPath>System.Data.dll</HintPath>
71 <Private>False</Private>
72 </Reference>
73 <Reference Include="System.Xml" >
74 <HintPath>System.Xml.dll</HintPath>
75 <Private>False</Private>
76 </Reference>
77 </ItemGroup>
78 <ItemGroup>
79 </ItemGroup>
80 <ItemGroup>
81 <Compile Include="TerrainEngine.cs">
82 <SubType>Code</SubType>
83 </Compile>
84 <Compile Include="Properties\AssemblyInfo.cs">
85 <SubType>Code</SubType>
86 </Compile>
87 </ItemGroup>
88 <Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets" />
89 <PropertyGroup>
90 <PreBuildEvent>
91 </PreBuildEvent>
92 <PostBuildEvent>
93 </PostBuildEvent>
94 </PropertyGroup>
95</Project>
diff --git a/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj.user b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj.user
new file mode 100644
index 0000000..d47d65d
--- /dev/null
+++ b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.csproj.user
@@ -0,0 +1,12 @@
1<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2 <PropertyGroup>
3 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5 <ReferencePath>C:\New Folder\second-life-viewer\opensim-dailys2\opensim15-07\bin\</ReferencePath>
6 <LastOpenVersion>8.0.50727</LastOpenVersion>
7 <ProjectView>ProjectFiles</ProjectView>
8 <ProjectTrust>0</ProjectTrust>
9 </PropertyGroup>
10 <PropertyGroup Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " />
11 <PropertyGroup Condition = " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " />
12</Project>
diff --git a/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.dll.build b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.dll.build
new file mode 100644
index 0000000..e1a3d2d
--- /dev/null
+++ b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/OpenSim.Terrain.BasicTerrain.dll.build
@@ -0,0 +1,43 @@
1<?xml version="1.0" ?>
2<project name="OpenSim.Terrain.BasicTerrain" default="build">
3 <target name="build">
4 <echo message="Build Directory is ${project::get-base-directory()}/${build.dir}" />
5 <mkdir dir="${project::get-base-directory()}/${build.dir}" />
6 <copy todir="${project::get-base-directory()}/${build.dir}">
7 <fileset basedir="${project::get-base-directory()}">
8 </fileset>
9 </copy>
10 <csc target="library" debug="${build.debug}" unsafe="False" define="TRACE;DEBUG" output="${project::get-base-directory()}/${build.dir}/${project::get-name()}.dll">
11 <resources prefix="OpenSim.Terrain.BasicTerrain" dynamicprefix="true" >
12 </resources>
13 <sources failonempty="true">
14 <include name="TerrainEngine.cs" />
15 <include name="Properties/AssemblyInfo.cs" />
16 </sources>
17 <references basedir="${project::get-base-directory()}">
18 <lib>
19 <include name="${project::get-base-directory()}" />
20 <include name="${project::get-base-directory()}/${build.dir}" />
21 </lib>
22 <include name="System.dll" />
23 <include name="System.Data.dll" />
24 <include name="System.Xml.dll" />
25 <include name="../bin/libTerrain-BSD.dll" />
26 </references>
27 </csc>
28 <echo message="Copying from [${project::get-base-directory()}/${build.dir}/] to [${project::get-base-directory()}/../bin/" />
29 <mkdir dir="${project::get-base-directory()}/../bin/"/>
30 <copy todir="${project::get-base-directory()}/../bin/">
31 <fileset basedir="${project::get-base-directory()}/${build.dir}/" >
32 <include name="*.dll"/>
33 <include name="*.exe"/>
34 </fileset>
35 </copy>
36 </target>
37 <target name="clean">
38 <delete dir="${bin.dir}" failonerror="false" />
39 <delete dir="${obj.dir}" failonerror="false" />
40 </target>
41 <target name="doc" description="Creates documentation.">
42 </target>
43</project>
diff --git a/OpenSim-Source/OpenSim.Terrain.BasicTerrain/Properties/AssemblyInfo.cs b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..bd74993
--- /dev/null
+++ b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
1using System.Reflection;
2using System.Runtime.CompilerServices;
3using System.Runtime.InteropServices;
4
5// General Information about an assembly is controlled through the following
6// set of attributes. Change these attribute values to modify the information
7// associated with an assembly.
8[assembly: AssemblyTitle("OpenSim.Terrain.BasicTerrain")]
9[assembly: AssemblyDescription("")]
10[assembly: AssemblyConfiguration("")]
11[assembly: AssemblyCompany("")]
12[assembly: AssemblyProduct("OpenSim.Terrain.BasicTerrain")]
13[assembly: AssemblyCopyright("Copyright © 2007")]
14[assembly: AssemblyTrademark("")]
15[assembly: AssemblyCulture("")]
16
17// Setting ComVisible to false makes the types in this assembly not visible
18// to COM components. If you need to access a type in this assembly from
19// COM, set the ComVisible attribute to true on that type.
20[assembly: ComVisible(false)]
21
22// The following GUID is for the ID of the typelib if this project is exposed to COM
23[assembly: Guid("3263f5b5-0a41-4ed5-91a2-9baaaeecc849")]
24
25// Version information for an assembly consists of the following four values:
26//
27// Major Version
28// Minor Version
29// Build Number
30// Revision
31//
32// You can specify all the values or you can default the Revision and Build Numbers
33// by using the '*' as shown below:
34[assembly: AssemblyVersion("1.0.0.0")]
35[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/OpenSim-Source/OpenSim.Terrain.BasicTerrain/TerrainEngine.cs b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/TerrainEngine.cs
new file mode 100644
index 0000000..a0f37f9
--- /dev/null
+++ b/OpenSim-Source/OpenSim.Terrain.BasicTerrain/TerrainEngine.cs
@@ -0,0 +1,453 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Drawing;
5using libTerrain;
6
7namespace OpenSim.Terrain
8{
9 public class TerrainEngine
10 {
11 /// <summary>
12 /// A [normally] 256x256 heightmap
13 /// </summary>
14 public Channel heightmap;
15
16 /// <summary>
17 /// Whether or not the terrain has been modified since it was last saved and sent to the Physics engine.
18 /// Counts the number of modifications since the last save. (0 = Untainted)
19 /// </summary>
20 public int tainted;
21
22 int w, h;
23
24 /// <summary>
25 /// Generate a new TerrainEngine instance and creates a new heightmap
26 /// </summary>
27 public TerrainEngine()
28 {
29 w = 256;
30 h = 256;
31 heightmap = new Channel(w, h);
32
33 tainted++;
34 }
35
36 /// <summary>
37 /// Converts the heightmap to a 65536 value 1D floating point array
38 /// </summary>
39 /// <returns>A float[65536] array containing the heightmap</returns>
40 public float[] getHeights1D()
41 {
42 float[] heights = new float[w * h];
43 int i;
44
45 for (i = 0; i < w * h; i++)
46 {
47 heights[i] = (float)heightmap.map[i / w, i % w];
48 }
49
50 return heights;
51 }
52
53 /// <summary>
54 /// Converts the heightmap to a 256x256 value 2D floating point array.
55 /// </summary>
56 /// <returns>An array of 256,256 values containing the heightmap</returns>
57 public float[,] getHeights2D()
58 {
59 float[,] heights = new float[w, h];
60 int x, y;
61 for (x = 0; x < w; x++)
62 {
63 for (y = 0; y < h; y++)
64 {
65 heights[x, y] = (float)heightmap.map[x, y];
66 }
67 }
68 return heights;
69 }
70
71 /// <summary>
72 /// Imports a 1D floating point array into the 2D heightmap array
73 /// </summary>
74 /// <param name="heights">The array to import (must have 65536 members)</param>
75 public void setHeights1D(float[] heights)
76 {
77 int i;
78 for (i = 0; i < w * h; i++)
79 {
80 heightmap.map[i / w, i % w] = heights[i];
81 }
82
83 tainted++;
84 }
85
86 /// <summary>
87 /// Loads a 2D array of values into the heightmap
88 /// </summary>
89 /// <param name="heights">An array of 256,256 float values</param>
90 public void setHeights2D(float[,] heights)
91 {
92 int x, y;
93 for (x = 0; x < w; x++)
94 {
95 for (y = 0; y < h; y++)
96 {
97 heightmap.set(x,y,(double)heights[x,y]);
98 }
99 }
100 tainted++;
101 }
102
103 /// <summary>
104 /// Processes a terrain-specific command
105 /// </summary>
106 /// <param name="args">Commandline arguments (space seperated)</param>
107 /// <param name="resultText">Reference that returns error or help text if returning false</param>
108 /// <returns>If the operation was successful (if not, the error is placed into resultText)</returns>
109 public bool RunTerrainCmd(string[] args, ref string resultText)
110 {
111 string command = args[0];
112
113 try
114 {
115
116 switch (command)
117 {
118 case "help":
119 resultText += "terrain regenerate - rebuilds the sims terrain using a default algorithm\n";
120 resultText += "terrain seed <seed> - sets the random seed value to <seed>\n";
121 resultText += "terrain load <type> <filename> - loads a terrain from disk, type can be 'F32', 'F64' or 'IMG'\n";
122 resultText += "terrain save <type> <filename> - saves a terrain to disk, type can be 'F32' or 'F64'\n";
123 resultText += "terrain save grdmap <filename> <gradient map> - creates a PNG snapshot of the region using a named gradient map\n";
124 resultText += "terrain rescale <min> <max> - rescales a terrain to be between <min> and <max> meters high\n";
125 resultText += "terrain erode aerobic <windspeed> <pickupmin> <dropmin> <carry> <rounds> <lowest>\n";
126 resultText += "terrain erode thermal <talus> <rounds> <carry>\n";
127 resultText += "terrain multiply <val> - multiplies a terrain by <val>\n";
128 return false;
129
130 case "seed":
131 setSeed(Convert.ToInt32(args[1]));
132 break;
133
134 case "erode":
135 switch (args[1].ToLower())
136 {
137 case "aerobic":
138 // WindSpeed, PickupMinimum,DropMinimum,Carry,Rounds,Lowest
139 heightmap.AerobicErosion(Convert.ToDouble(args[2]), Convert.ToDouble(args[3]), Convert.ToDouble(args[4]), Convert.ToDouble(args[5]), Convert.ToInt32(args[6]), Convert.ToBoolean(args[7]));
140 break;
141 case "thermal":
142 heightmap.thermalWeathering(Convert.ToDouble(args[2]), Convert.ToInt32(args[3]), Convert.ToDouble(args[4]));
143 break;
144 default:
145 resultText = "Unknown erosion type";
146 return false;
147 }
148 break;
149
150 case "regenerate":
151 hills();
152 break;
153
154 case "rescale":
155 setRange(Convert.ToSingle(args[1]), Convert.ToSingle(args[2]));
156 break;
157
158 case "multiply":
159 heightmap *= Convert.ToDouble(args[1]);
160 break;
161
162 case "load":
163 switch (args[1].ToLower())
164 {
165 case "f32":
166 loadFromFileF32(args[2]);
167 break;
168
169 case "f64":
170 loadFromFileF64(args[2]);
171 break;
172
173 case "img":
174 resultText = "Error - IMG mode is presently unsupported.";
175 return false;
176
177 default:
178 resultText = "Unknown image or data format";
179 return false;
180 }
181 break;
182
183 case "save":
184 switch (args[1].ToLower())
185 {
186 case "f32":
187 writeToFileF32(args[2]);
188 break;
189
190 case "f64":
191 writeToFileF64(args[2]);
192 break;
193
194 case "grdmap":
195 exportImage(args[2], args[3]);
196 break;
197
198 default:
199 resultText = "Unknown image or data format";
200 return false;
201 }
202 break;
203
204 default:
205 resultText = "Unknown terrain command";
206 return false;
207 }
208 return true;
209 }
210 catch (Exception e)
211 {
212 resultText = "Error running terrain command: " + e.ToString();
213 return false;
214 }
215 }
216
217 /// <summary>
218 /// Renormalises the array between min and max
219 /// </summary>
220 /// <param name="min">Minimum value of the new array</param>
221 /// <param name="max">Maximum value of the new array</param>
222 public void setRange(float min, float max)
223 {
224 heightmap.normalise((double)min, (double)max);
225 tainted++;
226 }
227
228 /// <summary>
229 /// Loads a file consisting of 256x256 doubles and imports it as an array into the map.
230 /// </summary>
231 /// <remarks>TODO: Move this to libTerrain itself</remarks>
232 /// <param name="filename">The filename of the double array to import</param>
233 public void loadFromFileF64(string filename)
234 {
235 System.IO.FileInfo file = new System.IO.FileInfo(filename);
236 System.IO.FileStream s = file.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
237 System.IO.BinaryReader bs = new System.IO.BinaryReader(s);
238 int x, y;
239 for (x = 0; x < w; x++)
240 {
241 for (y = 0; y < h; y++)
242 {
243 heightmap.map[x, y] = bs.ReadDouble();
244 }
245 }
246
247 bs.Close();
248 s.Close();
249
250 tainted++;
251 }
252
253 /// <summary>
254 /// Loads a file consisting of 256x256 floats and imports it as an array into the map.
255 /// </summary>
256 /// <remarks>TODO: Move this to libTerrain itself</remarks>
257 /// <param name="filename">The filename of the float array to import</param>
258 public void loadFromFileF32(string filename)
259 {
260 System.IO.FileInfo file = new System.IO.FileInfo(filename);
261 System.IO.FileStream s = file.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
262 System.IO.BinaryReader bs = new System.IO.BinaryReader(s);
263 int x, y;
264 for (x = 0; x < w; x++)
265 {
266 for (y = 0; y < h; y++)
267 {
268 heightmap.map[x, y] = (double)bs.ReadSingle();
269 }
270 }
271
272 bs.Close();
273 s.Close();
274
275 tainted++;
276 }
277
278 /// <summary>
279 /// Writes the current terrain heightmap to disk, in the format of a 65536 entry double[] array.
280 /// </summary>
281 /// <param name="filename">The desired output filename</param>
282 public void writeToFileF64(string filename)
283 {
284 System.IO.FileInfo file = new System.IO.FileInfo(filename);
285 System.IO.FileStream s = file.Open(System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
286 System.IO.BinaryWriter bs = new System.IO.BinaryWriter(s);
287
288 int x, y;
289 for (x = 0; x < w; x++)
290 {
291 for (y = 0; y < h; y++)
292 {
293 bs.Write(heightmap.get(x,y));
294 }
295 }
296
297 bs.Close();
298 s.Close();
299 }
300
301 /// <summary>
302 /// Writes the current terrain heightmap to disk, in the format of a 65536 entry float[] array
303 /// </summary>
304 /// <param name="filename">The desired output filename</param>
305 public void writeToFileF32(string filename)
306 {
307 System.IO.FileInfo file = new System.IO.FileInfo(filename);
308 System.IO.FileStream s = file.Open(System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
309 System.IO.BinaryWriter bs = new System.IO.BinaryWriter(s);
310
311 int x, y;
312 for (x = 0; x < w; x++)
313 {
314 for (y = 0; y < h; y++)
315 {
316 bs.Write((float)heightmap.get(x, y));
317 }
318 }
319
320 bs.Close();
321 s.Close();
322 }
323
324 /// <summary>
325 /// Sets the random seed to be used by procedural functions which involve random numbers.
326 /// </summary>
327 /// <param name="val">The desired seed</param>
328 public void setSeed(int val)
329 {
330 heightmap.seed = val;
331 }
332
333 /// <summary>
334 /// Raises land in a sphere around the specified coordinates
335 /// </summary>
336 /// <param name="rx">Center of the sphere on the X axis</param>
337 /// <param name="ry">Center of the sphere on the Y axis</param>
338 /// <param name="size">The radius of the sphere</param>
339 /// <param name="amount">Scale the height of the sphere by this amount (recommended 0..2)</param>
340 public void raise(double rx, double ry, double size, double amount)
341 {
342 lock (heightmap)
343 {
344 heightmap.raise(rx, ry, size, amount);
345 }
346
347 tainted++;
348 }
349
350 /// <summary>
351 /// Lowers the land in a sphere around the specified coordinates
352 /// </summary>
353 /// <param name="rx">The center of the sphere at the X axis</param>
354 /// <param name="ry">The center of the sphere at the Y axis</param>
355 /// <param name="size">The radius of the sphere in meters</param>
356 /// <param name="amount">Scale the height of the sphere by this amount (recommended 0..2)</param>
357 public void lower(double rx, double ry, double size, double amount)
358 {
359 lock (heightmap)
360 {
361 heightmap.lower(rx, ry, size, amount);
362 }
363
364 tainted++;
365 }
366
367 /// <summary>
368 /// Generates a simple set of hills in the shape of an island
369 /// </summary>
370 public void hills()
371 {
372 lock (heightmap)
373 {
374 heightmap.hillsSpheres(200, 20, 40, true, true, false);
375 heightmap.normalise();
376 heightmap *= 60.0; // Raise to 60m
377 }
378
379 tainted++;
380 }
381
382 /// <summary>
383 /// Multiplies the heightfield by val
384 /// </summary>
385 /// <param name="meep">The heightfield</param>
386 /// <param name="val">The multiplier</param>
387 /// <returns></returns>
388 public static TerrainEngine operator *(TerrainEngine meep, Double val) {
389 meep.heightmap *= val;
390 meep.tainted++;
391 return meep;
392 }
393
394 /// <summary>
395 /// Returns the height at the coordinates x,y
396 /// </summary>
397 /// <param name="x">X Coordinate</param>
398 /// <param name="y">Y Coordinate</param>
399 /// <returns></returns>
400 public float this[int x, int y]
401 {
402 get
403 {
404 return (float)heightmap.get(x,y);
405 }
406 set
407 {
408 tainted++;
409 heightmap.set(x,y,(double)value);
410 }
411 }
412
413 /// <summary>
414 /// Exports the current heightmap to a PNG file
415 /// </summary>
416 /// <param name="filename">The destination filename for the image</param>
417 /// <param name="gradientmap">A 1x*height* image which contains the colour gradient to export with. Must be at least 1x2 pixels, 1x256 or more is ideal.</param>
418 public void exportImage(string filename, string gradientmap)
419 {
420 try
421 {
422 Bitmap gradientmapLd = new Bitmap(gradientmap);
423
424 int pallete = gradientmapLd.Width;
425
426 Bitmap bmp = new Bitmap(heightmap.w, heightmap.h);
427 Color[] colours = new Color[pallete];
428
429 for (int i = 0; i < pallete; i++)
430 {
431 colours[i] = gradientmapLd.GetPixel(1, i);
432 }
433
434 Channel copy = heightmap.copy();
435 for (int x = 0; x < copy.w; x++)
436 {
437 for (int y = 0; y < copy.h; y++)
438 {
439 // 512 is the largest possible height before colours clamp
440 int colorindex = (int)(Math.Max(Math.Min(1.0, copy.get(x, y) / 512.0), 0.0) * pallete);
441 bmp.SetPixel(x, y, colours[colorindex]);
442 }
443 }
444
445 bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
446 }
447 catch (Exception e)
448 {
449 Console.WriteLine("Failed generating terrain map: " + e.ToString());
450 }
451 }
452 }
453}