Jump to content
  • Advertisement
Sign in to follow this  

[GLSL] How to invert texture RGB components?

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 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:

    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

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

    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);

    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 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

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
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... Edited by Kaptein

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!