It's one thing to get the graphics aligned perfectly, it's another to do this for every single resolution and not take too much time and/or make the code unreadable due to size. Games like Battlefield 3 and Minecraft seem to manage this. But what do they do to keep things from stretching or going off the screen? I don't know any algorithms to do this. I'd like some help on this topic. I've always programmed games that only handle a single resolution, so help would be appreciate.
1 Answer
The standard 3D pipeline is completely agnostic to screen resolution. For most projection matrix algorithms, including the "standard" ones in GL/D3D, an aspect ratio is part of the matrix allowing the projection to properly cope with different "shapes" of resolution. Just be sure to pass in the current resolution to the viewport setting routines calls and the current aspect ratio to the projection matrix routines and 3D is more or less automatically resolution-independent. The 3D projection/math all works in a normalized coordinate system where the upper-left corner of your screen is (-1,-1)
and the bottom-right is (+1,+1)
. This is then mapped to the viewport which is defined by the resolution of the area you're drawing to (typically the whole client window), which is basically just the hardware mapping those NDC ranges to pixel coordinates. So long as the viewport's aspect ratio matches the one in the projection matrix the image won't be stretched; otherwise the projection matrix might be mapping 3D coordinates to a differently-shaped rectangle than what's actually being draw to.
For the 2D elements, these are specified independently of the screen. E.g. a compass in the upper right is located with its upper-right corner at (0.95, 0.05)
or some such, and its size can be specified to be a ratio of the vertical or horizontal resolution as most appropriate. The GUI might be specified in pixels which is mapped back to those ratios for GPU rendering (which then maps them back to pixels for final rasterization). Point is, just don't use fixed offsets for 2D elements, rather use relative coordinates and sizes to the corners/sides of the viewport and then the 2D elements will properly place themselves no matter what the resolution or aspect ratio is.
With very very little effort a game can Just Work(tm) at any resolution.
-
1\$\begingroup\$ Thats not changing the fact, that you either have to stretch or showing more or less of the world either vertically or horizontally, i. e. in split screen. Since stretching is not an option in most cases, since it screws up your art work, I'd say its a big issue to consider, far from "just works". Since seeing more or less of the world can be game play relevant and touches other relevant topics like performance. \$\endgroup\$ Commented Jun 29, 2013 at 23:39
-
1\$\begingroup\$ For most games... it doesn't matter. Many even give users FOV options to adjust as they like, and most of course work with any resolution. For the others, just use a fixed ratio viewport (letterbox). Either way, the math and rendering does "just work." Whether you want odd viewports or not is an entirely separate problem. \$\endgroup\$ Commented Jun 30, 2013 at 0:37