orthographic projection

Started by Uli, December 14, 2003, 20:35:02

Previous topic - Next topic

Uli

If I use
GL.glViewport(0, 0, 1024, 768);
GL.glOrtho(0f,1024.0f, 0f, 768.0f, 0.0f, -100.0f);
points at x=0 are not drawn.

I instead I use
GL.glOrtho(-1f,1023.0f, 0f, 768.0f, 0.0, -100.0);
points at x=0 are drawn.

Seems like I'm to stupid or something. Could someone please explain how this works?

elias

Maybe you should give some slack in the z direction. As far as I can tell, you're using the interval [0;100] for the z clip planes, and if I'm not mistaken, your point is lying the z=0 plane as well. Try -50;50 or move the point slightly.

- elias

princec

Normally one uses [-1.0f, 1.0f] as the interval for Z in orthographic projection. At least that's what the GLU function does I believe.

Cas :)

cfmdobbie

Quote from: "princec"At least that's what the GLU function does I believe.

Yep, absolutely correct.  Hardly worth them providing that function, really. :?


But that's not your problem, is it?  You're drawing a point at e.g. (0, 50) and you're not seeing it.  Well, the trick is that whole-numbered coordinates in OpenGL exist between pixels, not on the centre of pixels.  (It may seem a bit odd at first, but makes perfect sense once you think about it.)  Your vertex is likely being clipped away by the frustum culler, or things are being rounded in a way you don't expect.

What you need to do is perturb your coordinates by half a pixel, so to colour in the bottom left pixel on a screen defined as gluOrtho2D(0, 1024, 0, 768), you need to draw your vertex at (0.5f, 0.5f).

There is a nasty hack presented in the RedBook to allow for nicer code when drawing 2D primitives which I believe is to translate the scene by (0.375f, 0.375f) before drawing anything.  But quite frankly it's much better just getting used to the way OpenGL operates rather than trying to work against it.
ellomynameis Charlie Dobbie.

Uli

This is confusing. Adding 0.5 to every vertex helps, but there are still pixels drawn at wrong coordinates. Also, when I change the screen resolution, drawing behaves different. All I wanted was doing some 2d graphics with opengl  :cry:
Should I better go back to java2d for a 2d game???

spasi

Have you tried the 0.375 trick? Anyway, you should read the OpenGL specification for line/polygon rasterization details (the "diamond-exit" stuff), it should give clues on how to best handle these problems.

We had problems with GL_LINES rasterization on our interface, but lots of testing and trying different values hides most of them. The trick seems to be to overlap line edges. Different cards handle those starting and ending pixels differenently (ATI cards were more predictable/accurate in our tests...) and antialiasing seems to make things worse (an otherwise straight line becomes jaggy), but at least shows where the problem lies.