A simple function to generate index buffers for D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP

Started by
-1 comments, last by LHLaurini 9 years, 3 months ago

Hi!

Before I start, I want you to know this is not a question, but a topic. I'm sorry for my bad English because I'm Brazilian.

Okay. So, I was trying to write index buffers so I could display polygons - not only triangles - using a vertex list (always clockwise) together with D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP. Soon, I noticed that it makes a pattern.


4 vertices   1  2  0  3
5 vertices   1  2  0  3  4
6 vertices   1  2  0  3  5  4
7 vertices   1  2  0  3  6  4  5
8 vertices   1  2  0  3  7  4  6  5
9 vertices   1  2  0  3  8  4  7  5  6
10 vertices  1  2  0  3  9  4  8  5  7  6
11 vertices  1  2  0  3 10  4  9  5  8  6  7
12 vertices  1  2  0  3 11  4 10  5  9  6  8  7
13 vertices  1  2  0  3 12  4 11  5 10  6  9  7  8
14 vertices  1  2  0  3 13  4 12  5 11  6 10  7  9  8
15 vertices  1  2  0  3 14  4 13  5 12  6 11  7 10  8  9
16 vertices  1  2  0  3 15  4 14  5 13  6 12  7 11  8 10  9

I didn't test all of them, but I did test 4, 5, 6, 7, 8, 9, 10 and 16. All of them worked. This is the 16 vertices polygon (it's a bit ugly because I didn't spent too much work on it):

[attachment=25580:polygon.png]

Then I've made a little function:


#include <iostream>

using namespace std;

unsigned int* Generate(unsigned int cVertices)
{
	unsigned int *pIndexBuffer;

	pIndexBuffer = new unsigned int[cVertices];
	pIndexBuffer[0] = 1;
	pIndexBuffer[1] = 2;
	pIndexBuffer[2] = 0;

	for (int i = 3, n = 3, m = 1; i < cVertices; i++)
	{
		if (i & 1) // odd
		{
			pIndexBuffer[i] = n++;
		}
		else // even
		{
			pIndexBuffer[i] = cVertices - m++;
		}
	}
	
	return pIndexBuffer;
}

int main()
{
	for (int i = 4; i <= 25; i++)
	{
		unsigned int *pIndexBuffer = Generate(i);

		printf("%2i: ", i);
		for (int n = 0; n < i; n++)
		{
			printf("%3i", pIndexBuffer[n]);
		}

		cout << endl;

		delete[] pIndexBuffer;
	}

	getchar();
}

And that's it. I'm not sure if it works for any CONVEX polygon.

So, that's my little adventure. Any thoughts are welcome.

This topic is closed to new replies.

Advertisement