28
Texture Mapping Texture Mapping CAP4730: Computational Structures in Computer Graphics

Texture Mapping CAP4730: Computational Structures in Computer Graphics

Embed Size (px)

Citation preview

Page 1: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Texture MappingTexture Mapping

CAP4730: Computational Structures in Computer Graphics

Page 2: Texture Mapping CAP4730: Computational Structures in Computer Graphics

OutlineOutline

What is texture mapping?What is texture mapping? Math behind texture mappingMath behind texture mapping OpenGL texture mappingOpenGL texture mapping Advanced Topics in texture Advanced Topics in texture

mappingmapping

Page 3: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Marching towards Visual Marching towards Visual RealismRealism

http://www.3drender.com/jbirn/productions.html

Page 4: Texture Mapping CAP4730: Computational Structures in Computer Graphics

How do we do this?How do we do this?

Page 5: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Another ExampleAnother Example

Page 6: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Using only TrianglesUsing only Triangles

Using only triangles to Using only triangles to model everything is hardmodel everything is hard

Think about a label on a Think about a label on a soup cansoup can

Instead of interpolating Instead of interpolating colors, map a patterncolors, map a pattern

Run DemoRun Demo How do you create a How do you create a

pattern?pattern?

Page 7: Texture Mapping CAP4730: Computational Structures in Computer Graphics

TextureTexture

A color array that holds an imageA color array that holds an image Similar to a framebufferSimilar to a framebuffer gluint buffer[x*y*3]gluint buffer[x*y*3] 255 0 255255 0 255

Page 8: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Procedural TextureProcedural Texture

Calculate pixel values via functionsCalculate pixel values via functions

Page 9: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Image TexturesImage Textures

Load pixel values from a file

Page 10: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Let’s look at some gamesLet’s look at some games

Page 11: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Texture SpaceTexture Space

How would you store this in your How would you store this in your program?program?

How much memory?How much memory?s

t

s = [0…1]

t = [0…1]

Page 12: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Object SpaceObject Space

s

t

(u0,v0)

(u1,v1)

(u2,v2)

(u3,v3)

Texture -> Object

Transformation

s = [0…1]

t = [0…1]

u = [0…1]

v = [0…1]

Page 13: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Going from pixels to Going from pixels to texturestextures

Object -> Texture Transformation Image -> Object Transformation

(s,t) (u,v) (x,y)

Page 14: Texture Mapping CAP4730: Computational Structures in Computer Graphics

What is the question?What is the question? A pixel (x,y) corresponds to A pixel (x,y) corresponds to

what texture element (s,t)?what texture element (s,t)? Q: How do we convert (Q: How do we convert (ss,,tt) )

into texture element into texture element coordinates?coordinates?

TEXture Elements (TEXELS)TEXture Elements (TEXELS) A: Linearly Interpolate A: Linearly Interpolate

texture coordinates!texture coordinates! http://graphics.lcs.mit.edu/http://graphics.lcs.mit.edu/

classes/6.837/F98/classes/6.837/F98/Lecture21/Slide05.htmlLecture21/Slide05.html

Page 15: Texture Mapping CAP4730: Computational Structures in Computer Graphics

TilingTiling

Let’s look at the texture Let’s look at the texture coordinatescoordinates

s

t

(u0,v0)(u1,v1)

(u2,v2)(u3,v3)

Page 16: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Textures built to TileTextures built to Tile Think Windows BackgroundThink Windows Background

Why would you want this? Discuss the Why would you want this? Discuss the Pros and Cons of tiling. Pros and Cons of tiling.

Look at codeLook at code

0,0

1,10,1

1,0 0,0

2,20,2

2,0

Page 17: Texture Mapping CAP4730: Computational Structures in Computer Graphics

FilteringFiltering

Why does this happen?Why does this happen?(u0,v0)

(u1,v1)

(u2,v2)

Page 18: Texture Mapping CAP4730: Computational Structures in Computer Graphics

FilteringFiltering

Why does this happen?Why does this happen?

What are some alternatives?What are some alternatives?

(uj,vj)(ui,vi)

Page 19: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Combining Lighting + Combining Lighting + TexturingTexturing

If you notice there is no lighting If you notice there is no lighting involved with texture mapping!involved with texture mapping!

They are independent operations, They are independent operations, which MAY (you decide) be which MAY (you decide) be combinedcombined

It all depends on how you “apply” It all depends on how you “apply” the texture to the underlying the texture to the underlying triangletriangle

Page 20: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Combining Lighting + Combining Lighting + TexturingTexturing

CCTT = Texture Color = Texture Color

CCC C = Base Triangle = Base Triangle

Replace, CReplace, CFF = C = CTT

Blend, CBlend, CFF = C = CTT * C * CCC

OpenGL:OpenGL:– glTexEnv(GL_TEXTURE_ENV, glTexEnv(GL_TEXTURE_ENV,

GL_TEXTURE_ENV_MODE, GL_BLEND);GL_TEXTURE_ENV_MODE, GL_BLEND);– http://trant.sgi.com/opengl/docs/man_pages/http://trant.sgi.com/opengl/docs/man_pages/

hardcopy/GL/html/gl/texenv.htmlhardcopy/GL/html/gl/texenv.html

Page 21: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Screen space vs. world Screen space vs. world spacespace

P0(x,y,z)

P1(x,y,z)

V0(x,y)

V1(x,y)

Page 22: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Interpolation in Screen Interpolation in Screen SpaceSpace

P0(x,y,z)

P1(x,y,z)

V0(x’,y’)

V1(x’,y’)

0

0

1

1

0

0

010

z

x

z

xt

z

xtP

VVtVtP

z’=1

Page 23: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Interpolation in World Interpolation in World SpaceSpace

P0(x,y,z)

P1(x,y,z)

V0(x’,y’)

V1(x’,y’)

0

0

1

1

0

0

010

z

x

z

xs

z

xtP

PPsPtP

z’=1

Page 24: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Comparing the twoComparing the two

What do we need to interpolate in What do we need to interpolate in screen space now?screen space now?

What happens if we don’t have Z? What happens if we don’t have Z? What have we discussed that What have we discussed that doesn’doesn’t have Z?

Perspectively Correct Z http://graphics.lcs.mit.edu/http://graphics.lcs.mit.edu/

classes/6.837/F98/Lecture21/classes/6.837/F98/Lecture21/Slide14.htmlSlide14.html

0

0

1

1

0

0

010

z

x

z

xs

z

xtP

PPsPtP

0

0

1

1

0

0

010

z

x

z

xt

z

xtP

VVtVtP

)(

)(

)(

)(

)(

010

010

101

0

010

010

0

0

1

1

0

0

vvsvv

uusuu

zztz

tzs

zzsz

xxsx

z

x

z

xt

z

x

Page 25: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Other solutionsOther solutions

Cost of screen space interpolation Cost of screen space interpolation now?now?

Increase # of trianglesIncrease # of triangles

)(

)(

)(

)(

)(

010

010

101

0

010

010

0

0

1

1

0

0

vvsvv

uusuu

zztz

tzs

zzsz

xxsx

z

x

z

xt

z

x

Page 26: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Linear Interpolation is Linear Interpolation is wrong?wrong?

Gouraud is wrong, but why does it Gouraud is wrong, but why does it look okay?look okay?

Q: Why does Texture mapping not Q: Why does Texture mapping not look right even though we are doing look right even though we are doing the same thing?the same thing?

A: All about the slope of the A: All about the slope of the interpolation and the “gradualness” interpolation and the “gradualness” or “abruptness” of the changeor “abruptness” of the change

Page 27: Texture Mapping CAP4730: Computational Structures in Computer Graphics

TransparenciesTransparencies

We will revisit this later, but we We will revisit this later, but we can use the alpha channel to can use the alpha channel to stencil out textures.stencil out textures.

Thus Thus per pixelper pixel, set alpha = 1 or , set alpha = 1 or alpha = 0. Where alpha = 0, alpha = 0. Where alpha = 0,

Page 28: Texture Mapping CAP4730: Computational Structures in Computer Graphics

Let’s Look at some gamesLet’s Look at some games Pick a favorite game from the classPick a favorite game from the class