Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created February 16, 2013 13:45
Show Gist options
  • Save darkwave/4966994 to your computer and use it in GitHub Desktop.
Save darkwave/4966994 to your computer and use it in GitHub Desktop.
A modified version of the Buffer class suggested by chicoatomico to recreate the Picking library for Processing 2.0
import java.nio.IntBuffer;
import java.nio.ByteOrder;
public class Buffer extends PGraphics3D
{
protected int current_color = 0;
public Buffer(PApplet parent)
{
setParent(parent);
setSize(parent.width, parent.height);
}
protected int getPixel(int x, int y) {
IntBuffer pixelColorBuffer = IntBuffer.allocate(1);
int[] testArray = new int[1];
beginPixelsOp(OP_READ);
try {
// The readPixels() call in inside a try/catch block because it appears
// that (only sometimes) JOGL will run beginDraw/endDraw on the EDT
// thread instead of the Animation thread right after a resize. Because
// of this the width and height might have a different size than the
// one of the pixels arrays.
pgl.readPixels(x, height - y, 1, 1, PGL.RGBA, PGL.UNSIGNED_BYTE,
pixelColorBuffer);
}
catch (IndexOutOfBoundsException e) {
// Silently catch the exception.
}
endPixelsOp();
try {
// Idem...
//getIntArray(pixelBuffer, pixels);
//nativeToJavaARGB(pixels, width, height);
//getIntArray(pixelColorBuffer, testArray);
testArray[0] = nativeToJavaARGB(pixelColorBuffer.get(0));
}
catch (ArrayIndexOutOfBoundsException e) {
}
return testArray[0];
}
int nativeToJavaARGB(int pixelColor) {
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { // RGBA to ARGB
return (pixelColor & 0xff000000) |
((pixelColor >> 8) & 0x00ffffff);
} else { // ABGR to ARGB
return (pixelColor & 0xff000000) |
((pixelColor << 16) & 0xff0000) |
(pixelColor & 0xff00) |
((pixelColor >> 16) & 0xff);
}
}
@Override
public boolean displayable() {
return true;
}
public void callCheckSettings() {
super.checkSettings();
}
@Override
public void lights() {
}
@Override
public void smooth() {
}
@Override
public void fill(int arg) {
}
@Override
public void fill(float arg) {
}
@Override
public void fill(float arg, float arg_1) {
}
@Override
public void fill(int arg, float arg_1) {
}
@Override
public void fill(float arg, float arg_1, float arg_2) {
}
@Override
public void fill(float arg, float arg_1, float arg_2, float arg_3) {
}
@Override
public void stroke(int arg) {
}
@Override
public void stroke(float arg) {
}
@Override
public void stroke(float arg, float arg_1) {
}
@Override
public void stroke(int arg, float arg_1) {
}
@Override
public void stroke(float arg, float arg_1, float arg_2) {
}
@Override
public void stroke(float arg, float arg_1, float arg_2, float arg_3) {
}
@Override
public void textureMode(int arg) {
}
@Override
public void texture(PImage arg) {
}
@Override
public void vertex(float x, float y, float z, float u, float v) {
super.vertex(x, y, z);
}
@Override
public void image(PImage arg, float arg_1, float arg_2) {
}
@Override
public void image(PImage arg, float arg_1, float arg_2, float arg_3, float arg_4) {
}
@Override
public void image(PImage arg, float arg_1, float arg_2, float arg_3, float arg_4, int arg_5, int arg_6, int arg_7, int arg_8) {
}
@Override
protected void imageImpl(PImage image, float x1, float y1, float x2, float y2, int u1, int v1, int u2, int v2) {
}
public void setCurrentId(int i)
{
// ID 0 to 16777214 => COLOR -16777215 to -1 (white)
// -16777216 is black
current_color = i - 16777215;
super.fill(current_color);
}
public int getId(int x, int y)
{
// COLOR -16777216 (black) to -1 => ID -1 (no object) to 16777214 */
int id = getPixel(x, y) + 16777215;
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment