Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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)
        {
            irr::video::S3DVertex v(sinf(irr::core::degToRad(f)),
                                    cosf(irr::core::degToRad(f)),
                                    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 this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!