packages/Windows API Code Pack 1.1/source/Samples/DirectX/CS/UtilitiesLibrary/MeshLoader/XMeshLoader.cs
/// <summary>
/// Loads the indexed normal vectors for a mesh
/// </summary>
/// <param name="meshNormalData"></param>
/// <returns></returns>
Loads the indexed normal vectors for a mesh
Loads the indexed normal vectors for a mesh
IndexedMeshNormals LoadMeshNormals(IXDataObject dataObject)
        {
            IndexedMeshNormals indexedMeshNormals = new IndexedMeshNormals();
            Match normalCount = findArrayCount.Match(dataObject.Body);
            if(!normalCount.Success)
                throw new System.IO.InvalidDataException("problem reading mesh normals count");
            indexedMeshNormals.normalVectors = new List<Vector4F>();
            int normals = int.Parse(normalCount.Groups[1].Value, CultureInfo.InvariantCulture);
            int dataOffset = normalCount.Index + normalCount.Length;
            for(int normalIndex = 0; normalIndex < normals; normalIndex++)
            {
                Match normal = findVector3F.Match(dataObject.Body, dataOffset);
                if(!normal.Success)
                    throw new System.IO.InvalidDataException("problem reading mesh normal vector");
                else
                    dataOffset = normal.Index + normal.Length;
                indexedMeshNormals.normalVectors.Add(
                    new Vector4F(
                        float.Parse(normal.Groups[1].Value, CultureInfo.InvariantCulture),
                        float.Parse(normal.Groups[2].Value, CultureInfo.InvariantCulture),
                        float.Parse(normal.Groups[3].Value, CultureInfo.InvariantCulture),
                        1.0f));
            }
            Match faceNormalCount = findArrayCount.Match(dataObject.Body, dataOffset);
            if(!faceNormalCount.Success)
                throw new System.IO.InvalidDataException("problem reading mesh normals count");
            indexedMeshNormals.normalIndexMap = new List<Int32>();
            int faceCount = int.Parse(faceNormalCount.Groups[1].Value, CultureInfo.InvariantCulture);
            dataOffset = faceNormalCount.Index + faceNormalCount.Length;
            for(int faceNormalIndex = 0; faceNormalIndex < faceCount; faceNormalIndex++)
            {
                Match normalFace = findVertexIndex.Match(dataObject.Body, dataOffset);
                if(!normalFace.Success)
                    throw new System.IO.InvalidDataException("problem reading mesh normal face");
                else
                    dataOffset = normalFace.Index + normalFace.Length;
                string[] vertexIndexes = normalFace.Groups[2].Value.Split(new char[] { ',' });
                for(int n = 0; n <= vertexIndexes.Length - 3; n ++)
                {
                    indexedMeshNormals.normalIndexMap.Add(int.Parse(vertexIndexes[0], CultureInfo.InvariantCulture));
                    indexedMeshNormals.normalIndexMap.Add(int.Parse(vertexIndexes[1 + n], CultureInfo.InvariantCulture));
                    indexedMeshNormals.normalIndexMap.Add(int.Parse(vertexIndexes[2 + n], CultureInfo.InvariantCulture));
                }
            }
            return indexedMeshNormals;
        }
IndexedMeshNormals LoadMeshNormals ( IXDataObject dataObject ) { IndexedMeshNormals indexedMeshNormals = new IndexedMeshNormals ( ) ; Match normalCount = findArrayCount . Match ( dataObject . Body ) ; if ( ! normalCount . Success ) throw new System . IO . InvalidDataException ( " " ) ; indexedMeshNormals . normalVectors = new List < Vector4F > ( ) ; int normals = int . Parse ( normalCount . Groups [ 1 ] . Value , CultureInfo . InvariantCulture ) ; int dataOffset = normalCount . Index + normalCount . Length ; for ( int normalIndex = 0 ; normalIndex < normals ; normalIndex ++ ) { Match normal = findVector3F . Match ( dataObject . Body , dataOffset ) ; if ( ! normal . Success ) throw new System . IO . InvalidDataException ( " " ) ; else dataOffset = normal . Index + normal . Length ; indexedMeshNormals . normalVectors . Add ( new Vector4F ( float . Parse ( normal . Groups [ 1 ] . Value , CultureInfo . InvariantCulture ) , float . Parse ( normal . Groups [ 2 ] . Value , CultureInfo . InvariantCulture ) , float . Parse ( normal . Groups [ 3 ] . Value , CultureInfo . InvariantCulture ) , 1.0f ) ) ; } Match faceNormalCount = findArrayCount . Match ( dataObject . Body , dataOffset ) ; if ( ! faceNormalCount . Success ) throw new System . IO . InvalidDataException ( " " ) ; indexedMeshNormals . normalIndexMap = new List < Int32 > ( ) ; int faceCount = int . Parse ( faceNormalCount . Groups [ 1 ] . Value , CultureInfo . InvariantCulture ) ; dataOffset = faceNormalCount . Index + faceNormalCount . Length ; for ( int faceNormalIndex = 0 ; faceNormalIndex < faceCount ; faceNormalIndex ++ ) { Match normalFace = findVertexIndex . Match ( dataObject . Body , dataOffset ) ; if ( ! normalFace . Success ) throw new System . IO . InvalidDataException ( " " ) ; else dataOffset = normalFace . Index + normalFace . Length ; string [ ] vertexIndexes = normalFace . Groups [ 2 ] . Value . Split ( new char [ ] { ' ' } ) ; for ( int n = 0 ; n <= vertexIndexes . Length - 3 ; n ++ ) { indexedMeshNormals . normalIndexMap . Add ( int . Parse ( vertexIndexes [ 0 ] , CultureInfo . InvariantCulture ) ) ; indexedMeshNormals . normalIndexMap . Add ( int . Parse ( vertexIndexes [ 1 + n ] , CultureInfo . InvariantCulture ) ) ; indexedMeshNormals . normalIndexMap . Add ( int . Parse ( vertexIndexes [ 2 + n ] , CultureInfo . InvariantCulture ) ) ; } } return indexedMeshNormals ; }
Loads the indexed normal vectors for a mesh
Loads the indexed normal vectors for a mesh
None
dataObject
IXDataObject
meshNormalData
None