Q:

How to convert pixel coordinates to GL coordinates in 2D space?

I know that if you want to display a sprite on screen (in 2D) you can use glOrtho and essentially make 1 gl unit equal to 1 pixel, so when I plot out the vertices for say a 128x128 image (on a quad), I can define the vertices as -64/64, -64-64, etc and then when I map my texture coords to that quad, the image is displayed at a 1:1 ratio.
However, lets say I wanted to not use glOrtho and wanted to have a perspective view, so I can combine 2D sprites with 3D models and whatnot? I'm at a loss on how to convert/set up the coordinates for the planes/quads I want to draw images to, in a way that will match the resolution of the image. That is, define coordinates or convert them in such a way so that when a 2D sprite is right at the near plane, that sprite is mapped as 128x128 pixels on the screen. 

A:

You can calculate the size of the screen at the near plane by using a little trigonometry:
screen_width_in_world = 2.0 * tan(0.5 * horizontal_fov) * near_clip_distance
screen_height_in_world = 2.0 * tan(0.5 * vertical_fov) * near_clip_distance

That's the size in world coordinate units of the "window" on the near plane that represents the screen.  If you multiply those by 128 / screen_width_in_pixels or 128 / screen_height_in_pixels, you'll get the appropriate world space width and height for your sprite, to make it 128x128 at the near plane (and proportionally smaller as it gets further away).  This assumes the sprite is screen-facing.

