Drawing OpenGL into PGraphics with alpha 0.5f produces extremely faint output, more like 0.1f
I'm pretty sure this is a bug, so I filed it as one ( https://github.com/processing/processing/issues/2083 ).
I couldn't find anything on here or in the bugs database that is the same.
But off the odd chance it isn't a bug, I thought I'd ask here, and if it turns out I've been wrong I'll delete the bug report.
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!
EDIT:
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!
End EDIT.
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();
}