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.

  • 1
    completely off topic: have more self confidence! – Ali1S232 Sep 21 '11 at 23:50
  • (not big enough for a full answer, so I leave a comment) You can use two projections and draw your scene in two passes. Just don't erase or flood fill the back buffer between passes. – Patrick Hughes Sep 22 '11 at 1:01
  • 1
    This is easy enough to do. If it's not well answered when I get home from work, I'll do a big write up for you. – Jordaan Mylonas Sep 22 '11 at 1:16
  • Nathans answer actually makes sense to me and it was what I was thinking all along, I just thought perhaps there was a better way. But please by all means do a big write up for me! lol the more information the better! – user6484 Sep 22 '11 at 1:29
  • 1
    @Ascension, it's a policy on SE sites to make questions more useful. The parts I removed were irrelevant to the core of your question. meta.stackexchange.com/questions/2950/… – Tetrad Sep 22 '11 at 5:32
up vote 2 down vote accepted

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.