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

Author Topic: [SOLVED] Problem when getting things to fade to black  (Read 2943 times)

0 Members and 1 Guest are viewing this topic.

AniCator

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED] Problem when getting things to fade to black
« on: May 20, 2013, 12:44:04 pm »
Hello everyone,

I setup a RenderTexture today to use for a little test project I've been working on.
I apply a shader to it which darkens the original result and then I render the latest frame/draw call onto the texture.

I expected the colours to fade to black basically creating this nice sort of 'tail'.
Instead the colours stop fading at some point. This 'threshold' changes depending on the darkening amount I pick.



Draw code (The red layer is the RenderTexture.)
//Darken red layer
        sf::RectangleShape fxRect;
        fxRect.setPosition(0,0);
        fxRect.setSize(sf::Vector2f(1280,720));
        fxRect.setTexture(&redLayer.getTexture());

        fxShader.setParameter("texture", sf::Shader::CurrentTexture);
        redLayer.draw(fxRect, &fxShader);

        //Draw red layer
        redLayer.display();
        sf::RectangleShape redRect;
        redRect.setPosition(0,0);
        redRect.setSize(sf::Vector2f(1280,720));
        redRect.setTexture(&redLayer.getTexture());
        appRef->GetWindow().draw(redRect);

        swiz_ent_list_t::iterator iter;
        for (iter = ent_list.begin(); iter != ent_list.end(); ++iter)
        {
                SwizEntity* ent = *iter;
                int entType = ent->GetBaseType();
                if((entType & ENT_DRAWABLE) != 0)
                {
                        if(SwizDrawableEntity* drawable_ent = dynamic_cast<SwizDrawableEntity*>(ent))
                        {
                                if((entType & ENT_CRITTER) != 0)
                                {
                                        redLayer.draw(drawable_ent->GetDrawable());
                                }
                                else
                                {
                                        appRef->GetWindow().draw(drawable_ent->GetDrawable());
                                }
                        }
                }
        }

Vertex shader:
void main()
{
    // transform the vertex position
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    // transform the texture coordinates
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    // forward the vertex color
    gl_FrontColor = gl_Color;
}

Fragment shader:
uniform sampler2D texture;

void main()
{
    // lookup the pixel in the texture
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
        vec4 blackpixel = vec4(0.0f,0.0f,0.0f,0.01f);

    // multiply it by the color
    gl_FragColor = gl_Color * pixel * blackpixel;
}

PS: The circle symbol in the screenshot is the mouse cursor.
« Last Edit: May 20, 2013, 02:30:48 pm by AniCator »

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Problem when getting things to fade to black
« Reply #1 on: May 20, 2013, 02:07:28 pm »
Hi there. I had the same problem a while ago. I don't remember if we ever found the exact cause of the problem, but I think it was some integer/float issue on the graphics card. Using a subtractiv blendmode solved the problem for me.

edit: here is the old thread

AniCator

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem when getting things to fade to black
« Reply #2 on: May 20, 2013, 02:30:38 pm »
Thanks.
I fixed it. The solution seems a bit dirty but it works. :)



Updated fragment shader:
uniform sampler2D texture;

void main()
{
    // lookup the pixel in the texture
        int range = 255;
        float unit = 1.0f/255;
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
       
        int red = (int)(pixel.r / unit);
        int green = (int)(pixel.g / unit);
        int blue = (int)(pixel.b / unit);
        int alpha = (int)(pixel.a / unit);
       
        red -= 1;
        green -= 1;
        blue -= 1;
        alpha -= 1;
       
        vec4 newCol = vec4((float)red*unit,(float)green*unit,(float)blue*unit,(float)alpha*unit);

    // multiply it by the color
    gl_FragColor = gl_Color * newCol;
}