glReadPixels...--> Fliped image

i use the glreadpixels to make a snap of a opengl context
glReadPixels(0, 0, bmpWidth, bmpHeight, GL_RGB , GL_UNSIGNED_BYTE, @bmp)

i works fine, but the bitmap is fliped horizontaly !.
any idea how to perform it with correct position ?


Do you mean it is really flipped horizontally or just vertically?

Readpixels outputted images are flipped vertically, that means [0,0] is the bottom left corner of the framebuffer. You’ll have to change the row order in order to fix that, though most image formats (including BMP/DIB, TGA) store it upside down too.

Make sure you are drawing/saving the data properly.

Well, exactly its fliped vertically.
and i think its due to the row order ([0,0] is the bottom left corner). how chaw it to top left ?

Easy, something like this:

for x=1 to width
 for y=1 to height
  data_cpy[x, y] = data_orig[x, height-y]

Or you can render your scene upside down if you don’t want to make a copy of the data in memory.

well, without the the “for” bloc. how can i render the scene upside down?
are there any direct OpenGL way to perform it ?

Put glScalef(1,-1,1); somewhere in your code, or modify the glFrustum parameters.

Or change the glOrtho/gluOrtho2d parameters.

Well, i am very beginner in OpenGl .
i put glScalef(1,-1,1); befor my line of
glReadPixels … and nothing happen :frowning:
can u please give me more explainations…


What exactly are you trying to achieve? Do you want something like a screenshot function? If so, to what image format are you writing it? How are you viewing the image data?

You can use the glScalef command to flip the entire scene upside down, so you must call it somewhere in your view setup. It’s a matrix operation.

well, it is something like a Screenshot to a 32 bits image.
it works but the only one problem is the image is fliped.

i tried with glScalef but i dont get any changes !!

Call glScalef before rendering your object, apply it to your modelview matrix.
Also, some formats like bmp, you can put a negative height value to indicate the order of the image.

glScalef doesn’t effect glReadPixels
It’s for matrices like your modelview matrix
and matrices are for transforming vertices, normals, texcoords.

the problem i think is that glreadPixels starts from Bottom-Left .
how can i change it to starts from Top-Left ?

Originally posted by PixIn:
the problem i think is that glreadPixels starts from Bottom-Left .
how can i change it to starts from Top-Left ?

It can not be changed, all of OpenGL is designed around bottom up framebuffers and textures, many people do not realize this because the bottom up/top down order does not matter on textures (except when using glCopyTexSubImage which operates in native bottom up order).

Generally you want to flip the image before saving your screenshot, assuming the image format you are saving does not support bottom up pixels (TGA and BMP do for example, and use that by default).

To accomplish this you want to have two arrays, one to receive the glReadPixels, and another to copy it into using the for loop that was mentioned earlier (however that could be done a little faster using memcpy on the rows rather than an inner loop).