Honza Kovář
Honza Kovář

Reputation: 764

XNA - HeightData to heightMap img

Hello I have heightData in memory and sometimes (when I edit it) I wanna save it to jpg. This is my code:

float multi = 0.2f;
float[,] heightData = quadTree.HeightData;
Color[] heightMapColors = new Color[heightData.Length];

for (int x = 0; x < heightData.GetLength(0); x++)
{
    for (int y = 0; y < heightData.GetLength(1); y++)
    {
         byte colorData = (byte)(heightData[x, y] / multi);

         heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
    }
}

Texture2D heightMap = new Texture2D(device, heightData.GetLength(0), heightData.GetLength(1), false, SurfaceFormat.Color);
heightMap.SetData<Color>(heightMapColors);

using (System.IO.Stream stream = System.IO.File.OpenWrite(@"D:\test.jpg"))
{
     heightMap.SaveAsJpeg(stream, heightData.GetLength(0), heightData.GetLength(1));
}

I am 100% sure that I have data in heightMapColors, but saved jpg is only black. :/ Is it a good way how to do it or something is wrong?

Upvotes: 0

Views: 403

Answers (2)

Blau
Blau

Reputation: 5762

Alpha should not be zero

     heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
     heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
     heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
     heightMapColors[x + y * heightData.GetLength(0)].A = 255;

Upvotes: 2

Ani
Ani

Reputation: 10896

A JPG is probably not a good format to store a heightmap into because it is a lossy format. You should be putting it into a BMP pr PNG. That said, what is the range of your "height"? It looks like your height is a float which means it is probably not in the right range to be displayed or even converted to discrete values.

If your allowed height range is Xf to Yf, convert that to a 0 - 255 range using

byteValue = (byte)(((OldValue - OldMin) * (255 - 0)) / (OldMax - OldMin)) + 0

and then give it a shot.

Upvotes: 1

Related Questions