• Advertisement
Sign in to follow this  

glReadPixels wrong values :(

This topic is 1925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to read the color values of a pixel on the screen, however the values I am getting from glReadPixel are whatever color my glClearColor is.

Making a point:

glColor3f(1.0, 0.4, 0.2);
glBegin(GL_POINTS);
glVertex2i(100 , 100);
glEnd();


code to read pixel color:


glutSwapBuffers();
vector<float> rcolors(3);
glReadPixels(100, 100, 1, 1, GL_RGB, GL_FLOAT, &rcolors[0]);
cout << rcolors[0] << endl
<< rcolors[1] << endl
<< rcolors[2] << endl;


I am able to see the red pixel, but the values returned in cout are my clear color, which is black in my case.

Share this post


Link to post
Share on other sites
Advertisement
my window setup:
[source lang="cpp"]int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(300, 100);
glutInitWindowSize(1200, 800);
glutCreateWindow("Project 1");

glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);

glutDisplayFunc(display);
glutMainLoop();

return 0;
}
[/source]

And no, i haven't checked errors. I don't know how the OpenGL error checking system works. I'll go look that up now. Edited by Kevn

Share this post


Link to post
Share on other sites
If you set up the matrix like that, then the coordinate (100, 100) is on the exact corner between the four pixels at (99,99), (99,100), (100, 99) and (100,100), and which of the four pixels is rendered is up to how integer rounding and floating point errors behave in that particular case. Any of the four pixels could be rendered. You need to offset the projection matrix by one half pixel to ensure that integer coordinates ends up on pixel centers and not pixel corners.

gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
glTranslatef(0.5, 0.5, 0);

Note that this translation is necessary for points and lines, but shall not be used filled primitives such as triangles.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement