Accessing texture data (another one...)

Hi guys,
I would like to know how to access the 4 neighbor texels of a texture coordinate. In other words I want to get the 4 values around the fractional texture coordinates inorder to do my own filterring.
How should I define the texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) or
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
What’s the significance of NEAREST / LINEAR in the fragment shader?
I would be very thanksful to get any additional information about this subject.

Many thanks,

Yossi

GL_NEAREST and GL_LINEAR mean the same thing they always do. You should use GL_NEAREST, and then offset your texture coordinates by 1/width and 1/height to get neighboring texels.

– Tom

Thank you Tom for the answer.
But still something isn’t clear to me.
If I get the nearest texel than the neighbors are at (1/width, 1/height) or (-1/width/, -1/height)?
If I define the texture as GL_NEAREST I guess I lose the original fractional texture coordinates?
Maybe if I define it as GL_LINEAR than I get the accurate fractional texture coordinates and than have an access to the neighbors that encompass the accurate coordinates.
Hope I am clear.
Thanks,
Yossi

Originally posted by yossi:
Thank you Tom for the answer.
But still something isn’t clear to me.
If I get the nearest texel than the neighbors are at (1/width, 1/height) or (-1/width/, -1/height)?
If I define the texture as GL_NEAREST I guess I lose the original fractional texture coordinates?
Maybe if I define it as GL_LINEAR than I get the accurate fractional texture coordinates and than have an access to the neighbors that encompass the accurate coordinates.
Hope I am clear.
Thanks,
Yossi

Texture addressing is performed in the texture addressing units at texel fetch time. That process is completely isolated from your fragment program, i.e., the texture coordinates you see in the fragment program are always “accurate”, they are never rounded/clamped or otherwise depending on the filtering mode.

So, to answer your question, if you want to know which texel you are addressing in NEAREST mode and which are the neighbouring texels, you have to do the calculation yourself by something like taking the integer part of texcoord*TEXTURE_WIDTH (look at the OpenGL spec for the exact formula).

Once you have that texel index, the neighbouring are 1 index away, which in 0.0f … 1.0f range is 1/TEXTURE_WIDTH away.

If you are bilinear filtering, then what you do is to interpolate four texels, so there’s no “one texel and one neighbour”, all texels are “neighbours” of your sample point (again, look at the OpenGL spec and see how the alfa & beta interpolants are calculated to perform the filtering).

Many thanks evanGLizr for your answer. It helped me alot.

Yossi

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.