3
\$\begingroup\$

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?

\$\endgroup\$

3 Answers 3

7
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – scape
    Dec 16, 2012 at 20:21
  • \$\begingroup\$ 24 indices... wow thats a very cool ngon \$\endgroup\$ Dec 17, 2012 at 0:21
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Jan 28, 2014 at 16:31
  • \$\begingroup\$ @jhocking The script would be used when importing a mesh with quads into your game. It's not for use in Blender for exporting. \$\endgroup\$
    – House
    Jan 28, 2014 at 17:33
5
\$\begingroup\$

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

\$\endgroup\$
3
  • 3
    \$\begingroup\$ This is the right way to do it, don't know why anyone would recommend a script to the OP. \$\endgroup\$
    – Luke B.
    Jan 28, 2014 at 12:58
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Jan 28, 2014 at 16:30
  • \$\begingroup\$ Yes, the long anwser confused me too and I overread the simple answer at the end \$\endgroup\$ Feb 12, 2014 at 10:26
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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! \$\endgroup\$
    – scape
    Dec 22, 2012 at 14:49
  • \$\begingroup\$ 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 \$\endgroup\$
    – scape
    Dec 23, 2012 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .