# How to generate a triangle fan index list for a circle shape

This topic is 2020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to create my index list to render a circle shape that starts out with 3 points but grows and eventually has 360 or whatever amount of points.

//Using indices like this: 0 1 2   0 2 3   0 3 4  ... for a fan list

std::vector<irr::u16> indices;

for(int x = 0; x < obj->GetLevel(); ++x)

{

indices.push_back(0);

}

I am not sure how to calculate the logic of make a 0,1,2 then 0,2,3 0,3,4 tuple in the for()

Thanks!

##### Share on other sites

For triangle n, the indices are 0, n+1 and n+2.

##### Share on other sites

Also, be careful with the range you loop over.

indices.reserve(3*(n_vertices-2));

for (int n=2; n<n_vertices; ++n) {
indices.push_back(0);
indices.push_back(n-1);
indices.push_back(n);
}

##### Share on other sites

Hmm thanks both of you for the info.

Alvaro, that works great, but I am for some reason brain farting, why I can't draw quad with 4 points and use a triangle fan.

I use the code below to calculate the circle but I start at a triangle, and want to move up to a circle. So when I move from 3 sides to  4 sides I am not able to render a quad, keeps rendering a triangle....

Here is the code to Calculate the vertices and indices, and render

inline void Calculate(int level, float radius, const irr::video::SColor& color)
{
vertex.clear();
float angle = 360.0f/float(level);
for(float f = 0.0f; f < 360.0f; f+=angle)
{
0.0f,
0.0f, 0.0f, 0.0f,
color,
0.0f, 0.0f);

vertex.push_back(v);
}
for(int n = 2; n < level; ++n)
{
index.push_back(0);
index.push_back(n - 1);
index.push_back(n);
}
}

drawIndexedTriangleList(&vertex[0],vertex.size(), &index[0], index.size() / 3);

drawIndexedTriangleList ( const S3DVertex *  vertices,     u32 vertexCount,     const u16 *  indexList,     u32 triangleCount)
Edited by MARS_999

##### Share on other sites

You clear the vertex' array but not the index' array. That seems wrong...

Also, the loop that uses a float as the control variable may cycle level' times or level'+1 times, depending on how 360.0f/float(level)' gets rounded. For instance, if you are drawing a regular heptagon, 360.0f/7.0f == 51.428569793701171875f exactly, which is short by .000001634870256696428571428571428571428... When you add that 7 times, you get 359.999988555908203125, and the test f < 360.0f' passes, so you'll end up with 8 vertices. It is much better to loop over integers (which don't have these nasty surprises) instead.

Also, `level' is a strange name for the number of sides...

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013594
×