We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
updatePixels() in PGraphicsGL (Read 2713 times)
updatePixels() in PGraphicsGL
May 9th, 2005, 12:38pm
 
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.
Re: updatePixels() in PGraphicsGL
Reply #1 - May 9th, 2005, 3:24pm
 
those functions were late additions, and the code hasn't been cleaned up/sorted out for them.

in opengl, you have to reverse the y coordinates of data as well as shift the RGBA/ARGB bits around based on the endian-ness of the platform. so the functions you see there are swapping the y coordinate, the others are not. in the future i will probably merge the two in order to have less code to debug. it just means that i have to swap the v coordinate on textures.

using glCopyPixels() would work only if you haven't called loadPixels() and updatePixels(). there are many ways like this to optimize the opengl library but i just haven't had the time yet. this is why it's beta.

as for converting the data between RGBA/ARGB, that's all these things do. all that << and >> stuff in the functions is moving the bits around to the proper ordering for the platform. another optimization will be to use opengl pixel formats that require less bit shifting, which might be able to cut this code in half.
Page Index Toggle Pages: 1