Open GL Texture Mapping 1 Objectives Introduce the
Open. GL Texture Mapping 1
Objectives • Introduce the Open. GL texture functions and options 2
Steps of texture mapping To use texture mapping, you perform these steps. • Create a texture object and specify a texture for that object. • Indicate how the texture is to be applied to each pixel. • Enable texture mapping. • Draw the scene, supplying both texture and geometric coordinates
Texture • How can we define a texture? • There are two ways: • first, we can hard-code the texel values into the texture array – a tedious process which is only really suitable for regular patterns; • second, we can use an existing image (a JPG, PNG, GIF, BMP, etc), and read its pixels into the texture array. This is by far the most common way of define textures. • A 2 D texture in Open. GL is a 2 D array of colours, and each element in the array is called a ‘texel’.
Texture Mapping y z x geometry t display image s 5
Texture Example • The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective 6
Texture Mapping and the Open. GL Pipeline • Images and geometry flow through separate pipelines that join during fragment processing “complex” textures do not affect geometric complexity vertices image geometry pipeline pixel pipeline fragment processor 7
Specifying a Texture Image • Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512]; • Define as any other pixel map Scanned image Generate by application code • Enable texture mapping gl. Enable(GL_TEXTURE_2 D) Open. GL supports 1 -4 dimensional texture maps 8
Define Image as a Texture gl. Tex. Image 2 D( target, level, components, w, h, border, format, type, texels ); type of texture, e. g. GL_TEXTURE_2 D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels: pointer to texel array target: gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, 3, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); 9
Mapping a Texture • Based on parametric texture coordinates • gl. Tex. Coord*() specified at each vertex t 0, 1 Texture Space Object Space 1, 1 a (s, t) = (0. 2, 0. 8) A (0. 4, 0. 2) c b 0, 0 1, 0 s B C (0. 8, 0. 4) 10
Interpolation Open. GL uses interpolation to find proper texels from specified texture coordinates Can be distortions texture stretched good selection of tex coordinates poor selection of tex coordinates over trapezoid showing effects of bilinear interpolation 11
Texture Parameters • Open. GL has a variety of parameters that determine how texture is applied • Wrapping parameters determine what happens if s and t are outside the (0, 1) range • Filter modes allow us to use area averaging instead of point samples • Mipmapping allows us to use textures at multiple resolutions • Environment parameters determine how texture mapping interacts with shading 12
Wrapping • Define the texture wrapping parameters. This will control what happens when a texture coordinate greater than 1. 0 or less than 0. 0 is encountered: • gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, wrap); • gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, wrap); • where wrap is: • GL_REPEAT specifies that this pattern will repeat (i. e. , wrap texture coordinates less than 0. 0 or greater than 1. 0 are encountered. • GL_CLAMP specifies that the pattern will “stick” to the value at 0. 0 or 1. 0.
Wrapping Clamping: if s, t > 1 use 1, if s, t <0 use 0 Wrapping: use s, t modulo 1 gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_S, GL_CLAMP ) gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t s texture GL_REPEAT wrapping GL_CLAMP wrapping 14
Filter parameter • Define the texture filter parameters. This will control what happens when a texture is scaled up or down. • gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, filter); • gl. Tex. Parameteri( GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, filter); • GL_NEAREST specifies that point sampling is to be used when the texture map needs to be magnified or minified. • GL_LINEAR specifies that bilinear interpolation among the four nearest neighbors is • to be used when the texture map needs to be magnified or minified.
Magnification and Minification More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values Texture Polygon Magnification Texture Polygon Minification 16
- Slides: 16