Presentation is loading. Please wait.

Presentation is loading. Please wait.

Texture Mapping. 2 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border,

Similar presentations


Presentation on theme: "Texture Mapping. 2 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border,"— Presentation transcript:

1 Texture Mapping

2 2

3 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, void *data); tell OpenGL everything it needs to know about how to interpret the texture data pointed to by the data parameter. Example –gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());

4 4 target: GL_TEXTURE_1D, GL_TEXTURE_2D, or GL_TEXTURE_3D level: number of mipmap level, always 0 for the moment Internalformat width, height, depth: width, height and depth of the texture border: let’s set 0 format: texel format type: data type for pixel data data: pointer to the data

5 5 Applying Textures I Three steps to applying a texture 1.specify the texture read or generate image assign to texture enable texturing 2.assign texture coordinates to vertices 3.specify texture parameters wrapping, filtering

6 6 Textures reside in texture memory. When we assign an image to a texture it is copied from processor memory to texture memory where pixels are formatted differently. Texture coordinates are vertex attributes like color and normals. As with colors, OpenGL interpolates texture inside geometric objects. Because textures are really discrete and of limited extent, texture mapping is subject to aliasing errors that can be controlled through filtering.

7 7 Texture Objects Have OpenGL store your images –one image per texture object –may be shared by several graphics contexts Generate texture names glGenTextures( n, *texIds ); request n texture ids and return those values back to you in texIds.

8 8 Texture Objects (cont'd.) Create texture objects with texture data and state Bind textures before using glBindTexture( target, id ); –The target is one of GL_TEXTURE_{123}D – To have OpenGL use a particular texture object, call glBindTexture() with the target and id of the object you want to be active. –All texturing calls become part of the object until the next glBindTexture() is called.

9 9 Define a texture image from an array of texels in CPU memory glTexImage2D( target, level, components, w, h, border, format, type, *texels ); –transfer the texels in CPU memory to OpenGL, where they will be processed and converted into an internal format. –dimensions of image must be powers of 2 Specifying a Texture Image

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

11 11 glBegin (GL_QUADS); glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, 0.0); glTexCoord2f (1.0, 0.0); glVertex3f (10.0, 0.0, 0.0); glTexCoord2f (1.0, 1.0); glVertex3f (10.0, 10.0, 0.0); glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 10.0, 0.0); glEnd ();

12 12

13 13 Applying Textures II –specify textures in texture objects –set texture filter –set texture function –set texture wrap mode –set optional perspective correction hint –bind texture object –enable texturing –supply texture coordinates for vertex coordinates can also be generated

14 14 Filter Modes –minification or magnification ( GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER) –GL_NEAREST, GL_LINEAR –special mipmap minification filters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); Texture Application Methods

15 15 Filter Modes TexturePolygon MagnificationMinification PolygonTexture Example: glTexParameteri( target, type, mode );

16 16 Mipmapped Textures Mipmap allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glTexImage*D( GL_TEXTURE_*D, level, … ) GLU mipmap builder routines gluBuild*DMipmaps( … )

17 17 Wrapping Mode Example: glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) texture s t GL_CLAMP wrapping GL_REPEAT wrapping

18 18 Texture Functions Controls how texture is applied glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param ) GL_TEXTURE_ENV_MODE modes –GL_MODULATE –GL_BLEND –GL_REPLACE –GL_ADD Set blend color with GL_TEXTURE_ENV_COLOR glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); GLfloat fColor[4] = { 1.0f, 0.0f, 0.0f, 0.0f }; glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, fColor);

19 19 Corresponder Functions  The parameter-space values (s,t) are not necessarily in the range of [0,1). Corresponder functions determine the behavior of (s,t) outside the range [0,1).  wrap, repeat, or tile: repeat the image across the surfaces by dropping the integer part of the parameter values, i.e. (s´, t´) = (s-  s , t-  t  ). So, the left/right edges and top/bottom edges should match.  mirror: mirrored on every other repetition is good for providing some continuity along the edges of the texture.  clamp: values outside the range [0,1) are clamped to those of the edges of the image texture.  border: parameter values outside[0,1) are rendered with a separately defined border color.

20 20 Texture Blending Operations: Replace  Recall that, in Gouraud shading, the lighting equation is evaluated per vertex and the RGB colors at vertices are interpolated at the rasterizer stage.  The texel RGB values obtained in the texture mapping process should interact with the colors computed by the lighting equation: replace, modulate or decal.  In the replace mode, any lighting computed for the surface is replaced by the texture. So, the texture’s color always appears the same regardless of changing light conditions. It would be good when e.g. drawing a can with an opaque label.

21 21 Texture Blending Operations: Modulate  In the modulate mode, the lighting color is multiplied by the texture color.  The modeler sets the ( s, t ) values at vertices.  A white material is typically used in computing the lighting at each vertex.  The computed lighting and texture-space values are interpolated across the polygon.  At each pixel, the texel color is obtained and modulated/multiplied by the lighting color.

22 22 Texture Blending Operations: Decaling  Suppose you have a tree texture and do not want its background to affect the scene.  Extend the RGB texture map into RGBA, and assign an  value of 0 to a texel to be transparent.  It’s called decaling (or alpha mapping, in general) which is often used for e.g. an insignia on an airplane wing.  In general, decaling refers to “drawing one image atop another.” texture map (1-bit)  -map

23 23 Texture Blending Operations: Summary  The replace, modulate, and decal modes can be described as follows:  replace : C f =C t and A f =A t  modulate: C f =C t C l and A f =A t A l  decal : C f =(1-A t )C l + A t C t and A f =A l where the subscript f denotes final, t texture color, and l lighting color.  In some systems, A f is often set to A t A l for implementing the decal mode.

24 24 Magnification  Consider the two polygons to be textured: one is smaller than the image and the other is bigger.  Magnification can be depicted as follows. minification magnification There are more pixels than texels!! texel grid pixel

25 25 Magnification  Two common techniques for magnification are nearest neighbor and bilinear interpolation. In general, bilinear interpolation is better. s i1i1 i2i2 i4i4 i3i3 t iaia ibib nearest neighbor bilinear interpolation (  u+0.5 ,  v+0.5  )

26 26 Minification  Minification can be depicted as follows.  We can also use nearest neighbor or bilinear interpolation, but these two may cause severe aliasing problems. minification There are less pixels than texels!! texel grid pixel no influence nearest neighbor bilinear interpolation Imagine a texture where x is black and all the others are white. What if a pixel is influenced by more than 4 texels?

27 27 Mipmapping  It’s the most popular method of antialiasing for textures, where mip stands for multum in parvo (many things in a small place).  Texture image size is restricted to 2 m ×2 n texels, or sometimes even 2 m ×2 m square.  The texture is downsampled to a quarter of the original area. Each new texel value is typically computed as the average of the four neighbor texels. It’s a box filter. We can use Cone or Gaussian filters. The reduction is performed recursively until one or both of the dimensions of the texture equals one texel. It’s a box filter! a texel level 0 level 1level 2

28 28 Mipmapping (cont ’ d)  Consider the two perfect cases.  If a pixel covers 2 2 ×2 2 texels, go to level 2 and get a texel.  If a pixel covers 2 1 ×2 1 texels, go to level 1 and get a texel.  In general, which level to go? a texel level 0level 1level 2 a pixel’s center level 0level 1

29 29 Mipmapping (cont ’ d)  We ‘could’ use the longer edge of the quadrilateral formed by the pixel’s cell to compute d. (In fact, more popular is using differentials.)  A pixel’s center is assigned a texture-space value.  Let’s approximate the pixel’s quadrilateral by connecting the 4 adjacent pixels’ texture-space values.  In the example, the longest edge is of length about 4. Go to level d =log 2 4=2.  As the pixel center normally does not coincide with a texel’s center, we need bilinear interpolation. a pixel’s center texel grid level 0level 1level 2 a texel 4

30 30 Mipmapping (cont ’ d)  Note that d is not necessarily an integer. For example, assume that d is 1.7.  Go to level 1, and do bilinear interpolation to get v 1.  Go to level 2, and do bilinear interpolation to get v 2.  Do linear interpolation between v 1 and v 2 : 0.3*v 1 +0.7*v 2.  It’s a tri-linear interpolation. level 2 level 1 a pixel’s center texel grid level 0 a texel level 1.7 

31 31 Problems of Mipmapping  Suppose that a pixel cell’s quadrilateral covers a large number of texels along one dimension but only a few along the other dimension.  If the texture image of 64x64 texels is covered by 32x32 pixels, d is 1. If 64X32 texels vs. 8x16 pixels, d is 3!!!! Such a case, like the above example, leads to over-blurring. It’s OpenGL approach.  There are many techniques to tackle this problem: summed-area table, etc. level 2 level 1 a pixel cell’s quadrilateral texel grid level 0 a texel The pixel covers about 18 texels at level 0, but actually takes all of 64 texels!!!

32 32 What are Summed Area Tables (SATs)? Pre-integrated texture representation Invented by Frank Crow, 1984 Each texel is the sum of all texels below and to the left of it Allows rapid box filtering (average) over any rectangle for a fixed cost Based on the algebraic identity: (x+a)(y+b) – (x+a)y – x(y+b) + xy = ab

33 33 Summed Area Table 1111 1111 1111 1111 481216 36912 2468 1234 Regular image Summed area table

34 34 Summed Area Table (x, 0)(x+a, 0) (x+a, y+b) (0, 0) (0, y) (0, y+b) (x+a, y) a b (x, y) ab = S[x+a, y+b] – S[x+a, y] – S[x, y+b] + S[x, y] (x, y+b)


Download ppt "Texture Mapping. 2 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border,"

Similar presentations


Ads by Google