Ortho Bounds

Ugh, this has been driving me insane. I always use glOrtho() to set up the projection matrix for my 2D drawing section, and usually it’s something along the lines of:

glOrtho(0.0, 640.0, 480.0, 0.0, -1.0, 1.0);

So I can have it fit rather uniformly in my 640x480 viewport. Now, is that TL-BR (0,0)-(640,480) inclusive or exclusive or is it not that at all? I’ve noticed if I do a GL_QUAD with vertices that have X coordinates of 1.0 there is a pixels width of black along the edge, but with GL_LINE_LOOP an X coordinate of 1.0 will be the very first pixel. Quite disconcerting, disruptive, and disorienting.

I guess my question is, are the boundaries passed to glOrtho() inclusive, and if so, am I going the right way to get a matrix set up where there will be a 1:1 ratio of coordinates to pixels in the viewport?

There’s a small problem with pixel center vs coordinates here. (0,0) is the top left corner of the window (using that ortho-projection), but the center of the top left pixel has the coordinate (0.5, 0.5). Have a look at this “picture”.

0 1 2 3
0 ---------------
| | | |
| x | x | x |
| | | |
1 |—|—|—|–
| | | |
| x | x | x |
| | | |
2 |—|—|—|–
| | | |

Each square is a pixel, and x means pixel center.
The numbers indicate the coordinate for each point.

You set the top left corner to (0,0), but note, the top left corner is the corner of the pixel, not the center. The coordinate (1,1) is in the corner of four pixels, and since different techniques is used to draw lines and triangles, this coordinate might acrually hit different pixels when rasterized. To hit the center of the pixel, you must use the cooridnate (x+0.5, y+0.5).

But there’s another problem here too, if you want “exact” pixel coordinates. If you have 640 pixels in one direction, you need to specify the coordinates 0 to 639. Between 0 and 640, there’s 641 different integer values, which you spread over 640 pixels. If you don’t get it, consider this: between 0 and 5, there’s 0, 1, 2, 3, 4 and 5, i.e. 6 numbers.

That, and the offset by one half, you should get something like this: glOrtho(-0.5, 639.5, 479.5, -0.5, -1, 1). Now you can use (0,0) to hit the top left pixel in the center, and (639,479) to hit the bottom right pixel in ther center. I haven’t tried it, but I think that’s the way it is.

Ooooh. The .5 pixel thing makes sense. I’ll try implementing that and see what happens.