Monday, November 9, 2015

opengl - How to invert background pixel's color


I'm writing a game and map editor using Java and jMonkeyEngine. In the map editor, I've got a brush done by wireframed sphere. My problem is: I want to make it visible everywhere, so I want to invert color of the pixel "behind" this brush. I wanted to do it with fragment shader (GLSL), but I don't know how to get color of this pixel.



Answer



The quick and easy way to do this is to use the standard blending equation with the following parameters:


glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);

If src is the source colour (the one from your new object) and dst is the destination colour (the one which is in the framebuffer) it will compute the new colour as follows:



new = src * (1 - dst) + dst * 0
= src * (1 - dst)

So you just need to set src = 1, ie. white, and you will get inverse video. The GLSL fragment shader will be:


void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...