Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Texture Mapping

Similar presentations


Presentation on theme: "OpenGL Texture Mapping"— Presentation transcript:

1 OpenGL Texture Mapping
1

2 Objectives Introduce the OpenGL texture functions and options

3 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

4 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 2D texture in OpenGL is a 2D array of colours, and each element in the array is called a ‘texel’.

5 Texture Mapping y z x geometry display t image s

6 Texture Example The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective

7 Texture Mapping and the
OpenGL Pipeline Images and geometry flow through separate pipelines that join during fragment processing “complex” textures do not affect geometric complexity vertices geometry pipeline fragment processor image pixel pipeline

8 Specifying a Texture Image
Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512]; Define as any other pixel map Scanned image Generate by application code Enable texture mapping ­glEnable(GL_TEXTURE_2D) OpenGL supports 1-4 dimensional texture maps

9 Define Image as a Texture
glTexImage2D( target, level, components, w, h, border, format, type, texels ); target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of in pixels texels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glTexImage2D(GL_TEXTURE_2D, GL_RGB, GL_UNSIGNED_BYTE, 0, 3, 512, 512, 0, my_texels);

10 Mapping a Texture Based on parametric texture coordinates
•glTexCoord*() specified at each vertex Texture Space Object Space (s, t) = (0.2, 0.8) A t 0, 1 1, 1 a c (0.4, 0.2) b B C (0.8, 0.4) 0, 0 1, 0 s

11 Interpolation OpenGL uses interpolation to find proper texels from specified texture coordinates Can be distortions texture stretched over trapezoid showing effects of bilinear interpolation good selection of tex coordinates poor selection of tex coordinates

12 Texture Parameters OpenGL 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

13 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: glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap); glTexParameteri( GL_TEXTURE_2D, 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.

14 Wrapping Clamping: if s,t > 1 use 1, if s,t <0 use 0
Wrapping: use s,t modulo 1 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t s GL_REPEAT wrapping GL_CLAMP wrapping texture

15 Filter parameter Define the texture filter parameters. This will control what happens when a texture is scaled up or down. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); glTexParameteri( GL_TEXTURE_2D,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.

16 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 Texture Polygon Minification Magnification


Download ppt "OpenGL Texture Mapping"

Similar presentations


Ads by Google