PDA

View Full Version : glReadPixels...--> Fliped image



PixIn
03-16-2007, 11:55 AM
Hi
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 ?

Thanks

remdul
03-17-2007, 03:10 AM
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.

PixIn
03-17-2007, 07:46 AM
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 ?

remdul
03-17-2007, 09:56 AM
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.

PixIn
03-17-2007, 10:42 AM
well, without the the "for" bloc. how can i render the scene upside down?
are there any direct OpenGL way to perform it ?

remdul
03-17-2007, 11:26 AM
Put glScalef(1,-1,1); somewhere in your code, or modify the glFrustum parameters.

Or change the glOrtho/gluOrtho2d parameters.

PixIn
03-17-2007, 11:44 AM
Well, i am very beginner in OpenGl .
i put glScalef(1,-1,1); befor my line of
glReadPixels .... and nothing happen :(
can u please give me more explainations...

Thanks

remdul
03-17-2007, 08:26 PM
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.

PixIn
03-18-2007, 12:59 PM
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 !!

V-man
03-19-2007, 12:50 AM
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.

PixIn
03-19-2007, 07:02 PM
well,
the problem i think is that glreadPixels starts from Bottom-Left .
how can i change it to starts from Top-Left ?

Forest Hale
03-19-2007, 11:52 PM
Originally posted by PixIn:
well,
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).