GL question- gluOrtho2D inexact alignment?

Sage
Posts: 1,232
Joined: 2002.10
Post: #1
basic question-

When I set up my projection matrix for 2D drawing, and fill a window with a single texture of the same size, I don't get exact alignment. The texels near the origin are aligned, but at the opposite corner they are off by a fraction of a pixel which blurs the texture slightly.

Code & pictures at:
http://homepage.mac.com/arekkusu/bugs/gl...exact.html

It's not off by one pixel either, it's a fraction of a pixel.

I looked at the Apple sample code (like AGLString) and it has the same issue.

What's the correct way to set up orthographic projection so that each texel exactly aligns with one pixel?
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
my suggestion would be to scale the picture by ± 0.5 or 1 pixels, and see what happens. I think it is possible that the pixels are off. Also try to shift the whole polygon by 0.5 units in any direction. I am sure the info is in the opengl redbook, but not easy to find.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
If your window is w by h pixels, your projection should be 0, w - 1, 0, h - 1. Also, you should not draw on integer coordinates, but some fraction value offset. From memory, the Red Book recommends adding 0.25 to each coordinate.
Quote this message in a reply
w_reade
Unregistered
 
Post: #4
Yes, it is 0.25 that the Redbook recommends.

As if OSC could be wrongÖ
Quote this message in a reply
henryj
Unregistered
 
Post: #5
He's wrong every day at work. Maybe he uses up all his good ideas @ idev

Rasp
Quote this message in a reply
w_reade
Unregistered
 
Post: #6
That's sort of reassuring and sort of disappointingÖ never mind, we're delighted to have all the good ideas. Wink
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
Well, sorry but I'm going to say here that you're all wrong.

I had already read the Red Book; the recommended offset is 0.375. But that doesn't apply to polygons. Let me quote the relevant part here:

Quote:
If exact two-dimensional rasterization is desired, you must carefully specify both the orthographic projection and the vertices of primitives that are to be rasterized. The orthographic projection should be specified with integer coordinates, as shown in the following example:

gluOrtho2D(0, width, 0, height);

where width and height are the dimensions of the viewport. Given this projection matrix, polygon vertices and pixel image positions should be placed at integer coordinates to rasterize predictably. For example, glRecti(0, 0, 1, 1) reliably fills the lower left pixel of the viewport, and glRasterPos2i(0, 0) reliably positions an unzoomed image at the lower left of the viewport. Point vertices, line vertices, and bitmap positions should be placed at half-integer locations, however. For example, a line drawn from (x1, 0.5) to (x2, 0.5) will be reliably rendered along the bottom row of pixels int the viewport, and a point drawn at (0.5, 0.5) will reliably fill the same pixel as glRecti(0, 0, 1, 1).

An optimum compromise that allows all primitives to be specified at integer positions, while still ensuring predictable rasterization, is to translate x and y by 0.375, as shown in the following code fragment. Such a translation keeps polygon and pixel image edges safely away from the centers of pixels, while moving line vertices close enough to the pixel centers.



So, I reiterate my question: how can I get exact alignment? Did any of you look at the link I posted? I humbly ask you (challenge you!!) to go spend 30 minutes writing code that works correctly. Apple's code doesn't! Yet, there must be a way to do it, since Quartz Extreme is supposedly making textures out of everything on the screen, and it doesn't have this problem.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #8
I assume that a polygon's texture is fully inside the polygon. So, I would set up the ortho view as usual, from 0 to xsize and ysize, but make the textured rectangle 1 pixel unit bigger, origin at 0 - 0, so that the line going around it really goes around the screen. This should cause the rendered line to be off, but since you are not rendering it, it should be fine, and the triangle itself should be fine. No warranty on this one, but it makes sense to me.

- D.G
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #9
Quote:Originally posted by DoooG
I assume that a polygon's texture is fully inside the polygon.


That is a bad assumption. My real application is doing all kinds of things with the object/texture matricies (rotation etc) as well as splitting the texture into four quads which each only use a subregion of the full texture.

However, the problem still happens with the simple case of a 512x480 texture mapped directly to a 512x480 polygon in a 512x480 viewport.

Quote:
No warranty on this one, but it makes sense to me.


Again, I invite you to go write code to test your theory. If you get it working, post the code.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
The code on your website has gluOrtho2D(0, 512, 480, 0);
change that to gluOrtho2D(0, 511, 479, 0); as I suggested, and see if that fixes your problem.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #11
Quote:Originally posted by OneSadCookie
The code on your website has gluOrtho2D(0, 512, 480, 0);
change that to gluOrtho2D(0, 511, 479, 0); as I suggested, and see if that fixes your problem.


My suggestion would be to leave gluOrtho2D(0, 512, 480, 0) but make the texture size 0, 513, 481, 0. forgive me the mixed up numbers, you get the idea.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #12
I did a test program to figure this out, and came to the conclusion that zero texture offset, and the size being exactly as much as the size of the texture leads to the correct results. I have written a small app to test horizontal/vertical alignment with two grid textures, and they work just as we all thought in the first place. I even made them linearly filtered, so I could check when the thing is really right, eg. the centers of the texels are put to screen.

My app is not direct OpenGL code, but it basically does this:
gluOrtho2D(0.0, width, 0.0, height);
...
setup matrix scale so 1 gl unit is 1 pixel on screen
...
draw a quad with texsize*texsize, and tex coords ranging from 0.0 to 1.0.
...and voi'la, the same as if I would draw the bitmap directly to screen.

My question to you is: Don't you maybe forget something? Since you are using multiple polygons to represent the screen, are you sure you got the texture border setup correctly, and are not cheating the border from the actual texture? I have no other explanation.

If you are on a G4, I can send you the demo app I wrote, drop me a line on yahoo or aim.

- D.G
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #13
Quote:Originally posted by OneSadCookie
The code on your website has gluOrtho2D(0, 512, 480, 0);
change that to gluOrtho2D(0, 511, 479, 0); as I suggested, and see if that fixes your problem.


That was one of the first things I tried before posting here, it doesn't work because the texture is not off by one pixel, it's off by a fraction. It does improve the problem if I slightly shrink the projection matrix, the closest I have been able to get is:

gluOrtho2D(0, 512-(81.0/1024), 480-(81.0/1024), 0);

which is still not exact.



Quote:Originally posted by DoooG
I did a test program to figure this out
...

My app is not direct OpenGL code, but it basically does this:
gluOrtho2D(0.0, width, 0.0, height);
...
setup matrix scale so 1 gl unit is 1 pixel on screen
...
draw a quad with texsize*texsize, and tex coords ranging from 0.0 to 1.0.
...and voi'la, the same as if I would draw the bitmap directly to screen.

My question to you is: Don't you maybe forget something?

In the simple case, that is exactly what I am doing, except that I am using GL_TEXTURE_RECTANGLE_EXT so my texture coordinates go from 0..width and 0..height, as doc'd in the extension guide. I have also tried it with regular GL_TEXTURE_2D and had the same problem.

Quote:
If you are on a G4, I can send you the demo app I wrote, drop me a line on yahoo or aim.

I would very much like to look at it. I am developing on a TiBook, but I have access to all current Apple hardware. You can just email a .dmg directly at arekkusu@mac.com, thanks.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #14
Quote:Originally posted by DoooG
I did a test program to figure this out


DoooG, thanks for your test program. I ran it here and found exactly the same problems as in my application. I've written you a mail with photos showing the problem, but smtp.mac.com appears to be down right now so the mail isn't going out.

If you try your app with a texture that uses a larger grid, like I show in the mail, you can see the problem.

I'm going to re-ask this question on mac-opengl, since it seems it isn't as "basic" as I thought.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #15
I am waiting for your mail. I made bigger textures myself made them 512 size, and it seems to be exactly fine, so I can't possible imagine what's happening on your machine. I hope the pictures will clear it up.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  gluOrtho2D Problems Nick 15 5,663 Sep 6, 2004 07:06 PM
Last Post: Nick