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

Author Topic: Fill sf::Sprite with constant color  (Read 2632 times)

0 Members and 1 Guest are viewing this topic.

mrAndersen

  • Newbie
  • *
  • Posts: 3
    • View Profile
Fill sf::Sprite with constant color
« on: November 24, 2017, 12:43:52 pm »
Hi! I have some problems changing color of sf::Sprite. As said in manual - i can multiply sprite color by some value with sprite->setColor(...) this is perfectly fine, until i want full sprite to be specific color.

In other words. How i can change all pixels of sf::Sprite that are not transparent to specific RGB value on an existing sprite?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Fill sf::Sprite with constant color
« Reply #1 on: November 24, 2017, 12:53:49 pm »
Easiest solution is to use a shader. Otherwise you'll have to do some CPU texture processing.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fill sf::Sprite with constant color
« Reply #2 on: November 24, 2017, 01:22:31 pm »
This could be achieved with a custom blend mode involving a constant color value rather than the actual source/destination, but unfortunately SFML doesn't expose this option. So yes, a simple shader is your best option.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Fill sf::Sprite with constant color
« Reply #3 on: November 24, 2017, 05:21:27 pm »
I used this one

#define SHADER_WHITE                                          \
        "uniform sampler2D texture;                             " \
        "                                                       " \
        "void main() {                                          " \
        "    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);" \
        "                                                       " \
        "    pixel.r = 1.0f;                                    " \
        "    pixel.g = 1.0f;                                    " \
        "    pixel.b = 1.0f;                                    " \
        "                                                       " \
        "    gl_FragColor = pixel;                              " \
        "}                                                      "

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fill sf::Sprite with constant color
« Reply #4 on: November 24, 2017, 05:52:16 pm »
Quote
I used this one
I think it is important to mention that this shader doesn't do what the OP wants :P
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Fill sf::Sprite with constant color
« Reply #5 on: November 24, 2017, 07:07:15 pm »
I used this one

#define SHADER_WHITE                                          \
        "uniform sampler2D texture;                             " \
        "                                                       " \
        "void main() {                                          " \
        "    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);" \
        "                                                       " \
        "    pixel.r = 1.0f;                                    " \
        "    pixel.g = 1.0f;                                    " \
        "    pixel.b = 1.0f;                                    " \
        "                                                       " \
        "    gl_FragColor = pixel;                              " \
        "}                                                      "

 

OP wants it to ignore transparent pixels though

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: Fill sf::Sprite with constant color
« Reply #6 on: November 24, 2017, 07:41:41 pm »
I used this one

#define SHADER_WHITE                                          \
        "uniform sampler2D texture;                             " \
        "                                                       " \
        "void main() {                                          " \
        "    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);" \
        "                                                       " \
        "    pixel.r = 1.0f;                                    " \
        "    pixel.g = 1.0f;                                    " \
        "    pixel.b = 1.0f;                                    " \
        "                                                       " \
        "    gl_FragColor = pixel;                              " \
        "}                                                      "

 

OP wants it to ignore transparent pixels though

as you can see i do NOT change the pixel.a value

 

anything