- 181 Views
- Uploaded on
- Presentation posted in: General

Texture Mapping

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Texture Mapping

=

+

RGB texture 2D

(color-map)

3D geometry

(quads mesh)

- The fragment operations can access a specialized RAM
- The Texture RAM
- Organized in a set of Textures

- Each texture is an array
1D, 2D o 3D

of Texels (texture elements) of the same type

- Typical examples of texels:
- each texel is a color (components: R-G-B, or R-G-B-A)
- The texture is a "color-map"

- each texel is an alpha value
- the texture is an "alpha-map"

- each texel is a normal (components: X-Y-Z)
- the texture is a "normal-map" or "bump-map"

- each texel contains a specularity value
- the texture is a "shininess-map"

- ...

- each texel is a color (components: R-G-B, or R-G-B-A)

+

=

Ed Catmull

(MEGA-MEGA-GURU)

- 1974 introduced by Ed Catmull
- In its Phd Thesis

- Only in 1992 (!) we have texture mapping hardware
- Silicon Graphics RealityEngine

- 1992 on: increasingly used and integrated in graphic cards
- First of all by low end graphic boards

- Today: a fundamental rendering primitive
- the main image-based technique

v

1.0

512 texels

u

1.0

1024 texels

texel

Texture Space

(or "parametric space" or "u-v space")

Texture 2D

A Texture is defined

In the region [0,1] x [0,1]

of the "parametric space"

v

u1,v1

u0,v0

u1,v1

Position of the

1st vertex

Attributes of the

1st vertex

u0,v0

u2,v2

u2,v2

u

Texture Space

- We associate to each vertex (of each triangle) its u,v coordinates in the texture space

x1,y1

x0,y0

x2,y2

Screen Space

- More precisely, we define a mapping between the 3D triangle 3D and a texture triangle

Screen Space

Texture Space

texture look-up

- each fragment has its own u,v coordinatesin the texture space

Screen Space

Texture Space

texturelook-up

including:

texture

coordinates

(per vertex!)

interpolation

texture

coordinates

texture coordinatesinterpolated

set-up

points rasterizer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

Screen

buffer

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

Texture RAM

- Not true for perspective projection!
- It is only an approximation
- It works fine to interpolate colors, normals, ..
- Not applicable to interpolate texture coordinates...

f( v3 )

V3

f( v2 )

projection f

f(p)

p

V2

f( v1 )

V1

f(p) has barycentric coordinates a,b,c in the triangle f(v1)f(v2) f(v3)

p has barycentric coordinates a,b,c

In the triangle v1v2v3

v

1

u

1

u,v= (1,0)

u1,v1= (1,1)

u1,v1= (0,0)

u1,v1= (0,1)

- Example:

- Example:

A1,B1...

A0,B0...

p

A2,B2...

- p has barycentric coordinates c0 c1 c2

p =c0 v0 +c1v1+c2v2

V1

V0

= ( x0, y0, z0, w0)

Attributes of p:

(not considering the “perspective correction")

V2

Ap=c0 A0 +c1A1+c2A2

Bp=c0 B0 +c1B1+c2B2

A1,B1...

A0,B0...

A2,B2...

- p has barycentric coordinates c0 c1 c2

p =c0 v0 +c1v1+c2v2

Attributes of p:

(not considering the “perspective correction")

V1

V0

= ( x0, y0, z0, w0)

p

V2

A0

A1

A2

Ap =c0 A0+c1A1+c2A2

w0

w1

w2

Ap=

w0

w1

w2

1

1

1

Ap =c0 A0+c1A1+c2A2

set-up

points rasterizer

Screen

buffer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

interpolate

A'and w'

Final fragment attribute:

A'/w'

Apply transformations

compute: A' = A / w

and

w' = 1 / w

A0

Original attribute A

A1

A2

c0 +c1+c2

w0

w1

w2

Ap=

1

1

1

c0 +c1+c2

w0

w1

w2

- Without

With

- Texture mapping with perspective correction
- Also known as Perfect texture mapping

LOAD

Texture RAM

set-up

points rasterizer

Screen

buffer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

- From hard disk to main RAM memory
- (in the motherboard)

- From main RAM memory to Texture RAM
- (on board of the graphics HW)
Both steps are quite slow. It is not possible to accomplish them once per frame!

- (on board of the graphics HW)

glEnable(GL_TEXTURE_2D);

glBindTexture (GL_TEXTURE_2D, ID);

glTexImage2D (

GL_TEXTURE_2D,

0, // mipmapping

GL_RGB, // original format

imageWidth, imageHeight,

0, // border

GL_RGB, // RAM format

GL_UNSIGNED_BYTE,

imageData);

- As an example:

texturelook-up

including:

coordinatestexture

(per vertex!)

including:

texture

coordinates

(per vertex!)

interpolation

texture

coordinates

Interpolated

texture

coordinates

set-up

points rasterizer

Screen

buffer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

Texture RAM

- 2 possibilities:
- Computing textures coordinates on the fly
- During the rendering…

- Precomputing
- (and store them within the mesh)

- Computing textures coordinates on the fly

The choice is application-dependent!

- Associate texture coordinates to each vertex of the mesh
- During preprocessing

v

v

u

u

Hand-made

or automated

- Like any other attribute

TexCoord2d( u,v )

texturelook-up

interpolating

texture coordinates

texture coordinates interpolated

set-up

points rasterizer

Screen

buffer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

texture coordinates

(transformed)

including:

texture

coordinates

Texture RAM

- 2 possibilities:
- Computing textures coordinates on the fly
- During the rendering…

- Precomputing
- (and store them within the mesh)

- Computing textures coordinates on the fly

texturelook-up

Interpolated

texture coordinates

interpolating

texture coordinates

set-up

points rasterizer

Screen

buffer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

set-up

Segments rasterizer

compute

texture coordinates

Using the position

texture coordinates

Texture RAM

- Idea: from (x,y,z) to (u,v) - Linearly
- Using object or view coordinate
- (before or after the trasformation)

- Examples:

1D texture!

- Even 1D

- 2 possibilities:
- Computing textures coordinates on the fly
- During the rendering…

- Precomputing
- (and store them within the mesh)

- Computing textures coordinates on the fly

Environment map: a texture containing the color of the environment “reflexed by each normal of the half-sphere”.

The texture coordinate is the transformed normal!

Simulates a mirror-like object reflecting a far-away background

simulates a complex material

(fixed lighting)

above

left

front

right

back

below

interpolating

3D texture

coordinates

interpolated

coordinates 3D texture

set-up

points rasterizer

Projected Vertices

& computed attributes

Fragments

& interpolated attributes

Vertices

& their attributes

set-up

Triangles rasterizer

vertexcomputation

Fragment

computations

Screen

buffer

set-up

Segments rasterizer

Project on the cube, look-up the corresponding face

compute

3D Texture

coordinates

[-1,+1] x [-1,+1] x [-1,+1]

As view ray reflexed by the normal

Texture RAM

above

left

front

right

back

below

- Spherical:
- one texel for each direction in the half-sphere
- Projected on a circle

- the texture coordinate is the normal
- It has the "headlight“ effect:
- I can only rotate the object while the viewpoint does not change

- one texel for each direction in the half-sphere

- Cube
- one texel for each direction in the sphere
- Projected on the surface of the cube

- the texture coordinate is the view direction reflexed by the normal
- The viewpoint can rotate around a steady object

- one texel for each direction in the sphere

S, T, R, Q

1- abilitate:

glEnable(GL_TEXTURE_GEN_S);

2- choice of the mode:

glTexGeni(GL_S , GL_TEXTURE_GEN_MODE , mode )

Computes the texture coordinates from the position in object coordinates

(before the trasformation)

GL_OBJECT_LINEAR

GL_EYE_LINEAR

GL_SPHERE_MAP

Computes the texture coordinates from the position in view coordinates

(after the MODEL-VIEW)

=

mode

The texture coordinates is the reflexed view ray (using the normal)

(after the MODEL-VIEW)

3- choice of the plane

S, T, R, Q

glTexGenfv(GL_S, GL_EYE_PLANE , v);

or

EYE OBJECT

4 elements vector

The resulting texture coordinate = vT• pos_vertex

(It’s the distance from the plane!)

v

1

u

1

if (u<0) u←0; if (u>1) u←1;

if (v<0) v←0; if (v>1) v←1;

u ← u – [ u ]

v ← v – [ v ]

v

1

u

1

- Typical use:

Note: the texture must be TILEABLE

Very space-efficient!

note:

u and v treated separately

example: repeat u and clamp v

Texture parameters. each texture loaded in memory has its own parameters.

glTexParameteri(

GL_TEXTURE_2D,

GL_TEXTURE_WRAP_S,

GL_CLAMP );

or

glTexParameteri(

GL_TEXTURE_2D,

GL_TEXTURE_WRAP_S,

GL_REPEAT );

texture look-up

- A fragment can have non-integer coordinates
(in texels)

Screen Space

Texture Space

texel

pixel

magnification

pixel

minification

one pixel = less than one texel

Screen Space

Texture Space

one pixel = more than one texel

7.5

6.5

5.5

4.5

3.5

2.5

1.5

0.5

0.5

1.5

3.5

4.5

5.5

6.5

2.5

v

Solution 1:

Use the texel containg the pixel

(that is, the texel whose center

is closest to the u,v coordinates

of the fragment)

Equivalent to rounding up

the texel coordinates

to the nearest integer

"Nearest Filtering"

7.5

u

"texels are visible !"

Nearest Filtering: result

texture 128x128

7.5

6.5

5.5

4.5

3.5

2.5

1.5

0.5

0.5

1.5

3.5

4.5

5.5

6.5

2.5

v

Solution 2:

Compute the average of the four closest texels

Bilinear Interpolation

7.5

u

Bilinear Interpolation: result

texture 128x128

- Nearest filtering:
- Texels are visible
- Ok if texel borders are useful
- More efficient

- Bilinear Interpolation
- Usually provides better quality
- Less efficient
- Sometimes there is an “out-of-focus“ effect

Bilinear interpolation

Does not solve the problem

Nearest Filtering

MIP-map

level 3

MIP-map

level 2

MIP-map

level 1

MIP-map

level 4

(only one texel)

MIP-mapping: "Multum In Parvo"

MIP-map

level 0

- Define a scale factor, =texels/pixel
- is the maximum between x and y
- It can vary in the same triangle
- Can be derived from the transformation matrices, computed for the Vertices and interpolated for the fragments

- The mipmap level to use is: log2
- level 0 = maximum resolution
- if level<0 what is the reason?
- note: the level might not be an integer

MIP-mapping

Bilinear interpolation

5

4

3

2

1

Other example

0

Choose the magnification filter:

glTexParameteri(

GL_TEXTURE_2D,

GL_TEXTURE_MAG_FILTER,

GL_NEAREST);

or

glTexParameteri(

GL_TEXTURE_2D,

GL_TEXTURE_MAG_FILTER,

GL_LINEAR );

Trilinear interpolation

Choose the minification filter:

glTexParameteri(

GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER,

mode );

where

mode= GL_NEAREST

GL_LINEAR

GL_NEAREST_MIPMAP_NEAREST

GL_LINEAR_MIPMAP_NEAREST

GL_NEAREST_MIPMAP_LINEAR

GL_LINEAR_MIPMAP_LINEAR

- Load on the graphics card all the mipmapping levels.
- One-by-one:

glTexImage2D (

GL_TEXTURE_2D,

i, // MIP-map level

GL_RGB, // original format

imageWidth, imageHeight,

0, // border

GL_RGB, // RAM format

GL_UNSIGNED_BYTE,

imageData);

- Load on the graphics card all the mipmapping levels.
- All together (using the glu library):

glTexImage2D (

GL_TEXTURE_2D,

0, // MIP-map level

GL_RGB, // original format

imageWidth, imageHeight,

0, // border

GL_RGB, // RAM format

GL_UNSIGNED_BYTE,

imageData);

gluBuild2DMipmaps (