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

Author Topic: Problem manipulating gl_FragColor in my fragment shader  (Read 2070 times)

0 Members and 1 Guest are viewing this topic.

Mithra

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem manipulating gl_FragColor in my fragment shader
« on: November 09, 2015, 10:15:14 am »
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.
« Last Edit: November 09, 2015, 10:36:19 am by Mithra »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem manipulating gl_FragColor in my fragment shader
« Reply #1 on: November 09, 2015, 10:34:09 am »
gl_FragColor is not an input, it's only the output of the fragment shader.

You must build the fragment color yourself, from the input texture and texture coordinates. This is shown in the tutorial:
http://www.sfml-dev.org/tutorials/2.3/graphics-shader.php#minimal-shaders
Laurent Gomila - SFML developer

Mithra

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem manipulating gl_FragColor in my fragment shader
« Reply #2 on: November 09, 2015, 06:37:35 pm »
Thank you Laurent!

I did figure out at the end that I needed to acquire the color from the texture with a call to texture2D.   I should not be coding at 4:00 in the morning ;)