OpenGL ES 2.0 Object Picking on iOS (Using Color Coding)

六月ゝ 毕业季﹏ 提交于 2019-12-04 18:31:35

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.

+ (void) ProcessColourPick : (GLubyte*) out : (Float32) x : (Float32) y
{
    GLint viewport[4];
    //Get size of screen
    glGetIntegerv(GL_VIEWPORT,viewport);

    GLubyte pixel[4];
    //Read pixel from a specific point
    glReadPixels(x,viewport[3] - y,1,1,
             GL_RGBA,GL_UNSIGNED_BYTE,(void *)pixel);

    out[0] = pixel[0];
    out[1] = pixel[1];
    out[2] = pixel[2];
    out[3] = pixel[3];
}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!