Download Texture Mapping - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!
Texture Mapping
OpenGl and Implementation Details
Surface mapping
• Texture mapping
• Bump Mapping
• Displacement mapping
– Actually moving geometry
– ie Create screw from cylinder, Terrain, etc
An Overview
- Steps in Texture Mapping
- Specify the texture. (R, G, B, A, mipmapping)
- Indicate how the texture is to be applied to each pixel. (decal, modulate, blend)
- Enable texture mapping via glEnable(..) GL_TEXTURE_1D or GL_TEXTURE_2D
- Draw the scene, supplying both texture and geometric coordinates.
What does a pixel see?
Controlling Filtering
Texture Sampling
• Sinc(x) is not feasible in real time
• Box filter
(nearest-neighbor)
is poor quality
Interpolation
• Texture coordinate (pu, pv) in [0,1]
• Texture images size n*m texels
• Nearest neighbor would access:
(floor(nu), floor(mv))
• Interpolate 1D in x and y
Bilinear Interpolation
• Let t(u,v) access texture map
b(u,v) = filtered texel
(u’, v’) = (pu - floor(pu), pv - floor(pv))
b(pu, pv) = (1 - u’) (1-v’) t(xl,yb) +
u’ (1-v’) t(xr, yb) + (1-u’) v’ t(xl, yt) +
u’ v’ t(xr,yt)
Repeat, Mirror, Clamp, Border
Repeating and Clamping Textures
- You can assign texture coordinates outside the range [0,1] and have them either clamp or repeat.
- With repeating textures, if you have a large plane with texture coordinates running from 0.0 to 10.0 in both directions, for example, you’ll get 100 copies of the texture tiled together on the screen.
- For most applications where the texture is to be repeated, the texels at the top of the texture should match those at the bottom, and similarly for the left and right edges.
- To clamp the texture coordinates: Any values greater than 1.0 are set to 1.0, and any values less than 0.0 are set to 0.0. Clamping is useful for applications where you want a single copy of the texture to appear on a large surface.
- If the surface-texture coordinates range from 0.0 to 10.0 in both directions, one copy of the texture appears in the lower corner of the surface. The rest of the surface is painted with the texture’s border colors as needed.
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_S_WRAP GL_REPEAT);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_T_WRAP GL_REPEAT);
or
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_S_WRAP GL_CLAMP);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_T_WRAP GL_CLAMP);
You can also clamp in one direction and repeat in the other.
How to apply texturing
• Modulate
– Multiply texture with lighting
• Replace
– Just use texture color
• Add, sub, etc
– On newer hardware
Assigning Texture Coordinates
- Texture coordinates can comprise one, two, three, or four coordinates. They’re usually referred to as the s, t, r, and q coordinates.
- For one-dimensional textures, you use the s coordinate; for two-dimensional textures, you use s and t.
- The q, like w, is typically given the value 1 and can be used to create homogeneous coordinates
- The command to specify texture coordinates, glTexCoord(), is similar to glVertex(), glColor(), and glNormal(). Usually, texture-coordinate values range between 0 and 1; values can be assigned outside this range, however, with the results described in “ Repeating and Clamping Textures .”
void glTexCoord{1234}{sifd}{v}(TYPEcoords);
Computing Approximate Texture Coordinates
- Polygon aspect ratio 3/2 (w/h); texture aspect ration 1. To avoid distorting the texture: use texture coordinates of (0,0), (1,0), (1,2/3), (0,2/3)
- A can with label 4 units tall and 12 units around (aspect ratio 3/1). Textures must have aspect ratio of 2n^ to 1. We can copy and paste it twice to make an aspect ration of 1. Let’s approximate the can by 30 (412/30) polygons. We* can use the following texture coordinates: - (0,0), (1/30,0), (1/30,1/3),(0,1/3) - (1/30,0), (2/30,0), (2/30,1/3),(1/30,1/3) - … - (29/30,0), (1,0), (1,1/3),(29/30,1/3)
- Only a few curved surfaces (cone and cylinders) can be mapped to a flat surface without geodesic distortion. For example, a sphere (cos q cos f ,cos q sin f ,sin q ). The q - f rectangle can be mapped directly to a rectangular texture map, but the closer you get to the poles, the more distorted the texture.