Textures I Week 9, Wed Mar 14 - - PowerPoint PPT Presentation

textures i week 9 wed mar 14
SMART_READER_LITE
LIVE PREVIEW

Textures I Week 9, Wed Mar 14 - - PowerPoint PPT Presentation

University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Textures I Week 9, Wed Mar 14 http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007 Reading for Today and Next Time FCG Chap 11 Texture Mapping except 11.8


slide-1
SLIDE 1

University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007

Textures I Week 9, Wed Mar 14

slide-2
SLIDE 2

2

Reading for Today and Next Time

  • FCG Chap 11 Texture Mapping
  • except 11.8
  • RB Chap Texture Mapping
  • FCG Sect 16.6 Procedural Techniques
  • FCG Sect 16.7 Groups of Objects
slide-3
SLIDE 3

3

News

  • Q3 specular color should be (1,1,0)
  • P3: bug in sample implementation fixed
  • new reference images and sample binaries

posted

  • no change to template
slide-4
SLIDE 4

4

S =1− min(R,G,B) I Correction: HSV and RGB

  • HSV/HSI conversion from RGB
  • not expressible in matrix

3 B G R I + + =

[ ]

          − − + − − + − =

) )( ( ) ( ) ( ) ( 2 1 cos

2 1

B G B R G R B R G R H

slide-5
SLIDE 5

5

Review: Z-Buffer Algorithm

  • augment color framebuffer with Z-buffer or

depth buffer which stores Z value at each pixel

  • at frame beginning, initialize all pixel depths

to ∞

  • when rasterizing, interpolate depth (Z)

across polygon

  • check Z-buffer before storing pixel color in

framebuffer and storing depth in Z-buffer

  • don’t write pixel if its Z value is more distant

than the Z value already stored there

slide-6
SLIDE 6

6

Clarification/Review: Depth Test Precision

  • reminder: projective transformation maps

eye-space z to generic z-range (NDC)

  • thus zN ~= 1/zE

                ⋅                   − − − − + − − + − − + − =                

E E E E N N N N

w z y x n f fn n f n f b t b t b t n l r l r l r n w z y x 1 2 ) ( 2 2

E N E E N

z w w n f fn z n f n f z − = − − + − + − = , 2 ) (

E E N N

z w n f fn n f n f w z − + − + = 2

slide-7
SLIDE 7

7

Backface Culling

slide-8
SLIDE 8

8

Back-Face Culling

  • on the surface of a "solid" object, polygons

whose normals point away from the camera are always occluded:

note: backface culling alone doesn’t solve the hidden-surface problem!

slide-9
SLIDE 9

9

Back-Face Culling

  • not rendering backfacing polygons improves

performance

  • by how much?
  • reduces by about half the number of polygons

to be considered for each pixel

  • optimization when appropriate
slide-10
SLIDE 10

10

Back-face Culling: VCS

y y z z first idea: first idea: cull if cull if

<

Z

N

sometimes sometimes misses polygons that misses polygons that should be culled should be culled eye eye

slide-11
SLIDE 11

11

Back-face Culling: NDCS

y y z z eye eye VCS VCS NDCS NDCS eye eye works to cull if works to cull if

>

Z

N

y y z z

slide-12
SLIDE 12

12

Back-Face Culling: Manifolds

  • most objects in scene are typically “solid”
  • specifically: orientable closed manifolds
  • orientable: must have two distinct sides
  • cannot self-intersect
  • a sphere is orientable since has

two sides, 'inside' and 'outside'.

  • a Mobius strip or a Klein bottle is

not orientable

  • closed: cannot “walk” from one

side to the other

  • sphere is closed manifold
  • plane is not
slide-13
SLIDE 13

13

Back-Face Culling: Manifolds

Yes No

  • most objects in scene are typically “solid”
  • specifically: orientable closed manifolds
  • manifold: local neighborhood of all points isomorphic to

disc

  • boundary partitions space into interior & exterior
slide-14
SLIDE 14

14

Backface Culling: Manifolds

  • examples of manifold objects:
  • sphere
  • torus
  • well-formed CAD part
  • examples of non-manifold objects:
  • a single polygon
  • a terrain or height field
  • polyhedron w/ missing face
  • anything with cracks or holes in boundary
  • one-polygon thick lampshade
slide-15
SLIDE 15

15

Invisible Primitives

  • why might a polygon be invisible?
  • polygon outside the field of view / frustum
  • solved by clipping
  • polygon is backfacing
  • solved by backface culling
  • polygon is occluded by object(s) nearer the viewpoint
  • solved by hidden surface removal
slide-16
SLIDE 16

16

Texturing

slide-17
SLIDE 17

17

Rendering Pipeline

Geometry Database Geometry Database Model/View Transform. Model/View Transform. Lighting Lighting Perspective Transform. Perspective Transform. Clipping Clipping Scan Conversion Scan Conversion Depth Test Depth Test Texturing Texturing Blending Blending Frame- buffer Frame- buffer Geometry Processing Geometry Processing Rasterization Rasterization Fragment Processing Fragment Processing

slide-18
SLIDE 18

18

Texture Mapping

  • real life objects have

nonuniform colors, normals

  • to generate realistic
  • bjects, reproduce

coloring & normal variations = texture

  • can often replace

complex geometric details

slide-19
SLIDE 19

19

Texture Mapping

  • introduced to increase realism
  • lighting/shading models not enough
  • hide geometric simplicity
  • images convey illusion of geometry
  • map a brick wall texture on a flat polygon
  • create bumpy effect on surface
  • associate 2D information with 3D surface
  • point on surface corresponds to a point in

texture

  • “paint” image onto polygon
slide-20
SLIDE 20

20

Color Texture Mapping

  • define color (RGB) for each point on object

surface

  • two approaches
  • surface texture map
  • volumetric texture
slide-21
SLIDE 21

21

Texture Coordinates

  • texture image: 2D array of color values (texels)
  • assigning texture coordinates (s,t) at vertex with
  • bject coordinates (x,y,z,w)
  • use interpolated (s,t) for texel lookup at each pixel
  • use value to modify a polygon’s color
  • or other surface property
  • specified by programmer or artist

glTexCoord2f(s,t) glVertexf(x,y,z,w)

slide-22
SLIDE 22

22

Texture Mapping Example

+ =

slide-23
SLIDE 23

23

Example Texture Map

glTexCoord2d(0,0); glVertex3d (0, -2, -2); glTexCoord2d(1,1); glVertex3d (0, 2, 2);

slide-24
SLIDE 24

24

Fractional Texture Coordinates

(0,0) (1,0) (0,1) (1,1) (0,0) (.25,0) (0,.5) (.25,.5) texture image

slide-25
SLIDE 25

25

Texture Lookup: Tiling and Clamping

  • what if s or t is outside the interval [0…1]?
  • multiple choices
  • use fractional part of texture coordinates
  • cyclic repetition of texture to tile whole surface

glTexParameteri( …, GL_TEXTURE_WRAP_S, GL_REPEAT, GL_TEXTURE_WRAP_T, GL_REPEAT, ... )

  • clamp every component to range [0…1]
  • re-use color values from texture image border

glTexParameteri( …, GL_TEXTURE_WRAP_S, GL_CLAMP, GL_TEXTURE_WRAP_T, GL_CLAMP, ... )

slide-26
SLIDE 26

26

glTexCoord2d(1, 1); glVertex3d (x, y, z); (1,0) (0,0) (0,1) (1,1)

Tiled Texture Map

glTexCoord2d(4, 4); glVertex3d (x, y, z); (4,4) (0,4) (4,0) (0,0)

slide-27
SLIDE 27

27

Demo

  • Nate Robbins tutors
  • texture
slide-28
SLIDE 28

28

Texture Coordinate Transformation

  • motivation
  • change scale, orientation of texture on an object
  • approach
  • texture matrix stack
  • transforms specified (or generated) tex coords

glMatrixMode( GL_TEXTURE ); glLoadIdentity(); glRotate();

  • more flexible than changing (s,t) coordinates
  • [demo]
slide-29
SLIDE 29

29

Texture Functions

  • once have value from the texture map, can:
  • directly use as surface color: GL_REPLACE
  • throw away old color, lose lighting effects
  • modulate surface color: GL_MODULATE
  • multiply old color by new value, keep lighting info
  • texturing happens after lighting, not relit
  • use as surface color, modulate alpha: GL_DECAL
  • like replace, but supports texture transparency
  • blend surface color with another: GL_BLEND
  • new value controls which of 2 colors to use
  • indirection, new value not used directly for coloring
  • specify with glTexEnvi(GL_TEXTURE_ENV,

GL_TEXTURE_ENV_MODE, <mode>)

  • [demo]
slide-30
SLIDE 30

30

Texture Pipeline

Texel color (0.9,0.8,0.7) (x, y, z) Object position (-2.3, 7.1, 17.7) (s, t) Parameter space (0.32, 0.29) Texel space (81, 74) (s’, t’) Transformed parameter space (0.52, 0.49) Final color (0.45,0.4,0.35) Object color (0.5,0.5,0.5)

slide-31
SLIDE 31

31

Texture Objects and Binding

  • texture object
  • an OpenGL data type that keeps textures resident in memory

and provides identifiers to easily access them

  • provides efficiency gains over having to repeatedly load and

reload a texture

  • you can prioritize textures to keep in memory
  • OpenGL uses least recently used (LRU) if no priority is

assigned

  • texture binding
  • which texture to use right now
  • switch between preloaded textures
slide-32
SLIDE 32

32

Basic OpenGL Texturing

  • create a texture object and fill it with texture data:
  • glGenTextures(num, &indices) to get identifiers for the objects
  • glBindTexture(GL_TEXTURE_2D, identifier) to bind
  • following texture commands refer to the bound texture
  • glTexParameteri(GL_TEXTURE_2D, …, …) to specify

parameters for use when applying the texture

  • glTexImage2D(GL_TEXTURE_2D, ….) to specify the texture data

(the image itself)

  • enable texturing: glEnable(GL_TEXTURE_2D)
  • state how the texture will be used:
  • glTexEnvf(…)
  • specify texture coordinates for the polygon:
  • use glTexCoord2f(s,t) before each vertex:
  • glTexCoord2f(0,0); glVertex3f(x,y,z);
slide-33
SLIDE 33

33

Low-Level Details

  • large range of functions for controlling layout of texture data
  • state how the data in your image is arranged
  • e.g.: glPixelStorei(GL_UNPACK_ALIGNMENT, 1) tells

OpenGL not to skip bytes at the end of a row

  • you must state how you want the texture to be put in memory:

how many bits per “pixel”, which channels,…

  • textures must be square and size a power of 2
  • common sizes are 32x32, 64x64, 256x256
  • smaller uses less memory, and there is a finite amount of

texture memory on graphics cards

  • ok to use texture template sample code for project 4
  • http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=09
slide-34
SLIDE 34

34

Texture Mapping

  • texture coordinates
  • specified at vertices

glTexCoord2f(s,t); glVertexf(x,y,z);

  • interpolated across triangle (like R,G,B,Z)
  • …well not quite!
slide-35
SLIDE 35

35

Texture Mapping

  • texture coordinate interpolation
  • perspective foreshortening problem
slide-36
SLIDE 36

36

Interpolation: Screen vs. World Space

  • screen space interpolation incorrect
  • problem ignored with shading, but artifacts

more visible with texturing

P1(x,y,z) V0(x’,y’) V1(x’,y’) P0(x,y,z)

slide-37
SLIDE 37

37

Texture Coordinate Interpolation

  • perspective correct interpolation
  • α, β, γ :
  • barycentric coordinates of a point P in a triangle
  • s0, s1, s2 :
  • texture coordinates of vertices
  • w0, w1,w2 :
  • homogeneous coordinates of vertices

2 1 2 2 1 1

/ / / / / / w w w w s w s w s s γ β α γ β α + + ⋅ + ⋅ + ⋅ =

(s1,t1) (s0,t0) (s2,t2) (x1,y1,z1,w1) (x0,y0,z0,w0) (x2,y2,z2,w2) (α,β,γ) (s,t)?

slide-38
SLIDE 38

38

Reconstruction

(image courtesy of (image courtesy of Kiriakos Kutulakos Kiriakos Kutulakos, U Rochester) , U Rochester)

slide-39
SLIDE 39

39

Reconstruction

  • how to deal with:
  • pixels that are much larger than texels?
  • apply filtering, “averaging”
  • pixels that are much smaller than texels ?
  • interpolate
slide-40
SLIDE 40

40

MIPmapping

Without MIP-mapping Without MIP-mapping With MIP-mapping With MIP-mapping

use use “ “image pyramid image pyramid” ” to to precompute precompute averaged versions of the texture averaged versions of the texture store whole pyramid in store whole pyramid in single block of memory single block of memory

slide-41
SLIDE 41

41

MIPmaps

  • multum in parvo -- many things in a small place
  • prespecify a series of prefiltered texture maps of decreasing

resolutions

  • requires more texture storage
  • avoid shimmering and flashing as objects move
  • gluBuild2DMipmaps
  • automatically constructs a family of textures from original

texture size down to 1x1

without with

slide-42
SLIDE 42

42

MIPmap storage

  • only 1/3 more space required
slide-43
SLIDE 43

43

Texture Parameters

  • in addition to color can control other

material/object properties

  • surface normal (bump mapping)
  • reflected color (environment mapping)
slide-44
SLIDE 44

44

Bump Mapping: Normals As Texture

  • object surface often not smooth – to recreate correctly

need complex geometry model

  • can control shape “effect” by locally perturbing surface

normal

  • random perturbation
  • directional change over region
slide-45
SLIDE 45

45

Bump Mapping

slide-46
SLIDE 46

46

Bump Mapping

slide-47
SLIDE 47

47

Embossing

  • at transitions
  • rotate point’s surface normal by _ or - _
slide-48
SLIDE 48

48

Displacement Mapping

  • bump mapping gets

silhouettes wrong

  • shadows wrong too
  • change surface

geometry instead

  • only recently

available with realtime graphics

  • need to subdivide

surface

slide-49
SLIDE 49

49

Environment Mapping

  • cheap way to achieve reflective effect
  • generate image of surrounding
  • map to object as texture
slide-50
SLIDE 50

50

Environment Mapping

  • used to model object that reflects

surrounding textures to the eye

  • movie example: cyborg in Terminator 2
  • different approaches
  • sphere, cube most popular
  • OpenGL support
  • GL_SPHERE_MAP, GL_CUBE_MAP
  • others possible too
slide-51
SLIDE 51

51

Sphere Mapping

  • texture is distorted fish-eye view
  • point camera at mirrored sphere
  • spherical texture mapping creates texture coordinates that

correctly index into this texture map

slide-52
SLIDE 52

52

Cube Mapping

  • 6 planar textures, sides of cube
  • point camera in 6 different directions, facing
  • ut from origin
slide-53
SLIDE 53

53

Cube Mapping

A B C E F D

slide-54
SLIDE 54

54

Cube Mapping

  • direction of reflection vector r selects the face of the

cube to be indexed

  • co-ordinate with largest magnitude
  • e.g., the vector (-0.2, 0.5, -0.84) selects the –Z face
  • remaining two coordinates (normalized by the 3rd

coordinate) selects the pixel from the face.

  • e.g., (-0.2, 0.5) gets mapped to (0.38, 0.80).
  • difficulty in interpolating across faces
slide-55
SLIDE 55

55

Review: Texture Objects and Binding

  • texture objects
  • texture management: switch with bind, not reloading
  • can prioritize textures to keep in memory
  • Q: what happens to textures kicked out of memory?
  • A: resident memory (on graphics card) vs.

nonresident (on CPU)

  • details hidden from developers by OpenGL
slide-56
SLIDE 56

56

Volumetric Texture

  • define texture pattern over 3D

domain - 3D space containing the object

  • texture function can be

digitized or procedural

  • for each point on object

compute texture from point location in space

  • common for natural

material/irregular textures (stone, wood,etc…)

slide-57
SLIDE 57

57

Volumetric Bump Mapping

Marble Bump

slide-58
SLIDE 58

58

Volumetric Texture Principles

  • 3D function ρ

∀ρ = ρ(x,y,z)

  • texture space – 3D space that holds the

texture (discrete or continuous)

  • rendering: for each rendered point P(x,y,z)

compute ρ(x,y,z)

  • volumetric texture mapping function/space

transformed with objects