Hey all, I'm kind of stuck on something. Any help would be appreciated!
I'm trying to convert a game I wrote in XNA to SFML and I'm having difficulty porting an HLSL pixel shader I wrote over to GLSL. It should be a simple thing but somehow I'm not getting the desired output.
Basically, I want to render a sprite with transparency, but setting the non-transparent pixels to a passed value so that it might appear solid-color like so :
My sprites load and display correctly under the default shader, but somehow what I'm writing ignores the alpha channel or I am not doing an operation correctly, I don't know.
My shader.frag looks like so:
uniform vec4 indexColor;
void main()
{
if( gl_FragColor.a > 0.01) // set color to passed value
{
gl_FragColor = indexColor;
gl_FragColor.a = 1.0;
}
else // otherwise do not render this pixel
{
gl_FragColor = 0;
}
}
This particular code renders everything transparent, so somehow I'm not evaluating the alpha channel correctly.
My code is calls this once per update:
window.clear( sf::Color( COLOR_BLACK));
shader.setParameter("texture", sf::Shader::CurrentTexture);
Then in my loop, for each sprite I call :
shader.setParameter("indexColor", sf::Color( 1.0f, 0.8f, 0.6f, 1.0f));
window.draw( sprite, &shader);
I do know the shader is loading because I can force it to draw everything a solid color. Any ideas? Thanks!
EDIT :
As per usual I may have resolved my own question, sorry!
I got it to work with this shader :
uniform sampler2D texture;
uniform float r;
uniform float g;
uniform float b;
void main()
{
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
if( pixel.a > 0.1)
{
pixel.r = r;
pixel.g = g;
pixel.b = b;
pixel.a = 1.0;
gl_FragColor = pixel;
}
else
{
gl_FragColor = vec4( 1.0, 1.0, 0.0, 0.0);
}
}
I'm sure I don't need to pass the RGB individually but it works for now.