0
\$\begingroup\$

I'm trying to parse a .x file using SlimDX. I can create the XFile object and register templates but I'm having problems with the enumeration object. The enumeration object has a child count of 0 for a file I know to have valid data. Here is code to create file, enumeration, and data objects:

public void Parse(string filename, string templates, ref Frame aParam)
        {
            XFile xfile = null;
            XFileEnumerationObject enumObj = null;
            XFileData dataObj = null;

            // create file object
            xfile = new XFile();

            // register templates
            if (xfile.RegisterTemplates(XFile.DefaultTemplates).IsFailure)
            {
                Console.WriteLine(Result.Last);
                xfile.Dispose();
                return;
            }

            // create enumeration object
            enumObj = xfile.CreateEnumerationObject(filename, System.Runtime.InteropServices.CharSet.Auto);
            if (enumObj == null)
            {
                xfile.Dispose();
                return;
            }

            // get child count( returns 0 here )
            long ncElements = enumObj.ChildCount;

            for (int i = 0; i < ncElements; ++i)
            {
                // never reached...
                dataObj = enumObj.GetChild(i);
                if (dataObj.IsReference) continue;

                try { Parse(dataObj, ref aParam); }
                catch (Exception e) { e.Write(); }
                finally { dataObj.Dispose(); }
            }

            enumObj.Dispose();
            xfile.Dispose();
        }

...There are no exceptions thrown by this function...the child count is 0 so the conditional loop breaks right away, the file objects are disposed of and the function returns...

Here is .x file...a simple cube:

xof 0303txt 0032

Frame Root {
  FrameTransformMatrix {
     1.000000, 0.000000, 0.000000, 0.000000,
     0.000000, 1.000000, 0.000000, 0.000000,
     0.000000, 0.000000, 1.000000, 0.000000,
     0.000000, 0.000000, 0.000000, 1.000000;;
  }
  Frame Cube {
    FrameTransformMatrix {
       1.000000, 0.000000, 0.000000, 0.000000,
       0.000000, 1.000000, 0.000000, 0.000000,
       0.000000, 0.000000, 1.000000, 0.000000,
       0.000000, 0.000000, 0.000000, 1.000000;;
    }
    Mesh Cube{ //Cube Mesh
      36;
      -1.000000; 1.000000; 1.000000;,
      -1.000000;-1.000000; 1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000;-1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000; 0.999999; 1.000000;,
      -1.000000; 1.000000; 1.000000;,
       0.999999;-1.000001; 1.000000;,
      -1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000; 1.000000; 1.000000;,
       1.000000; 1.000000;-1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
       1.000000; 1.000000;-1.000000;,
       1.000000; 0.999999; 1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;,
      -1.000000; 1.000000; 1.000000;,
       1.000000; 1.000000;-1.000000;,
      -1.000000;-1.000000; 1.000000;,
      -1.000000;-1.000000;-1.000000;,
       0.999999;-1.000001; 1.000000;,
       1.000000;-1.000000;-1.000000;,
      -1.000000;-1.000000;-1.000000;,
      -1.000000; 1.000000;-1.000000;;
      12;
      3;0;1;2;,
      3;3;4;5;,
      3;6;7;8;,
      3;9;10;11;,
      3;12;13;14;,
      3;15;16;17;,
      3;18;19;20;,
      3;21;22;23;,
      3;24;25;26;,
      3;27;28;29;,
      3;30;31;32;,
      3;33;34;35;;
      MeshNormals { //Mesh Normals
        36;
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
         0.000000;-0.000000; 1.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-1.000000;-0.000000;,
        -0.000000;-0.000000; 1.000000;,
        -0.000000;-0.000000; 1.000000;,
        -0.000000;-0.000000; 1.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         1.000000;-0.000001; 0.000000;,
         1.000000;-0.000001; 0.000000;,
         1.000000;-0.000001; 0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
        -1.000000; 0.000000;-0.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         0.000000; 0.000000;-1.000000;,
         1.000000; 0.000000;-0.000000;,
         1.000000; 0.000000;-0.000000;,
         1.000000; 0.000000;-0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
         0.000000; 1.000000; 0.000000;,
        -0.000000;-1.000000; 0.000000;,
        -0.000000;-1.000000; 0.000000;,
        -0.000000;-1.000000; 0.000000;,
         0.000000;-0.000000;-1.000000;,
         0.000000;-0.000000;-1.000000;,
         0.000000;-0.000000;-1.000000;;
        12;
        3;0;1;2;,
        3;3;4;5;,
        3;6;7;8;,
        3;9;10;11;,
        3;12;13;14;,
        3;15;16;17;,
        3;18;19;20;,
        3;21;22;23;,
        3;24;25;26;,
        3;27;28;29;,
        3;30;31;32;,
        3;33;34;35;;
      } //End of Mesh Normals
      MeshMaterialList { //Mesh Material List
        1;
        12;
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0;;
        Material Material {
           0.640000; 0.640000; 0.640000; 1.000000;;
           96.078431;
           0.500000; 0.500000; 0.500000;;
           0.000000; 0.000000; 0.000000;;
           TextureFilename {"Yellow.jpg";}
        }
      } //End of Mesh Material List
      MeshTextureCoords UVMap{ //Mesh UV Coordinates
        36;
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 0.000000;,
         0.000000; 1.000000;,
         1.000000; 1.000000;,
         1.000000; 0.000000;;
      } //End of Mesh UV Coordinates
    } //End of Mesh Mesh
  } //End of Cube
} //End of Root Frame
\$\endgroup\$
1
\$\begingroup\$

Ok figured out the problem...The template strings in the XFile object do not include the standard templates(eg. Frame, Mesh, MeshMaterial, MeshNormals...etc). In order to register those templates you must save the following templates into a text file, load the text file at runtime using FileStream, and register the file in memory ... Here is text file with standard templates...be sure not to use any extra newline characters:

xof 0303txt 0032 
template Frame {
<3d82ab46-62da-11cf-ab39-0020af71e433>
[...]
}
template Matrix4x4 {
<f6f23f45-7686-11cf-8f52-0040333594a3>
array FLOAT matrix[16];
}
template FrameTransformMatrix {
<f6f23f41-7686-11cf-8f52-0040333594a3>
Matrix4x4 frameMatrix;
}
template Vector {
<3d82ab5e-62da-11cf-ab39-0020af71e433>
FLOAT x;
FLOAT y;
FLOAT z;
}
template MeshFace {
<3d82ab5f-62da-11cf-ab39-0020af71e433>
DWORD nFaceVertexIndices;
array DWORD faceVertexIndices[nFaceVertexIndices];
}
template Mesh {
<3d82ab44-62da-11cf-ab39-0020af71e433>
DWORD nVertices;
array Vector vertices[nVertices];
DWORD nFaces;
array MeshFace faces[nFaces];
[...]
}
template MeshNormals {
<f6f23f43-7686-11cf-8f52-0040333594a3>
DWORD nNormals;
array Vector normals[nNormals];
DWORD nFaceNormals;
array MeshFace faceNormals[nFaceNormals];
}
template Coords2d {
<f6f23f44-7686-11cf-8f52-0040333594a3>
FLOAT u;
FLOAT v;
}
template MeshTextureCoords {
<f6f23f40-7686-11cf-8f52-0040333594a3>
DWORD nTextureCoords;
array Coords2d textureCoords[nTextureCoords];
}
template ColorRGBA {
<35ff44e0-6c7c-11cf-8f52-0040333594a3>
FLOAT red;
FLOAT green;
FLOAT blue;
FLOAT alpha;
}
template ColorRGB {
<d3e16e81-7835-11cf-8f52-0040333594a3>
FLOAT red;
FLOAT green;
FLOAT blue;
}
template Material {
<3d82ab4d-62da-11cf-ab39-0020af71e433>
ColorRGBA faceColor;
FLOAT power;
ColorRGB specularColor;
ColorRGB emissiveColor;
[...]
}
template MeshMaterialList {
<f6f23f42-7686-11cf-8f52-0040333594a3>
DWORD nMaterials;
DWORD nFaceIndexes;
array DWORD faceIndexes[nFaceIndexes];
[Material <3d82ab4d-62da-11cf-ab39-0020af71e433>]
}
template TextureFilename {
<a42790e1-7810-11cf-8f52-0040333594a3>
STRING filename;
}
template VertexDuplicationIndices {
<b8d65549-d7c9-4995-89cf-53a9a8b031e3>
DWORD nIndices;
DWORD nOriginalVertices;
array DWORD indices[nIndices];
}

You can add skin templates and any others just be cautious of newline characters...

Here is updated code:

public void Parse(string filename, string templates, ref Frame aParam)
        {
            XFile xfile = null;
            XFileEnumerationObject enumObj = null;
            XFileData dataObj = null;

            try
            {
                byte[] temp = null;
                using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                    + @"\DXTemplates.txt", FileMode.Open, FileAccess.Read))
                {
                    temp = new byte[fs.Length];
                    fs.Read(temp, 0, (int)fs.Length);
                }

                // create file object
                xfile = new XFile();

                // Register the templates from the text file
                xfile.RegisterTemplates(temp);               


                // create enumeration object
                enumObj = xfile.CreateEnumerationObject(filename, System.Runtime.InteropServices.CharSet.Auto);

                // get child count( returns 1 here now )
                long ncElements = enumObj.ChildCount;

                for (int i = 0; i < ncElements; ++i)
                {
                    // never reached...
                    dataObj = enumObj.GetChild(i);
                    if (dataObj.IsReference) continue;

                    try { Parse(dataObj, ref aParam); }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { dataObj.Dispose(); }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                if(enumObj != null)enumObj.Dispose();
                if(xfile != null)xfile.Dispose();
            }
        }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.