Sign in to follow this  
CDProp

Writing a mesh exporter for Blender, for use with Direct3D

Recommended Posts

I'm trying to write a mesh exporter for Blender. I don't want to use the .x files because I'd rather write my own format. Conveniently, the Mesh module contains a list of verts. Each vert has a position, a normal, and an index. If I were to print out the verts list for a unit cube, it would look something like this: for v in cubeMesh.verts: print v [MVert (1.000000 1.000000 -1.000000) (0.577349 0.577349 -0.577349) 0] [MVert (1.000000 -1.000000 -1.000000) (0.577349 -0.577349 -0.577349) 1] [MVert (-1.000000 -1.000000 -1.000000) (-0.577349 -0.577349 -0.577349) 2] [MVert (-1.000000 1.000000 -1.000000) (-0.577349 0.577349 -0.577349) 3] [MVert (1.000000 1.000000 1.000000) (0.577349 0.577349 0.577349) 4] [MVert (1.000000 -1.000000 1.000000) (0.577349 -0.577349 0.577349) 5] [MVert (-1.000000 -1.000000 1.000000) (-0.577349 -0.577349 0.577349) 6] [MVert (-1.000000 1.000000 1.000000) (-0.577349 0.577349 0.577349) 7] Ok, so each line that starts with MVert is a vertex in the mesh. The first set of three numbers is the position of the vertex, the second set of three numbers is the normal of the vertex, and the last number is the index. As you can see, it prints them in order of index from 0-7. The mesh also has a list of faces. Each face has it's own list of verts. In my mesh, since it's triangulated, there will be three verts in this list for each of the faces. Each of the verts in this list will simply be a redundant copy of one of the verts in the above "master" list. This is convenient for me, because it allows me to easily create an indexed mesh format. I could simply use the above master list for the vertex buffer. For the index buffer, I would need the indices, which I could get using the faces list like so:
for f in cubeMesh.faces:
    for v in f.verts:
        print v.index
And I would get this: 7 0 3 4 0 7 2 6 7 2 7 3 1 5 2 5 6 2 0 4 1 4 5 1 4 7 6 4 6 5 3 0 2 2 0 1 Ok great, so now I have my 8 vertices and my 36 indices (12 triangles, 3 indices per triangle). Ok, here's my problem. This is great for when all I need are positions and normals, but what if I need vertex colors and UVs? The problem is that, with DirectX, these elements are part of the vertex. However, in Blender, these elements are part of the face. Just as each face has a list of verts, it also has a list of colors, and a list of UV's. What that means is that even though two faces might share a vertex, the UV and color at that vertex can be different for each face. There isn't necessarily a 1:1 ratio of vertices and vertex colors or uv coordinates. So, in order to make this how I need it, I'm thinking I'm just going to have to create my own vertex structure in my python script, then go through each face in the mesh, and add each and every vertex position, normal, color and uv. This will essentially create a non-indexed list. If I want to index it, then I'd have to go through my list of vertices and remove the redundant ones, and adjust the list of indices accordingly. I dunno, I'm interested to hear if anyone else has had this same problem, and how they dealt with it.

Share this post


Link to post
Share on other sites
I think everyone, who wanted to do more, than just display vertices had to deal with these problems.
I had the same when I wanted to load a mesh that was uv mapped and I also had overlapping vertices.

The important thing is, that both the list of vertices, containing the normals and position and the list of vertices for the uv coordinates use the same triangulation and the same face indices. You can still profit from an index buffer, but what you need to do is create 2 vertices (or more if you have multiple normals or multiple colors) for the overlapping entries.

See my post some years ago: http://www.gamedev.net/community/forums/topic.asp?topic_id=377177

Share this post


Link to post
Share on other sites
I ran into this problem myself as well a couple of months ago.

UV coordinates might be the least of your problems. Do you plan on doing skinning? You'll also run into a problem with vertex weights and bones if you do.

How about exporting animation?

I'm not familiar with Blender, but does it export to VRML? If it does and you don't plan on using skinning, just export to VRML and convert using that. It should export both the UV coordinates and animation data.

What I ended up doing do was breaking down and getting the student version of 3D Studio Max to write my own export plugin using 3DXI. There is a bit of a learning curve, but if you can write your own file converter from scratch, you should have no problem.

This was a hard lesson learned. I actually wrote my own skinning program before knowing that I could just write a plugin for max.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this