SFML community forums

Help => General => Topic started by: Xrey274 on August 25, 2019, 12:13:28 am

Title: How to convert shape to vertex array.
Post by: Xrey274 on August 25, 2019, 12:13:28 am
I'm working on a drawing program and I'm curious how would one go about transforming a (ex.) sf::CircleShape to a vertex array? I've read on various topic on this forum that .draw() calls are quite performance taxing. My line tool is basically a bunch of circle shapes connected with rectanagle shapes, which means a ton of draw calls for a single line.
Title: Re: How to convert shape to vertex array.
Post by: G. on August 25, 2019, 06:59:31 am
You can build the circle by yourself using:
center.x + radius * cos(angle)
center.y + radius * sin(angle)
for each point of the circle.

Or use getPoint to get the local position of each point of your sf::CircleShape (or other shape), getTransform to get its sf::Transform and use transformPoint on each point to convert them to their global position.
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 25, 2019, 10:43:35 pm
Tried taking the points of a rectangle, but it only returned 4(the corners). I though getPointCount() return every point(pixel) that makes up the shape.
Title: Re: How to convert shape to vertex array.
Post by: G. on August 26, 2019, 08:38:08 am
In this context, points and vertices are interchangeable terms.
(and rectangles always have 4)
Title: Re: How to convert shape to vertex array.
Post by: Hapax on August 28, 2019, 12:44:25 am
It seems you want thick lines with round corners.

You should consider trying out Selba Ward (https://github.com/Hapaxia/SelbaWard/wiki). In particular, its Spline (https://github.com/Hapaxia/SelbaWard/wiki/Spline) because look:
(https://i.imgur.com/JzyDo0i.png)
(https://i.imgur.com/AZTZiQd.png)

 8)
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 28, 2019, 06:04:41 pm
I wrote a function that converts sf::CircleShape to VertexArray. It works wonderfully. Problem is with sf::RectangleShape - more specifically rotation of ingeneral their transform. Any idea how go about it?
Title: Re: How to convert shape to vertex array.
Post by: eXpl0it3r on August 28, 2019, 09:23:45 pm
You can get the transform and apply it to the vertices.
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 29, 2019, 01:31:00 pm
I can only transform a single vertex array? I can't do it on a vertex by vertex basis?
Title: Re: How to convert shape to vertex array.
Post by: eXpl0it3r on August 29, 2019, 03:04:20 pm
It's the other way around, you can't transform a whole vertex array, but you can transform single points.
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 29, 2019, 03:31:31 pm
The only way I saw to apply a transform to a vertex array is to apply it when drawing it. Is there a way to apply a transform before drawing? I want to apply different transform to different vertices.
Title: Re: How to convert shape to vertex array.
Post by: G. on August 29, 2019, 03:42:05 pm
First reply? I even gave the function name. ???
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 29, 2019, 03:44:18 pm
I didn't see the transformPoint in the documentation of sf::Vertex.
Title: Re: How to convert shape to vertex array.
Post by: Xrey274 on August 29, 2019, 03:51:01 pm
This is the code I use to check if a vertex belongs to the rectangle shape.

void FreeDraw::convertToVertex(sf::RectangleShape box)
{
    box.setOrigin(sf::Vector2f(0, 0));    

    sf::Vector2f minCoords = box.getPosition();
    sf::Vector2f maxCoords = box.getPosition() + box.getSize();

     for(int y = minCoords.y; y < maxCoords.y; ++y)
     {
        for(int x = minCoords.x; x < maxCoords.x; ++x)
        {
            if(box.getGlobalBounds().contains(sf::Vector2f(x, y)))
            {
                sf::Vertex dot;
                dot.position = sf::Vector2f(x, y);
                dot.color    = sf::Color::Blue;

                line.append(dot);
            }
        }
    }
}