Pixel and texel coordinates

Eeps,

the other day I had to write a blitter that imitates the way OGL rasterizes a textured, viewer-facing rectangular quad (ie the kind you use to have an accelerated Bitblt). It was for a medical app, and I almost had to be able to prove that the same texels would end up at the same pixels, despite non-integral panning and zooming (magnification with GL_LINEAR).

Anyway, it’s not very difficult, but you have to know how all the pieces about glViewport, pixel coordinates, texel coordinates and fragment centers fit together… So I gathered up all my notes and wrote a small text about it, some sort of tutorial if you want… I noticed this kind of stuff comes back once in a while as a faq from people doing 2D, so maybe this will help.

The only thing is, I’m not sure I interpreted everything correctly For instance, the way I read it, I’m not sure there is any good reason why glViewport requires integer coordinates…

Anyway, here is the link, http://www.bpeers.com/articles/glpixel/ ,
if you have some comments, or spot errors, etc, let me know !

Thanks

First of all, glViewport uses integer values simply because it removes annoying issues since it is related to screen coordinates directly. And if you really want to use floating coordinates, you still can emulate it though it will be a bit slower than if it were done by hardware directly.

Secondly, in your document I’ve seen a picture which shows a grid that represents a 4x4 texture map. I’m afraid you’re not mapping texture coordinates according to OpenGL specifications. To be exact, the T coordinate 0, for instance, is not really at the bottom part of the texture : it is at the center of the bottom texel.

That is, you map :

 1-  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
 0-  +---+---+---+---+
  
     |---------------|
     0               1

But OpenGL specifies to map :

     +---+---+---+---+
     |   |   |   |   |
 1-  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  |   |   |   |   |
  |  +---+---+---+---+
  |  |   |   |   |   |
 0-  |   |   |   |   |
     |   |   |   |   |
     +---+---+---+---+
  
       |-----------|
       0           1

And it’d be easier to interpret if the texture was shown as a grid where intersection points reprensent texels, rather than a grid where squares represent texels.

[This message has been edited by vincoof (edited 10-14-2002).]

Texture coordinate 0 is indeed the edge of the texture, and not the center of the edge-texel. The center of a texel is always located at integer+0.5 coordinates.

Bob: do you mean that bpeers’ scheme is the right one ? In that case, that would mean that repeating a texture would yield seems.

(Edit: Oops I’ve just read again what I’ve written and I’m WRONG. Sorry)

[This message has been edited by vincoof (edited 10-14-2002).]

This depends on the wrap mode.

I don’t think that wrap mode has something to do in that case. Wrapping should only affect the selection of the texel close to borders or beyond.

I was having difficulty interpreting your diagrams.

I am sure that OpenGL does not do what you say it does in those diagrams, it does the exact opposite. i.e. the first figure is what OpenGL does, the second is is not what OpenGL does. The coordinate 0,0 maps to the lower left corner of the lower left texel not the center.

If it is wrapped then the filter for that location (coordinate 0,0) is effectively half way between all four corners for filtering purposes, but that’s just a confusing detail :-). To help visualize this aspect you should draw sample locations for each texel and not just squares.

[This message has been edited by dorbie (edited 10-15-2002).]

Thanks dorbie, but I’ve already seen that
In fact, that was what I was trying to say when I apologized three posts above

Oi !! I completely missed this discussion after it fell off the frontpage ! Thanks for the feedback, it’s good to hear it was read after all I thought the text had no replies, and thus was stupid, but it looks like it really helps with something that’s easy to get confused about… so maybe I should start linking it. Thanks !

PS I’m not sure what you mean with annoying issues for floating viewports… Even with integer screenedges, raster positions will almost always fall on non-integer coordinates anyway ? So it wouldn’t matter too much to add an extra nudge to compensate for the fractions the edges lie on… I don’t know :\

[Edit spoeling]

[This message has been edited by bpeers (edited 10-27-2002).]