texture mapping. 2 3 loading textures void glteximage2d (glenum target, glint level, glint...

34
Texture Mapping

Upload: gilbert-bennett

Post on 14-Dec-2015

242 views

Category:

Documents


2 download

TRANSCRIPT

Texture Mapping

2

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

• 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

Applying Textures I

• Three steps to applying a texture1. specify the texture

• read or generate image• assign to texture• enable texturing

2. assign texture coordinates to vertices3. specify texture parameters

• wrapping, filtering

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

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

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

• 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

• Based on parametric texture coordinates• glTexCoord*() specified at each vertex

s

t1, 1

0, 1

0, 0 1, 0

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

bc

Texture Space Object Space

Mapping a Texture

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

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

• 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

Filter Modes

Texture Polygon

Magnification Minification

PolygonTexture

Example:

glTexParameteri( target, type, mode );

16

Mipmapped Textures

• Mipmap allows for prefiltered texture maps of decreasing resolutions

• Lessens interpolation errors for smaller textured objects

• Declare mipmap level during texture definitionglTexImage*D( GL_TEXTURE_*D, level, … )

• GLU mipmap builder routinesgluBuild*DMipmaps( … )

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_CLAMPwrapping

GL_REPEATwrapping

18

Texture Functions

• Controls how texture is appliedglTexEnv{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 OperationsTexture Blending Operations:: Replace 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 OperationsTexture Blending Operations:: Modulate 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: Cf=Ct and Af=At

modulate: Cf=CtCl and Af=At Al decal: Cf=(1-At)Cl+AtCt and Af=Al

where the subscript f denotes final, t texture color, and l lighting color.

In some systems, Af is often set to AtAl 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!!

texelgrid

pixel

25 25

Magnification

Two common techniques for magnification are nearest neighbor and bilinear interpolation. In general, bilinear interpolation is better.

ab

34b

12a

i)t1(tii

i)s1(sii

i)s1(sii

s

i1 i2

i4

i3

t

ia

ib

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!!

texelgrid

pixel

no influence

nearest neighbor bilinear interpolation

Imagine a texturewhere x is blackand all the othersare 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 2m×2n texels, or sometimes even 2m×2m 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 1 level 2

28 28

Mipmapping (cont’d)

Consider the two perfect cases. If a pixel covers 22×22 texels, go to level 2 and get a texel. If a pixel covers 21×21 texels, go to level 1 and get a texel.

In general, which level to go?

a texel

level 0 level 1 level 2

a pixel’s center

level 0 level 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=log24=2. As the pixel center normally does not coincide with a texel’s center, we

need bilinear interpolation. a pixel’s center

texelgrid

level 0 level 1 level 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 v1. Go to level 2, and do bilinear interpolation to get v2. Do linear interpolation between v1 and v2: 0.3*v1+0.7*v2. It’s a tri-linear interpolation.

level 2level 1

a pixel’s center

texelgrid

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 2level 1

a pixel cell’squadrilateral

texelgrid

level 0

a texelThe 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

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

4 8 12 16

3 6 9 12

2 4 6 8

1 2 3 4

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)