BannerLeftBannerRightToymaker

 

3D Models with .x files

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:

What is the .x file format?

This is a Microsoft model format for use with DirectX 9. 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. Note that DirectX 10 and 11 do not contain support for .x files directly, instead they use a sdkmesh format that they provide functions for and a tool to convert from .x or .obj to. See the FAQ answer below.

Exporting / Viewing / Converting .x files

Exporting

Microsoft provide some exporter plugins for 3DS Max and Maya. Other (often better) ones are available on the Internet. 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.

Viewing

There is a utility found under Start / Programs / DirectX / Utilities called DirectX viewer that allows you to load and view .x and .fx files.

Converting

There are a number of ways to convert from other formats to the .x format. If you have a 3D package that has an exporter for .x files then you can of course just load a model into that in one format and then export it into the .x format. If you don't have access to one of these there are a couple of utilities you can use.

MeshConvert - You will find this command line tool in the DirectX SDK Utilities directory. It allows conversion from .obj, .x and .sdkmesh to .x (binary or text) and .sdkmesh. .sdkmesh is the new format Microsoft are using for their DirectX samples.

Conv3ds.exe - this tool converts from 3DS .3ds files to .x files. It used to ship with the DirectX SDK but unfortunately no longer does so I am providing it here for you: conv3ds.rar. This includes a windows front end that makes it easier to use. It is very old though so may not handle some models.

There are many places on the Internet where you can get free models that you can then convert into .x format, some sites are listed here: Free 3D Models. Some sample .x files are also in the DirectX SDK samples/media directory.

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 also written a small library to load models with animation that you can use (XAnimator).

  • 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 also contain information on skinning.
  • XAnimator - a small library I have written to support the loading and rendering of .x files with animation

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:

Further Reading

Some Links:

See the resources section for more Books, Links and Assets

.x File FAQ

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.

How do I load .x files in Direct3D 10 or 11?

Direct3D 10 and 11 do not have support for .x file loading and manipulation like Direct3D 9 does however there is a new mesh type called sdkmesh that can be created from a .x file (or .obj file) via the command line tool meshconvert.exe. Functions for working with the sdkmesh format are contained in the DXUT library.



© 2004-2010 Keith Ditchburn  (A lecturer on the Games Programming Course at Teesside University)