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:
- New .x file interfaces
- Exporting .x files from a 3D package
- Loading .x files via code
- Saving .x files via code
- Skinned Mesh Animation
- Further Reading
From the December 2004 SDK update a number of new .x file interfaces were introduced. The old interfaces are still available but are now marked as legacy. I suspect they may be removed in a later update. There is very little information available about working with .x files and none that I can find that use the new interfaces. I use the old interfaces below when talking about loading .x files but these pages will be updated soon. The new page on saving .x files is right up to date and uses the new interfaces.
The DirectX SDK comes with some exporter plugins for 3DS Max and Maya. They are in the SDK extras folder. Other (better) ones are available on the Internet. Some sample .x files are also in the SDK samples/media directory.
There is a utility found under Start / Programs / DirectX / Utilities called mesh viewer that allows you to load, view and optimise .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.
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.
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 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:
I have added notes on skinned mesh animation to the Load X Hierarchy page.
- Triple Buffer have some great articles on .x file loading
- Pandasoft – .x file exporter for Maya and Max:
- Blender – popular free 3D package
- DeleD 3D Editor – another free 3D editor
- gameSpace – from the makers of Truespace
- Modular D3D SkinnedMesh article on Flipcode
See the resources section for more Books, Links and Assets
- What is the .x file format?
- How do I get 3D models in .x file format?
- How do I load .X files?
- How can I animate the .X files?
- When I load my x file I get errors about missing textures
- What texture formats are supported by .x files?
- I am still having problems with .x files, what steps can I take to resolve it?
- I have created a .x level in max and now want proper collisions?
- I want to access the vertex data in my .x file, how do I do it?
- I do not get colours \ textures on my model, why?
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.
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.
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.
Currently: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga
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.
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:
// party on the bits!
// Finished so unlock
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.