Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 04 June 2013 - 06:55 AM

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!



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8607

Like
1Likes
Like

Posted 04 June 2013 - 07:08 AM

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



#3 Álvaro   Crossbones+   -  Reputation: 13915

Like
0Likes
Like

Posted 04 June 2013 - 07:37 AM

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);
}


#4 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 04 June 2013 - 11:29 AM

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, 04 June 2013 - 11:37 AM.


#5 Álvaro   Crossbones+   -  Reputation: 13915

Like
0Likes
Like

Posted 04 June 2013 - 02:03 PM

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...






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS