views:

383

answers:

1

I'm trying to figure out the best way to pick informations rendered using shaders. A window manager buffer (the window) is used for outputting rendering. On user event I should pick the geometry rendered. Using gluPick is easy, but I'm trying to pick using only shaders.

Flexibility of shaders doesn'y aid me to find a correct solution. I've found the following solutions:

  • Rendering using the system provided buffer (indeed using colors). Use glScissor to limit the rendering on a certain region and use glReadPixels for fetching colors, mapped with geometry identifiers
  • Rendering using a separate framebuffer object. Renderbuffer formats need to defined. Maybe can I use plain integers for putting names?
  • Rendering using a framebuffer object, both for rendering and picking. An additionally renderbuffer object containing rendered names.

It seems that the second solution is the best, since doesn't alterate the window buffers and it's possible to define a custom way to specify geometry names.

Any suggestion?

P.S:

Since in a fragment shader i declare an array of vec4 for defining the fragment data, how I could setup a framebuffer holding an render buffer having only the R component, stored in a sized integer internal format?

Is it possible to link a fragment shader output variable with a specific attached render buffer?

+1  A: 

I am not sure that shaders are really any help here, except maybe as some form of optimization whereby you render to multiple targets and thus are able to generate your picking values and color values in one pass.

If you are doing picking using rasterization, you're always going to be stuck with a glReadPixels() call or equivalent, and you're always going to be basically drawing your scene to a buffer of some sort with picking values.

If you really wanted to get creative and diverge from the norm, you could try using shaders to perform a ray intersection with the shapes in your scene. This would require a good bit of specialized shader development, and it's hard to see the benefit unless you're doing a LOT of picking (maybe a raytracer?).

spurserh
The frequency of the picking operation is a good point. Maybe storing info for picking during the rasterization is a good choice whenever I should pick the object every frame (i.e.: mouse hover); however, picking without rendering color could be enhanced using scissor box.Don'I don't know if raytracing is feasible for me. And I absolutely ignore how to implement a raytracer using shaders!
Luca