Render offscreen & glReadPixels

Hi there,

i’ve some problems, with glReadPixels. The function where it appears should create a small thumbnail-image with only a few pixels of width and height from a bigger texture. So at first, the texture is renderd offscreen to a temporary texture of the image-size and the the data shold be copied, where i use wxWidgets image-class (wxImage) to store the imagedata. The wxImage-class has a method to get a pointer to the image-data. Copying image-data to a texture works like this:

glTexImage2D    (GL_TEXTURE_2D, 0, GL_RGBA, W, H, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLuint*)wxImagePointer->GetData());

Unfortuately i can’t get it otherwise around. The image has wrong colors with:
“glReadPixels(0,0, w,h-1, GL_RGB, GL_UNSIGNED_BYTE, dst->GetData());” and with
“glReadPixels(0,0, w-1,h-1, GL_RGB, GL_UNSIGNED_BYTE, dst->GetData());” colors ar okay but image shers to left.

Here ist the code of the function. The class clsBuffer is used to store the large texture.

clsBuffer *clsBuffer::RenderImage (wxImage *dst) {  

    int w = dst->GetWidth();        int h = dst->GetHeight();
    int MipMapMode = GL_LINEAR;

    GLuint tmpTex;
    glGenTextures  (1, &tmpTex);
    glBindTexture  (GL_TEXTURE_2D, tmpTex);
    glTexImage2D   (GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    GLuint FBO=0;
    glGenFramebuffers(1, &FBO);
    glBindFramebuffer(GL_FRAMEBUFFER, FBO);
    glBindTexture(GL_TEXTURE_2D, tmpTex);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 0, GL_TEXTURE_2D, tmpTex, 0);

    glViewport      (0, 0, (GLint)w, (GLint)h);


    glBindTexture  (GL_TEXTURE_2D, tmpTex);

    glReadPixels(0,0, w-1,h-1, GL_RGB, GL_UNSIGNED_BYTE, dst->GetData());
    glBindFramebuffer   (GL_FRAMEBUFFER, 0);
    glActiveTexture     (GL_TEXTURE0);
    glDeleteFramebuffers(1, &FBO);
    glDeleteTextures    (1, &tmpTex);

    return this;    

Note, that the function calld with “Render2D(MipMapMode);” obviously works. because it is used for other renderings also.

Any ideas what the problem might be?

Kind regards,

Try setting the pack and unpack alignments to 1 with glPixelStore(). The default alignment is 4 bytes.

GL_UNPACK_ALIGNMENT affects glTexImage2D(), while GL_PACK_ALIGNMENT affects glReadPixels().