3D Models with .X files

Note Feb 2013:: the .x format is no longer being developed however you will still find some models that use it and you can use the MeshConvert utility in the DirectX SDK to convert between .obj, .x and the new Microsoft format .sdkmesh (my XFileEditor tool provides a GUI for this).

In order to easily use 3D objects created in a modelling package we can use the .x file format. This is a Microsoft format and is not normally used by published games but is a quick and easy way to get started. This page contains the following sections:

Tools

I have written a tool to allow some alterations to .x files and conversion using the Micorosoft MeshConvert utility. You can get it here: XFileEditor

This tool uses a library I have written to allow loading and playback of .x file animations. You can get it here: XAnimator

Exporting .x files

I would recommend you use the Panda exporter to export from 3DS Max, it is better than the one that comes with the DirectX SDK. It can be downloaded from here: Pandasoft  - simply place the file in the 3DS plugins folder. To save your model in .x format in 3DS select export from the file menu and change the type to Panda.x.

There are some free 3D editors available that also support the .x file format. For example the DeleD 3D Editor and a light version of gameSpace or the popular blender.

For blender try this .x file exporter: mindfloaters. Note: make sure none of your mesh or materials have a name with a . (dot) in it - this causes mesh view to be unable to open the .x file. Make sure you add UV co-ordinates and define your textures per face.

There are some places on the Internet where you can get free models that you can then convert into .x format.  www.3dcafe.com probably has the largest collection. I have listed others on the resources page here: Free 3D Models.

Loading .x files

There are two ways to load .x files in Direct3D, the hard way and the easy way. The hard way maintains the object hierarchy but involves some complex code. The easy way flattens the hierarchy and so does not allow you to do animation.Stick with the easy way unless you want to do animation. I have written notes on both methods, please follow the required link below:

  • X File Loading - the loading main page
    • Load X Simply - uses D3DXLoadMeshFromX which does not support animation but is simple to code
    • Load X Hierarchy -  uses D3DXLoadMeshHierarchyFromX - supports animation but is harder to code. These notes also shows multiple animations, how to combine them together from multiple .x files and mix animation tracks together for smooth change overs. They have recently been updated to include information on skinning.

Saving .x files

Saving your mesh data to the .x file format is far from easy. I struggled with this myself for a long time so I have written some notes to help others:

Skinned Mesh Animation

I have added notes on skinned mesh animation to the Load X Hierarchy page.

Further Reading

Some Links:

.x File FAQ

What is the .x file format?

This is a Microsoft model format for use with DirectX. Its not the most simple format and it is probably not the best. Normally when writing a game you will define your own format and use the 3DS Max SDK (or other modelling package) to create your own exporter. However the .x format allows you to quickly get objects loaded. The advantages of using .x are that there are exporters for Max and other packages and DirectX has a number of helper functions for working with the loaded mesh data.

How do I get the 3D models in .x file format?

The best way is to use 3DS  Max to convert them. There is a plugin freely available in the extras part of  the DirectX SDK that allows you to export in .x format. After installing the  plugin select the object you want to export and then choose the export selected  menu option in max and select to export as a .x file. There is another .x file exporter plugin for Max that is better, it can be obtained here: pandasoft

There are some free 3D editors available that also support the .x file format. For example the DeleD 3D Editor and a light version of gameSpace

If all else fails there is an old DOS utility, also in the extras folder,  called conv3ds.exe. This converts from .3ds into .x file format, however  it requires opening a command prompt and is not always very good at converting  the files. Avoid if at all possible.

When I load my x file it complains about missing textures?

As well as  the converted .x file you also have to have the textures used for the model and importantly they must be in the same directory as the executable file or the program will not find them. Alternatively you would need to write code to locate the correct directory. Often people put the x files in a data directory and then load them from there. If you do this you must alter the texture path to point to the correct directory as well or you could set the current directory to the location of the x file and set it back again afterward. You can get the current directory by using GetCurrentDirectory(...) and set it using SetCurrentDirectory(...) functions, look in the MSDN help for how to use these.

What texture formats are supported by DirectX in .x files?

Currently: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga

I am still having problems with .x files, what steps can I take to solve  it?

When exporting .x files from max you can export them as text files.  These are bigger than binary files but allow you to examine the data. Have a  look through the .x file and see if you can see how it refers to the textures  and see if there is a problem. You can even edit the file e.g. to change texture paths etc.

I have created a .x saved level in max and now want proper collisions?

When you  load a .x file using D3DXLoadMeshFromX it is collapsed into one big object so if you have created a whole level in max doing collisions against it is very difficult. For that reason I would  advise you against this. Instead create individual objects in max and assemble them into a level in your code. That way you can do bounding box collisions against individual objects. You can also load an x file without using this function and parse the frame structure yourself so you end up with a tree with each frame and mesh in it. This is more complex however, see the MultiAnimation example with the SDK and also my animation notes.

I want to access the vertex data in my .x file, how can I ?

The .x file is loaded into a D3DX helper class called D3DXMESH. It provides a number of functions. If you look in the dx help at the ID3DXBaseMesh section you will see a number of functions you can call, like getting the vertex and face data. An example of getting access to the vertex data can be seen here:

BYTE* pVertices=NULL;

hr=m_mesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);

// party on the bits!

// Finished so unlock
m_mesh->UnlockVertexBuffer();

I do not get colours / textures on my model, why?

Firstly check that your textures are being loaded. It may be that they cannot be found because they are not in the expected directory. The other possibility is that your render states have not been set up incorrectly. You may have changed them for some other rendering and not set them back correctly.



© 2004-2016 Keith Ditchburn