Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


glReadPixels wrong values :(


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 15 October 2012 - 02:42 AM

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.

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 9543

Like
0Likes
Like

Posted 15 October 2012 - 03:28 AM

Have you set up the matrices correctly so that your point ends up at window coordinate (100,100)?

#3 Ashaman73   Crossbones+   -  Reputation: 11963

Like
0Likes
Like

Posted 15 October 2012 - 04:02 AM

Have you checked for errors after calling glReadPixels ?

Ashaman

 

Gnoblins: Website - Facebook - Twitter - Youtube - Steam Greenlit - IndieDB - Gamedev Log


#4 Kevn   Members   -  Reputation: 163

Like
0Likes
Like

Posted 15 October 2012 - 04:50 AM

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, 15 October 2012 - 04:51 AM.


#5 Brother Bob   Moderators   -  Reputation: 9543

Like
1Likes
Like

Posted 15 October 2012 - 05:56 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS