jsmars
jsmars

Reputation: 1928

Reading quads from FBX files in XNA, or converting a triangulated polygon mesh into quads

I'm importing an FBX model as an XNA model object, and reading through the vertexbuffer to get a hold of the triangles, indices, UVs, and put it into my custom model class. The problem is that the buffer only holds data on each triangle, not the quads they once were in 3dsmax.

I need to retain the quad structure to use the mesh as I want. The following points are different ways I believe I can achieve this, but I would need some help to get it done.

  1. One method I've tried is simply create quads in the order that the triangles appear. This works great for primitives which have the correct vertex/triangle order where each triangle of a quad comes after another, but edited meshes have an obstructed order which breaks the flow. If someone knows a method on how to rearrange vertex/triangle order before export this would still work.

  2. Is there any way to read off the FBX file (preferebly after it's loaded as a Model object, to avoid rewriting a seperate model pipeline) which triangles are connected as quads?

  3. The 'coding' approach. Given a list of vertices/indices, order them up accordingly so that the quad structure becomes evident. 3dsmax has a 'quadrify' button that works quite well, but can't figure out a straightforward way of doing it. edit; For this approach, what I mean is, what is an optimal way to look at an unordered structure of triangles to find the best "quad" version of the mesh, similarly to the 'quadrify' in 3dsmax tools.

Upvotes: 0

Views: 822

Answers (1)

Ani
Ani

Reputation: 10896

It is possible to recover quad information using pure indexing information - for example, take the case of two quads decomposed into 4 triangles.

1       2       3
+-------+-------+
|\      |\      |
|  \    |  \    |
|    \  |    \  |
|      \|      \|
+-------+-------+
4       5       6

Now, your triangles are (assuming a clockwise winding order)

A=(1, 5, 4) B=(1, 2, 5) C=(2, 6, 5) D=(2, 3, 6)

The edges of your triangles are

A=(1-5, 5-4, 4-1) B=(1-2, 2-5, 5-1) C=(2-6, 6-5, 5-2) D=(2-3, 3-6, 6-2)

Now it's easy to see which triangles share an edge

  • A and B share exactly 1 edge 1-5 (because it is the same as 5-1)
  • B and C share 2-5
  • C and D share 2-6

Based on this information, we could quad-ify it as

  • 1 quad = (1, 2, 6, 5) + 2 triangles (1, 5, 4) and (2, 3, 6)

or

  • 2 quads (1, 2, 5, 4) and (2, 3, 6, 5)

A brute force method would be to determine all quad-ifications and choose the one that left the least number of degenerate triangles. If you're starting with perfect quad-based meshes, this should be zero.

Also, note that this method only examines and modifies index data. The total number of vertices or their values are left untouched.

Upvotes: 1

Related Questions