Getting pixel color !

How can i get pixel color?.
I’ve try to get with glReadPixels and glReadBuffer but i did’nt success.
Please help me…

glReadPixels is what you need. Show us what you do with it, cause if it doesn’t work, you’re doing something wrong with it.

Here is the simple example:

GLfloat c[3];
reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}

display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.850,0,0);

glBegin(GL_QUADS);
glVertex2f(10,10);
glVertex2f(10,50);
glVertex2f(50,50);
glVertex2f(50,10);

glEnd();

glReadBuffer(GL_FRONT);
glReadPixels(50,50,1,1,GL_RGBA,GL_FLOAT,c);

glutSwapBuffers();

}

In this example i want to get only one pixel color.That is lower left corner of quad.
But i get this value on c.
r g b
0.945 0.843 0.94354

why i can not take right value?

sorry i mean RGB.(not RGBA)

Check your pixel storage mode (set with glPixelStore*).

I’ve try all the mode but i can’t get right values.What i have to use?

GL_PACK_LSB_FIRST,
GL_PACK_ALIGNMENT…

Is there anybody who will write simple executable code for me?That is very important for me.
Thanks…

glReadBuffer(GL_FRONT);
glReadPixels(50,50,1,1,GL_RGBA,GL_FLOAT,c);

glutSwapBuffers();

Are you trying to get the pixel of the scene previously drawn here? Because that’s what you’re going to get. Until you call glutSwapBuffers, your scene is in the back buffer, but you said to read from the front buffer.

Call glPixelStorei(GL_PACK_ALIGNMENT, 1). The default for this parameter is 4, which means it will always write a multiple of 4 bytes to each row. In the example you gave, this would write beyond the end of the array c.

The default for this parameter is 4, which means it will always write a multiple of 4 bytes to each row.

No, it means that each row starts on 4 byte boundary. If you read, say, 5 bytes per row, you have 3 unused bytes in the end of the row.

You’re right. Well, technically PACK_ALIGNMENT=4 doesn’t mean that each row will start on a 4-byte boundary; it means that each row will start on a 4-byte boundary relative to the pointer passed to glReadPixels. If you read more than one row, and you’re not expecting this, it will write beyond the end of the array. But with reading just one pixel, this certainly wouldn’t be a problem. Sorry. I think the problem is probably the buffer swapping thing.

I usually set the PACK_ALIGNMENT to 1 anyways to avoid confusion and simplify my code.