I have a function that creates 3D meshes procedurally from the given parameters. It creates a four-sided box, which is (for example) 1.0 wide, 1.0 high and 100.0 long. The box is then subdivided into multiple parts, so if the aforementioned box is subdivided to three parts, it will have vertices at 0f, 33.3f, 66.7f and 100.0f on the longest sides. Now, I have the vertex generation code up and running, but I have a problem generating the triangle lists from the vertices. The box does only contain the sides, not the caps.


The picture here shows a box with a division index of 2. So, the data I already have is the vertex positions, marked as red on the picture. Now, I need to create the triangles, which is done by specifying the three vertices that make up the triangle, for example (0, 1, 2, 1, 2, 3) would make two triangle from the corner points 0, 1, 2 and 3, which are the vertex indices. The problem is, how can this be done for any subdivision count? I have already implemented it for a plane, but the box gives me a brainache because I would like to share the vertices with sides and so on.

Thanks in advance for any help and suggestions :)

Recommended Answers

All 5 Replies

In that picture, could you identify the indices of each vertex? And could you identify how you assign indices to each vertex for a division index of n?

I can set the vertex indices in just about any order, but at the moment, they are like this (if it's not too blurry):

So, 1, 2, 3, 4, 5, and 6 on the top, and 7, 8, 9, 10, 11 and 12 on the bottom.

I assume your problem is about the vertical planes? And I don't get this:

for example (0, 1, 2, 1, 2, 3) would make two triangle from the corner points 0, 1, 2 and 3, which are the vertex indices

The general concept of triangulating the box is the problem here. As I mentioned, I can do this per-plane, but that means I cannot share the vertices with joining sides, which I really would like to do.

If you number the verticies clockwise around each square (starting with the upper left one), you'd get (staring with 1):
Front square 1 2 3 4
Middle square 5 6 7 8
Back square 9 10 11 12

Now the triangles are
1 2 6
2 3 7
3 4 8
4 1 5

5 6 1
6 7 2
7 8 3
8 9 4

5 6 10
6 7 11
7 8 12
8 6 9

9 10 5
10 11 6
11 12 7
12 9 8

You should be able to determine the pattern from that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.