This might appear as a related question:
OpenGL ES 2.0 Object Picking on iOS
Which says Color picker is a good solution, and in deed after reading about it:
http://www.lighthouse3d.com/opengl/picking/index.php?color1
It does seem like a very simple solution so this brings me to this question
OpenGL ES color picking on the iPhone
Which unfortunately uses opengl es 1.0, I am trying to do it in 2.0 so I have no access to the functions described in that question.
But the theory seems simple and here is what I think I should do:
On touches begin I render my objects with a unique color.
On touches ended I get the pixel from that position and check it for the color to get my object. (probably with glReadPixels)
The problem is that I dont know how to do the "Render to the back buffer and read from it".
My code so far simply uses "Draw", I suspect I have to do something like glBindthe other buffer but I would appreciate some help.
My Drawing code is like this:
glClearColor(0, 0, 0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set the Projection Matrix
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), 2.0/3.0, 0, 50);
glUseProgram(_programHD);
glBindVertexArrayOES(_vao);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _textureBuffer[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glUniform1i(uniforms[UNIFORM_TEXTURE_HD], 1);
// Drawing starts here //
// Pass the Model View Matrix to Open GL
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix,rotationMatrix);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX_HD], 1, GL_FALSE, _modelViewProjectionMatrix.m);
// Change texture coordinates to draw a different image
glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, offSet.v);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
//glUniform2i(uniforms[TEXTURE_OFFSET], 7, -5);
glUniform2fv(uniforms[TEXTURE_OFFSET_HD], 1, borderHD.v);
glDrawElements(GL_LINE_STRIP, 6, GL_UNSIGNED_SHORT, 0);
glBindVertexArrayOES(0);
glUseProgram(0);
I have stripped the drawing calculations to make it more understandable. The point is I do not see anywhere where I specify to "where" am i drawing.
Thanks for your help.
I've actually just finished implementing a colour picking function into my iPhone game, using openGL ES 2.0, using the lighthouse tutorial funny enough.
You should be drawing to the frame buffer.
If you want to read from the frame buffer, then you're correct in that you want to use glReadPixels. More information is here:
http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml
The only thing that's different from the lighthouse tutorial is that you also want to store the alpha values. Here's a quick function to get the colour of a specific pixel. Feel free to improve it or change it, but it does the job.
Hope this helps.