Skip to content

Conversation

@JakubValtar
Copy link
Contributor

Java2D

  • Source and destination were swapped, so nothing worked as expected. Now fixed.

OpenGL

Alpha 255
b1

Alpha 80 (other modes than REPLACE, BLEND, ADD and SUBTRACT are not comparable as JAVA2D uses SRC alpha to interpolate between DST and OUTPUT, OpenGL just uses OUTPUT)
b2

Test code:

PGraphics java2d;
PGraphics p2d;

int[] blendModes = {REPLACE, BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN};
String[] blendLabels = {"REPLACE", "BLEND", "ADD", "SUBTRACT", "LIGHTEST", "DARKEST", "EXCLUSION", "MULTIPLY", "SCREEN"};

public void setup() {
    size(900,900, P2D);
    frame.setAlwaysOnTop(true);
    java2d = createGraphics(300, 800 / blendModes.length);
    p2d = createGraphics(300, 800 / blendModes.length, P2D);
}

public void draw() {
    background(0xFF000000);
    stroke(100);
    for (int x = 0; x < width; x += 20) {
        line(x, 0, x, height);
    }
    for (int y = 0; y < width; y += 20) {
        line(0, y, width, y);
    }
    blendMode(BLEND);
    textSize(25);

    text("JAVA2D", 350, 30);
    text("P2D", 655, 30);

    for (int i = 0; i < blendModes.length; i++) {
        text(blendLabels[i], 50, 50 + (i + 0.5f) * (java2d.height + 5));

        drawPG(java2d, blendModes[i]);
        image(java2d, 250, 50 + i * (java2d.height + 5));
        flush();

        drawPG(p2d, blendModes[i]);
        image(p2d, 555, 50 + i * (p2d.height + 5));
        flush();
    }
}

void drawPG(PGraphics pg, int blendMode) {
    pg.beginDraw();
    pg.noStroke();
    pg.blendMode(blendMode);
    switch (blendMode) {
    case ADD:
    case LIGHTEST:
    case SCREEN:
        pg.background(0);
        break;
    default:
        pg.background(255);
    }
    int alpha = 80;
    int green1 = blendMode == DARKEST ? 255 : 0;
    int green2 = blendMode == DARKEST ? 0 : 255;
    for (int x = 0; x < pg.width; x += 12) {
        for(int y = 0; y < pg.height; y += 12) {
            int red =   (int) map(x,   0, pg.width,             0,      255);
            int green = (int) map(y*x, 0, pg.height * pg.width, green1, green2);
            int blue =  (int) map(x,   0, pg.height,            0,      255) % 255;
            pg.fill(red, green, blue, alpha);
            pg.rect(x, y, 12, pg.height - y);
        }
    }
    pg.flush();
    pg.endDraw();
}

@codeanticode
Copy link
Contributor

Hello Jakub, this is amazing, thanks once again for all your work.

All the fixes look good, but I have a question with the SUBTRACT mode.
According to the OpenGL documentation, the equation for the GL_FUNC_REVERSE_SUBTRACT​ mode is

O = sD - dS

By doing

pgl.blendFuncSeparate(PGL.SRC_ALPHA, PGL.ONE, PGL.ONE, PGL.ONE);

we are setting s = SA and d = 1, if I'm not mistaken, which would lead to

O = D * SA - S

instead of

O = D - (S * SA)

Am I missing something?

@JakubValtar
Copy link
Contributor Author

The OpenGL wiki seems to be misleading, because the glBlendEquation docs specify this to be

O = dD - sS

The test pattern I used supports this. Honestly I spent a lot of time on that wiki page, but didn't notice the parameters were swapped and I also didn't check the docs. I just swapped it because parameter names are (srcRGB, dstRGB, srcAlpha, dstAlpha).

@codeanticode
Copy link
Contributor

Yes, O = dD - sS is what makes sense and it is in the glBlendEquation doc, so it is definitely a typo in the wiki.

I think this is ready for merging...

@benfry This PR makes some changes to the blending in PGraphicsJava2D, basically the source and destination colors are inverted. I looked at the code and seems that the fix from Jakub is correct, just wanted to check with you since I'm not very familiar with the image manipulations in Java2D.

@benfry
Copy link
Contributor

benfry commented Nov 3, 2014

Looks good on a quick glance—I'll go ahead and merge.

We really appreciate you looking into this @JakubValtar. This sort of thing (digging into the issue, testing it across renderers) is a huge help.

benfry added a commit that referenced this pull request Nov 3, 2014
Multiple blending fixes & improvements
@benfry benfry merged commit 62ed401 into processing:master Nov 3, 2014
@JakubValtar JakubValtar deleted the bugfix-blendmodes branch November 3, 2014 20:33
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drawing low-alpha graphics over an opaque background alters the resulting alpha in a PGraphics with P2D/P3D Problems with blur/blend methods

3 participants