Modulating Paint

Does anyone know how to modulate the colour of pixels drawn to a Graphics2D? You know, like when you do a glTexEnvMode(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) and glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)?

I’ve figured out how to use an AlphaComposite but can’t figure out the modulation part.

Cas :slight_smile:

Here’s the relationship between ogl modulating modes and java2d compositing rules:


{
   { GL_ZERO,                GL_ZERO                }, /* 0 - Nothing      */
    { GL_ZERO,                GL_ZERO                }, /* 1 - RULE_Clear   */
    { GL_ONE,                 GL_ZERO                }, /* 2 - RULE_Src     */
    { GL_ONE,                 GL_ONE_MINUS_SRC_ALPHA }, /* 3 - RULE_SrcOver */
    { GL_ONE_MINUS_DST_ALPHA, GL_ONE                 }, /* 4 - RULE_DstOver */
    { GL_DST_ALPHA,           GL_ZERO                }, /* 5 - RULE_SrcIn   */
    { GL_ZERO,                GL_SRC_ALPHA           }, /* 6 - RULE_DstIn   */
    { GL_ONE_MINUS_DST_ALPHA, GL_ZERO                }, /* 7 - RULE_SrcOut  */
    { GL_ZERO,                GL_ONE_MINUS_SRC_ALPHA }, /* 8 - RULE_DstOut  */
    { GL_ZERO,                GL_ONE                 }, /* 9 - RULE_Dst     */
    { GL_DST_ALPHA,           GL_ONE_MINUS_SRC_ALPHA }, /*10 - RULE_SrcAtop */
    { GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA           }, /*11 - RULE_DstAtop */
    { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, /*12 - RULE_AlphaXor*/
}

Note that these assume that the source is alpha-premultiplied.

Does this answer your question?

Nope, that gives me more or less the blending… what I need to know is how to do the colour modulation that is achieved by glTexEnv.

ie. I’ve got a white bitmap, I want to modulate it with red, so I can draw it red instead. With as much acceleration as possible.

Cas :slight_smile:

[quote]ie. I’ve got a white bitmap, I want to modulate it with red, so I can draw it red instead. With as much acceleration as possible.
[/quote]
Correct me if I’m wrong, but it sounds like you want something closer to glColor(). You could always achieve that with an image filter, but you’d need to use an in memory backbuffer instead of direct hardware calls. Anyone know a better way?

Well, I’m beginning to suspect I can’t get it hardware accelerated, and also beginning to suspect that TexturePaint might be the key to this. Or a tweaked subclass.

Cas :slight_smile:

[quote]Well, I’m beginning to suspect I can’t get it hardware accelerated, and also beginning to suspect that TexturePaint might be the key to this. Or a tweaked subclass.

Cas :slight_smile:
[/quote]
Hey Cas, I see what you’re trying to achieve (we use GL_MODULATE in some of our OGL pipeline internals). I think RescaleOp will give you the effect you’re looking for: set up the scale factors using the components of your modulation color. For example, if you want to modulate with red, your scale factors would be {1.0, 0.0, 0.0}. Unfortunately this won’t be hardware accelerated. You could also tweak TexturePaint, as you suggest, but again this won’t be accelerated.

I’ve considered accelerating some BufferedImageOps in hardware, such as RescaleOp and ConvolveOp, since they’re fairly easy to implement using fragment shaders. If this would help your app, could you please file an RFE (e.g. faster modulation)? It always helps us prioritize our work when its evident that it will help fill a customer’s need.

Thanks,
Chris

No real need to be honest… I’ve got everything I need in LWJGL (or JOGL for that matter). I’m looking at RescaleOp now and trying to figure out how the hell it works…

Cas :slight_smile:

RescaleOp worked a treat but with the following caveat: there’s no Graphics2D.drawImage(src, sx0, sy0, sx1, sy1, dx0, dy1, Operation) function - I had to copy each individual glyph out into separate tiny BufferedImages first. Bit of a pain.

Cas :slight_smile:

Couldn’t you chain together an AffineTransformOp (to do the scaling) and a ConvolveOp(to do the modulation) ?

No, because I’m sick of having to figure out how to do easy things in complicated ways.

Cas :slight_smile: