I’m trying to figure out the ‘best’ way to get a color picking id (unsigned int) through the shader and to a render buffer that I can glReadPixels() from.
Ideally it would work for both mac 10.6 (opengl 2.1) and opengl es 2.0.
Right now I have something like the following.
Vertex shader has:
attribute unsigned int itemId;
flat varying unsigned int itemIdent;
void main() {
…
itemIdent = itemId;
}
The fragment shader seems like the tricky part, potentially I’d use unpackUnorm4x8() but it doesn’t seem available on my platform.
What I have currently is:
flat varying unsigned int itemIdent;
…
void main() {
…
vec4 convertedColorId = vec4(
(itemIdent >> 24u) & 0x0FFu,
(itemIdent >> 16u) & 0x0FFu,
(itemIdent >> 8u) & 0x0FFu,
(itemIdent >> 0u) & 0x0FFu);
gl_FragData[0] = convertedColorId / 255.0;
}
And finally my setup and read from buffer code:
// setup
…
glBindRenderbuffer(GL_RENDERBUFFER, _renderItemId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
…
// reading
GLuint itemId;
glReadPixels(pt.x, pt.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &itemId);
Is this really the best way? It seems convoluted in that I need to convert to and from a float vec4 to do the gl_FragData assignment.
Thanks!