PDA

View Full Version : Texture coord to memory coord mapping



Budric
05-26-2010, 12:37 PM
Hi,
I hope someone can clarify some of these concepts. Does OpenGL define texture coordinate (0,0) at the center or the edge of the pixel?

In other words suppose I have a texture that's 4x4 pixels in size. Using texture coordinates if I wanted to access original pixel (0,0) (memory coordinate) would texture coordinates be (0,0) or (1/4/2, 1/4/2)? Likewise would the pixel (3,3) be (1,1) or (0.75 + 1/4/2, 0.75 + 1/4/2) in texture coords?

I've been looking at figure 3.6 page 190 of the spec. It seems like the center of the pixel is used as origin for coordinates, and texture coordinates (0,0), (0.25, 0.25), (0.5, 0.5) ... will give me original un-interpolated pixel values.

Dark Photon
05-26-2010, 03:38 PM
Hi,
I hope someone can clarify some of these concepts. Does OpenGL define texture coordinate (0,0) at the center or the edge of the pixel?
For normalized texture coordinates, (0,0) is the lower-left corner of the lower-left texel, and the data is considered to reside at the center of each texel.


In other words suppose I have a texture that's 4x4 pixels in size. Using texture coordinates if I wanted to access original pixel (0,0) (memory coordinate) would texture coordinates be (0,0) or (1/4/2, 1/4/2)? Likewise would the pixel (3,3) be (1,1) or (0.75 + 1/4/2, 0.75 + 1/4/2) in texture coords?

Well first, instead of "access original pixel" I assume you mean "nail the center of the texel", where the data value is conceptually located. If you do so, even with linear filtering enabled, then you should get the value stored in that texel (subject to float precision of course).

So the answer: The latter on your first question. Neither one on the second.

In general to map a (0..1) "cell centered" value to texture coordinates:

texcoord = val0_to_1 * (N-1)/N + 0.5/N

where N = the resolution of the texture (and this assumes you're using the full width of the texture for data). Basically, this shrinks by 1 texel and then offsets by 0.5 texels.

So for your latter query, you have:

3/4 * 3/4 + 0.5/4

Another option is to use RECT textures which take integer texture coordinates, and texelFetch lets you do this regardless of texture type, IIRC.

Budric
05-27-2010, 08:31 AM
Thanks a lot for your answer. Just to clarify did you mean:
ONE * 3/4 + 0.5/4
Or am I not getting why you're using 3/4 * 3/4 + 0.5/4?

Dark Photon
05-27-2010, 06:02 PM
Thanks a lot for your answer. Just to clarify did you mean:
ONE * 3/4 + 0.5/4
Or am I not getting why you're using 3/4 * 3/4 + 0.5/4?
Oh, nuts. Yeah, my bad. I saw 3 and thought 3rd, not 4th. Sorry. Been programming in C/C++ long enough I'm embarrassed at that one.