Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] How to correctly draw a triangle fan?  (Read 3020 times)

0 Members and 1 Guest are viewing this topic.

Nortski

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED] How to correctly draw a triangle fan?
« on: May 17, 2024, 07:49:40 pm »
So so close to getting my visibility polygon working. I'm using an SFML triangle fan with all the vertex points sorted by angle size with position 0 set to the mouse coords and the last element set to element 2, I needed to do this to fill in the gap for the last triangle.
However; a triangle appears to be missing at certain locations, see video.
I suspect that I'm having an out by one issue with my loop. I've tried rearranging a few numbers but this is the closest I can get.
https://streamable.com/d46yih

sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 1);
visibilityPolygon[0] = ray[0].position;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i-1].position.x, vectorAngleContainer[i-1].position.y), sf::Color::Red);
}
visibilityPolygon[vectorAngleContainer.size()] = visibilityPolygon[1].position;

vectorAngleContainer() is a vector of structs that holds a point and and angle.
« Last Edit: May 19, 2024, 12:48:12 pm by Nortski »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to correctly draw a triangle fan?
« Reply #1 on: May 17, 2024, 10:46:51 pm »
It seems that there is an issue when those triangles are thin - possibly infinitely thin or even inverted due to rounding errors.
Triangle fans are not supposed to have overlapping triangles so it could have an issue with this when those triangles are 'thin'.

You could try replacing it with a triangle strip; this should be able to do similar but would require specifying the centre point multiple times. Plus, the strip might also suffer from the same issues, depending on Open GL feels about that.

The safest bet, I would say, is to use separate triangles. The formation of the triangles does not matter so if they overlap or 'go backwards', it's fine since each triangle doesn't affect any other triangle.

Of course, you would need almost 3 times as many vertices (it would be 3 times the vertices you have now minus 2) since you would just simply specify each triangle's vertices separately, repeating quite a few.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nortski

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to correctly draw a triangle fan?
« Reply #2 on: May 18, 2024, 11:55:50 am »
The safest bet, I would say, is to use separate triangles. The formation of the triangles does not matter so if they overlap or 'go backwards', it's fine since each triangle doesn't affect any other triangle.

I may have to try this just to see if it works. However; I will need to find a solution using a triangle fan as this was specifically instructed in the course I'm following.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to correctly draw a triangle fan?
« Reply #3 on: May 18, 2024, 11:18:29 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nortski

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to correctly draw a triangle fan?
« Reply #4 on: May 19, 2024, 11:38:23 am »
Ok, I will try your suggestions.
It's weird though as it only happens on the right side of that shape and nowhere else.

Nortski

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to correctly draw a triangle fan?
« Reply #5 on: May 19, 2024, 12:42:34 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).

Thank you for taking the time to help me with this.

I actually fixed the problem. As it turned out it was an off by one problem with my loop. A triangle fan needs to be 2 elements larger than the number of vertex points in my vector, 1 for the central position and 1 to close the gap between the last vertex and the first vertex.

// Make polygon 2 larger than vertex container
sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 2);
visibilityPolygon[0] = ray[0].position;
visibilityPolygon[0].color = sf::Color::Red;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i - 1].position.x, vectorAngleContainer[i - 1].position.y), sf::Color::Red);
}

// Set the last point == to the first vertex [1] (not [0] as that is the central point)
visibilityPolygon[vectorAngleContainer.size() + 1] = visibilityPolygon[1].position;
visibilityPolygon[vectorAngleContainer.size() + 1].color = sf::Color::Red;

 

anything