title: Using glReadPixels to read value at Tap Point ? OpenGL 2.0 iOS
body: <p>Say I have a square which is defined as follows :</p>
  
  <pre><code>typedef struct {
  float Position[3];
  float Color[4];
  } Vertex;
  
  const Vertex Vertices[] = {
  {{2, 0, 0}, {1, 0, 0, 1}},
  {{4, 0, 0}, {1, 0, 0, 1}},
  {{4, 2, 0}, {1, 0, 0, 1}},
  {{2, 2, 0}, {1, 0, 0, 1}}
  };
  
  const GLubyte Indices[] = {
  0, 1, 2,
  2, 3, 0
  };
  </code></pre>
  
  <p>And I am applying the following projection and modelview matrices :</p>
  
  <pre><code>- (void)update {
  
  //Projection matrix.
  float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
  projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(_projectionAngle), aspect, 4.0f, 10.f);
  self.effect.transform.projectionMatrix = projectionMatrix;
  
  //Modelview matrix.
  modelViewMatrix = GLKMatrix4MakeTranslation(_xTranslation, _yTranslation, -7.0);
  self.effect.transform.modelviewMatrix = modelViewMatrix;
  
  }
  </code></pre>
  
  <p>I now want to read the pixel colour of the object where a user taps on the screen. I am trying glkMathUnproject in combination with glReadPixels as follows, however glReadPixels is returning incorrect colour values for the tap point :</p>
  
  <pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint tapLoc = [touch locationInView:self.view];
  
  bool testResult;
  
  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);
  
  GLKVector3 nearPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-1024)*-1, 0.0), modelViewMatrix, projectionMatrix, &amp;viewport[0] , &amp;testResult);
  
  GLKVector3 farPt = GLKMathUnproject(GLKVector3Make(tapLoc.x, (tapLoc.y-1024)*-1, 1.0), modelViewMatrix, projectionMatrix, &amp;viewport[0] , &amp;testResult);
  
  farPt = GLKVector3Subtract(farPt, nearPt);
  
  GLubyte pixelColor[4];
  glReadPixels(farPt.v[0], farPt.v[1], 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &amp;pixelColor[0]);
  NSLog(@"pixelColor %u %u %u %u", pixelColor[0],pixelColor[1],pixelColor[2], pixelColor[3]);
  
  }
  </code></pre>
  
  <p>Can anyone advise how I can get the pixel colour accurately ?</p>