Drawing a framed rectangle

Hi All,

I am struggling with the drawing of a 100x100 rectangle (as lines) that surround a 98x98 rectangle (filled).

I draw this in Ortho2D mode using mouse coordinates.

Guess what? Sometimes the inner rect overlaps to outer (wireframe) one.

How can I achieve a consistent raterization?

I did try what the RedBook suggests without success:

// 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
// gl.Translatef(0.375f, 0.375f, 0.0f);

Thanks in advance,


Forget about this 0.375 crutch, that’s silly and not generally working, e.g. not with antialiasing.

For a 1-1 ortho2D setup the corners of the frustum need to be at the lower left corner of the lower left pixel and the upper right corner of the upper right pixel.

Let’s say you have a 640*480 screen then setup the projection like this:

glOrtho(0.0, 640.0, 0.0, 480.0, -1.0, 1.0);
glViewport(0, 0, 640, 480);

With that rendering filled rectangles need to march on the coordinates of pixel corners (0.0, 1.0, 2.0, …), lines and points need to be rendered on coordinates in the center of pixels (0.5, 1.5, 2.5, …)! That’s all.

Example: Draw a 2x2 pixel quad one pixel away from the lower left corner of the window:

glColor3f(0.0f, 0.0f, 1.0f); // Let it be blue
glBegin(GL_QUADS); // 2x2 pixels
glVertex2f(1.0f, 1.0f)
glVertex2f(2.0f, 1.0f)
glVertex2f(2.0f, 2.0f)
glVertex2f(1.0f, 2.0f)

Now outline that in yellow keeping the 2x2 blue pixels intact:

glColor3f(1.0f, 1.0f, 0.0f); // Let it be yellow.
glVertex2f(0.5f, 0.5f)
glVertex2f(2.5f, 0.5f)
glVertex2f(2.5f, 2.5f)
glVertex2f(0.5f, 2.5f)
glVertex2f(0.5f, 0.5f)

glFlush() or SwapBuffers() depending on pixelformat.

Now look at this with a zoom tool. It should show a 4x4 pixel block, outline in yellow, 2x2 interior in blue.