This project has moved and is read-only. For the latest updates, please go here.

Rendering: Best Polygon to Vertex Buffer Approach? Triangulate issues...

Topics: User Forum
Mar 17, 2011 at 2:23 AM
Edited Mar 17, 2011 at 2:24 AM

I'm creating a rendering system for my game terrain and would like an opinion on the best approach for breaking down polygon's to triangles (which can then be used in a trianglelist via vertex buffer).  I understand the whole process as I've done it many times without FPE, but I'm having difficulties in triangulating. 

My approach at a high level:

1. Generate a random level saving all the information to texture.

2. Process the texture on level load, at which point we are left with a simple transparent and black image (with the smallest shape being 2x2 pixels)

3. Run it through the Earclip Decomposer

4. Create a polygon body for each of the decomposed polygons and add them to the world.

5.  ?

 At this point I know I need to use the decomposed polygons to create triangles for my vertex buffer, but the triangulation methods I've found don't seem to work.  Particularly I've used EarclipDecomposer.TriangulatePolygon and Triangulator, but both have the main problem of being unable to triangulate all of my those decomposed polygons.  From what I can tell they are all simple polygons... talking 4 or 5 sided convex polygons at the most complex.  The Triangulator can triangulate the best but its output is random, and even crashes sometimes.

Is there a better approach? Should I go with one triangulation function over the other for my situation? What are some of the caveats or best approaches when using these functions that may be causing me these problems?

EDIT: using latest source


Mar 17, 2011 at 4:00 AM

First off, I would just create one body for all of your terrain because the terrain never moves, so you only need one thing to controll it. Second, I would use the Beyizit Decomposer, because it works most of the time and you can just eat the exceptions that it sends you, and it will chug along like nothing happened. Third, the way that I do the drawing is to first choose a point that is the basis of my triangle fan, then I go to the next two points (p1 and p2) in the list of vertexes for the body and create a triangle, then I set p1 as p2 and set p2 as the next point in the list. I then create a triangle with the p1 and p2 and my central fan point. I then proceed in this fashion until all of the points are used. I could be wrong, but I think this is about as fast as it can get, especially if you keep the points in a static pool and reuse them so as to reduce garbage allocations. Hope this helps.

Also, you need to make sure that the points you actually draw with are converted to the screen cordinates and make sure that you either wind them clockwise, or turn off back face culling or nothing will work at all.

Mar 18, 2011 at 3:13 AM

Good idea about using one body.

I guess I got in the mindset that I had to use something built into the library even though I'm perfectly capable of writing my own triangulator.  I guess it could be done a little more efficiently, but what you suggested seemed as straight forward as anything I could come up with so I implemented it and it works out just fine.