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

Author Topic: Inverting a sprite  (Read 4390 times)

0 Members and 1 Guest are viewing this topic.

aeron

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - aeron005@gmail.com
    • View Profile
    • http://aeron.uni.cc/
Inverting a sprite
« on: July 13, 2011, 03:03:42 am »
Hello, I'm trying to invert the colors for a single sprite and I can't seem to get it right. How do I receive the current pixel's color (from the sprite) in the shader? At first I tried gl_Color to no avail:
Code: [Select]

uniform bool invert;

void main()
{
    if(invert)
        gl_FragColor = vec4(255.0 - gl_Color.r, 255.0 - gl_Color.g, 255.0 - gl_Color.b, 1.0);
    else
        gl_FragColor = gl_Color;
}


I also tried another google-inspired solution:
Code: [Select]

uniform bool invert;
uniform sampler2D screenColorBuffer;

void main()
{
    vec4 color =  gl_TexCoord[0];
    if(invert)
    {
        gl_FragColor = 1.0-texture2D(screenColorBuffer,gl_TexCoord[0].st);
        gl_FragColor.a = 1.0;
    }
    else
        gl_FragColor = color;
}


Maybe I'm just not sure what to search for... this GLSL stuff is impossible to sort out for some reason. Is there a certain variable I should use in this scenario?

Thanks

Megatron

  • Newbie
  • *
  • Posts: 22
    • View Profile
Inverting a sprite
« Reply #1 on: July 13, 2011, 04:12:02 am »
I'm not sure if this is the most efficient way, but it works
Just make sure the sprite's texture is loaded to texture:


Code: [Select]
uniform sampler2D texture;

void main()
{
vec4 ref = texture2D(texture, gl_TexCoord[0].xy) * gl_Color;
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) - ref;
gl_FragColor.a = ref.a;
}

aeron

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - aeron005@gmail.com
    • View Profile
    • http://aeron.uni.cc/
Inverting a sprite
« Reply #2 on: July 13, 2011, 04:22:48 am »
Thanks that did the trick! Complete shader:
Code: [Select]
uniform bool invert;
uniform sampler2D textu1;

void main()
{
    vec4 ref = texture2D(textu1, gl_TexCoord[0].xy) * gl_Color;
    if(invert)
    {

       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) - ref;
       gl_FragColor.a = ref.a;
    }
    else
        gl_FragColor = ref;
}


You can toggle the inversion with:
Code: [Select]

invert.SetParameter("invert",true);

 

anything