OpenGL ES 2.0 Object Picking on iOS (Using Color C

2019-04-15 07:12发布

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.

1条回答
乱世女痞
2楼-- · 2019-04-15 08:00

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.

查看更多
登录 后发表回答