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.

  • \$\begingroup\$ Does jMonkeyEngine support post-process effects? You'll probably have to go this way. \$\endgroup\$ – sam hocevar May 5 '12 at 10:41
  • \$\begingroup\$ Yeah, jMonkeyEngine supports post-process effects, but I'm not too familiar with them at this moment. Thanks for suggestion. \$\endgroup\$ – m4tx May 5 '12 at 11:10
  • \$\begingroup\$ Mmmh, nevermind, there are simpler ways. I'll try to answer properly. \$\endgroup\$ – sam hocevar May 5 '12 at 12:17
  • \$\begingroup\$ Can you use glLogicOp(GL_XOR)? opengl.org/sdk/docs/man/xhtml/glLogicOp.xml \$\endgroup\$ – Adam May 5 '12 at 13:12
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – 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:


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);
| improve this answer | |
  • 4
    \$\begingroup\$ Sounds like you know your alpha blending! \$\endgroup\$ – bobobobo May 5 '12 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.