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

Author Topic: Colour individual triangles in a sf::TriangleStrip  (Read 709 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: 11030
    • 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/

 

anything