Ejemplo n.º 1
0
        private void SetVertexIndices(Mesh mesh, Vertex vert, VertexData vertData, ShapeVertexDescriptor descriptor, int vertIndex)
        {
            if (descriptor.CheckAttribute(GXVertexAttribute.Position))
            {
                Vector3D posVec   = mesh.Vertices[vertIndex];
                uint     posIndex = (uint)vertData.Positions.IndexOf(posVec.ToOpenTKVector3());
                vert.SetAttributeIndex(GXVertexAttribute.Position, posIndex);
            }
            if (descriptor.CheckAttribute(GXVertexAttribute.Normal))
            {
                Vector3D normVec   = mesh.Normals[vertIndex];
                uint     normIndex = (uint)vertData.Normals.IndexOf(normVec.ToOpenTKVector3());
                vert.SetAttributeIndex(GXVertexAttribute.Normal, normIndex);
            }

            for (int color = 0; color < 2; color++)
            {
                if (descriptor.CheckAttribute(GXVertexAttribute.Color0 + color))
                {
                    Color4D      assimpColor = mesh.VertexColorChannels[color][vertIndex];
                    List <Color> colorData   = (List <Color>)vertData.GetAttributeData(GXVertexAttribute.Color0 + color);
                    uint         colIndex    = (uint)colorData.IndexOf(assimpColor.ToSuperBMDColorRGBA());
                    vert.SetAttributeIndex(GXVertexAttribute.Color0 + color, colIndex);
                }
            }

            for (int tex = 0; tex < 8; tex++)
            {
                if (descriptor.CheckAttribute(GXVertexAttribute.Tex0 + tex))
                {
                    Vector3D texVec = mesh.TextureCoordinateChannels[tex][vertIndex];
                    List <OpenTK.Vector2> texData = (List <OpenTK.Vector2>)vertData.GetAttributeData(GXVertexAttribute.Tex0 + tex);
                    uint texIndex = (uint)texData.IndexOf(texVec.ToOpenTKVector2());
                    vert.SetAttributeIndex(GXVertexAttribute.Tex0 + tex, texIndex);
                }
            }
        }
Ejemplo n.º 2
0
        private SHP1(EndianBinaryReader reader, int offset)
        {
            Shapes     = new List <Shape>();
            RemapTable = new List <int>();

            reader.BaseStream.Seek(offset, System.IO.SeekOrigin.Begin);
            reader.SkipInt32();
            int shp1Size   = reader.ReadInt32();
            int entryCount = reader.ReadInt16();

            reader.SkipInt16();

            int shapeHeaderDataOffset = reader.ReadInt32();
            int shapeRemapTableOffset = reader.ReadInt32();
            int unusedOffset          = reader.ReadInt32();
            int attributeDataOffset   = reader.ReadInt32();
            int matrixIndexDataOffset = reader.ReadInt32();
            int primitiveDataOffset   = reader.ReadInt32();
            int matrixDataOffset      = reader.ReadInt32();
            int PacketInfoDataOffset  = reader.ReadInt32();

            reader.BaseStream.Seek(offset + shapeRemapTableOffset, System.IO.SeekOrigin.Begin);

            // Remap table
            for (int i = 0; i < entryCount; i++)
            {
                RemapTable.Add(reader.ReadInt16());
            }

            int highestIndex = J3DUtility.GetHighestValue(RemapTable);

            // Packet data
            List <Tuple <int, int> > packetData = new List <Tuple <int, int> >(); // <packet size, packet offset>
            int packetDataCount = (shp1Size - PacketInfoDataOffset) / 8;

            reader.BaseStream.Seek(PacketInfoDataOffset + offset, System.IO.SeekOrigin.Begin);

            for (int i = 0; i < packetDataCount; i++)
            {
                packetData.Add(new Tuple <int, int>(reader.ReadInt32(), reader.ReadInt32()));
            }

            // Matrix data
            List <Tuple <int, int> > matrixData    = new List <Tuple <int, int> >(); // <index count, start index>
            List <int[]>             matrixIndices = new List <int[]>();

            int matrixDataCount = (PacketInfoDataOffset - matrixDataOffset) / 8;

            reader.BaseStream.Seek(matrixDataOffset + offset, System.IO.SeekOrigin.Begin);

            for (int i = 0; i < matrixDataCount; i++)
            {
                reader.SkipInt16();
                matrixData.Add(new Tuple <int, int>(reader.ReadInt16(), reader.ReadInt32()));
            }

            for (int i = 0; i < matrixDataCount; i++)
            {
                reader.BaseStream.Seek(offset + matrixIndexDataOffset + (matrixData[i].Item2 * 2), System.IO.SeekOrigin.Begin);
                int[] indices = new int[matrixData[i].Item1];

                for (int j = 0; j < matrixData[i].Item1; j++)
                {
                    indices[j] = reader.ReadInt16();
                }

                matrixIndices.Add(indices);
            }

            // Shape data
            List <Shape> tempShapeList = new List <Shape>();

            reader.BaseStream.Seek(offset + shapeHeaderDataOffset, System.IO.SeekOrigin.Begin);

            for (int i = 0; i < highestIndex + 1; i++)
            {
                byte matrixType = reader.ReadByte();
                reader.SkipByte();

                int packetCount          = reader.ReadInt16();
                int shapeAttributeOffset = reader.ReadInt16();
                int shapeMatrixDataIndex = reader.ReadInt16();
                int firstPacketIndex     = reader.ReadInt16();
                reader.SkipInt16();

                BoundingVolume shapeVol = new BoundingVolume(reader);

                long curOffset = reader.BaseStream.Position;

                ShapeVertexDescriptor descriptor = new ShapeVertexDescriptor(reader, offset + attributeDataOffset + shapeAttributeOffset);

                List <Packet> shapePackets = new List <Packet>();
                for (int j = 0; j < packetCount; j++)
                {
                    int packetSize   = packetData[j + firstPacketIndex].Item1;
                    int packetOffset = packetData[j + firstPacketIndex].Item2;

                    Packet pack = new Packet(packetSize, packetOffset + primitiveDataOffset + offset, matrixIndices[j + firstPacketIndex]);
                    pack.ReadPrimitives(reader, descriptor);

                    shapePackets.Add(pack);
                }

                tempShapeList.Add(new Shape(descriptor, shapeVol, shapePackets, matrixType));

                reader.BaseStream.Seek(curOffset, System.IO.SeekOrigin.Begin);
            }

            for (int i = 0; i < entryCount; i++)
            {
                Shapes.Add(tempShapeList[RemapTable[i]]);
            }

            reader.BaseStream.Seek(offset + shp1Size, System.IO.SeekOrigin.Begin);
        }