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:
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();
}
Hi!
I have now updated my Mother library for it to work with Processing 2.0, and took the opportunity to also address the usability problems the previous version had. This one should be much easier to get started with!
If this is the first you hear of it, Mother is a standalone host for running multiple Processing sketches in parallel, and mixing their output, in a manner analogous to VJing (more info and videos at www.onar3d.com/mother/).
I’d greatly appreciate if you would give it a try and let me know how you get along, treating this release as a Beta for now!
It works fine on Windows 7 & 8, 32bit and 64bit.
Edit: On OSX the newer Beta 2 works fine for me!
Getting started instructions are at the top of the “Mother Documentation.pdf” included in the library zip file, along with more info on the library.
https://processing-mother.googlecode.com/files/Mother%201.0%20Beta%202.zip
https://processing-mother.googlecode.com/files/MotherDeliveryEclipse%201.0%20Beta%202.zip
For the previous version I did get complaints about it being convoluted to use, particularly for Processing users not advanced enough to work with Eclipse.
I have here done my best to address this issue, do please let me know what you think, what errors you run into, and how you would suggest I further improve the library!
Ilias B.