picking in 3d enviroment

i have 27 cubes arranged in 3x3x3.
i don’t understand what code i need, to be able to get a cube to do something when clicked on.


This is a tutorial in a very popular technique:

Another approach renders objects of different colors into the back buffer, then reads the pixel at the mouse position back to determine the selection. This is the so-called “color id” approach.

Yet another approach uses geometrical line-segment intersections with world objects to determine the selection.