Search code examples
qtqpainter

Does Qt have an additive blend mode?


QPainter has many composition modes but none called additive. I'm interested because additive blending is used all the time in games for lighting / particles whatever. The overlay mode is the only one that had something like the effect of lighting.

EDIT: I figured it out, heres how you can efficiently make different coloured lights in Qt.

In constructor or where ever, not in paint event:

light = QPixmap("light.png");
QPainter pix(light);
pix.setCompositionMode(QPainter::CompositionMode_Overlay);
pix.fillRect(light.rect(), QColor(255, 0, 0, 255)); // colorize the light in any color

Paint Event:

// Do drawing, e.g. a background
p.drawPixmap(0, 0, QPixmap("background.png"));

// draw the lighting
p.setCompositionMode(QPainter::CompositionMode_Plus);
p.drawPixmap(100, 100, light);

You can reuse the same pixmap as much as you like and draw it with different opacity or size etc.


Solution

  • The documentation for QPainter::CompositionMode_Plus says:

    Both the alpha and color of the source and destination pixels are added together.