In Ortho mode - negatives to flipping coordinates

  1. Are there any downsides when flipping the opengl coordinate system?
    glOrtho(0, w, h-1, 0, -1, 1);

And I was having 1 pixel off problems when I drew a line starting from the middle of the window until I used this:
glTranslatef (.375, .375, 0);
2. Am I safe using that?
3. Why does it work?

Ok, now I’m frustrated!

It only works on some sometimes when drawing lines! So I tried:
glOrtho(0, w-.05f, h-1-.05f, 0, -1, 1);
instead of
glTranslatef(.375f, .375f, 0);
I also removed the -1 with both. I still have different off-by-one error with everything!

What is wrong!!!

I must admit I never tried to reverse the axis with than manner. Why are you doing so ? Simply declaring glOrtho with normal arguments, then do the reversing yourself.

I never understood the use of the 0.375 shift in opengl : what do you need it for ?

I need pixel perfect plotting accuracy in my window because I’m using opengl for all of my programs graphics (control drawing) instead of windows gdi.

Do you think the problem is with flipping the glOrtho coords?:
glOrtho(0, w, h-1, 0, -1, 1);
instead of:
glOrtho(0, w, 0, h, -1, 1);

don’t know, when you use the normal setup, does the pixel inaccuracies disapear ? What is your card/OS/drivers ?

When I use the upside down ortho I had to change alot of code but it ?LOOKS? like everything is fixed even without:
glTranslatef(.375f, .375f, 0); Do you think I should use this?

I have Windows with a nVidia card - very fast.
But I’m also concerned about other users. Will it be safer to leave everything upside down and do the flipping coordinates myself?

I made some tests with aligned quads, lines, and points, and I found that the empirical :
glTranslatef(0.375, 0.9, 0.0);

gave no pixel error either with glOrtho(0, width, height,0, 1, -1); or glOrtho(0, width, 0,height, 1, -1);

  1. Where did the .9 come from? Doesn’t .375 work right?

  2. I think I fixed it using .375. But here comes another question that caused my problem. When drawing a line vertex it seems that the run of the line is subtracted one from what it should be. For example, here is my line drawing function:

void DrawLine(aPen & p, int x1, int y1, int x2, int y2)
{
	glColor3ub(p.col.r, p.col.g, p.col.b);
	glBegin(GL_LINES);
	glVertex2i(x1, y1);
	glVertex2i(x2, y2);
	glEnd();
}

If I send it (0,0,2,0) the line only runs across 2 pixels but it should run across 3, ending on index 2. How can I fix it?

  1. glOrtho has RECT arguments (left, right instead of x, w) but we send it x and w coordinates. Why does it work?

  2. Are there any downsides when flipping the opengl coordinate system like I’m doing?

W.r.t. your point 2: that’s the way it’s supposed to work according to the OpenGL specs.
If you want the line to be actually N pixels long when rendered, you’ll need to specify a line that is one pixel longer.

W.r.t point 3:
the only difference between an ortho projection of 0,width and one of -width/2,width/2 is which pixel coordinate 0 will end up in.
The width is still the same.
glOrtho simply takes the bottomleft corner coordinates and the topright corner as arguments (plus depth, which you’re probably not interested in).

One remark about your glOrtho, though:
you’re specifying
glOrtho(0, w, h-1, 0, -1, 1);
Let me repeat that:
you’re specifying
glOrtho(0, w , h-1 ,0,-1,1);

I know, it’s a tiny difference, but your width will be just a little bit squashed compared to your height.

W.r.t. point 4: There should be no downside. The projection matrix is always applied, regardless of the direction.

about 1), I made a slowly evolving gltranslate, and printed offsets when it looked right. The problem with the official 0.375 in both directions, was with GL_POINTS, they were a pixel off vertically, and 0.9 fixed it.

So I should remove the -h? I’m still confused about the glOrtho arguments. Let’s say the client width is 24. If left is 5 and right is 7 then the total ortho width is 7 instead of 2?