Reputation: 169
Here is a excerpt from Peter Shirley's Fundamentals of computer graphics:
11.1.2 Texture Arrays
We will assume the two dimensions to be mapped are called u and v. We also assume we have an nx and ny image that we use as the texture. Somehow we need every (u,v) to have an associated color found from the image. A fairly standard way to make texturing work for (u,v) is to first remove the integer portion of (u,v) so that it lies in the unit square. This has the effect of "tiling" the entire uv plane with copies of the now-square texture. We then use one of the three interpolation strategies to compute the image color for the coordinates.
My question is: What are the integer portion of (u,v)? I thought u,v are 0 <= u,v <= 1.0. If there is an integer portion, shouldn't we be dividing u,v by the texture image width and height to get the normalized u,v values?
Upvotes: 0
Views: 2185
Reputation: 21
Peter O's answer is excellent. I want to add a high level point that the coordinate systems used in graphics are a convention that people just stick to as a defacto standard-- there's no law of nature here and it is arbitrary (but a decent standard thank goodness). I think one reason texture mapping is often confusing is that the arbitrariness of this stardard isn't obvious. This is that the image has a de facto coordinate system on the unit square [0,1]^2. Give me a (u,v) on the unit square and I will tell you a point in the image (for example, (0.2,0.3) is 20% to the right and 30% up from the bottom-left corner of the image). But what if you give me a (u,v) that is outside [0,1]^2 like (22.7, -13.4)? Some rule is used to make that on [0.1]^2, and the GL modes described are just various useful hacks to deal with that case.
Upvotes: 2
Reputation: 32898
UV values can be less than 0 or greater than 1. The reason for dropping the integer portion is that UV values use the fractional part when indexing textures, where (0,0), (0,1), (1,0) and (1,1) correspond to the texture's corners. Allowing UV values to go beyond 0 and 1 is what enables the "tiling" effect to work.
For example, if you have a rectangle whose corners are indexed with the UV points (0,0), (0,2), (2,0), (2,2), and assuming the texture is set to tile the rectangle, then four copies of the texture will be drawn on that rectangle.
The meaning of a UV value's integer part depends on the wrapping mode. In OpenGL, for example, there are at least three wrapping modes:
GL_REPEAT
- The integer part is ignored and has no meaning. This is what allows textures to tile when UV values go beyond 0 and 1.GL_MIRRORED_REPEAT
- The fractional part is mirrored if the integer part is odd.GL_CLAMP_TO_EDGE
- Values greater than 1 are clamped to 1, and values less than 0 are clamped to 0.Upvotes: 3