Planetside Software Forums

General => Terragen Discussion => Topic started by: PeanutMocha on August 15, 2010, 02:18:03 PM

Title: Creating TER file in C#
Post by: PeanutMocha on August 15, 2010, 02:18:03 PM
I wrote a little C# program to create TER format height fields, but can't seem to get them to appear correctly in Terragen 2.

The border of the heightfield area is highlighted in the preview window, but no matter how I tweak parameters like base height, height scale and elevation points, the terrain is rendered flat.

I'm referencing the file format spec at http://www.planetside.co.uk/terragen/dev/tgterrain.html

Example TER file and TGD are attached.  The C# code appears below.  Once it's working, I'll be glad to share it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Heightmap
{
    public class TerFormat
    {
        // "TERRAGENTERRAIN "
        static private readonly byte[] TERRAGEN = new byte[8] { 84, 69, 82, 82, 65, 71, 69, 78 };
        static private readonly byte[] TERRAIN = new byte[8] { 84, 69, 82, 82, 65, 73, 78, 32 };
        static private readonly byte[] SIZE = new byte[4] { 83, 73, 90, 69 };
        static private readonly byte[] SIZEPAD = new byte[2] { 0, 0 };
        static private readonly byte[] EOF = new byte[4] { 69, 79, 70, 32 };
        static private readonly byte[] ALTW = new byte[4] { 65, 76, 84, 87 };
        static public void Write(string filePath, short width, short height, short baseHeight, short scale, short[] elevPoints)
        {
            if (width != height) throw new Exception("Non-square heightmaps not yet supported.  See http://www.planetside.co.uk/terragen/dev/tgterrain.html");

            short sizeMarker = (short)(width - 1);
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(TERRAGEN);
                    writer.Write(TERRAIN);
                    writer.Write(SIZE);
                    writer.Write(sizeMarker);
                    writer.Write(SIZEPAD);
                    writer.Write(EOF);

                    writer.Write(ALTW);
                    writer.Write(scale);
                    writer.Write(baseHeight);

                    for (int i = 0; i < elevPoints.Length; i++)
                    {
                        writer.Write(elevPoints);
                    }

                    writer.Close();
                }
            }
        }
    }
}
Title: Re: Creating TER file in C#
Post by: engineer on August 16, 2010, 02:12:10 PM
Looking at your example "Eric.ter" one thing is obvious: the terrain is flat. The height of this height field is constant (4242H).
Aside of this the header is not build up correctly. And the last issue might be that the EOF marker should be at the end of the file.

I would recommend to open an existing ter file with a hex editor to get some better feeling how a ter file should be generated.
Title: Re: Creating TER file in C#
Post by: PeanutMocha on August 16, 2010, 06:47:22 PM
The misplaced EOF element was the key. Somehow I thought it was supposed to mark the end of the header section.  Working now, thanks!