What is the best method to select objects that have been drawn in OpenGL ES 2.0 (iOS)?
I am drawing points.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is working prototype of color picking, tested on most old ipads and working well. This is actually some part of project called InCube Chess that one may find in app store. The main code you will see is located in a class derived from GLKViewController like this:
This means you have glkview in it: ((GLKView *)self.view).
Here are also some properties:
Don’t forget to synthesize them in your *.m file.
The idea is that you have chess pieces on your table (or some objects in your 3d scene) and you need to find a piece in your list of pieces by tapping on a screen. That is, you need to convert your 2d screen tap coords (@point in this case) to chess piece instance.
Each piece has its unique id that I call a “seal”. You can allocate the seals from from 1 up to something. Selection function returns piece seal found by tap coords. Then having the seal you can easily find your piece in pieces hash table or array in a way like this:
“sealhash” is a NSMutableDictionary.
Now this is the main selection function. Note, that my glkview is antialised and you can’t use its buffers for color picking. This mean you need to create your own off screen buffer with antialiasing disabled for picking purposes only.
Note that function takes into account display scale (retina or new iPads).
Here is render() function used in function above. Note, that for rendering purposes it clear s the buffer with some background color and for selecting case it makes it black so that you can easily check if you tapped on any piece at all or not.
Next is how we draw the piece.
This is how to use the functions shown above.
This is basically it. You will have very precise picking algorithm that is much better than ray tracing or others.
Here helpers for push/pop matrix things.
Here also glkview setup/cleanup functions that I used.
Hope this helps and may be closes “the picking question” forever 🙂