[GLSL] How to invert texture RGB components?

Started by
1 comment, last by Kaptein 10 years, 11 months ago

I found that a simple fragment shader for that would be something like:


uniform sampler2D Mtexture;

void main()
{
    vec4 color = texture2D(Mtexture, gl_TexCoord[0].st);
    gl_FragColor = vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a);
}


And the OGL code:


    glUseProgram(prog);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    glColor4f(1, 1, 1, 1);
    glBindTexture(GL_TEXTURE_2D, tex);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);  glVertex2i(0,0);    
    glTexCoord2f(1.0f, 1.0f);  glVertex2i(width,0);  
    glTexCoord2f(1.0f, 0.0f);  glVertex2i(width,height);  
    glTexCoord2f(0.0f, 0.0f);  glVertex2i(0,height);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glUseProgram(0);

However, it's not giving me the expected results, and this is driving me crazy.
Should I disable GL_BLEND before applying the filter? Case not, which function to use?
Should I clear my texture before using a shader on it? <-- Never understood this

Advertisement

your mixing old school and new school. i've never done that so i don't know how the driver will react. but where is your vertex shader? what results are you getting? if you comment out the color invert and just return the texture color do you the texture as expected? more info is needed bro.

mixing old and new works just fine anywhere, but there's certain restrictions, like you're stuck with the old VAO format
but, i'm guessing you are writing to the texture you're reading from
which if i was a driver maker would crash your computer, just to give you that good reason to read the manual :9

it's not allowed, because the gpu is a huge parallell thingie, and it can't guarantee the results

if you wanted to permanently change the components, you could do it many ways:
1. read texture data to memory and change them with code
2. write to new texture
3. just change the colors on the fly with a shader..

the last one is the easiest, and fastest, since vec4(vec3(1.0) - color.rgb, color.a) is not really slow...

This topic is closed to new replies.

Advertisement