Mouse coordinates into World coordinates

I'm trying to draw a quad on a 3D terrain under the mouse. To do this, I need to convert the mouse's window coordinates to world coordinates somehow. I Google'd the subject and found a bunch of tutorials, but they all talk about gluUnProject and legacy code. I'm using OpenGL 3.1. Is there any way to convert mouse coordinates to world coordinates without using legacy code?

Thanks for the code sample, but I've since figured out how to use glmUnProject and it's giving me the right results. On thing that tripped me up initially, though, was that even when you're rendering solid triangles, if you set the polygon mode to LINES, you only get the right results if you click on a line, and not the empty space in the middle of a polygon.

Regarding a line normal to the screen - wouldn't that only work in 2D space?

Regarding a line normal to the screen - wouldn't that only work in 2D space?

No, You get a real 3D line in MODELVIEW matrix coords, and, with rotation and translation of MODELVIEW, at the time You click the mouse it is normal to the screen.
For selection (picking) I use the pre-built opengl utility, with display lists, but if I select a segment, and I want to know which end of the segment is closer to the point(line) of the click, I use the code above, then I find the intersection point(or near-intersection points) between the segment and the mouse generated line.