# 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.

• Does jMonkeyEngine support post-process effects? You'll probably have to go this way. – sam hocevar May 5 '12 at 10:41
• Yeah, jMonkeyEngine supports post-process effects, but I'm not too familiar with them at this moment. Thanks for suggestion. – m4tx May 5 '12 at 11:10
• Mmmh, nevermind, there are simpler ways. I'll try to answer properly. – sam hocevar May 5 '12 at 12:17
• Can you use glLogicOp(GL_XOR)? opengl.org/sdk/docs/man/xhtml/glLogicOp.xml – Adam May 5 '12 at 13:12
• Note that inversion doesn't guarantee you visibility in a whole bunch of corner cases, like unsaturated grays, and provides poor visibility for many other cases. – Lars Viklund May 5 '12 at 16:13

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

• Sounds like you know your alpha blending! – bobobobo May 5 '12 at 16:29