www.xbdev.net xbdev - software development
Tuesday January 22, 2019
home | about | contact | Donations

Prt-1- .x File Format

 

 

In the beginning there was nothing...

 

Well before rushing and writing lines of code to create your very own .x 3D File Loader/Renderer we should first take a look at the inner layout of the x file format....its pieces, what makes it tick etc...

 

Lets point out the main points that we know of first:

  • Allows complex 3d mesh's
  • Bone Configuration (e.g. move the body and the arms move as well)
  • Allows us to store Animations
  • Free to use...anyone can use it.

Now a thing to remember in the .x file format, is that where going to work with the .x text version here for this tutorial, as it offers us all the power and understanding...but the same principles apply to the binary version.

 

xof 0302txt 0032

Header {
1;
0;
1;
}

......

 

Okay this is the start of all our .x files......

xof      - is so we know its a x 3d file (required)

0302  - version number Majour Version (03) Minor Version (02)

txt     - format type (txt in this case, also you could have bin, tzip or bzip)

0032  - tells us how many bits a float is (accuracy thing) it could be 0064 for 64bit accuracy.

 

(Comments)

When I'm doing text file .x file's you can put comments in your file...great, as you could open it up in notepad and put a description at the top etc, date created how to use it or whatever you want.

# This is a comment.
// This is another comment.
 

 

Also remember, if you know how a file format works...e.g. its format/layout, you can write a loader program in any language....not just using DirectX ;)  But for the first part, I'll use the DirectX API's to show you some really cool effects.

 

Header {

1;

0;

1;

}

isn't compulsory, and is optional....application-specific information, such as version information is placed in here....

 

)( Syntax Note: )(

Commas are used to separate array members.  ( , )

Semicolons terminate each data item. ( ; )

 

Now onto Templates....a mountain of information...

This has to be one of the biggest things to grasp with the .x file format...templates....now DirectX has a specific number of templates which you can look up...the Main ones are listed here:

 


 

Header
Vector
Coords2d
Quaternion
Matrix4x4
ColorRGBA
ColorRGB
Indexed Color
Boolean
Boolean2d
Material
TextureFilename
MeshFace
MeshFaceWraps
MeshTextureCoords
MeshNormals
MeshVertexColors
MeshMaterialList
Mesh
FrameTransformMatrix
Frame
FloatKeys
TimedFloatKeys
AnimationKey
AnimationOptions
Animation
AnimationSet

 


Type (e.g. Int, DWORD, FLOAT etc) is in brackets ()

 

Header { Major Version(WORD); Minor Version(WORD); Flags(DWORD);  }
Vector { x (FLOAT); y (FLOAT); z (FLOAT); }
Coords2d { u (FLOAT); v (FLOAT); }
Quaternion { s (FLOAT); v (Vector); }
Matrix4x4 { x1 (FLOAT); x2(FLOAT); x3 (FLOAT)........x16 (FLOAT); }
ColorRGBA { red (FLOAT); green (FLOAT); blue (FLOAT); alpha (FLOAT); }
ColorRGB { red (FLOAT); green (FLOAT); blue (FLOAT); }
Indexed Color { index (DWORD); colour (ColorRGBA); }
Boolean { truefalse (DWORD); }
Material { faceColour (ColorRGBA); power (FLOAT); specularColour (ColorRGB); emissiveColour (ColorRGB); }
TextureFilename { szString (CHAR); } // Null terminated string
MeshFace { numFaceVertexIndices (DWORD); faceVertexIndices[1].....faceVertexIndices[numFaceVertexIndices] (DWORD); }
MeshFaceWraps { nFaceWrapValues (DWORD); faceWrapValues (Boolean2d); }
MeshTextureCoords { nTextCoords (DWORD); textCoords[1]..textCoords[nTextCoords] (Coords2d); }
MeshNormals { nNormals (DWORD); normals[1]..normals[nNormals] (Vector);   nFaceNormals (DWORD) facenormals[1]...facenormals[nFaceNormals] (MeshFace); }
MeshVertexColors { nVertexColours (DWORD); vertexColour[1]...vertexColour[nVertexColours] (IndexColor); }
MeshMaterialList { nMaterials (DWORD); nFaceIndexes (DWORD); faceIndex[1]...faceIndex[nFaceIndexes] (DWORD); }
Mesh { nVertices (DWORD); vertices[1]...vertices[nVertices] (Vector);  nFaces (DWORD); faces[1]...faces[nFaces] (MeshFace); }
FrameTransformMatrix { frameMatrix (Matrix4x4); }
Frame contains any number of Mesh's and a FrameTransformMatrix's
FloatKeys { nValues (DWORD); value[1]...value[nValues] (FLOAT); }
TimedFloatKeys { time (DWORD); tfkeys (FloatKeys); }
AnimationKey { keyType (DWORD); nKeys (DWORD);  keys[1]...keys[nKeys] (TimeFloatKeys); }
AnimationOptions { openclosed (DWORD); positionquality (DWORD); } openclosed ( 0 or 1), positionquality( 0 for spline positions or 1 for linear positions)
Animation {} - contains at least one AnimationKey, and optional AnimationOptions
AnimationSet {} - one or more Animation Objects

 

I think thats nearly all of them......

 

Dear god there's a lot there....If I was me I'd be worries..hehe....but don't worry...what your looking at above is the whole DirectX file format structure's (also called templates).

 

Lets take a look at a basic... x file...I'll call it cube.x....loads of them around.

 

xof 0303txt 0032

Header {
1;
0;
1;
}

// Definition of Mesh
//{ nVertices (DWORD); vertices[1]...vertices[nVertices] (Vector);
// nFaces (DWORD); faces[1]...faces[nFaces] (MeshFace); }


Mesh CubeMesh {
8; // 8 vertices
1.000000;1.000000;-1.000000;, // vertex 0
-1.000000;1.000000;-1.000000;, // vertex 1
-1.000000;1.000000;1.000000;, // etc...
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;;

12; // 12 faces
3;0,1,2;, // face 0 has 3 vertices
3;0,2,3;, // etc...
3;0,4,5;,
3;0,5,1;,
3;1,5,6;,
3;1,6,2;,
3;2,6,7;,
3;2,7,3;,
3;3,7,4;,
3;3,4,0;,
3;4,7,6;,
3;4,6,5;; // <- note that we put ;; indicates the end of a containter!
}

 

 

The biggest thing to grasp in the beggining to understand the .x file format is the syntax!  To end an array of Templates you put ";;" ....( 2 semicolons ) ...when you put an array of values..e.g. DWORD's you would seperate them with a "," (comma)....

 

As above you look at what the Definition of Mesh would be:

 

Definition of 'Mesh'
{ nVertices (DWORD); vertices[1]...vertices[nVertices] (Vector);  nFaces (DWORD); faces[1]...faces[nFaces] (MeshFace); }

 

Now we look at what the definition of 'Vector' and 'MeshFace' which is:

 

'Vector'
{ x (FLOAT); y (FLOAT); z (FLOAT); }

 

And finally the definition of 'MeshFace'
{ numFaceVertexIndices (DWORD); faceVertexIndices[1].....faceVertexIndices[numFaceVertexIndices] (DWORD); }
 


 

So we take it step by step, and see how the Mesh template is built up...so you can sort of see where the commas and semicolons go etc...

 

<1>

 

Mesh OptionalName

{

}

 

<2>

Mesh OptionalName

{

8; // Number of vertices

vertices1, vertices2, vertices3, vertices4, vertices5, vertices6, vertices7, vertices8;

12; // Number of faces

face1, face2, face3, face4, face5, face6, face7, face8, face9, face10, face11, face12;

}

 

<3> Replace 'vertices' and 'face' with there equivalents

So "vertices1" would become "1.000000;1.000000;-1.000000;"

and

"face1" would become "3;0,1,2;"

 

Mesh OptionalName {
8; // Number of vertices, 8 vertices in this case

//   x                y               z
1.000000;1.000000;-1.000000;, // vertex 0
-1.000000;1.000000;-1.000000;, // vertex 1
-1.000000;1.000000;1.000000;, // etc...
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;; // <- note the ";;"

12; // // Number of faces, 12 faces here
3;0,1,2;, // face 0 has 3 vertices
3;0,2,3;, // etc...
3;0,4,5;,
3;0,5,1;,
3;1,5,6;,
3;1,6,2;,
3;2,6,7;,
3;2,7,3;,
3;3,7,4;,
3;3,4,0;,
3;4,7,6;,
3;4,6,5;; // <- note that we put ;; indicates the end of a containter!
}

 

 

Its very important to see how to build up a .x file first...so that you can understand how the information is extracted and used later on to draw your 3D world or character.

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.