PDA

View Full Version : The meaning of the 0.375 pixel translation



rombust
11-17-2008, 05:07 AM
The opengl red book refers to a "magic number" 0.375

(Quote from: )
http://glprogramming.com/red/appendixg.html#name1
or http://msdn.microsoft.com/en-us/library/ms537007(VS.85).aspx


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 sample. 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 */

I have had a look to find out what the magic number "0.375" actually is.

After a lot of searching, the only suggestion that I can think of is that it has something to do with floating point to integer rounding errors.

Does anyone know why it is 0.375? (and not 0.374 or 0.376 etc)

ZbuffeR
11-17-2008, 06:50 AM
0.250 + 0.125

arekkusu
11-17-2008, 10:36 AM
It's explained right there in the first sentence-- it's a compromise, not a correct solution.

A correct solution is to specify points and lines on pixel centers, so you should bias by 0.5, 0.5. Filled primitives like triangles should not be biased at all. This means you need to shift your geometry or a matrix around, depending on the primitive type you're drawing. The compromise avoids that, at the cost of slightly offset rendering (which is more visible if you use any form of antialiasing.)

rombust
11-18-2008, 12:08 AM
Thank you very much, that makes sense.

Mark.