why does glOrtho + GL_LINE_LOOP lead to missing pixels at the corner?

Hi Folks,

i try to draw some squares using glBegin(GL_LINE_LOOP).
Strange thing is that theres always one lost pixel at one of the corners.
As you can see on the example code below, i’ve setup glOrtho/glViewport as mentioned at “avoiding 16 common OpenGL pitfalls” so i realy wonder how this could happen.
Please let me know if you have an idea why the cornerpixels get lost.

Thank’s in advance!

#define XRES 640
#define YRES 480
#define CX (XRES/2)
#define CY (XRES/2)

void DrawSomeSquares(void)
{
int i;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
glDisable(GL_TEXTURE_2D);
glDisable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glViewport(0,0,XRES, YRES);
glOrtho(0,XRES,0,YRES,-1,1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

for (i=0; i<100; i++)
{
glBegin(GL_LINE_LOOP);
    glColor4f(0.0f, 0.8f, 0.0f, 1.0f);
        glVertex2i( CX-i*5, CY-i*5);
        glVertex2i( CX-i*5, CY+i*5);
        glVertex2i( CX+i*5, CY+i*5);
        glVertex2i( CX+i*5, CY-i*5);
    glEnd();
}

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glDisable (GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glFlush ();

}

Because with that projection matrix, on integer coordinated you find pixel edges. If you want to hit the pixel center with integer coordinates, you need to offset the projection matrix by -0.5.

When you hit a pixel edge (actually, it’s the corner where four pixels meet), small roundoff errors may cause the line drawing algorithm to choose wrong pixel of the four.

Hi Bob,
thaks for the quick reply!
Now that i’ve ‘glTranslated’ it anything works fine!

Thanks again!

[This message has been edited by XFire (edited 09-19-2003).]