Exemple #1
0
        public VxlReader(Stream s)
        {
            if (!s.ReadASCII(16).StartsWith("Voxel Animation"))
            {
                throw new InvalidDataException("Invalid vxl header");
            }

            s.ReadUInt32();
            LimbCount = s.ReadUInt32();
            s.ReadUInt32();
            bodySize = s.ReadUInt32();
            s.Seek(770, SeekOrigin.Current);

            // Read Limb headers
            Limbs = new VxlLimb[LimbCount];
            for (var i = 0; i < LimbCount; i++)
            {
                Limbs[i]      = new VxlLimb();
                Limbs[i].Name = s.ReadASCII(16);
                s.Seek(12, SeekOrigin.Current);
            }

            // Skip to the Limb footers
            s.Seek(802 + 28 * LimbCount + bodySize, SeekOrigin.Begin);

            var limbDataOffset = new uint[LimbCount];

            for (var i = 0; i < LimbCount; i++)
            {
                limbDataOffset[i] = s.ReadUInt32();
                s.Seek(8, SeekOrigin.Current);
                Limbs[i].Scale = s.ReadFloat();
                s.Seek(48, SeekOrigin.Current);

                Limbs[i].Bounds = new float[6];
                for (var j = 0; j < 6; j++)
                {
                    Limbs[i].Bounds[j] = s.ReadFloat();
                }
                Limbs[i].Size = s.ReadBytes(3);
                Limbs[i].Type = (NormalType)s.ReadByte();
            }

            for (var i = 0; i < LimbCount; i++)
            {
                s.Seek(802 + 28 * LimbCount + limbDataOffset[i], SeekOrigin.Begin);
                ReadVoxelData(s, Limbs[i]);
            }
        }
Exemple #2
0
        static void ReadVoxelData(Stream s, VxlLimb l)
        {
            var baseSize = l.Size[0] * l.Size[1];
            var colStart = new int[baseSize];

            for (var i = 0; i < baseSize; i++)
            {
                colStart[i] = s.ReadInt32();
            }
            s.Seek(4 * baseSize, SeekOrigin.Current);
            var dataStart = s.Position;

            // Count the voxels in this limb
            l.VoxelCount = 0;
            for (var i = 0; i < baseSize; i++)
            {
                // Empty column
                if (colStart[i] == -1)
                {
                    continue;
                }

                s.Seek(dataStart + colStart[i], SeekOrigin.Begin);
                var z = 0;
                do
                {
                    z += s.ReadUInt8();
                    var count = s.ReadUInt8();
                    z            += count;
                    l.VoxelCount += count;
                    s.Seek(2 * count + 1, SeekOrigin.Current);
                } while (z < l.Size[2]);
            }

            // Read the data
            l.VoxelMap = new Dictionary <byte, VxlElement> [l.Size[0], l.Size[1]];
            for (var i = 0; i < baseSize; i++)
            {
                // Empty column
                if (colStart[i] == -1)
                {
                    continue;
                }

                s.Seek(dataStart + colStart[i], SeekOrigin.Begin);

                var  x = (byte)(i % l.Size[0]);
                var  y = (byte)(i / l.Size[0]);
                byte z = 0;
                l.VoxelMap[x, y] = new Dictionary <byte, VxlElement>();
                do
                {
                    z += s.ReadUInt8();
                    var count = s.ReadUInt8();
                    for (var j = 0; j < count; j++)
                    {
                        var v = new VxlElement();
                        v.Color  = s.ReadUInt8();
                        v.Normal = s.ReadUInt8();

                        l.VoxelMap[x, y].Add(z, v);
                        z++;
                    }

                    // Skip duplicate count
                    s.ReadUInt8();
                } while (z < l.Size[2]);
            }
        }