Beispiel #1
0
        public static void Vox2Glyc(string dirName, string name)
        {
            Console.Write("Vox2Glyc converting: " + name + " ... ");
            VoxFile_VoxelSet voxels = null;
            voxels = ReadVox(dirName + name + ".vox");

            Grid grid = RasterLib.RasterApi.CreateGrid(64, 64, 64, 4);

            for (int i = 0; i < voxels.voxels.Length; i++)
            {
                VoxFile_Voxel voxel = voxels.voxels[i];
                CellProperties cp = new CellProperties();

                byte r = (byte)voxel.I;
                byte g = (byte)voxel.I;
                byte b = (byte)voxel.I;
                byte a = (byte)voxel.I;
                r = (byte)((voxels.palette[voxel.I] >> 0) & 255);
                g = (byte)((voxels.palette[voxel.I] >> 8) & 255);
                b = (byte)((voxels.palette[voxel.I] >> 16) & 255);
                a = (byte)((voxels.palette[voxel.I] >> 24) & 255);

                cp.Rgba = RasterLib.RasterApi.Rgba2Ulong(r, g, b, a);
                grid.Plot(voxel.X, voxel.Y, voxel.Z, cp);
            }

            RectList rects = RasterLib.RasterApi.GridToRects(grid);
            RasterLib.Language.Code code = RasterLib.RasterApi.RectsToCode(rects);
            //Console.WriteLine(code.codeString);

            RasterLib.RasterApi.CodeToGlyC(dirName + name + ".glyc", name + ",\n" + code.codeString);
        }
Beispiel #2
0
        public static VoxFile_VoxelSet ReadVox(string filename)
        {
            //Console.WriteLine(filename);
            VoxFile_VoxelSet voxels = null;
            byte[] bytes = File.ReadAllBytes(filename);

            if (bytes[0] != 'V' ||
                bytes[1] != 'O' ||
                bytes[2] != 'X')
            {
                throw new Exception("VOX header missing from " + filename);
            }
            int versionNumber = bytes[4];//4 bytes

            int index = 8;
            while (index < bytes.Length - 4)
            {
                if (bytes[index + 0] == 'M' &&
                    bytes[index + 1] == 'A' &&
                    bytes[index + 2] == 'I' &&
                    bytes[index + 3] == 'N')
                {
                    index += 4;
                    Console.Write("[MAIN]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);
                }
                else if (bytes[index + 0] == 'P' &&
                         bytes[index + 1] == 'A' &&
                         bytes[index + 2] == 'C' &&
                         bytes[index + 3] == 'K')
                {
                    index += 4;
                    Console.Write("[PACK]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);
                    int numModels = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    Console.Write(" numModels " + numModels);
                }
                else if (bytes[index + 0] == 'S' &&
                         bytes[index + 1] == 'I' &&
                         bytes[index + 2] == 'Z' &&
                         bytes[index + 3] == 'E')
                {
                    index += 4;
                    Console.Write("[SIZE]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);
                    int sizeX = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int sizeY = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int sizeZ = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    Console.Write(" sxyz " + sizeX + "," + sizeY + "," + sizeZ);
                }
                else if (bytes[index + 0] == 'X' &&
                         bytes[index + 1] == 'Y' &&
                         bytes[index + 2] == 'Z' &&
                         bytes[index + 3] == 'I')
                {
                    index += 4;
                    Console.Write("[XYZI]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);

                    int numVoxels = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    Console.Write(" voxels=" + numVoxels);

                    voxels = new VoxFile_VoxelSet(numVoxels);

                    for (int vox = 0; vox < numVoxels; vox++)
                    {
                        VoxFile_Voxel voxel = new VoxFile_Voxel()
                        {
                            X = bytes[index++],
                            Z = bytes[index++],
                            Y = bytes[index++],
                            I = bytes[index++]
                        };
                        voxels.voxels[vox] = voxel;
                    }
                }
                else if (bytes[index + 0] == 'R' &&
                         bytes[index + 1] == 'G' &&
                         bytes[index + 2] == 'B' &&
                         bytes[index + 3] == 'A')
                {
                    index += 4;
                    Console.Write("[RGBA]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);

                    voxels.alternate_palette = new uint[256];
                    voxels.palette = voxels.alternate_palette;
                    for (int i = 0; i < 256; i++)
                    {
                        voxels.alternate_palette[i] |= (uint)(bytes[index++] << 0);
                        voxels.alternate_palette[i] |= (uint)(bytes[index++] << 8);
                        voxels.alternate_palette[i] |= (uint)(bytes[index++] << 16);
                        voxels.alternate_palette[i] |= (uint)(bytes[index++] << 24);
                    }
                }
                else if (bytes[index + 0] == 'M' &&
                         bytes[index + 1] == 'A' &&
                         bytes[index + 2] == 'T' &&
                         bytes[index + 3] == 'T')
                {
                    index += 4;
                    Console.Write("[MATT]");
                    int chunkContent = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    int chunkChildren = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    //Console.WriteLine("chunk content " + chunkContent + " children " + chunkChildren);
                    index += 4;//id
                    index += 4;//mat type
                    index += 4;// material weight

                    int propertyBits = bytes[index++] + (bytes[index++] << 8) + (bytes[index++] << 16) + (bytes[index++] << 24);
                    index += 4 * propertyBits;
                }

                //Console.Write(index+" ");
            }

            Console.WriteLine(" ok");
            return voxels;
        }