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

Author Topic: How to convert shape to vertex array.  (Read 3624 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
How to convert shape to vertex array.
« 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.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to convert shape to vertex array.
« Reply #1 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.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #2 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.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to convert shape to vertex array.
« Reply #3 on: August 26, 2019, 08:38:08 am »
In this context, points and vertices are interchangeable terms.
(and rectangles always have 4)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to convert shape to vertex array.
« Reply #4 on: August 28, 2019, 12:44:25 am »
It seems you want thick lines with round corners.

You should consider trying out Selba Ward. In particular, its Spline because look:



 8)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #5 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: How to convert shape to vertex array.
« Reply #6 on: August 28, 2019, 09:23:45 pm »
You can get the transform and apply it to the vertices.
« Last Edit: August 29, 2019, 12:58:28 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #7 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: How to convert shape to vertex array.
« Reply #8 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #9 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.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to convert shape to vertex array.
« Reply #10 on: August 29, 2019, 03:42:05 pm »
First reply? I even gave the function name. ???

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #11 on: August 29, 2019, 03:44:18 pm »
I didn't see the transformPoint in the documentation of sf::Vertex.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: How to convert shape to vertex array.
« Reply #12 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);
            }
        }
    }
}