/// <summary>
 /// Initializes a new instance of the <see cref="MeshReader"/> class.
 /// </summary>
 /// <param name="mesh">The mesh.</param>
 /// <param name="definition">The mesh's definition data.</param>
 public MeshReader(Mesh mesh, RenderGeometryResourceDefinition definition)
 {
     Mesh = mesh;
     Definition = definition;
     VertexStreams = new VertexBufferDefinition[StreamCount];
     IndexBuffers = new IndexBufferDefinition[IndexBufferCount];
     BindVertexStreams();
     BindIndexBuffers();
 }
        /// <summary>
        /// Get an Assimp matrix for the mesh of a rendermesh.
        /// This is an approximation of the logic applied by the 
        ///  game to scaling the model using the assigned node.
        /// </summary>
        public static Assimp.Matrix4x4 getMatForMesh(RenderModel model, Mesh mesh)
        {
            var rot_mat = Assimp.Matrix3x3.Identity;
            int node_idx = mesh.RigidNodeIndex;
            if (node_idx < 0) {
                node_idx = 0;
            }
            if (node_idx < model.Nodes.Count)
            {
                float s = model.Nodes[node_idx].DefaultScale;
                RenderModel.Node node = model.Nodes[node_idx];
                rot_mat = new Assimp.Matrix3x3(node.InverseForward.X *s , node.InverseForward.Y * s, node.InverseForward.Z*s,
                                                    node.InverseLeft.X*s, node.InverseLeft.Y*s, node.InverseLeft.Z*s,
                                                    node.InverseUp.X*s, node.InverseUp.Y*s, node.InverseUp.Z*s);
                //Console.WriteLine("Loaded matrix: {0}", rot_mat);

            }

            var matrix_local = new Assimp.Matrix4x4(rot_mat);
            return matrix_local;
        }
Example #3
0
        /// <summary>
        /// Reads the index buffer data and converts it into a triangle list if necessary.
        /// </summary>
        /// <param name="reader">The mesh reader to use.</param>
        /// <param name="part">The mesh part to read.</param>
        /// <param name="resourceStream">A stream open on the resource data.</param>
        /// <returns>The index buffer converted into a triangle list.</returns>
        private static uint[] ReadIndexes(MeshReader reader, Mesh.Part part, Stream resourceStream)
        {
            // Use index buffer 0
            var indexBuffer = reader.IndexBuffers[0];
            if (indexBuffer == null)
                throw new InvalidOperationException("Index buffer 0 is null");

            // Read the indexes
            var indexStream = reader.OpenIndexBufferStream(indexBuffer, resourceStream);
            indexStream.Position = part.FirstIndex;
            switch (indexBuffer.Type)
            {
                case PrimitiveType.TriangleList:
                    return indexStream.ReadIndexes(part.IndexCount);
                case PrimitiveType.TriangleStrip:
                    return indexStream.ReadTriangleStrip(part.IndexCount);
                default:
                    throw new InvalidOperationException("Unsupported index buffer type: " + indexBuffer.Type);
            }
        }