Get the value of colors of a pixel on the screen efficiently ...

Hi there,
in Processing, get() grab the value of colors of a particular pixel on the screen.

Is there something like that with OF ?

Of course we can use grabscreen for a portion of the screen but that would require a lot of call in my case of procedural painting with agents (each agent would have to call grabscreen to allocate and image and would have to find the value of colors for this 1x1 image pixel)

Any ideas ?

in processing, depending on the mode, the graphics are rendered in RAM so getting pixels are easier. In opengl, the pixels are on the graphics card and need to be read back, which can be done in different ways. i’m not sure how much you need to read back, but can you try this first:

  
  
unsigned char color[3];  
glReadPixels(x , ofGetHeight() - y , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , color);  
  

where x and y are the pixel positions of the color, and see how slow it is? color is an array which will hold the red (color[0]), green (color[1]) and blue (color[2]) values of the pixel’s color.

the only thing that you have to take care about is that in opengl, the bottom left corner is 0,0, so that’s why we say (ofGetHeight() - y), since it’s coordinate system is reversed from ours.

just tested, it is hardcore.

In this generative piece, I have around 500 agents drawing on the screen and each one have to check the pixel where it is going to draw in the other loop turn.

I guess it is optimizable … but how ?! I don’t know right now.

yop… optimized a bit only with some variable creation moved as global variables etc … seems possible!
posting more about that as soon as I have it working fine!