Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to use Blender for making models yet realize some of the polygons are not triangles but contain quads or more (example: cylinder top and bottom). I could export the the mesh as a basic mesh file and import it in to an openGL application and workout rendering the quads as tris, but anything with more than 4 vert indices is beyond me. Is it typical to convert the mesh to a triangle-based mesh inside blender before exporting it? I actually tried this through the quads_convert_to_tris method within a blender py script and the top of the cylinder does not look symmetrical. What is typically done to render a loaded mesh as a tri?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Yes, it's typical to convert into triangles. When reading the mesh in, it's simple to convert a quad into a triangle. It will depend on the format you're exporting to. For example, the format I use, Blender will export all the vertices, then it will export index information for triangles and quads. So it's a simple matter of arranging the indices to take a quad and create two triangles.

if (indexCount == 3) { //triangle face
    int one = readNextIndex();
    int two = readNextIndex();
    int three = readNextIndex();
    indicies.add(one);
    indicies.add(two);
    indicies.add(three);

} else if (indexCount == 4) { //quad face
    int one = readNextIndex();
    int two = readNextIndex();
    int three = readNextIndex();
    int four = readNextIndex();

    indicies.add(one);
    indicies.add(two);
    indicies.add(three);

    indicies.add(three);
    indicies.add(four);
    indicies.add(one);
}

Where the index information looks like:

//NumberOfElements;Element1;...;ElementN;,
4;40;41;42;43;,
3;44;45;46;,

Alternatively, using Blender:

  1. Go into Edit Mode
  2. Press A to select all
  3. Press Ctrl+T to convert Quads to Triangles
  4. Re-export mesh with triangles
share|improve this answer
    
thanks for the code snippet, that's helpful. the quads_convert_to_tris is basically the control+T but automated. one thing I found is a cylinder top, or other complex shape, might not appear correct when converted, it also has more than 4 indices, more like 16-24 depending on the complexity. i think it might be best to remove the face and place a single center point vertex then try and make a bunch of tris from it, if that makes sense. –  scape Dec 16 '12 at 20:21
    
24 indices... wow thats a very cool ngon –  Gustavo Maciel Dec 17 '12 at 0:21
    
I'm assuming you explained that script so that you don't have to triangulate the model permanently, and it'll just be triangulated automatically in the exported mesh. Although you didn't say this outright, so I'm just assuming. –  jhocking Jan 28 at 16:31
    
@jhocking The script would be used when importing a mesh with quads into your game. It's not for use in Blender for exporting. –  Byte56 Jan 28 at 17:33

If you want to permanently change quads to tris in Blender use Ctrl+T in Edit-Mode

share|improve this answer
3  
This is the right way to do it, don't know why anyone would recommend a script to the OP. –  Luke B. Jan 28 at 12:58
2  
This is already mentioned in the accepted answer. –  concept3d Jan 28 at 13:34
    
I'm assuming he explained that script so that you don't have to triangulate the model permanently, and it'll just be triangulated automatically in the exported mesh. Although he didn't say this outright, so I'm just assuming. –  jhocking Jan 28 at 16:30
    
Yes, the long anwser confused me too and I overread the simple answer at the end –  Oliver Zendel Feb 12 at 10:26

In your export script, you can ask for tessellated data so that you only have to deal with triangles and quads. Turning a quad into two triangles is trivial (as Byte56 has already answered).

ob = bpy.context.active_object
me = ob.data
me.calc_tessface()
for v in me.vertices:
    print("v", v.co.x, v.co.y, v.co.z)
for f in me.tessfaces:
    if len(f.vertices) == 3:
        print("f", f.vertices[0], f.vertices[1], f.vertices[2])
    else:
        print("f", f.vertices[0], f.vertices[1], f.vertices[2])
        print("f", f.vertices[0], f.vertices[2], f.vertices[3])

Another option (if you're not writing your own exporter) is to add a "Triangulate" modifier (new in Blender 2.6) to your mesh; with the "Beauty Subdivide" option it will always pick the shorter of the two choices for where to split quads.

share|improve this answer
    
Thanks for this, I was looking in to tessfaces but couldn't figure out the usage. I started to implement this code in to my export code. I was worried I was going to have to manually convert each cylinder end-piece in my mesh to get rid of ngons, something I was hesitant doing. Doing this, should I just make the polygon indices based on this information? It was nice being able to just iterate through polygons and export that data. If I do iterate through these vertices, should I consider these as GL_Triangles or GL_TriangleStrip? Thanks! –  scape Dec 22 '12 at 14:49
    
Scratch that last comment, I initially didn't see your code on iterating through the tessface objects. I also did some looking around and from what I gather GL_Triangles is typically what is used from a blender export. Thanks again –  scape Dec 23 '12 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.