texture mapping - retrieving RGB after stretching


I have texture mapped a jpg (256*256 pixels) to a quadrilateral (-1,-1 as bottomleft and 1,1 as topright corner). Then I stretch the texture (-3,-3 and 3,3) to get the zooming effect. Now I am interested in retrieving the RGB values of this stretched texture (texels). How do i do that? glreadpixels returns garbage values. glGetTexImage is giving memory problems (when i try to write the array in a file).

Also, I would like to know how do i determine the size of the texture if the window is 500, 500 and i map the jpeg to the quad as (-1,-1 and 1,1).

Please help at your earliest.

Thanks for your time and efforts.

glReadPixels is how you do it. Perhaps you should investigate why it is returning garbage for you.

Edit: Render-to-texture is another option, but that seems like an overkill for this situation.

[This message has been edited by al_bob (edited 09-04-2003).]

Thanks for your prompt reply!

glReadPixels(0, 0, 10, 10, GL_BLUE, GL_BYTE, pixels);
is what i am using… the window is 100 by 100 and i am using -1,-1 and 1,1 as the extremes. so how do i know what should be the lowermost pixel to be input to glreadpixels?
Also i wanted to clarify regarding the sequence of the code.
i have put the glreadpixels() after texture mapping (ie, immediately after glTexCoord2f(0.0f, 0.0f), glVertex3f(-1, 1, 0)… (as the scene is being drawn here)
Am I writing the glreadpixels() in the correct place?
I get memory error saying that the memory could not be written, and it won’t allow me to debug saying there was an error in initializing the application… Please suggest some pointers.


glReadPixels can’t be called inside a glBeing()/glEnd() pair. Make sure you called glEnd() before glReadPixels().

glReadPixels() takes viewport pixel coordinates, which aren’t transformed.

Finally, the “pixels” pointer needs to be memory that you allocated. That memory has to be large enough to fit the contents of the read pixels.

So if you want to read the blue channel of your entire 100x100 window, you need to do something like:

/* Draw code goes here */

GLubyte pixels = new GLubyte[100*100];
glReadPixels(0, 0, 100, 100, GL_BLUE, GL_UNSIGNED_BYTE, pixels);

/* Do work with the read pixels */

delete [] pixels;

Edit: delete vs delete[]

[This message has been edited by al_bob (edited 09-04-2003).]

Also, make sure you are reading from the right buffer if you are using double buffering. If you do a glRead before you swap buffers, use glReadBuffer(GL_BACK); If you do the read after you swap buffers, you’ll need glReadBuffer(GL_FRONT); By default the read buffer is GL_FRONT for single buffered apps and GL_BACK for double buffered apps.

Thanks for the replies!

glReadPixels() works fine (I was using GL_INT as one pf the parameters rather than GL_UNSIGNED_BYTE which gave me huge numbers…)

One more thing I would like to know is when the window is 100 * 100 and i am pasting a 20 by 20 jpeg (on a quad with -1,-1 and 1,1 as extremes) how do i know what is the bottomleft co-ordinate and the width & height that the jpeg occupies on the screen? (it is at the center of the screen). As later on i am interested in stretching the jpeg and then retrieving the RGB values…