Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf ·...

59
Computer Graphics Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhi

Transcript of Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf ·...

Page 1: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Computer Graphics

Subodh Kumar

Dept of Computer Sc. & Engg. IIT Delhi

Page 2: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture: Control Illumination

! Vary a surface property non-linearly ! Rasterization does bi-linear interpolation

! Examples of usage: ! Paste a picture

! picture of environment ! picture of scene

! Modify surface normals ! Create bumps and detail

too pristine

Page 3: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture: Control Illumination

! Vary a surface property non-linearly ! Rasterization does bi-linear interpolation

! Examples of usage: ! Paste a picture

! picture of environment ! picture of scene

! Modify surface normals ! Create bumps and detail

too pristine

Page 4: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Mapping

0,0 1

1Texture image

0,00,0.5 0,1

1,0 1,1

1,0.5

u

v

Vertex Texture coordinates Interpolated at each fragment

Page 5: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Examples

Normal MappingBump Mapping

Page 6: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Examples

Normal MappingBump Mapping

Page 7: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Examples

Normal MappingBump Mapping

Page 8: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Mapping

! Texture aka function of a surface ! f (x,y,z) = some attribute value ! Would like f to be arbitrary

! No continuity constraints

! 3D Look-up table? ! Not all x,y,z has the surface through it ! Surface is only a 2-manifold

! 2D look-up table ! But want to map every point on a surface to a 2D

space <u,v> ! parameterization

Page 9: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Space

2D Euclidean/Projective Space

u

vCompute f(u,v)

Page 10: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Space

2D Euclidean/Projective Space

u

vCompute f(u,v)

Sample f(u,v) Reconstruct as needed

Page 11: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Space

2D Euclidean/Projective Space

u

vCompute f(u,v)

Sample f(u,v) Reconstruct as needed

Restrict domain Or, recreate samples

Page 12: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

v =1

Texture Space

2D Euclidean/Projective Space

u

vCompute f(u,v)

Sample f(u,v) Reconstruct as needed

Restrict domain Or, recreate samples

u=1

Page 13: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

v =1

Texture Space

2D Euclidean/Projective Space

u

vCompute f(u,v)

Sample f(u,v) Reconstruct as needed

Restrict domain Or, recreate samples

u=1

Page 14: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Map from Object SpaceB

C

P

P = uA + vB + wC

Should P’s texture coordinates = (u, v)?

Barycentric Coordinates of P = [u, v, w]

u = |PBC|/|ABC| etc..

A

Page 15: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Coordinates

A

B

C

P

P = uA + vB + wC

Barycentric Coordinates of P = [u, v, w]

u = |PBC|/|ABC| etc..

Allow indirection: T(P) = uT(A) + vT(B) + wT(C)

Page 16: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Parameterization

10

Page 17: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Parametrically defined surface

11

x(t), y(t), z(t)x(t) = at2 + bt + c

x(u, v), y(u, v), z(u, v)

Page 18: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Surface Parameterization

u

v

Page 19: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Surface Parameterization

u

v

Page 20: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

General Parameterization

13

Page 21: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

General Parameterization

13

Energy = 1/2 ∑ kele2

Force on vertex i = ∑ kij eijvertex j connected to i

all edges e

pi

Page 22: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

General Parameterization

13

Energy = 1/2 ∑ kele2

Force on vertex i = ∑ kij eijvertex j connected to i

all edges e

Net Force on pi = ∑kij (pi – pj)

pi

∑kij pi = ∑kij pj

𝞴ij = kij/∑kij

j i

Page 23: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

General Parameterization

13

Energy = 1/2 ∑ kele2

Force on vertex i = ∑ kij eijvertex j connected to i

all edges e

Net Force on pi = ∑kij (pi – pj)

pi

∑kij pi = ∑kij pj

pi = ∑𝞴ij pj 𝞴ij = kij/∑kijaffine combination

j i

Page 24: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

‘Mean’ Barycentric Coordinates

14

Pj αij

βij

kij =tan

αij

2+ tan

βij

2|eij |

Mean value Coordinates

On the surface

Pi

Page 25: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

‘Mean’ Barycentric Coordinates

14

Pj αji

βji αij

βij

kij =tan

αij

2+ tan

βij

2|eij |

Mean value Coordinates

On the surface

Pi

Page 26: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

‘Mean’ Barycentric Coordinates

14

Pj αji

βji αij

βij

kij =tan

αij

2+ tan

βij

2|eij |

Mean value Coordinates

On the surface

Pi

Page 27: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Fix shape

Parameterize by chord-length

‘Mean’ Barycentric Coordinates

14

Pj αji

βji αij

βij

kij =tan

αij

2+ tan

βij

2|eij |

Mean value Coordinates

r

l = ∑li

li

2πr li/l

On the surface

Pi

Page 28: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Coordinates

u

v

Page 29: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Pixel Texture Coordinates

Screen

Pixel

Texture

Object

Page 30: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Pixel Texture Coordinates

Screen

Pixel

Texture

Object

Page 31: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Pixel Texture Coordinates

Screen

Pixel

Texture

Object

Page 32: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Pixel Texture Lookup

Pixel

Texel

Texture sample

Look up texture coordinate

Reconstruct from samples

Page 33: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture-Minification

Pixels

Texels

Page 34: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture-Minification

Pixels

Texels

Page 35: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture-Minification

Pixels

Texels

Page 36: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture-Minification

Pixels

Texels

These texels should also contribute to the pixel

Page 37: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture-Minification

Pixels

Texels

These texels should also contribute to the pixel

Page 38: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

! Filter 2x2 texel blocks into one texel ! Weighted average

! Log(n) levels of an nxn texture map ! Choose level closest to 1 texel/pixel

! Change in texture coordinate per pixel

Mip-Mapping

Page 39: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

! Filter 2x2 texel blocks into one texel ! Weighted average

! Log(n) levels of an nxn texture map ! Choose level closest to 1 texel/pixel

! Change in texture coordinate per pixel

Mip-Mapping

Page 40: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

! Filter 2x2 texel blocks into one texel ! Weighted average

! Log(n) levels of an nxn texture map ! Choose level closest to 1 texel/pixel

! Change in texture coordinate per pixel

Mip-Mapping

Page 41: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Derivative

Pixels

t1

Tex coord: t0

Texels

WxH texture => texel size = 1/W x 1/H

t1 – t0 = (∆u , ∆v )

t2 t3

Page 42: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Derivative

Pixels

t1

Tex coord: t0

Texels

WxH texture => texel size = 1/W x 1/H

t1 – t0 = (∆u , ∆v )

t2 t3

t2 – t0 = (∆uy , ∆vy)

∆t = max (∆ux , ∆uy), max(∆vx , ∆vy)

x x

t-1

∆upixel = max(du/dx du/dy) ∆vpixel = max(dv/dx dv/dy)

Page 43: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Derivative

Pixels

t1

Tex coord: t0

Texels

WxH texture => texel size = 1/W x 1/H

t1 – t0 = (∆u , ∆v )

t2 t3

t2 – t0 = (∆uy , ∆vy)

∆t = max (∆ux , ∆uy), max(∆vx , ∆vy)

x x

t-1

∆upixel = max(du/dx du/dy) ∆vpixel = max(dv/dx dv/dy)

Choose a level so that ∆u and ∆v are near the texel size

Page 44: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Anisotropic Filter

Pixels

Texels

Page 45: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Anisotropic Filter

Pixels

Texels

Page 46: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Anisotropic Filter

Pixels

Texels

Page 47: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Anisotropic Filter

Pixels

Texels

Using Smallest minification may blur too much

Page 48: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Anisotropic Filter

Pixels

Texels

Using Smallest minification may blur too much

Page 49: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

Page 50: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

Page 51: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

Page 52: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

Page 53: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

W

Page 54: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Texture Border

! Extra texel needed for linear interpolation

u = 0

u' = u(W-2)/W+1/W

W

Page 55: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

Textures in OpenGL! Texture = Object containing images (1 or more)

! Encapsulates: Image, Sampler, Parameters ! Has an ID/handle for reference

! Texture has an image format ! 1D, 2D, 3D organization

! Dimension: WxHxD ! Several specialized types

! Upto 4 components per sample ! Data format of each component

! Read or Write to it ! Use one of several ‘texture units’ to apply a texture ! Look-up with tex-coord [u,v,w]

! Also an LOD [MIP-MAP level of detail] ! Can be interpolated from per-vertex tex-coord 23

Page 56: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

uniform sampler2D colorSampler; uniform sampler2D normalSampler; out vec3 color;

void main() { // Formats are in range 0:1 vec3 tcolor = texture(colorSampler, UVcolor).rgb; vec3 normal = normalize(texture(normalSampler, UVnormal).rgb*2.0 - 1.0);

color = shade(tcolor, normal, ..); }

SHADER

Example

24

Page 57: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

uniform sampler2D colorSampler; uniform sampler2D normalSampler; out vec3 color;

void main() { // Formats are in range 0:1 vec3 tcolor = texture(colorSampler, UVcolor).rgb; vec3 normal = normalize(texture(normalSampler, UVnormal).rgb*2.0 - 1.0);

color = shade(tcolor, normal, ..); }

SHADER

Example

24

glUseProgram(program); GLint colorSampler = glGetUniformLocation(program, "colorSampler"); GLint normalSampler = glGetUniformLocation(program, "normalSampler"); glUniform1i(colorSampler, 0); // Associate Texture unit 0 with this sampler glUniform1i(normalSampler, 5); // Associate Texture unit 5 with this sampler

glActiveTexture(GL_TEXTURE0); // We will set up Texture unit 0 glBindTexture(GL_TEXTURE_2D, colorMap); // Buffer filled earlier glBindSampler(0, linearSampler); glActiveTexture(GL_TEXTURE0 + 5); // May also use GL_TEXTURE5 glBindTexture(GL_TEXTURE_2D, normalMap); // Buffer filled earlier glBindSampler(5, linearSampler);

render();

RENDERING

Page 58: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

uniform sampler2D colorSampler; uniform sampler2D normalSampler; out vec3 color;

void main() { // Formats are in range 0:1 vec3 tcolor = texture(colorSampler, UVcolor).rgb; vec3 normal = normalize(texture(normalSampler, UVnormal).rgb*2.0 - 1.0);

color = shade(tcolor, normal, ..); }

SHADER

Example

24

glUseProgram(program); GLint colorSampler = glGetUniformLocation(program, "colorSampler"); GLint normalSampler = glGetUniformLocation(program, "normalSampler"); glUniform1i(colorSampler, 0); // Associate Texture unit 0 with this sampler glUniform1i(normalSampler, 5); // Associate Texture unit 5 with this sampler

glActiveTexture(GL_TEXTURE0); // We will set up Texture unit 0 glBindTexture(GL_TEXTURE_2D, colorMap); // Buffer filled earlier glBindSampler(0, linearSampler); glActiveTexture(GL_TEXTURE0 + 5); // May also use GL_TEXTURE5 glBindTexture(GL_TEXTURE_2D, normalMap); // Buffer filled earlier glBindSampler(5, linearSampler);

render();

RENDERINGglGenTextures(1, &colorMap); glBindTexture(GL_TEXTURE_2D, colorMap); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0, GL_RGB,

GL_UNSIGNED_BYTE, data); glGenTextures(1, &normalMap); glBindTexture(GL_TEXTURE_2D, normalMap); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0, GL_RGB, GL_FLOAT, data);

glGenSamplers(1, &linearSampler); glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

INITIALIZE

Page 59: Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhisubodh/courses/CSL781/pdfslides/textures.pdf · Texture Mapping 0,0 1 1 Texture image 0,0 0,0.5 0,1 1,0 1,1 1,0.5 u v Vertex Texture

uniform sampler2D colorSampler; uniform sampler2D normalSampler; out vec3 color;

void main() { // Formats are in range 0:1 vec3 tcolor = texture(colorSampler, UVcolor).rgb; vec3 normal = normalize(texture(normalSampler, UVnormal).rgb*2.0 - 1.0);

color = shade(tcolor, normal, ..); }

SHADER

Example

24

glUseProgram(program); GLint colorSampler = glGetUniformLocation(program, "colorSampler"); GLint normalSampler = glGetUniformLocation(program, "normalSampler"); glUniform1i(colorSampler, 0); // Associate Texture unit 0 with this sampler glUniform1i(normalSampler, 5); // Associate Texture unit 5 with this sampler

glActiveTexture(GL_TEXTURE0); // We will set up Texture unit 0 glBindTexture(GL_TEXTURE_2D, colorMap); // Buffer filled earlier glBindSampler(0, linearSampler); glActiveTexture(GL_TEXTURE0 + 5); // May also use GL_TEXTURE5 glBindTexture(GL_TEXTURE_2D, normalMap); // Buffer filled earlier glBindSampler(5, linearSampler);

render();

RENDERINGglGenTextures(1, &colorMap); glBindTexture(GL_TEXTURE_2D, colorMap); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0, GL_RGB,

GL_UNSIGNED_BYTE, data); glGenTextures(1, &normalMap); glBindTexture(GL_TEXTURE_2D, normalMap); glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, w, h, 0, GL_RGB, GL_FLOAT, data);

glGenSamplers(1, &linearSampler); glSamplerParameteri(linearSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glSamplerParameteri(linearSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

INITIALIZE

glGenerateTextureMipmap(textureID); glSamplerParameterf(samplerID, GL_TEXTURE_MAX_ANISOTROPY, 4.0f);

textureProj(sampler, projTexCoord); textureLod(sampler, texCoord, lod); textureGrad(sampler, texCoord, dtdx, dtdy);

ALSO SEE