tags:

views:

50

answers:

1

I have a set of co-planar, connected triangles, i.e., a 2D mesh. Now I need to extrude it out a few units in the z-axis. The mesh is defined by a set of vertices which the renderer makes sense of by matching up against an array of triangles.

Example Mesh:

Vertices: (0,0,0), (10,0,0), (10,10,0), (0,10,0) <-- (x,y,z) Triangles: (1, 2, 3) & (3, 4, 1) <-- numbers here reference the position of a vertex above.

So here we have a 2D square. Now I need to add more vertices and triangles to that list to make an extruded shape. Triangles must be in clockwise direction, otherwise they're backface-culled.

Is there a simple algorithm for this? Thank you.

A: 

Assuming you want to extrude by a distance z, you need to follow these steps:

0) let n be the original number of vertices (4 in your example)

1) For each vertex in your vertex array, add (0,0,z) to it, and add the result to your vertex array, for a total of 2*n vertices. So, for your example, you will add the vertices (0,0,z), (10,0,z), (10,10,z), (0,10,z) to your vertex array, for a total of 2*4=8 vertices.

2) Create a list of boundary (as opposed to internal) edges for your original mesh. To do this, create a list of all triangle edges (3 edges going in clockwise order for each triangle). Then remove pairs of equal but opposite edges (these are the internal edges). For your example, you will start with 6 edges, and end up with 4 edges after removing the edge pair (3,1) and (1,3).

3) for each triangle (a,b,c) in your triangle list, create a corresponding triangle (a+n,b+n,c+n). These will be the extruded faces

4) Finally, you want to create the sides of your extruded shape. For each edge (a,b) in the boundary edge list you created in step 2, add the triangles (a,b,b+n) and (b+n,a+n,a)

That's it. Assuming no typos on my part, and no typos on your part, you should now have your desired mesh.

brainjam
@Liquid: feel free to upvote this answer as well as mark it accepted. Thanks!
brainjam