I've been converting my own personal OGLES 2.0 framework to take advantage of the functionality added by the new iOS 5 framework GLKit
.
After pleasing results, I now wish to implement the colour-based picking mechanism described here. For this, you must access the back buffer to retrieve a touched pixel RGBA value, which is then used as a unique identifier for a vertex/primitive/display object. Of course, this requires temporary unique coloring of all vertices/primitives/display objects.
I have two questions, and I'd be very grateful for assistance with either:
I have access to a
GLKViewController
,GLKView
,CAEAGLLayer
(of theGLKView
) and anEAGLContext
. I also have access to all OGLES 2.0 buffer related commands. How do I combine these to identify the color of a pixel in the EAGLContext I'm tapping on-screen?Given that I'm using Vertex Buffer Objects to do my rendering, is there a neat way to override the colour provided to my vertex shader which firstly doesn't involve modifying buffered vertex (colour) attributes, and secondly doesn't involve the addition of an IF statement into the vertex shader?
I assume the answer to (2) is "no", but for reasons of performance and non-arduous code revamping I thought it wise to check with someone more experienced.
Any suggestions would be gratefully received. Thank you for your time
UPDATE
Well I now know how to read pixel data from the active frame buffer using glReadPixels. So I guess I just have to do the special "unique colours" render to the back buffer, briefly switch to it and read pixels, then switch back. This will inevitably create a visual flicker, but I guess it's the easiest way; certainly quicker (and more sensible) than creating a CGImageContextRef
from a screen snapshot and analyzing that way.
Still, any tips as regards the back buffer would be much appreciated.
Well, I've worked out exactly how to do this as concisely as possible. Below I explain how to achieve this and list all the code required :)
In order to allow touch interaction to select a pixel, first add a
UITapGestureRecognizer
to yourGLKViewController
subclass (assuming you want tap-to-select-pixel), with the following target method inside that class. You must make yourGLKViewController
subclass aUIGestureRecognizerDelegate
:After instantiating your gesture recognizer, add it to the
view
property (which inGLKViewController
is actually aGLKView
):Set the target action for your gesture recognizer; you can do this when creating it using a particular
init...
however I created mine using Storyboard (aka "the new Interface Builder in Xcode 4.2") and wired it up that way.Anyway, here's my target action for the tap gesture recognizer:
The pick method called in there is one I've defined inside my
GLKViewController
subclass:This takes advantage of a handy new method
snapshot
that Apple kindly included inGLKView
to produce aUIImage
from the underlyingEAGLContext
.What's important to note is a comment in the
snapshot
API documentation, which states:This gave me a clue as to why my earlier attempts to invoke
glReadPixels
in attempts to access pixel data generated anEXC_BAD_ACCESS
, and the indicator that sent me down the right path instead.You'll notice in my
pickAtX:Y:
method defined a moment ago I call apickPixelAtX:Y:
on theUIImage
. This is a method I added toUIImage
in a custom category:Here is the implementation; it's the final code listing required. The code came from this question and has been amended according to the answer received there:
I originally tried some related code found in an Apple API doc entitled: "Getting the pixel data from a CGImage context" which required 2 method definitions instead of this 1, but much more code is required and there is data of type
void *
for which I was unable to implement the correct interpretation.That's it! Add this code to your project, then upon tapping a pixel it will output it in the form:
Of course, you should write some means of extracting those RGBA int values (which will be in the range 0 - 255) and using them however you want. One approach is to return a
UIColor
from the above method, instantiated like so: