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

Author Topic: Colour individual triangles in a sf::TriangleStrip  (Read 1758 times)

0 Members and 1 Guest are viewing this topic.

BottleNeck

  • Newbie
  • *
  • Posts: 1
    • View Profile
Colour individual triangles in a sf::TriangleStrip
« on: November 15, 2024, 06:53:24 am »
Background:
I'm trying to make a falling sand simulation game which requires displaying a very large number of objects at once. I was drawing each "grain" of sand using a sf::RectangleShape, however for larger grids (e.g 500x500) this was far too many draw calls.

I'm currently trying to implement drawing of the sand using a sf::VertexArray. If I use sf::Triangle primitives, I'll have 6 vertices per grain, which is a lot for larger grid sizes.

Now I'm attempting to use the sf::TriangleStrip primitive to display the entire grid, using degenerate triangles to transition between rows. This reduces the number of vertices required by about two thirds, however now I have a problem with colouring the grains.

Question:
When using a sf::VertexArray to display a sf::TriangleStrip, is it possible to colour individual triangles within the strip different colours? Currently, if I want to colour two adjacent triangles in two different colours, the colours are interpolated between the vertices, forming a gradient. I want to be able to apply different colours to triangles without any gradient.

I've attached an image to show what I'm currently getting. I've drawn over the approximate locations of the triangles of the strip. I'd like the middle two triangles to be completely black.

« Last Edit: November 15, 2024, 06:55:05 am by BottleNeck »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Colour individual triangles in a sf::TriangleStrip
« Reply #1 on: November 19, 2024, 01:41:07 pm »
It's not possible with normal vertex coloring.

Not sure if there's some way with a shader, but that is usually the answer to anything you want to achieve graphics-wise.
In theory you could also use some texturing, mapping the triangle to a single color pixel. Not sure if that performs and won't cause some fun texture bleeding.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Colour individual triangles in a sf::TriangleStrip
« Reply #2 on: November 27, 2024, 04:41:17 pm »
You can do this with a triangle strip but it requires duplicate vertices and, if you're doing this often, it may be better to just use separate triangles.

e.g.
I'll assume you have 8 vertices in the example and they are bottom0, top0, bottom1, top1, bottom2, top2, bottom3, top3.

You can use (12 vertices) bottom0, top0, bottom1, top1, top1, bottom1, top2, bottom2, bottom2, top3, bottom3.

This allows change of colour over the doubled vertices.
Note that this works because the doubled vertex as well as the previous (or next) vertex create an infinitely thin triangle so those two triangles (where the colour transitions happen) are invisible.

A possibly simpler version
If you need each triangle pair to be the same direction then you can just duplicate the 2 vertices that represent the edge.
This works because, again, you're "drawing invisible lines".

That would be (12 vertices) bottom0, top0, bottom1, top1, bottom1, top1, bottom2, top2, bottom2, top2, bottom3, top3.
This could be more simple as you're still following the same bottom-to-top and left-to-right formula and you it looks a bit clearer in the code as you're effectively separating the vertices into groups of 4 (per quad).

Here's an image of all three of those examples with your original being the top one (number 1):


(click to show/hide)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*