tags:

views:

35

answers:

1

I am trying to figure out how to extract the RGB value from the pixel chosen. Every time I click though, it gives me a value of 0, even though I am clicking on a colored triangle.

void mouse(int button, int state, int x, int y) {
    if(state == GLUT_DOWN) {
        float mx = p2w_x(x); // pixel to world coordinates
        float my = p2w_y(y);
        float rgb[3];
        glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, rgb);
        std::cout << rgb[0] << std::endl; // prints 0 for red always
    }
}
+3  A: 

I don't think you should convert the pixel coordinates to world coordinates. I don't know GL, but it looks wrong to me. You get pixel coordinates from the mouse right? And you want a pixel color value, from the framebuffer.

Maybe you could also use an integer type for color value, with GL_UNSIGNED_BYTE and an array of unsigned char rgb[3]; instead. And use GL_RGB8 for format, instead of just plain GL_RGB.

Also see how to set up the pixel format (store) of the frame buffer. This will affect subsequent glReadPixels calls.

Amigable Clark Kant
That's right, use screen buffer coordinates.
alxx