Pixel color search

I am trying to figure out how to extract an RGB value from a selected pixel. Each time I click, it gives me a value of 0, although I click 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
source share
1 answer

I don’t think you should convert the pixel coordinates to world coordinates. I do not know GL, but it does not look right. Do you get pixel coordinates from the mouse? And you want the pixel color value from the framebuffer .

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

, () . glReadPixels.

+4

Source: https://habr.com/ru/post/1768388/


All Articles