quad/n-gon data.. how to convert to triangles?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Gorbstein
Posts: 37
Joined: Wed Nov 25, 2009 8:44 pm
Location: Scotland

quad/n-gon data.. how to convert to triangles?

Post by Gorbstein »

I'm in the process of writing a simple editor for an old and rather obscure game. I'm at the stage of writing code to import its 3d models into my engine built on irrlicht.

I have successfully managed to import the data into custom SMeshBuffer/SMesh objects, and largely it works well. However, I noticed that some objects have large gaps when rendered.

I since found out that this old file format supports objects which are described not only as triangles, but using any number of vertices per single face (polygons/n-gons). For example, you can describe a quad with 4 vertices and 4 face indices, whereas using triangles you'd need 4 vertices 6 indices. So for objects built using triangles it works 100%, but for others irrlicht isn't rendering the faces properly.

Obviously irrlicht is not going to realise that the data is stored in a format other than triangles. Is there a way I can convert n-gons to triangles? If not, is there a way to render these objects in irrlicht? Using a custom scenenode for example?

Thanks,

David
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, OpenGL supports generic polygon data, but it's not the best idea to use it. Better use triangle fans to triangulate the polygons. You can get the code from many of the mesh loaders, or google the basic algorithm (basically you fix the first vertex and create triangles from every next two vertices and that main vertex).
Gorbstein
Posts: 37
Joined: Wed Nov 25, 2009 8:44 pm
Location: Scotland

Post by Gorbstein »

Thanks, not sure why I didn't think of using the triangle fan idea to triangulate the quads. It now works fine. Concave polygons mess up, but that's ok because they're very rarely used by the developers.

Code I used was simply:

Code: Select all

if number_of_indices is a multple of 3
    add indices straight from file
else
    get first index
    for 1 to number_of_indices - 1
        add first index
        add current index
        add current index +1
     endfor
endif
Post Reply