tommyettinger / IsoVoxel

Generates isometric pixel art from MagicaVoxel .vox files
MIT License
344 stars 25 forks source link

IndexOutOfRangeException for simple vox model #3

Closed OliverEivak closed 7 years ago

OliverEivak commented 7 years ago

Hey, thanks for writing this cool program!

I found that IsoVoxel fails to render some models, for example I created a 20x20x20 voxel file and added one 20x20 layer of blocks on the ground. Trying to render it fails with:

System.IndexOutOfRangeException was unhandled
  Message=Index was outside the bounds of the array.
  Source=IsoVoxel
  StackTrace:
       at IsoVoxel.PaletteDraw.renderSmartOrtho45(MagicaVoxelData[] voxels, Byte xSize, Byte ySize, Byte zSize, OrthoDirection dir, Outlining o, Boolean shrink) in C:\git\IsoVoxel\IsoVoxel\PaletteDraw.cs:line 3412
       at IsoVoxel.PaletteDraw.processUnitSmart(MagicaVoxelData[] parsed, String u, Byte xSize, Byte ySize, Byte zSize, Outlining o, Int32 multiplier) in C:\git\IsoVoxel\IsoVoxel\PaletteDraw.cs:line 3557
       at IsoVoxel.PaletteDraw.Main(String[] args) in C:\git\IsoVoxel\IsoVoxel\PaletteDraw.cs:line 3682
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

I even ran it through a debugger in Visual Studio, it seems that the outlining code is the culprit, but to be honest that code is not very easy to understand for me :)

I could see that zbuffer was an int[26624] and the program was trying to access zbuffer[i + bmpData.Stride * 2 + 8], which with i=26107 andbmpData.Stride=256 would be zbuffer[26627].

zbuffer is the size of numbytes and the loop goes from i=3 to numbytes - 1, yet there are places where zbuffer[i + bmpData.Stride] and the like are accessed so it must be the argbValues that should keep the index from getting out of bounds?

example.zip

tommyettinger commented 7 years ago

Thanks for your thorough bug report, I'll check this out right away. The example you gave, with a "floor" fully filled at 20x20 voxels, definitely shouldn't be having that issue, and there is probably a bug in the conversion from voxel position to pixel position in the image (obviously an image can't have pixels stored in it with negative too-large positions, and I might have missed a check for that). I should probably start making a sample suite of models that I can test against before releasing.

tommyettinger commented 7 years ago

I'm pretty sure this is fully resolved now. There's some repeated code for the different rendering angles, but one (for orthographic directions with a 45-degree slanted camera) was missing some validity checks that are now all accounted for. I'm releasing 0.0.9 now; it also includes another fix (for appearance, not a crash). When you get the chance, can you try 0.0.9 in the releases section? Or you could build IsoVoxel from source if you really feel like it, but it should be the same result. If that release fixes the water.vox issue, feel free to close this issue, or I can once I know I didn't introduce some other bug, heh.

OliverEivak commented 7 years ago

Thanks for the quick response and fix. I tried a couple of models that failed previously and now they are all working :)

Running an automated test over some models sounds good, considering the amount of logic in this application.