PDA

View Full Version : Getting pixel color !



09-30-2002, 01:37 PM
How can i get pixel color?.
I've try to get with glReadPixels and glReadBuffer but i did'nt success.
Please help me..

Bob
09-30-2002, 01:58 PM
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.

09-30-2002, 02:22 PM
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?

09-30-2002, 02:25 PM
sorry i mean RGB.(not RGBA)

*Aaron*
09-30-2002, 04:07 PM
Check your pixel storage mode (set with glPixelStore*).

09-30-2002, 04:59 PM
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...

Deiussum
10-01-2002, 04:15 AM
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.

*Aaron*
10-01-2002, 06:00 AM
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.

Bob
10-01-2002, 06:39 AM
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.

*Aaron*
10-01-2002, 08:21 AM
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.