Reputation: 71
I'm texturing a 2D object in OpenGL, and while I have the texture loading fine, I'm just not quite sure how to invert the colours. How do I access the colour information of the bitmap and invert it? Would this be done best in a shader or in the Main program?
I'm leaving this kind of open as I'm not looking for a "fix my code" type of answer, but rather "this is how you access the colour information of the bitmap".
Upvotes: 6
Views: 14965
Reputation: 115
precision mediump float;
uniform sampler2D u_tex;
varying vec2 v_texCoord;
void main()
{
gl_FragColor = vec4(1,1,1,1) - texture2D(u_tex, v_texCoord);
}
Upvotes: 1
Reputation: 83
Actually, you have to invert the texture coordinates before sampling the texture to achieve that.
So, in fragment shader:
vec2 vert_uv_New=vec2(1-vert_uv.x,1-vert_uv.y);
Now sample it:
gl_FragColor=texture2D(sampler,vert_uv_New);
This will do the job.
Upvotes: 0
Reputation: 158
Its s simple as
gl_FragColor = vec4(1.0 - textureColor.r,1.0 -textureColor.g,1.0 -textureColor.b,1)
It is best to do this kind of thing in the shader, if you want to do it once and then reuse it for future draws just use render to texture. This is the fastest way to do color inversion.
EDIT: You use
vec4 textureColor = texture2D(uSampler,vTexCoords)
before your do gl_FragColor
using .r ,.g,.b and .a access the red, green, blue and alpha values respectively.
Upvotes: 8
Reputation: 3088
I'm writing a vision impaired game and for this I wanted to invert the colors, so white is black etc... which sounds like what you're doing. I eventually hit on this that works and does not involve shaders or modifying the textures. However it does kill blending. So I render to a texture buffer and then do
glLogicOp (GL_COPY_INVERTED);
glEnable (GL_COLOR_LOGIC_OP);
<bind to texture buffer and blit that>
glLogicOp (GL_COPY);
glDisable (GL_COLOR_LOGIC_OP);
Upvotes: 0