pixel perfect rendering in 2d

The problem I have is that when the rendering has to be scaled because a different screen mode than the optimal one is used, the 2d result isn’t pixel perfect. It’s not noticeable when moving around at high speeds, but when going slowly sharp lines in textures appear and disappear by every small incremental movement.

ortho is set to 1280x960, if the actual screen resolution is the same, everything renders pixel perfect (I only allow integer rendering positions).

any other resolution the rendered image is scaled on the fly, I guess a solution would be to set ortho same as screen res, and then multiply every position, width etc with screenWith/1280 (and likewise for y). And only allow for integer positions…

It’s much neater to just use glOrtho to have it appear the same… any ideas for making it pixel perfect when using the same ortho for all resolutions? (The effect occurs due to pixels not appearing on integer coordinates)

Most GUIs seem to use a layout manager of some kind to arrange components in the window - so your widgets are the same pixel size at any resolution, but get moved around as you suggest to fill the available space properly.

Well, in this case it’s a game screen, and you should see the same view of the world on any screen resolution chosen, the only difference is that the details in the textures are higher at higher screen res.

Anyways, I set the ortho to the same as screen res and just did the multiplication as I mentioned, it was very easy to change the code to that, and now the scaling effects are gone :slight_smile:

So problem is solved, and everyone is happy 8)

I read this topic and immediately the figure 0.375 popped into my head. Reading appendix H of the Red book has revealed why:

[quote]If exact two-dimensional rasterization is desired, you must carefully specify both the orthographic projection and the vertices of primitives that are to be rasterized. The orthographic projection should be specified with integer coordinates, as shown in the following example:

gluOrtho2D(0, width, 0, height);

where width and height are the dimensions of the viewport. Given this projection matrix, polygon vertices and pixel image positions should be placed at integer coordinates to rasterize predictably. For example, glRecti(0, 0, 1, 1) reliably fills the lower left pixel of the viewport, and glRasterPos2i(0, 0) reliably positions an unzoomed image at the lower left of the viewport. Point vertices, line vertices, and bitmap positions should be placed at half-integer locations, however. For example, a line drawn from (x1, 0.5) to (x2, 0.5) will be reliably rendered along the bottom row of pixels int the viewport, and a point drawn at (0.5, 0.5) will reliably fill the same pixel as glRecti(0, 0, 1, 1).

An optimum compromise that allows all primitives to be specified at integer positions, while still ensuring predictable rasterization, is to translate x and y by 0.375, as shown in the following code fragment. Such a translation keeps polygon and pixel image edges safely away from the centers of pixels, while moving line vertices close enough to the pixel centers.

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.0);
/* render all primitives at integer positions */
[/quote]
Does this help in your situation?