View Full Version : Draw bitmap through glDrawPixels+array

05-11-2010, 12:30 AM

Why I need exchange X, Y axis bitmap pixel for correct drawing through glDrawPixels?

Delphi Example:

Bitmap.LoadFromFile ('Claudia.bmp');
For i := 0 to ImageHeight - 1 do
For j := 0 to ImageWidth - 1 do
PixCol := Bitmap.Canvas.Pixels [j, i];
Image[ImageHeight - i - 1][j][0] := PixCol and $FF;
Image[ImageHeight - i - 1][j][1] := (PixCol and $FF00) shr 8;
Image[ImageHeight - i - 1][j][2] := (PixCol and $FF0000) shr 16;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDrawPixels(ImageWidth, ImageHeight, GL_RGB, GL_UNSIGNED_BYTE, @Image);

05-11-2010, 01:32 PM
Images in OpenGL are column minor.

+1 pixel in memory is + 1 pixel to the right, it is just the convention that OpenGL uses.

Your image array uses subscript j as the minor axis (w.r.t. i)and increments it up to image width. So this is correct.

You address the image as bytes so there is an additional minor axis for the color components.

You also use -i rather than i, this again is convention and depends which image format you use. OpenGL has a convention that images start lower left and progress upwards, some image formats start top left and need to be flipped as you have done here.

05-11-2010, 06:56 PM
P.S. globally substitute j for column or xpos and substitute i for row or ypos and you'll have a clearer understanding I think.