SFML community forums

Help => Graphics => Topic started by: mrAndersen on November 24, 2017, 12:43:52 pm

Title: Fill sf::Sprite with constant color
Post by: mrAndersen 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?
Title: Re: Fill sf::Sprite with constant color
Post by: eXpl0it3r 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.
Title: Re: Fill sf::Sprite with constant color
Post by: Laurent 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.
Title: Re: Fill sf::Sprite with constant color
Post by: achpile 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;                              " \
        "}                                                      "

 
Title: Re: Fill sf::Sprite with constant color
Post by: Laurent 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
Title: Re: Fill sf::Sprite with constant color
Post by: NGM88 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
Title: Re: Fill sf::Sprite with constant color
Post by: achpile 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