Line vertex positions are not correct

Hi, if i render a line with the following code:

glBegin(GL_LINES);
glVertex2d(50, 10);
glVertex2d(50, 50);
glEnd();

i get an line at position (51, 11) and (51, 50).
Can anyone explain me this behavior?

I tried to substract the half line width but the result isn’t correct, too.

glBegin(GL_LINES);
glVertex2d(50 - 0.5, 10 - 0.5);
glVertex2d(50 - 0.5, 50 - 0.5);
glEnd();

I get the positions (50, 10) and (50, 49). Whats wrong with this?

In general the values you pass to glVertex2d (note: the ‘d’ suffix here is to indicate you pass ‘double’ values instead of floats, not that you are drawing in window coordinates) are in object space coordinates. The location that gets drawn on your screen depends on the current modelview and projection matrices as well as on your viewport setup.

How do you set up modelview, projection and viewport?

The code for resizing looks like this:

void Window::ReSizeGLScene(int iWidth, int iHeight)
{
m_iWidth = iWidth;
m_iHeight = iHeight;

glViewport(0, 0, (GLsizei)iWidth,(GLsizei)iHeight);					
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)iWidth / (GLdouble)iHeight, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

Switching to the “2D-Mode” is done with this code:

void Window::EnableDrawing2D()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, m_iWidth, m_iHeight, 0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

Using glVertex2i or glVertex2d makes no difference.

You can adjust the ortho border values to align the edges of the viewport to specific numerical values, but also understand that rendering is a binary thing. If you’re pixel center is out be epsilon it will not hit the pixel on a polygon edge if exactly aligned, for lines the fill rules are naturally different and it should touch the pixel.

This ‘correct’ way to align this is to adjust the ortho values.

I found this

www.gamedev.net/community/forums/topic.asp?topic_id=447084&whichpage=1&
www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=248917

and added

glTranslated(0.5, 0.5, 0.0);

which works fine, because the center of a line now is located at pixel positions. Certainly I found out that for example

glLineWidth(1.0);

glBegin(GL_LINES);
glVertex2d(300,198);
glVertex2d(350,198);
glEnd();

glBegin(GL_LINE_STRIP);
glVertex2d(300,200);
glVertex2d(350,200);
glEnd();

glBegin(GL_LINE_STRIP);
glVertex2d(300,202);
glVertex2d(350,202);
glVertex2d(350,212);
glEnd();

draws two lines with an width from 50. But the last one has an width from 51. Thats confusing.