SlimDX and Parsing .X Files

Posted by P. Avery on Game Development See other posts from Game Development or by P. Avery
Published on 2014-04-06T05:42:40Z Indexed on 2014/05/29 22:06 UTC
Read the original article Hit count: 383

Filed under:
|
|

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

© Game Development or respective owner

Related posts about c#

Related posts about directx9