Results 1 to 5 of 5

Thread: Need help using glReadPixels to read framebuffer

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2000
    Location
    Springfield, VA, U.S.A.
    Posts
    8

    Need help using glReadPixels to read framebuffer

    Can someone who has used glReadPixels help?

    I need some suggestions for what's wrong with the way I'm using glReadPixels or a code snippet showing how to read RGB values from the framebuffer.

    Here's the situtation:

    1. I'm using double buffering and rendering a gray object that completely fills my GLUT window.
    2. Material reflectance values are RGBA = (0.55, 0.55, 0.55, 1.0).
    3. I am trying to extract the RGB values of pixels in the framebuffer. I should get (0.55, 0.55, 0.55) for RGB, or (140, 140, 140) since I'm using an unsigned byte to store each R, G and B value and float values between 0 and 1 are scaled from 0 to 255.

    Instead, the unsigned byte array I pass to glReadPixels appears to be unchanged after the call to glReadPixels.

    The block of code that I'm using is:

    // X location in pixels (relative to the screen origin) of the current window.

    int x = glutGet(GLUT_WINDOW_X);

    // Y location in pixels (relative to the screen origin) of the current window.

    int y = glutGet(GLUT_WINDOW_Y);

    const size_t width = glutGet(GLUT_WINDOW_WIDTH);
    const size_t height = glutGet(GLUT_WINDOW_HEIGHT);

    // Number of bytes in array to hold RGB values
    const size_t n_values = width*height*3;

    GLubyte * pixels = new GLubyte [n_values];

    size_t i;

    // Initialize all values to 0
    for (i = 0; i < n_values; i++)
    {
    pixels[i] = 0;
    }

    glReadBuffer(GL_FRONT_LEFT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelStorei(GL_PACK_ALIGNMENT, 1);

    glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

    // PROBLEM: At this point the pixels array still has zeros


    Vishnu

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Need help using glReadPixels to read framebuffer

    Hello

    Think I know what's wrong here... The first four arguments in glReadPixels() is relative to the window, not the screen. If you window is placed at (100,100) and size is (50,50), you will have to call glReadPixels() as glReadPixels(0,0,50,50...), not glReadPixels(100,100,50,50...)

    Bob

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2000
    Location
    Springfield, VA, U.S.A.
    Posts
    8

    Re: Need help using glReadPixels to read framebuffer

    I modified the first two parameters to glReadPixel. The call is now:

    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

    Now I get R=G=B=128 for all pixels. Shouldn't it be 140 for r=g=b=0.55?

    Also, the content of the pixel array set by glReadPixels doesn't change even when I change the color of the object rendered in the GLUT window.

    For example, I would expect that when I make the object filling the Glut window Red=1.0, Green=Blue= 0.0, the first byte in the pixels array should be 255, the second and third 0. But they are all 128.

    Vishnu

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: Need help using glReadPixels to read framebuffer

    Hello again

    Hmm, has been thinking about this for a while now, and I can't see anything wrong. I suggest you try to find some code on the net and have a look at it.

    Bob

  5. #5
    Senior Member Regular Contributor
    Join Date
    Feb 2000
    Location
    milano, italy
    Posts
    282

    Re: Need help using glReadPixels to read framebuffer

    snippet from a function i currently use to grab the framebuffer:

    byte *pixmap=(byte *)malloc(width*height*3);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glReadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_B YTE,pixmap);

    if it don't work, then the problem should be elsewhere...

    instead of using glReadBuffe(GL_FRONT_LEFT) use GL_FRONT: specs state that there is no difference, but who knows... moreover, it's more readable this way.

    oh, unless you're performing stereo rendering

    Dolo/\/\ightY
    Dolo/\/\ightY

Similar Threads

  1. Fail to Read From FrameBuffer
    By JohnHutch in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 11-11-2014, 11:54 AM
  2. Can shader read from target framebuffer?
    By kitfox in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 04-11-2012, 01:28 PM
  3. Read the framebuffer during writing?
    By rapheal in forum OpenGL: GLSL
    Replies: 8
    Last Post: 02-27-2009, 09:15 AM
  4. A fast way to read data from the framebuffer
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-16-2004, 01:37 AM
  5. how can read bmp to framebuffer?
    By kim1980 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-15-2001, 02:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean