I am working through some beginner OpenGL tutorials, and the current one teaches how to apply a texture to a simple rectangle. The tutorial states tells me to set the texture parameters GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T to GL_CLAMP_TO_EDGE, however I am uncertain as to what this actually does. I am aware it is something to do with how textures larger/smaller than the space they are being mapped onto are handled, but I'm not sure what behavior this actually causes. What does GL_CLAMP_TO_EDGE do, and how is this different from GL_CLAMP, GL_CLAMP_TO_BORDER, GL_REPEAT? Thanks very much in advance, and bonus helpfulness if you can provide images for an idiot like me.
2 Answers
Well S and T just mean U and V (or X and Y if you prefer), or in GLSL:
vec4.xyzw == vec4.rgba == vec4.strq
The GL_REPEAT mode has textures repeat when you go past (0,0) to (1,1) range
The GL_CLAMP_TO_EDGE mode has textures stop at the last pixel when you fall off the edge.
The GL_CLAMP and GL_CLAMP_TO_BORDER are depreciated because all texture borders must be 0 pixels, so the modes don't make sense anymore. (somewhere around GL3 I believe)
There are more modes so make sure to read the docs. (Examples: GL_MIRRORED_REPEAT, GL_MIRROR_CLAMP_TO_EDGE)
There are some good examples on http://www.flipcode.com/archives/Advanced_OpenGL_Texture_Mapping.shtml, here is a preview:
Wrap S : GL_CLAMP / Wrap T : GL_CLAMP
Wrap S : GL_CLAMP / Wrap T : GL_REPEAT
Wrap S : GL_REPEAT / Wrap T : GL_CLAMP
Wrap S : GL_REPEAT / Wrap T : GL_REPEAT
-
\$\begingroup\$ Thanks. Great answer. Do you know if there is any difference between GL_CLAMP_TO_EDGE and GL_CLAMP, and if there is anyway of just getting the texture to expand to fill the available space? \$\endgroup\$ Sep 22, 2013 at 15:00
-
\$\begingroup\$ Sorry for not clarifying, where I say CLAMP I mean GL_CLAMP_TO_EDGE, GL_CLAMP is gone in modern OpenGL. I'll edit the answer to emphasize that. \$\endgroup\$– MickLHSep 22, 2013 at 15:16
-
\$\begingroup\$ As for "expanding" if you simply mean stretching the texture into a different range, that should be handled during mesh parameterization. (UV mapping, artists job).. If you really must do it pragmatically then you will have to write your own formula to use in a shader. (Or I guess GL_TEXTURE matrix if your app is severely outdated already :P) \$\endgroup\$– MickLHSep 22, 2013 at 15:29
-
\$\begingroup\$ GL_CLAMP is deprecated, but GL_CLAMP_TO_BORDER isn't. \$\endgroup\$ May 10, 2021 at 16:14
Here is an extract of the OpenGL Reference page on glTexParameter as of 2014/02/07, formatted a bit for easier reading:
GL_TEXTURE_WRAP_S
Sets the wrap parameter for texture coordinate s to either GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT, GL_REPEAT, or GL_MIRROR_CLAMP_TO_EDGE.
GL_CLAMP_TO_EDGE causes s coordinates to be clamped to the range
, where N is the size of the texture in the direction of clamping.
GL_CLAMP_TO_BORDER evaluates s coordinates in a similar manner to GL_CLAMP_TO_EDGE.
However, in cases where clamping would have occurred in GL_CLAMP_TO_EDGE mode, the fetched texel data is substituted with the values specified by GL_TEXTURE_BORDER_COLOR.GL_REPEAT causes the integer part of the s coordinate to be ignored;
the GL uses only the fractional part, thereby creating a repeating pattern.GL_MIRRORED_REPEAT causes the s coordinate to be set to the fractional part of the texture coordinate if the integer part of s is even; if the integer part of s is odd, then the s texture coordinate is set to
, where
represents the fractional part of s.
GL_MIRROR_CLAMP_TO_EDGE causes the the s coordinate to be repeated as for GL_MIRRORED_REPEAT for one reptition of the texture, at which point the coordinate to be clamped as in GL_CLAMP_TO_EDGE.
Initially, GL_TEXTURE_WRAP_S is set to GL_REPEAT.