Mouse selection


I would need that when I click somewhere on my render, my program could know where I clicked (more or less), let’s say a kind of selection

I tried to read and understand at the best this

Basically there are three options:

  • selection render mode

  • render each primitive in an unique color

  • generate a pick ray

I opened this 3d to ask you guys, since that page has some years, if all of them are still valid, or there is a new method that is not listed there

Quick answer:
Use the rendering in a unique color technique.

Longer answer:
I wouldn’t use the old selection methods of OpenGL anymore. It isn’ available on all plattforms (MacOS with a 3.2 context, OpenGL ES if you want to port to that), it isn’t so widely used so I don’t think the performance of this has a high priority for driver devs (might be wrong).

Rendering everything in a unique color, read one pixel from the backbuffer, clear the buffer, render your scene normally (so you will never see the colored picking buffer on screen) works very well. You can also do this in a FBO in a lower resolution if you like etc. Quite flexible but also simple solution.

Ray casting involves implementing ray casting :wink: A lot of work and probably slower (there might be special cases where this is a good idea).

Hi Menzel,

thanks for your answer…

Actually I was discarding the unique-color option because I thought something different from what you explained me.

Your suggest looks interesting, but maybe there is a problem: if I am using VBOs is that still possible?

It doesn’t matter how you draw your models as long as you can color each individually.

That can be done with an alternative attribute for per-vertex colors (has the downside of a lot of redundancy) or better just as a uniform in a special fragment shader that just sets each fragment to the given color value. The underlying geometry is irrelevant.

  1. the limit of primitives (or triangles) is 2^(8+8+8) = 16777216?

  2. shall I use really use shader? Because I still dont know how to use them

  1. The limit ob individual objects you want to pick is 2^(8+8+8+8) for 8bit RGBA render targets. If you want to select objects and not individual triangles, that should be sufficient. You can also use render targets with 32bit per channel and multiple render targets if you use shaders… You will run out of memory for your geometry before the ‘picking-space’ gets too small.

  2. You don’t have to use them. Depending on what you also want to do, you might run into limitations of the fixed function pipeline later on…

Unfortunately I need to select individual triangles… how could I do?

You can set an individual color per triangle, either with the fixed-function pipeline, an extra attribute or even rerive a color in the shaders from gl_PrimitiveID. It depends on how you want to draw your stuff and whether you want to use shaders.