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

Author Topic: How to make sf::VertexArray object have an outline?  (Read 3621 times)

0 Members and 3 Guests are viewing this topic.

KKZiomekkk

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
How to make sf::VertexArray object have an outline?
« on: August 07, 2017, 07:39:45 pm »
Hello, I want to add outline to an sf::VertexArray object. I am aware that the class sf::ConvexShape exists, and has a member function to add an outline, but I need to use sf::VertexArray in order to make the shape have a gradient.

One option is to make another VertexArray which copies the positions of the original VertexArray and sets its primitive type to LineStrip, but then the outline will have only 1 pixel of thickness.

The other option I considered that has outline thickness is to make another VertexArray of the primitive type TriangleStrip this time, and make an outline using very thin triangles in a strip. Is this a good idea, or are there better ways to do this?
« Last Edit: August 07, 2017, 07:44:54 pm by KKZiomekkk »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make sf::VertexArray object have an outline?
« Reply #1 on: August 07, 2017, 10:04:14 pm »
The triangle strip option makes a lot of sense. Note that if you're creating both the inner shape and the outline together, you should consider making them both as a single vertex array. Whether that be a triangle strip, list of quads or a list of triangles, you can create both as one.

Saying that, if two draw calls is fine for the object and its outline, you may want to consider something like Selba Ward's Spline that can draw thick 'polylines' with corrected corners:
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

KKZiomekkk

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: How to make sf::VertexArray object have an outline?
« Reply #2 on: August 08, 2017, 03:49:43 pm »
Thanks @Hapax for the reply. I really like your sw::Spline solution to the problem, especially with the possibility of Bezier interpolation.

However, are there any other ways to do what I asked for, besides triangle strips and your sw::Spline class solution?

I just want to know some more of the possible ways and judge which is the most efficient for my purpose in application.

(By the way, I like your SelbaWard library very much @Hapax. How long did it take you to make it altogether?)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to make sf::VertexArray object have an outline?
« Reply #3 on: August 08, 2017, 10:00:53 pm »
I would say that the triangle strip is the most effecient for performance, mainly because it's a single draw call as opposed to two (or maybe more). It does however, require the calculation of all parallel vertices. It really depends on the type of shape you're outlining.
You would need to calculate the normal at each vertex (average of previous line and next line) and offset the outline's vertex by that normal. There is required amount of adjustment to this offset depending on the angle of that corner (it should be further for sharper corners).

I would then say that using Selba Ward's Spline would be more efficient for programming as all those calculations are done for you. If you then draw the spline as your outline, it costs two draw calls instead of one. This may add up if you have hundreds of these.

You could, of course, get the best of both worlds: form a sw::Spline to follow the outline and then retrieve the above calculations from that Spline so that you can add it to your own vertex array. This way you get it all as one draw call and all of the calculations are done by Spline 8)
Here's an example of getting the information from Spline. Note the inner (last) circle in the line of circles (trail) that follows the inside edge:




I like your SelbaWard library very much @Hapax. How long did it take you to make it altogether?
Thank you! :)
It seems that I 'published' it almost two years ago (see its forum thread) and I've developed and added quite a lot since that time (as you can see in the posts). The things I published at the beginning were things I created for my own use and then re-written to be free of any other library (except SFML of course).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*