I have a few questions about this function:
Code:
public void updatePixels() {
// flip vertically (opengl stores images upside down),
int index = 0;
int yindex = (height - 1) * width;
for (int y = 0; y < height/2; y++) {
if (BIG_ENDIAN) {
// and convert ARGB back to opengl RGBA components (big endian)
for (int x = 0; x < width; x++) {
int temp = pixels[index];
/*
pixels[index] =
((pixels[yindex] >> 24) & 0xff) |
((pixels[yindex] << 8) & 0xffffff00);
pixels[yindex] =
((temp >> 24) & 0xff) |
((temp << 8) & 0xffffff00);
*/
pixels[index] = ((pixels[yindex] << 8) & 0xffffff00) | 0xff;
pixels[yindex] = ((temp << 8) & 0xffffff00) | 0xff;
index++;
yindex++;
}
} else {
// convert ARGB back to native little endian ABGR
for (int x = 0; x < width; x++) {
int temp = pixels[index];
pixels[index] = 0xff000000 |
((pixels[yindex] << 16) & 0xff0000) |
(pixels[yindex] & 0xff00) |
((pixels[yindex] >> 16) & 0xff);
pixels[yindex] = 0xff000000 |
((temp << 16) & 0xff0000) |
(temp & 0xff00) |
((temp >> 16) & 0xff);
index++;
yindex++;
}
}
yindex -= width*2;
}
// re-pack ARGB data into RGBA for opengl (big endian)
/*
for (int i = 0; i < pixels.length; i++) {
pixels[i] = ((pixels[i] >> 24) & 0xff) |
((pixels[i] << 8) & 0xffffff00);
}
*/
//System.out.println("running glDrawPixels");
//gl.glRasterPos2i(width/2, height/2);
//gl.glRasterPos2i(width/2, 1); //height/3);
//gl.glRasterPos2i(1, height - 1); //1, 1);
// for some reason, glRasterPos(0, height) won't draw anything.
// my guess is that it's getting "clipped", so adding an epsilon
// makes it work. also, height-1 would be the logical start,
// but apparently that's not how opengl coordinates work
gl.glRasterPos2f(0.0001f, height - 0.0001f);
gl.glDrawPixels(width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels);
}
First of all, I've noticed there are some implemented javaToNativeRGB() and nativeToJavaRGB() in PGraphicsGL, why not using them?
Second, I think there is a function called glCopyPixels() why not use that one instead of glRasterPos2f()+glDrawPixels()? I'm guessing it would be faster.
Third, I don't see the transformation from ARGB (processing) to RGBA (opengl). The mask that we apply to the pixels is 0xff000000 which means that alpha is considered first ARGB (althought maybe I'm confused by the littleendian thing). But in the function gl.glDrawPixels(width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels) we specify that it is in RGBA.