1

I trying to implement a simple picking function using glReadPixels however when I click on an object that is 1 colour, I get different values back depending on where I clicked on that object? There not special lighting etc? Whats going on? Sometimes all zeros are returned. I turned everything off (textures etc) but still no joy.

I thought this functions returns the colour of the pixel you click on?

- (void)getPixelColour:(CGPoint)point {

    Byte pixelColour[4];
    glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColour);

    NSLog(@"%d %d %d", pixelColour[0], pixelColour[1], pixelColour[2]);

}

Update: added this to fix it:

glGetIntegerv( GL_VIEWPORT, __viewport );   
point.y = (float)__viewport[3] - point.y;
Burf2000
  • 5,001
  • 14
  • 58
  • 117
  • 4
    Is your CGPoint in Touch coordinates? Are you rendering OpenGL upsidedown? Remember that OpenGL renders from the bottom up and touches come in from the top down. – Ed Marty Dec 17 '10 at 01:26
  • Good point, yes cgpoint is the touch point, so I have to invert it somehow? – Burf2000 Dec 17 '10 at 08:04
  • Ed Post your comment as a answer as you helped to fix the issue! – Burf2000 Dec 17 '10 at 09:40

1 Answers1

1

glGetIntegerv( GL_VIEWPORT, _viewport );
point.y = (float)
_viewport[3] - point.y;

Burf2000
  • 5,001
  • 14
  • 58
  • 117