textures and normals in ray tracing - cornell universityone (for grayscale, or black and white,...

36
© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6 Textures and normals in ray tracing CS 4620 Lecture 6 1

Upload: others

Post on 27-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Textures and normalsin ray tracing

CS 4620 Lecture 6

1

Page 2: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture mapping

• Objects have properties that vary across the surface

2

Page 3: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture Mapping

• So we make the shadingparameters vary acrossthe surface

[Fol

ey e

t al

. / P

erlin

]

3

Page 4: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture mapping

• Adds visual complexity; makes appealing images

[Pi

xa r /

Toy

Sto

ry]

4

Page 5: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture mapping

• Surface properties are not the same everywhere– diffuse color (kd) varies due to changing pigmentation– brightness (ks) and sharpenss (p) of specular highlight varies

due to changing roughness and surface contamination

• Want functions that assign properties to points on the surface– the surface is a 2D domain– given a surface parameterization, just need function on plane– images are a handy way to represent such functions– can represent using any image representation– raster texture images are very popular

5

Page 6: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture mapping: a technique of defining surface properties (especially shading parameters) in such a way that they vary as a function of position on the surface.

A first definition

• This is very simple!– but it produces complex-looking effects

6

Page 7: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples

• Wood gym floor with smooth finish

– diffuse color kD varies with position

– specular properties kS, n are constant

• Glazed pot with finger prints

– diffuse and specular colors kD, kS are constant

– specular exponent n varies with position

• Adding dirt to painted surfaces• Simulating stone, fabric, …

– to approximate effects of small-scale geometry• they look flat but are a lot better than nothing

7

Page 8: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6 8

Page 9: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6 9

Page 10: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6 10

Page 11: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6 11

Page 12: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Mapping textures to surfaces

• Usually the texture is an image (function of u, v)– the big question of texture mapping: where on the surface

does the image go?– obvious only for a flat rectangle the same shape as the image– otherwise more interesting

12

Page 13: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Mapping textures to surfaces

• “Putting the image on the surface”– this means we need a function f that tells where each point

on the image goes– this looks a lot

like a parametricsurface function

– for parametricsurfaces (e.g. sphere, cylinder)you get f for free

13

Page 14: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture coordinate functions

• Non-parametrically defined surfaces: more to do– can’t assign texture coordinates as we generate the surface– need to have the inverse of the function f

• Texturecoordinate fn.

– when shading pget texture atφ(p)

14

Page 15: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Three spaces

• Surface lives in 3D world space• Every point also has a place where it goes

in the image and in the texture.

15

xy

z u

v

xs

ys

ڶδ

world spaceimage space texture space

Page 16: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture coordinate functions

• Define texture image as a function

– where C is the set of colors for the diffuse component

• Diffuse color (for example) at point p is then

16

Page 17: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Coordinate functions: parametric

• For parametric surfaces you already have coordinates• Need to be able to invert the parameterization• E.g. for a rectangle…• E.g. for a sphere…

17

Page 18: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• For non-parametric surfaces it is trickier– directly use world coordinates

• need to project one out

[Wol

fe /

SG97

Slid

e se

t]

18

Page 19: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• Planar projection

19

u

v

Page 20: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• Spherical projection

20

Page 21: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• Cylindrical projection

21

spherical cylindrical

Page 22: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• Complex surfaces: project parts to parametric surfaces

[Tito

Pag

an]

22

Page 23: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Examples of coordinate functions

• Triangles– specify (u,v) for each vertex– define (u,v) for interior by linear (barycentric) interpolation

(u,v)

(uc,vc)

(ub,vb)

(ua,va)

23

Page 24: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2012 Kavita Bala •(with previous instructors James/Marschner, and some slides courtesy Leonard McMillan)

Cornell CS4620/5620 Fall 2012 • Lecture 21

Example: UVMapper

http://www.uvmapper.com

24

Page 25: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture coordinate functions

• Mapping from S to D can be many-to-one– that is, every surface point gets only one color assigned– but it is OK (and in fact useful) for multiple surface points to

be mapped to the same texture point• e.g. repeating tiles

25

Page 26: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

i = unx

� 0.5

j = v ny

� 0.5© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Pixels in texture images (texels)

• Related to texture coordinates in the same way as normalized image coordinate to pixel coordinates

26

58 3. Raster Images

Figure 3.10. Coordinates of a four pixel by three pixel screen. Note that in some APIs they-axis will point downwards.

Again, these coordinates are simply conventions, but they will be important

to remember later when implementing cameras and viewing transformations.

3.2.1 Pixel Values

So far we have described the values of pixels in terms of real numbers, represent-

ing intensity (possibly separately for red, green, and blue) at a point in the image.

This suggests that images should be arrays of floating-point numbers, with either

one (for grayscale, or black and white, images) or three (for RGB color images)

32-bit floating point numbers stored per pixel. This format is sometimes used,

when its precision and range of values are needed, but images have a lot of pix-

els and memory and bandwidth for storing and transmitting images are invariably

scarce. Just one ten-megapixel photograph would consume about 115 MB of

RAM in this format.Why 115 MB and not 120MB?

Less range is required for images that are meant to be displayed directly.

While the range of possible light intensities is unbounded in principle, any given

device has a decidedly finite maximum intensity, so in many contexts it is per-

fectly sufficient for pixels to have a bounded range, usually taken to be [0, 1] forsimplicity. For instance, the possible values in an 8-bit image are 0, 1/255, 2/255, . . . , 254/255, 1.The denominator of 255,

rather than 256, isawkward, but being able torepresent 0 and 1 exactly isimportant.

Images stored with floating point numbers, allowing a wide range of values, are

often called high dynamic range (HDR) images to distinguish them from fixed-

range, or low dynamic range (LDR) images that are stored with integers. See

u=

l

u=

r

v = b

v = tj

i

i = –

.5

i = 3

.5

j = 2.5

j = –.5 u =

0

u =

1 v = 0

v = 1

for an image of nx by ny pixels

Page 27: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture lookups and wrapping

• In shading calculation, when you need a texture value you perform a texture lookup

• Convert (u, v) texture coordinates to (i, j) texel coordinates, and read a value from the image– simplest: round to nearest (nearest neighbor lookup)– various ways to be smarter and get smoother results

• What if i and j are out of range?– option 1, clamp: take the nearest pixel that is in the image

– option 2, wrap: treat the texture as periodic, so that falling off the right side causes the look up to come in the left

27

ipixel

= remainder(ilookup

, nx

)

ipixel

= max(0,min(nx

� 1, ilookup

))

Page 28: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Wrapping modes

28

clamp wrap

Page 29: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

• Given values of a function f(x) for two values of x, you can define in-between values by drawing a line

– there is a unique line through the two points

– can write down using slopes, intercepts

– …or as a value added to f(a)– …or as a convex combination

of f(a) and f(b)

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Linear interpolation, 1D domain

29

a b

f(a)

f(b)

See Shirley Sec. 2.6

f(x) = f(a) +x� a

b� a

(f(b)� f(a))

= (1� �)f(a) + �f(b)

= ↵f(a) + �f(b)

Page 30: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Linear interpolation in 1D

• Alternate story1. write x as convex combination of a and b

2. use the same weights to compute f(x) as a convex combination of f(a) and f(b)

30

x = ↵a+ �b where ↵+ � = 1

f(x) = ↵f(a) + �f(b)

Page 31: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Linear interpolation in 1D

31

a

Ơ ơ

x b

lƠOơO

a

Ơ ơ

x b

lƠOơO

f(a)

f(b)

f(x)Ơ

ơ

Page 32: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Linear interpolation in 2D

• Use the alternate story:1. Write x, the point where you want a value, as a convex linear

combination of the vertices

2. Use the same weights to compute the interpolated value f(x) from the values at the vertices, f(a), f(b), and f(c)

32

x = ↵a+ �b+ �c where ↵+ � + � = 1

f(x) = ↵f(a) + �f(b) + �f(c)

See Shirley Sec. 2.7

Page 33: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Interpolation in ray tracing

• When values are stored at vertices, use linear (barycentric) interpolation to define values across the whole surface that:1. …match the values at the vertices2. …are continuous across edges3. …are piecewise linear (linear over each triangle)

as a function of 3D position, not screen position—more later

• How to compute interpolated values1. during triangle intersection compute barycentric coords2. use barycentric coords to average attributes given at vertices

33

Page 34: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture coordinates on meshes

• Texture coordinates become per-vertex data like vertex positions– can think of them as a second position: each vertex has a

position in 3D space and in 2D texure space

• How to come up with vertex (u,v)s?– use any or all of the methods just discussed

• in practice this is how you implement those for curved surfaces approximated with triangles

– use some kind of optimization• try to choose vertex (u,v)s to result in a smooth, low

distortion map

34

Page 35: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

Texture mapping: a set of techniques for defining functions on surfaces, for a variety of uses.

A refined definition

• Let’s look at some examples of more general uses of texture maps.

35

Page 36: Textures and normals in ray tracing - Cornell Universityone (for grayscale, or black and white, images) or three (for RGB color images) 32-bit floating point numbers stored per pixel

© 2014 Steve Marschner • Cornell CS4620 Fall 2014 • Lecture 6

3D textures

• Texture is a function of (u, v, w)– can just evaluate texture at 3D

surface point– good for solid materials– often defined procedurally

[Wol

fe /

SG97

Slid

e se

t]

36