Skip to content

Commit

Permalink
Fixed compatibility with MagicaVoxel editor, and made the voxel volum…
Browse files Browse the repository at this point in the history
…e always sit on the lowest level.
  • Loading branch information
RosaryMala committed Jul 22, 2016
1 parent 36d8e42 commit f74276f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Voxel_Fortress"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
Title="Voxel Fortress" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
Expand Down
39 changes: 25 additions & 14 deletions MapMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ bool IsWaterMap(Color[,] elevationMap)
return false;
}

int[,] LoadElevations( Color[,] elevationMap, object sender)
int[,] LoadElevations( Color[,] elevationMap, object sender, out int min, out int max)
{
min = int.MaxValue;
max = int.MinValue;

int[,] elevations = new int[elevationMap.GetLength(0), elevationMap.GetLength(1)];

for (int x = 0; x < elevationMap.GetLength(0); x++)
Expand All @@ -46,32 +49,34 @@ bool IsWaterMap(Color[,] elevationMap)
elevations[x, y] = elevationMap[x, y].B;
else
elevations[x, y] = elevationMap[x, y].B + 25;
min = Math.Min(min, elevations[x, y]);
max = Math.Max(max, elevations[x, y]);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / elevationMap.GetLength(0), string.Format("Loading Heightmap: {0} of {1}", x, elevationMap.GetLength(0)));
}

return elevations;
}

int[,] LoadWaterElevations(Color[,] elevationMap, object sender)
int[,] LoadWaterElevations(Color[,] elevationMap, object sender, out int min, out int max)
{
min = int.MaxValue;
max = int.MinValue;

int[,] elevations = new int[elevationMap.GetLength(0), elevationMap.GetLength(1)];

for (int x = 0; x < elevationMap.GetLength(0); x++)
{
for (int y = 0; y < elevationMap.GetLength(1); y++)
{
if (elevationMap[x, y].R > 0)
{
elevations[x, y] = elevationMap[x, y].B + 25; //This means it's not water.
continue;
}
if (elevationMap[x, y].G > 0)
{
else if (elevationMap[x, y].G > 0)
elevations[x, y] = elevationMap[x, y].B; //Rivers
continue;
}
elevations[x, y] = elevationMap[x, y].B + 25;
else
elevations[x, y] = elevationMap[x, y].B + 25;
min = Math.Min(min, elevations[x, y]);
max = Math.Max(max, elevations[x, y]);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / elevationMap.GetLength(0), string.Format("Loading Water: {0} of {1}", x, elevationMap.GetLength(0)));
}
Expand Down Expand Up @@ -103,15 +108,21 @@ public void LoadMap(object sender, DoWorkEventArgs e)
{
Images images = (Images)e.Argument;

int min, max;

if (images.elevationMap != null)
{
if (IsWaterMap(images.elevationMap))
landElevations = LoadWaterElevations(images.elevationMap, sender);
landElevations = LoadWaterElevations(images.elevationMap, sender, out min, out max);
else
landElevations = LoadElevations(images.elevationMap, sender);
landElevations = LoadElevations(images.elevationMap, sender, out min, out max);
}
else if (images.colorMap != null)
{
landElevations = new int[images.colorMap.GetLength(0), images.colorMap.GetLength(1)];
min = 0;
max = 0;
}
else return; //nothing to work with.

int voxelWidth = landElevations.GetLength(0);
Expand All @@ -138,9 +149,9 @@ public void LoadMap(object sender, DoWorkEventArgs e)
if (y < voxelLength - 1)
zMin = Math.Min(zMin, landElevations[x, y + 1]);
if (images.colorMap != null)
voxels.SetColumn(images.colorMap[x, y], x, y, zMin, landElevations[x, y]);
voxels.SetColumn(images.colorMap[x, y], x, y, zMin - min, landElevations[x, y] - min);
else
voxels.SetColumn(Colors.White, x, y, zMin, landElevations[x, y]);
voxels.SetColumn(Colors.White, x, y, zMin - min, landElevations[x, y] - min);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / voxelWidth, string.Format("Generating Voxels: {0} of {1}", x, voxelWidth));
}
Expand Down
45 changes: 42 additions & 3 deletions XRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace Voxel_Fortress
Expand Down Expand Up @@ -131,38 +132,76 @@ public void ClearVoxel(int x, int y, int z)

public void SaveFile(string path, object sender)
{
bool lowColor = _palette.Count < 256;



BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create), Encoding.ASCII);

//Beginning of Header
writer.Write(Encoding.ASCII.GetBytes("XRAW")); //Magic number
writer.Write((byte)0); //Unsigned Int channels
writer.Write((byte)4); //RGBA
writer.Write((byte)8); //Bits per channel
writer.Write((byte)16); //Bits per index
if (lowColor)
writer.Write((byte)8); //Bits per index
else
writer.Write((byte)16); //Bits per index
writer.Write(_width); //x
writer.Write(_length); //y
writer.Write(_height); //z
writer.Write(_palette.Count); //number of palette colors
if (lowColor)
writer.Write(256); //Force 256 colors
else
writer.Write(_palette.Count); //number of palette colors

long count = 0;

//End of Header
for (int i = 0; i < _voxels.Length; i++)
{
foreach (var index in _voxels[i])
{
writer.Write(index);
if (lowColor)
{
if (index == ~0)
writer.Write((byte)0);
else
{
writer.Write((byte)(index + 1));
count++;
}
}
else
{
writer.Write(index);
if (index != ~0)
count++;
}
}
writer.Flush();
(sender as BackgroundWorker).ReportProgress(i * 2048 / _voxels.Length, "Saving " + path);
}

if(lowColor) //add an empty color in the beginning
{
writer.Write(Encoding.ASCII.GetBytes("FAKE"));
}

foreach (Color color in _palette)
{
writer.Write(color.R);
writer.Write(color.G);
writer.Write(color.B);
writer.Write(color.A);
}

for(int i = _palette.Count; i < 255;i++)
{
writer.Write(Encoding.ASCII.GetBytes("FAKE"));
}
writer.Close();
MessageBox.Show(string.Format("Saved {0}\nUsing {1} unique colors, and {2} total voxels.", path, _palette.Count, count));
}
}
}

0 comments on commit f74276f

Please sign in to comment.