Advertisement

You're blocking ads, which pay for BlenderNation. Read about other ways to support us.

Importing Blender 2.5 Files in DirectX apps

14

With the dropped support of the .X file format in DirectX 10, getting your Blender content into a DirectX app became hard or even impossible. Clemens Fehr has published C++ code that reduces this task to:

WorldObject *o = new WorldObject();
LoadBlenderFile("file.blend", &o->mesh, o->mtrls, o->textures, UV_DuplicateVertex);

Clemens writes:

In DirectX 9 there was built in support for the .X file format. You could easily load .X files with a few lines of code. This looked like the format of choice because .X files are supposed to be supported by almost all 3D tools like blender, 3ds Max and a lot of free or low cost tools.

You see the problems once you actually begin to work with .X files. I never got Blender to export .X files that would work with DirectX right away. Either the mesh data was wrong, normals were incorrect, orientation was mangled or UV coordinates didn't work. To get a model to show up in my 3d app I had to export in blender to .3ds without uv mapping, then do the uv mapping in another tool, then finally export the uv mapped model from that tool to .X file format. And this approach didn't even work reliably.

The final nail in the coffin for the .X file format came from Microsoft themselves: They completely abandoned .X file format support in DirectX 10.

The whole thing really got to my nerves. I wanted to try out 3d stuff graphically, not waste my time with file formats and imports/exports. At one time I thought about actually buying 3dsMax, but the cost was just too high. And even if their exporter would produce a X file I could import with DirectX 9 I would have problems switching my app to DirectX 10. Also, I felt bad to rely on an abandoned technology.

At some time I realized that there was no ready solution out there that would suit my needs completely. I wanted to stick to Blender so the only solution I saw was getting my hands dirty and do something on my own.

Link

About the Author

Avatar image for Bart Veldhuizen
Bart Veldhuizen

I have a LONG history with Blender - I wrote some of the earliest Blender tutorials, worked for Not a Number and helped run the crowdfunding campaign that open sourced Blender (the first one on the internet!). I founded BlenderNation in 2006 and have been editing it every single day since then ;-) I also run the Blender Artists forum and I'm Head of Community at Sketchfab.

14 Comments

  1. Combining this with some interesting open source 3D engines (like Irrlicht, Ogre3D) may give interesting workflow.

    Most probably, some additional conversion methods should be implemented to interoperate between 3D engine and this code in real application.

    But that's a good start, anyway, I've been struggling with .exporting valid X files from Blender to Irrlicht, some time ago. It was rather painful experience.

  2. DirectX is evil! Run away from it!
    Ah, why?
    Because, microsoft...
    Ah, why?
    Because business...
    Ah, why
    Because gold toilets...
    Ah, why?
    Oh well...

  3. You might find blender data structures a bit too complicated and since you are only kind of importing static meshes you might reconsider *.obj file format ("Wavefront OBJ" I believe it is it's proper name). It has rather easy syntax and is plain text and a lot simpler then .blend .

    Every version of blender has a rather good exporter for that file type and it can even export simple material properties (textures, color). I find it good as interchange format because if you want to be efficient you need to store data on disk in as close format as you are feeding to the graphics hardware. In respect to that neither .blend nor any other format used by modeling software is sufficient.

    Oh and to make things easy on importing .obj you might consider converting all quads to tris before export ( in blender 2.49 there was option for it right in obj exporter dialog).

    Last time I checked Irrlicht, Ogre and such all support reading .obj files as meshes

  4. Ahh! They would!

    Ah well, I suppose it's for the best. Now the .fbx format should be taking over.

    Alas, I just finished a new .X exporter for Blender 2.5 and it actually works!
    I submitted it a couple days ago, and it was recently moved to trunk.

    Here's the wiki page for it:
    http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/File_I-O/DirectX_Exporter

    Hopefully that should take care of everyone's exporter woes, but it seems that new apps are gonna have no need for it.... sigh.. :P

    What awful timing!

    Maybe I'll write an FBX exporter now?

  5. i luv direct x (cuz itz on meh graphics card)

    if only we could import to direct x 11 stuff and have blender tesselation lol

  6. Good riddance .x. It was never meant to be used as widely as it was. It's a simple format for simple meshes that were used in some old D3DX and D3D SDK example programs. There's no conspiracy here. .x support was never even part of the actual D3D9 api. It was just part of a small helper library that was used as common functionality to build their example programs off of.

    Anyone saying it's impossible to get blender data into a D3D10 program means they should probably go back and learn to program properly. Reading and writing or files is a trivial, beginner, task. Most people writing their own 3D software use their own, custom, optimized, formats anyways. The format long outlived it's usefulness. It has also been very hard to find any 2 programs that can read each other's .x files too.

    It's easy enough to open up Blender's scripting window and write something out to read back in with your own software. Output whatever information you need in a way that will make the most sense to your program. If file size isn't an issue, using XML makes this VERY easy.

    Once you can do that, you won't be worried about shoehorning things into, or living with the limitations of other formats.

    The original post up top reminds me of a post where someone is worried that they are going to starve in a grocery store.

  7. Yet another reason for people to ween themselves off Microsoft. They are constantly manipulating things for their own benefit and just causing problems for everyone else. Don't be lazy just learn Linux and dump Microsoft.

  8. Thanks for all the comments.

    Of course there are always many ways to do something. I chose to read .blend files because I liked the direct approach. No exporting needed. Actually, reading blender files was not too hard.
    The only thing that caused some headache was the inability of D3D to accept more than one pair of uv coordinates for a vertex. But this has to be solved somewhere. Either in blender exporter or in DirectX import.
    I just felt there is really no need to take the indirect route via another file format in between blender and application.

    Also, blender is a binary format that can be read pretty fast. I wouldn't like to bother with XML decoding in my app. I am not saying that this is the right approach to everything, but it felt like the right approach to hobby programmers like me.

  9. Thank you, lamour, for making your code available. Direct3D is not going to die, and your efforts can be expanded upon to good ends. I am glad that I stumbled upon your work nearly a year after your kind sharing. I am sure that I will be able to use your efforts after I translate the code into Pascal....yes, one of those Lazarus/Delphi people...LOL!! Thanks again, very much, and also for the link to the info on the file structure from your site...which is also real nice.

Leave A Reply

To add a profile picture to your message, register your email address with Gravatar.com. To protect your email address, create an account on BlenderNation and log in when posting a message.

Advertisement

×