# Exact 2D pixel coordinates

## Recommended Posts

TrueTom    110
What is the correct way to calculate pixel coordinates in 2D? For example drawing a box at 0,0-9,9 which should be 10 by 10 pixels?
glViewport(0,0,Width,Height);

glMatrixMode(GL_PROJECTION);

glOrtho(0, Width-1,Height-1,0.0f,-1.0f,1.0f);


This code should draw a triangle but fails to produce an one pixel per line increase:
glBegin(GL_QUADS);

for(int i = 0;i < 480;i++)
{
glColor3f(1.0, 0.0, 0.0);
glVertex2f(i, 0);

glColor3f(1.0, 0.0, 0.0);
glVertex2f(i+1, 0);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(i+1, i);

glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(i, i);
}

glEnd();


I googled a bit and many people seem to have the same problem but without a satisfying solution. For example adding some small jitter which I tried but doesn't work reliable in all situations. The problem seems to be some rounding errors.

##### Share on other sites
Brother Bob    10344
You shouldn't have -1 after width and height when setting the orthographic projection.

##### Share on other sites
TrueTom    110
Quote:
 Original post by Brother BobYou shouldn't have -1 after width and height when setting the orthographic projection.

Yes, that's right :)

I figured it out while using

glOrtho(0, 1, 1, 0, -1, 1);

and using coordinates from 0 to 1. (Which should have been wrong with my old thinking ;) )