texture mapping. typical application: mapping images on geometry 3d geometry (quads mesh) + rgb...

62
Texture Mapping

Upload: gary-maxwell

Post on 16-Dec-2015

237 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping

Page 2: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Typical application: mapping images on

geometry

3D geometry(quads mesh)

+

RGB texture 2D

(color-map)

=

Page 3: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

More examples

Page 4: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping

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

Each texture is an array1D, 2D o 3D

of Texels (texture elements) of the same type

Page 5: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texels

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"

– ...

Page 6: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

More examples

+ =

Page 7: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping: History 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 Ed Catmull

(MEGA-MEGA-GURU)

Page 8: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Notation

Texture 2D

u

v

texel

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

A Texture is definedIn the region [0,1] x [0,1]

of the "parametric space"

51

2 te

xels

1024 texels

1.0

1.0

Page 9: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Space

u

v

Texture Mapping We associate to each vertex (of each

triangle) its u,v coordinates in the texture space

Screen Space

x0,y0

x2,y2

x1,y1

u0,v0

u1,v1

u2,v2

Position of the 1st vertex

Attributes of the 1st vertex

u0,v0u1,v1

u2,v2

Page 10: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping More precisely, we define a mapping

between the 3D triangle 3D and a texture triangle

Texture SpaceScreen Space

Page 11: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping each fragment has its own u,v coordinates in

the texture space

Texture SpaceScreen Space

texture look-up

Page 12: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Mapping

Screen buffer

Texture RAM

interpolationtexture

coordinates

texture coordinat

esinterpolat

ed

including:texture

coordinates

(per vertex!)

texture look-up

Frag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 13: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Problem: linear interpolation of texture

coordinates

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

V1

V2

V3

p

3R 2R

f(p)

f( v1 )

f( v2 )

f( v3 )

projection f

p has barycentric coordinates a,b,cIn the triangle v1 v2 v3

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

Page 14: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Problem: linear interpolation of texture

coordinatesExample:

u

v

1

1u,v= (1,0)

u1,v1= (1,1)

u1,v1= (0,1)

u1,v1= (0,0)

Page 15: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Problem: linear interpolation of texture

coordinatesExample:

Page 16: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Solution: Perspective Correction

p has barycentric coordinates c0 c1 c2

V0

V2

V1

A0,B0 ...

A1,B1 ...

A2,B2 ...

p

p = c0 v0 + c1v1 + c2v2

Attributes of p:(not considering the “perspective correction")

Ap = c0 A0 + c1A1 + c2A2

Bp = c0 B0 + c1B1 +

c2B2

= ( x0 , y0 , z0 , w0 )

Page 17: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Solution: Perspective Correction

p has barycentric coordinates c0 c1 c2

V0

V2

V1

A0,B0 ...

A1,B1 ...

A2,B2 ...

p

p = c0 v0 + c1v1 + c2v2

Attributes of p:(not considering the “perspective correction")

w0 w1

Ap = c0 A0 + c1A1 + c2A2

A0

w0

A1

w1

A2

w2Ap =Ap = c0 A0 + c1A1 + c2A2

1 1 1w2

= ( x0 , y0 , z0 , w0 )

Page 18: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Solution: Perspective Correction

Screen buffer

Original attribute

A

Apply transformatio

ns

compute: A' = A / w

andw' = 1 / w

c0 + c1 + c2 A0

w0

A1

w1

A2

w2Ap = 1w0

1w1

1w2

interpolate

A' and w'

Final fragment attribute:

A' / w' c0 + c1 + c2 Fr

ag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 19: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Perspective Correction Without With

Page 20: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Perspective Correction

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

Page 21: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Note: the texture must be loaded

Screen buffer

Texture RAM

L O A DFr

ag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 22: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Note: the texture must be loaded

1. From hard disk to main RAM memory• (in the motherboard)

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

Page 23: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGLAs an example:

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);

Page 24: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

Screen buffer

Texture RAM

interpolation

texturecoordinates

Interpolated

texturecoordinate

s

including:coordinat

estexture

(per vertex!)

texture look-up

including:texture

coordinates

(per vertex!)

Frag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 25: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

2 possibilities:– Computing textures coordinates on the fly

• During the rendering…– Precomputing

• (and store them within the mesh)

The choice is application-dependent!

Page 26: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Difficult problem: u-v mapping

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

u

v

u

v

Page 27: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Difficult problem: u-v mapping

Hand-madeor automated

Page 28: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL

Like any other attribute

TexCoord2d( u,v )

Page 29: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

Screen buffer

Texture RAM

interpolating

texture coordinate

s

texture coordinate

s

interpolated

texture look-up

texture coordinat

es(transformed)

including:texture

coordinates

Frag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 30: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

2 possibilities:– Computing textures coordinates on the fly

• During the rendering…

– Precomputing• (and store them within the mesh)

Page 31: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

Screen buffer

Texture RAM

interpolating

texture coordinates

Interpolated

texture coordinat

es

texture look-up

texture coordinate

s

computetexture

coordinates

Using the position

Frag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 32: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Automatically computed Idea: from (x,y,z) to (u,v) - Linearly Using object or view coordinate

– (before or after the trasformation) Examples:

Page 33: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Automatically computed

Even 1D

1D texture!

Page 34: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Assigning texture coordinates to vertices

2 possibilities:– Computing textures coordinates on the fly

• During the rendering…

– Precomputing• (and store them within the mesh)

Page 35: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

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

The texture coordinate is the transformed normal!

Page 36: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Environment mapping: spherical

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

simulates a complex material(fixed lighting)

Page 37: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Environment mapping: cube

front right back

below

above

left

Page 38: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Environment mapping: cube

Screen buffer

Texture RAM

interpolating

3D texturecoordinate

s

interpolated

coordinates 3D

texture

Project on the cube, look-up the

corresponding face

compute3D Texturecoordinates

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

As view ray reflexed by the

normal

Frag

men

ts&

in

terp

ola

ted

att

rib

ute

s

Vert

ices

& t

heir

att

rib

ute

s

Pro

ject

ed

V

ert

ices

& c

om

pu

ted

att

rib

ute

s

Triangles

rasterizer

set-up

Segments rasterizer

set-up

points rasterizer

set-up

vert

ex

com

puta

tion

Fragm

ent

com

puta

tions

Page 39: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Environment mapping: cube

front right back

below

above

left

Page 40: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Environment mapping: cube and spherical

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

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

Page 41: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Automated computation of texture coordinates

glEnable(GL_TEXTURE_GEN_S);

1- abilitate:

2- choice of the mode:

glTexGeni(GL_S , GL_TEXTURE_GEN_MODE , mode )

S, T, R, Q

mode =

GL_OBJECT_LINEAR

GL_EYE_LINEAR

GL_SPHERE_MAP

Computes the texture coordinates from the position in object

coordinates (before the trasformation) Computes the texture

coordinates from the position in view coordinates

(after the MODEL-VIEW) The texture coordinates is the

reflexed view ray (using the normal)(after the MODEL-VIEW)

Page 42: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Automated computation of texture coordinates

glTexGenfv(GL_S, GL_EYE_PLANE , v);

3- choice of the plane S, T, R, Q

EYE OBJECTor

4 elements vectorThe resulting texture coordinate = vT • pos_vertex

(It’s the distance from the plane!)

Page 43: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

u

Texture Look-up out of bounds: “clamp” mode

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

1

1

v

Page 44: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Look-up out of bounds: “repeat” mode

u

v

1

1

u ← u – [ u ]

v ← v – [ v ]

Page 45: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Repeated textures

Typical use:

Very space-efficient!

Note: the texture must be TILEABLE

Page 46: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );

or

note: u and v treated

separately

example: repeat u and clamp v

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

Page 47: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Texture Look-up A fragment can have non-integer coordinates

(in texels)

Texture SpaceScreen Space

texture look-up

Page 48: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

pixel

Texture Look-up

Texture SpaceScreen Space

pixel texel

one pixel = less than one texel

one pixel = more than one texel

minification

magnification

Page 49: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Magnification

Solution 1:Use the texel containg the pixel

(that is, the texel whose centeris closest to the u,v coordinates

of the fragment)

Equivalent to rounding upthe texel coordinatesto the nearest integer

"Nearest Filtering"0.5 1.5 2.5 3.5 4.5 5.5 6.5

0.5

1.5

2.5

3.5

4.5

5.5

6.5

7.5

u

v

7.5

Page 50: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Magnification

texture 128x128

Nearest Filtering: result

"texels are visible !"

Page 51: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Magnification

Solution 2:Compute the average of the four

closest texels

Bilinear Interpolation

0.5 1.5 2.5 3.5 4.5 5.5 6.5

0.5

1.5

2.5

3.5

4.5

5.5

6.5

7.5

u

v

7.5

Page 52: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Magnification

texture 128x128

Bilinear Interpolation: result

Page 53: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Magnification

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

Page 54: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Minification

Nearest FilteringBilinear interpolation

Does not solve the problem

Page 55: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Minification: MIP-mapping

MIP-map level 0

MIP-map level 1

MIP-map level 2

MIP-map level 3

MIP-map level 4

(only one texel)

MIP-mapping: "Multum In Parvo"

Page 56: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Mipmap Math

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

Page 57: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Minification: MIP-mapping

Bilinear interpolationMIP-mapping

Page 58: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

Minification: MIP-mapping

0

1

2345

Other example

Page 59: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

or

Choose the magnification filter:

Page 60: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode );

mode = GL_NEAREST GL_LINEAR

GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR

where

Choose the minification filter:

Trilinear interpolation

Page 61: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL

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);

Page 62: Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =

In OpenGL 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 (