glReadPixels and resizing

Dear all,

I am new to JOGL and was hoping you could help me understand how to use glReadPixels.

Currently I am experimenting with 2D Metaballs. I create 2 circles with the color fading the further it is from the center.
Now, what I would like to do is identify the border area where the color is of a certain brightness. To do so I use glReadPixels to read back the color of all pixels in the buffer and everything works untill I start changing the width of the canvas. (Changing the height does not cause any problems)

All widths where width % 4 == 0 look fine but the pixels seem to be displaced for all other widths. Below is an image of the application for widths 300 through 304 drawn with a white border. The last picture is an example of the finished application, in a width where the pixel operation works.

http://www.dccj.org/files/billeder/pixelProblem_JOGL.jpg

Below is the code I use to draw the border:

int pH = panelHeight, pW = panelWidth;
int bufferSize = (pH+1)*(pW+1) * 3;
int bufferWidth = (pW) * 3;

java.nio.ByteBuffer pixelsRGB = ByteBuffer.allocateDirect( bufferSize );
gl.glReadPixels(0, 0, pW, pH, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixelsRGB);

gl.glBegin(GL.GL_POINTS);
for(int bufferY = 1; bufferY <= pH; bufferY++) {
   for(int bufferX = 1; bufferX <= pW; bufferX++) {

      // Retrieve Color code of current pixel from buffer 		
      int red = pixelsRGB.get(bufferY * bufferWidth + bufferX * 3 + 0);
      int green = pixelsRGB.get(bufferY * bufferWidth + bufferX * 3 + 1);
      int blue = pixelsRGB.get(bufferY * bufferWidth + bufferX * 3 + 2);
         
      // Make colors readable to me 0 - 255
      red = (red & 0x000000FF);
      green = (green & 0x000000FF);
      blue = (blue & 0x000000FF);
         
      // Calculate brightness
      int pot = red * red + green * green + blue * blue;
         
      if(pot < 128*128) { 
      // I draw the background in here
      }
      else {
         if(pot < 140*140) { 
         // draws the white outline
            gl.glColor3f(1, 1, 1);
            gl.glVertex2i(bufferX, bufferY);
         }
      }
   }
}
gl.glEnd(); 

I hope you can help me.

When manipulating blocks of color data, such as textures, raster images, and reading pixels, there is an opengl attribute called alignment that is the expected pixel alignment of the block. By default, it is set to 4. If an image block doesn’t have dimensions of the given alignment, opengl does some shifting or expanding (can’t remember off the top of my head) to make it fit the alignment.

Technically there are two alignments, one for packing pixels (getting them off of the graphics card) and one for unpacking them (sending them to the card). To solve your problem, call

gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); 

before calling glReadPixels.

If that doesn’t work, try switching PACK_ALIGNMENT to UNPACK_ALIGNMENT, I may have swapped the definitions of pack and unpack.

gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); worked like a charm, thank you very much for your help!