View Full Version : Understanding of GL_CLAMP_TO_*

rw802rw802

07-14-2017, 11:00 PM

As I see almost everywhere says that texture space is [0,1]. Does that mean GL_CLAMP_TO_EDGE will clamp the uv values to [0,1]? I read from OpenGL doc online here https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml and it says:

GL_CLAMP_TO_EDGE causes s coordinates to be clamped to the range [1/2N,1−1/2N], where N is the size of the texture in the direction of clamping.

So if I have a 2D texture whose size is 2 * 1. Then the uv will be clamped to [0.25, 0.75] for x, and [0.5, 0.5] for y.

Can anyone please clarify, what is wrong here? Thanks.

Dark Photon

07-15-2017, 11:42 AM

As I see almost everywhere says that texture space is [0,1]. Does that mean GL_CLAMP_TO_EDGE will clamp the uv values to [0,1]? I read from OpenGL doc online here https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexParameter.xhtml and it says:

GL_CLAMP_TO_EDGE causes s coordinates to be clamped to the range [1/2N,1−1/2N], where N is the size of the texture in the direction of clamping.

The quote is just the fancy math to say that it limits texture sampling so that it never samples within 1/2 texel of the edge of your texture.

That is, texture sampling is limited to the bounds of your texture but inset by 1/2 texel on all 4 sides so it doesn't get too close to the edge (assuming you set this for the S and T WRAP_MODE).

Why? With GL_LINEAR sampling, if it gets within a 1/2 texel of the edge, it's going to need to pull in color values for texels "outside" the texture to do the filtering. Limiting the range of the sampling to (1/(2N), 1 - 1/(2N)) prevents it from needing to do so.

So if I have a 2D texture whose size is 2 * 1. Then the uv will be clamped to [0.25, 0.75] for x, and [0.5, 0.5] for y.

Consider the first dimension (N = 2). The math says that texture sampling is limited to (1/(2N), 1 - 1/(2N)) == (0.25,0.75). In 0..1 texture coordinates (where 0..1 span the left-edge of the first texel to the right edge of the last texel), 0.25 and 0.75 are the center of the leftmost texel and the center of the rightmost texel. So it does what it says.

Now consider the 2nd dimension (N = 1). This gives us a range of (1/(2N), 1 - 1/(2N)) ==(0.5, 0.5), which basically constrains sampling to the center of the texel in the T (Y) dimension).

Together, these two constrain texture sampling for your 2x1 texture to a line which runs from the center of the left-most texel to the center of the right-most texel. This so that GL_LINEAR sampling and filtering doesn't need to pull in colors outside the texture to do its job. In other words, sampling is limited to an area inset by 1/2 texel on all 4 sides.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.