Results 1 to 3 of 3

Thread: Reading the color of a single pixel with glReadPixels

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2005
    Location
    Westfield State College, Westfield, MA
    Posts
    2

    Reading the color of a single pixel with glReadPixels

    I have to write a C function that finds the color of a single pixel at (x,y). I'm using gcc from the Cygwin distribution running on WinXP with 32-bit color, if that matters. I've read the documentation of glReadPixels and the best I've come up with is:

    void getPixel(int x, int y, int* color) {
    glReadPixels(x,y,1,1,GL_RGBA,
    GL_UNSIGNED_INT_8_8_8_8,color);
    }

    I must be really stupid about something, because this doesn't work. Any suggestions? Larry

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,450

    Re: Reading the color of a single pixel with glReadPixels

    I would do this:

    GLubyte rgba[4];

    void getPixel(int x, int y, GLubyte *color) {
    glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
    }

    getPixel(x,y, rgba);

    Mind that OpenGL's coordinates are bottom-left origin. If you use this with windows (mouse) coordinates, you need to invert y by doing

    getPixel(x, viewportHeight - 1 - y, rgba);

    Stating the obvious, you need to have an OpenGL context current.
    Make sure your glReadBuffer() points to the right buffer! For double buffered pixelformats, glDrawBuffer and glReadBuffer point to GL_BACK(_LEFT) per default. After SwapBuffers(), consider the backbuffer contents to be undefined if the pixelformatdescriptor has the PFD_SWAP_EXCHANGE flag set, that is, read before the SwapBuffers or read from front after the swap.
    Areas which are overlapped by other windows, will return undefined results since they fail the pixel ownership tests.
    If your pixelformat has no alpha bits, the above code will always return 1.0 (0xFF) for the alpha channel.
    Pixel pack alignment affects the call. The default is 4 which will just work fine here since you write four bytes.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2005
    Location
    Westfield State College, Westfield, MA
    Posts
    2

    Re: Reading the color of a single pixel with glReadPixels

    OK, I finally tracked this down to a mistake in
    setting up the viewing transformation in the
    main program. Mea culpa and thanks for the help
    I received.

Similar Threads

  1. Stencil bitplanes/glReadPixels() - pixel per byte or pixel per bit ?
    By Jon Mc Jon in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-09-2006, 08:24 PM
  2. reading single channel only
    By Jay2 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 10-06-2003, 09:11 AM
  3. trouble reading depth value at a single pixel
    By madmanchan in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 08-08-2003, 05:01 AM
  4. how to use glReadPixels for a single pixel
    By pjhancoc in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-18-2003, 08:33 AM
  5. Replies: 1
    Last Post: 08-16-2001, 02:40 PM

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