Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawing OpenGL into PGraphics with alpha 0.5f produces extremely faint output, more like 0.1f #2083

Closed
onar3d opened this issue Sep 17, 2013 · 3 comments
Assignees
Labels

Comments

@onar3d
Copy link

onar3d commented Sep 17, 2013

Hi!

I created this example from combining the OffscreenTest and LowLevelGL examples, but I've encountered this behavior in a lot of my own code as well.

This appears in the latest version (2.0.3) , running on Windows 7 32bit.

Pressing the spacebar toggles between drawing directly, and first drawing into PGraphics and then displaying that.

The two should be identical but are not!

In the method drawDirectly, the output is as expected for all alpha values.
In drawImage, alpha == 1.0f looks fine, but set this to 0.5f, and drawing is almost invisible!

Sketch code to reproduce:

import java.nio.*;

PGL pgl;
PGraphics pg;
PShader flatShader;
int vertLoc, colorLoc;
float[] vertices, colors;
FloatBuffer vertData, colorData;
boolean drawPGraphicsImage = true;
float alpha = 0.5f;

// Keys:
// 'a': toggles between alpha == 0.5f and alpha == 1.0f.
// spacebar: toggles between rendering directly, and into a PGraphics buffer.

// BUG DESCRIPTION:
// In drawDirectly, the appearance is as expected for all alpha values.
// In drawImage, alpha == 1.0f looks fine, but set this to 0.5f, and drawing is almost invisible!

void setup() {
size(800, 800, OPENGL);

pg = createGraphics(800, 800, OPENGL);

flatShader = loadShader("frag.glsl", "vert.glsl");

vertices = new float[12];
vertData = allocateDirectFloatBuffer(12);

colors = new float[12];
colorData = allocateDirectFloatBuffer(12);
}

void draw() {
updateGeometry();

background(0);

if(drawPGraphicsImage)
drawImage();
else
drawDirectly();
}

void keyPressed() {
if (key == ' ') {
drawPGraphicsImage = !drawPGraphicsImage;
}
else if (key == 'a') {
if(alpha == 0.5f)
alpha = 1.0f;
else
alpha = 0.5f;
}
}

// This works as expected
void drawDirectly() {
pgl = beginPGL();
flatShader.bind();

vertLoc = pgl.getAttribLocation(flatShader.glProgram, "vertex");
colorLoc = pgl.getAttribLocation(flatShader.glProgram, "color");

pgl.enableVertexAttribArray(vertLoc);
pgl.enableVertexAttribArray(colorLoc);

pgl.vertexAttribPointer(vertLoc, 4, PGL.FLOAT, false, 0, vertData);
pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 0, colorData);

pgl.drawArrays(PGL.TRIANGLES, 0, 3);

pgl.disableVertexAttribArray(vertLoc);
pgl.disableVertexAttribArray(colorLoc);

flatShader.unbind();

endPGL();
}

// This is most likey a bug!
void drawImage() {
pg.beginDraw();
pg.clear();

pgl = pg.beginPGL();
flatShader.bind();

vertLoc = pgl.getAttribLocation(flatShader.glProgram, "vertex");
colorLoc = pgl.getAttribLocation(flatShader.glProgram, "color");

pgl.enableVertexAttribArray(vertLoc);
pgl.enableVertexAttribArray(colorLoc);

pgl.vertexAttribPointer(vertLoc, 4, PGL.FLOAT, false, 0, vertData);
pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 0, colorData);

pgl.drawArrays(PGL.TRIANGLES, 0, 3);

pgl.disableVertexAttribArray(vertLoc);
pgl.disableVertexAttribArray(colorLoc);

flatShader.unbind();

pg.endPGL();

pg.endDraw();
image(pg, 0, 0, 800, 800);
}

void updateGeometry() {
// Vertex 1
vertices[0] = 0;
vertices[1] = 0;
vertices[2] = 0;
vertices[3] = 1;
colors[0] = 1;
colors[1] = 0;
colors[2] = 0;
colors[3] = alpha;

// Corner 2
vertices[4] = width/2;
vertices[5] = height;
vertices[6] = 0;
vertices[7] = 1;
colors[4] = 0;
colors[5] = 1;
colors[6] = 0;
colors[7] = alpha;

// Corner 3
vertices[8] = width;
vertices[9] = 0;
vertices[10] = 0;
vertices[11] = 1;
colors[8] = 0;
colors[9] = 0;
colors[10] = 1;
colors[11] = alpha;

vertData.rewind();
vertData.put(vertices);
vertData.position(0);

colorData.rewind();
colorData.put(colors);
colorData.position(0);
}

FloatBuffer allocateDirectFloatBuffer(int n) {
return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer();
}

@onar3d
Copy link
Author

onar3d commented Sep 17, 2013

I tested this also in Processing 2.0.1 and 2.0.2 and got different behavior showing that this is most likely a bug stemming out of recent development.

In 2.0.1 the issue is the same as reported for 2.0.3.

For 2.0.2 however, the output of drawImage() is as expected, while drawDirectly() seems to ignore alpha altogether.

@ghost ghost assigned codeanticode Sep 17, 2013
@codeanticode
Copy link
Member

The reason is that the offscreen surface is not opaque, and when it is blended onto the main surface, it looks fainter than one would expect by simply replacing the colors. So, if you do:

  blendMode(REPLACE);
  image(pg, 0, 0, 800, 800);
  blendMode(BLEND);

at the end of drawImage(), then the two outputs are the same.

This in fact a duplicate of #1844

@github-actions
Copy link

This issue 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 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants