howto select an object?


As beginner i´m searching a way to pick up or select a drawn quad.
I have no idea about getting the corresponding quad when click at X-Y-Position in the displayed scene.

The scene is very simple - just several textured quads with same size along on axis with variable distances between the quads.

To set up a traced ray, detect first collision in the scene and resolving, which object was hit… I´m sure, there must be a already set up and simple function somewhere in opengl´s functionality, isn´t it?

Thanx for your help.

A easy way to implement picking in your program is to draw each entity with a different color in another buffer (using a fbo) and use this buffer to select an object knowing its color.

There is a thing called “selection mode” in OpenGL, however, it is deprecated and shouldn’t be used. Ray-tracing or color picking (what dletouzen described) are better alternatives. You could also use occlusion queries :slight_smile:

I dont´t know the quad color.

But the quads coming out of an stl::vector with its index as quad_id.
There is no need to get back more then this id when click/select on one.

I´m not so used to buffers. Can you give me a hint/link to read more about it regarding this picking problem?
(i´m already googleing :wink: )

here is a good introduction to framebuffer objects:

I dont understand the beginning of your last post… With the “color method” you just have to generate an arbitrary color for each entity and then rendering it in an auxiliary buffer (fbo) (or in 2 pass in the framebuffer). Each entity is colored uniformly with the generated color. You can base the color generation on entities id in your array.

Sorry - It was a bit confusing, because i didn´t understand your previous post well.

Now i get it: For each quad i draw a significant color to the fbo, ask for this color when clicking on the scene, and determine the quad by it´s color in the fbo.

But some open questions remaining, because i don´t get the buffer stuff right:

  • Is a framebuffer like an auxiliary X-Y-matrix overlaying the hole scene? Like the scene gets rendered into this buffer? So if i rotate the quads/scene, the entries at a special position in this single fbo changes regarding the color value of the quad, which is seen behind this fbo-matrix-position?

(autsh my english :wink: )


  • Do I get for each quad its own buffer?

In both cases i don´t get it, how the XY-coors of the ClickEvent are related to the framebuffer or to the polygons i´ve clicked on?

The quads are shown in the display.
I click on it and get the x- and y-coordinate.
And than? I miss something like
Color getValueInFboAt(x,y)
return fboColorAt(x,y);

And btw: Do i really need to use a generated color or is it also possible to write an integer to the buffer. Would be nice to use the quad IDs direcly.

I read your link, but found it as a pro-version. Still searching a home-edition for dummies :wink:

Ok, first, I think you have to understand some basic concepts.

Before the EXT_framebuffer_object was introduced in hardware, OpenGL disposed of only one framebuffer which is the name given to a collection of rendering destinations, like color buffer (displayed on screen), depth buffer (used to the hidden parts removal) and others like stencil buffer, accumulation buffer…

The EXT_framebuffer_object extension provide the ability to create your own framebuffer object (fbo) with attached color buffers (There could be multiple color buffers, see MRT) and depth buffer (see color and depth attachment in the link I gave above).
Color and depth buffers attached to fbo could have different natures: “Render buffer” or “texture”. Typically, you choose a texture for the color buffer if you want to sample (read) it in the end. Otherwise, choose a render buffer (Depth buffer which is rarely read is often a render buffer).
In conclusion, a fbo is like a “custom framebuffer” that you manage independently of the fixed one. All rendering operations possible with the framebuffer provided to opengl by the window system are possible in a fbo and hardware accelerated. So a fbo behaves like an unvisible framebuffer.

This is my conception a framebuffer objects, maybe you would think it is still obscure :slight_smile: , I let people of the opengl community correct me and add precisions. Don’t hesitate to ask more questions.

What I meant was:

Rendering part:

  1. draw your quads the way your are used to, in the framebuffer
  2. redraw your quads in a fbo, with a 2D texture as color attachment and a renderbuffer as depth attachment if needed. As i said draw quads with a uniform and single color.
  3. repeat 1 and 2 each time the quads or camera are moved or the window resized.

Picking part:

  1. when you detect a mouse click in the window, retrieve the coordinates of the clicked point.
  2. Use them as screen coordinates to read a the texture drawn with the previous fbo. Use for example, glGetTexImage.

I don’t know if it is the most efficient way to do this. You can also, not use fbo at all and do the 2) in the framebuffer in a 2nd pass when the mouse is clicked in the window area.