Where are the pixels of the current scene stored

Hello guys

Where are the pixels of the current scene stored. Basically, I want to know get the pixels information of the current scene such as color, intensity and position.

Depending on how you are drawing your scene the normal case is the pixels end up in the current FrameBuffer.

You can read pixels from the FrameBuffer using glReadPixels…
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/readpixels.html

Be aware that this is not the fastest way to get data, so depending on what you are trying to do it may be worth looking at FBO (FrameBufferObjects) and “drawing to textures”, and then using shaders or other means to do computation on that data.

But for now, for what you need glReadPixels should do.

Also be aware that you may not get the exact same results as are displayed if you have antialiasing enabled (e.g. multisampling) as the same path is not necessarily used to perform the multisample resolve before scan-out as is used to perform the multisample resolve before readback.

So disable antialiasing and you can be reasonably sure you’re going to get the same thing with glReadPixels that you see on the screen.

I wrote this code to get the three colors of the pixels


int BIT_R=8;
		int BIT_G=8;
		int BIT_B=8;
		int WIDTH=2;
		int HEIGHT=2;
		glReadBuffer(GL_BACK);
		GLvoid *imageData = malloc(WIDTH*HEIGHT*(BIT_R+BIT_G+BIT_B)); 
		glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, imageData);

I tried to fetech the array but I don’t know what how the information stored into imageData array
So how can I store this information into the screen using “cout” as :
x y R G B
value value value value value

I tried to fetech the array but I don’t know what how the information stored into imageData array
So how can I store this information into the screen using “cout” as :
x y R G B
value value value value value

You mean, how to display imageData array values?

You can do something like:


int pixelNb = WIDTH*HEIGHT;
for( int i = 0; i< pixelNb; ++i )
{
    cout << hex << imageData[ i ] << imageData[ i + 1 ] << imageData[ i + 2 ] << endl;
}

Assuming imageData is a array of GLubyte values.

I tried the code but I get output like this:
???
???
???

the code that I used


int WIDTH=2;
		int HEIGHT=2;
		glReadBuffer(GL_BACK);
		GLubyte imageData[WIDTH*HEIGHT*4] ; 
		glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, imageData);
		int pixelNb = WIDTH*HEIGHT;
		for( int i = 0; i< pixelNb; ++i )
		{
			cout << hex  << imageData[ i ] << imageData[ i + 1 ] << imageData[ i + 2 ] << endl;
		}

You allocate to much memory though it is not the problem here, it should be:

GLubyte imageData[WIDTHHEIGHT3];

use the hex modifier as I suggested:

cout << hex << imageData[ i ] << imageData[ i + 1 ] << imageData[ i + 2 ] << endl;

still
???
???
???

Look at this:
http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/