Question: Last 2 weeks I started working on a box selection that selects shapes using GL\_SELECT and I just got it working finally.

When looking up resources online, there is a significant number of posts that say GL\_SELECT is deprecated in OpenGL 3.0, but there is no mention of what had replace that function.

I learnt OpenGL 1.2 in back in college 2 years back but checking wikipedia now, I realise we already have OpenGL 4.0 but I am unaware of what I need to do to keep myself up to date.

So, in the meantime, what would be the latest preferred method for box selection?

EDIT: I found <http://www.khronos.org/files/opengl-quick-reference-card.pdf> on page 5 this card still lists glRenderMode(GL\_SELECT) as part of the OpenGL 3.2 reference.

Answer: 
The way I do picking in core OpenGL is to assign a unique color to every object in the world and draw only the vertices (no texcoords, normals, etc.) with the assigned color to a [FBO](http://www.opengl.org/wiki/Framebuffer_Object).

Once all the objects have been drawn, you can call [glReadPixels](http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml) at the mouse coordinate with a width and height of 1 to get the pixel color at that point. Then all you have to do is lookup which object was assigned to that color and return it.

If you want to drag-select a rectangle, do the same thing, but store the pressed mouse coordinate and change the parameters of glReadPixels to read back the selected rectangle between the pressed mouse coordinate and the released mouse coordinate.
In modern OpenGL you shouldn't use GL\_SELECT for objects picking since OpenGL is an API for hardware accelarated graphics drawing. But there are other methods which you can use.

As @Robert wrote, you can use color selection in order to pick objects on the screen. Another thing you can do is to cast a ray from the mouse cursor to the game world and check for collisions between the ray and the world's geometry.

To construct the ray, you will need to get the mouse cursor's position in world space. That position will be of use to you in order to calculate the ray's direction. The ray's origin is the position of your camera. Now that you have the ray, you can make an intersection test between the ray and the geometry you are trying to pick. If the test succeeds, then you have a clicked object, else you don't have one.

The code concerning the ray's construction is the following:

```
Ray Mouse::GetPickRay(mat4 projMat, mat4 modelMat, 
                      vec4 cameraPos, 
                      int windowWidth, int windowHeight)
{
    float mouseX = float(this->currentPosition.x);
    float mouseY = float(this->currentPosition.y);

    vec4 mousePos_clipSpace = vec4(
                                  ((mouseX * 2.0f) / windowWidth) - 1.0f,
                                  (1.0f - (2.0f * mouseY) / windowHeight), 
                                  0.0f,
                                  1.0f);

    vec4 mousePos_viewSpace = inverse(projMat) * mousePos_clipSpace;

    mousePos_viewSpace = mousePos_viewSpace / mousePos_viewSpace.w;

    vec4 mousePos_worldSpace = inverse(modelMat) * mousePos_viewSpace;

    vec4 rayDirection = normalize(mousePos_worldSpace - cameraPos);
    vec4 rayOrigin = cameraPos;

    return Ray(rayOrigin, rayDirection);
}

```

[This article](http://trac.bookofhook.com/bookofhook/trac.cgi/wiki/MousePicking) describes very well the logic behind the unprojection of the mouse cursor's coordinates and the creation of the ray.

The only thing that's left is to check for intersections between the ray and the world's geometry. You can find various algorithms on the Internet about how to do that.