6.1 Texture Mapping Hao Li (lecturer for 9/28: Justin Solomon) - - PowerPoint PPT Presentation

6 1 texture mapping
SMART_READER_LITE
LIVE PREVIEW

6.1 Texture Mapping Hao Li (lecturer for 9/28: Justin Solomon) - - PowerPoint PPT Presentation

Fall 2014 CSCI 420: Computer Graphics 6.1 Texture Mapping Hao Li (lecturer for 9/28: Justin Solomon) http://cs420.hao-li.com 1 Quick Self-Introduction Justin Solomon http://www.stanford.edu/~justso1 justin.solomon@stanford.edu What I


slide-1
SLIDE 1

CSCI 420: Computer Graphics

Hao Li (lecturer for 9/28: Justin Solomon)

http://cs420.hao-li.com

Fall 2014

6.1 Texture Mapping

1

slide-2
SLIDE 2

Quick Self-Introduction

Justin Solomon

http://www.stanford.edu/~justso1 justin.solomon@stanford.edu

slide-3
SLIDE 3

What I Work On

3

slide-4
SLIDE 4

Outline

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

4

slide-5
SLIDE 5

How Do You Add Detail to a Cube?

5

http://dev.ryzom.com/projects/ryzom/wiki/ImportingMaxAssets

Six sides ⟹ six colors?!

slide-6
SLIDE 6

Two Ideas for Adding Detail

  • 1. More polygons
  • Slow and hard to edit!

2.

Map a texture from an image

  • Image size does not affect complexity
  • Built into hardware

6

slide-7
SLIDE 7

Trompe L’Oeil (“Deceive the Eye”)

7

  • Extra 3D structure is painted
  • Similar idea for texture mapping:

Replace intricate geometry with an image!

slide-8
SLIDE 8

Texture Maps

8

http://raweb.inria.fr/rapportsactivite/RA2006/iparla/uid33.html http://runfatgirl.files.wordpress.com/2008/05/bigstockphoto_skin_texture_108750.jpg

Image Geometry

slide-9
SLIDE 9

How To Store a Texture

Bitmap image

Potential sources:

  • Load using image library
  • Create within the program

9

slide-10
SLIDE 10

How To Store a Texture

Bitmap image

Vocabulary:

  • Texels: Pixels of texture
  • Texel coordinates: Coordinates (s,t) scaled to [0,1]

10

slide-11
SLIDE 11

In Code

  • 2D array:

unsigned char texture[height][width][4]

  • Unrolled into 1D array:

unsigned char texture[4*height*width]

11

slide-12
SLIDE 12

In Code

  • 2D array:

unsigned char texture[height][width][4]

  • Unrolled into 1D array:

unsigned char texture[4*height*width]

12

slide-13
SLIDE 13

Texture Map

13

(0,1) (0,0) (1,0) (1,1) 3D polygon Texture image

slide-14
SLIDE 14

Texture Map

14

slide-15
SLIDE 15

Texture Lookup

15

For each pixel, look into the texture to obtain color

(s,t) (s,t) Texture image Screen image

slide-16
SLIDE 16

The “st” Coordinate System

16

s 1 1 t (s,t) Also called uv space

slide-17
SLIDE 17

Texture Mapping: Key Slide

17

(0.35, 0.55) 1 1 t s (0.7, 0.55) (0.1, 0.7) (0,1,0) s = 0.1 t = 0.7 (2,-1,0) s = 0.35 t = 0.05 (-2,1,0) s = 0.7 t = 0.55 (0.35, 0.05)

slide-18
SLIDE 18

Specifying Texture Coordinates in OpenGL

  • Use glTexCoord2f(s,t)
  • State machine: Texture coordinates

remain valid until you change them

  • Example (from previous slide) :

18

glEnable(GL_TEXTURE_2D); // turn texture mapping on glBegin(GL_TRIANGLES); glTexCoord2f(0.35,0.05); glVertex3f(2.0,-1.0,0.0); glTexCoord2f(0.7,0.55); glVertex3f(-2.0,1.0,0.0); glTexCoord2f(0.1,0.7); glVertex3f(0.0,1.0,0.0); glEnd(); glDisable(GL_TEXTURE_2D); // turn texture mapping off s = 0.1 t = 0.7 s = 0.7 t = 0.55 s = 0.35 t = 0.05

slide-19
SLIDE 19

What if texture coordinates are outside of [0,1] ?

19

1 1 t s (s,t)

slide-20
SLIDE 20

Solution 1: Repeat texture

20

1 1 t s

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

(s,t)

slide-21
SLIDE 21

Solution 2: Clamp to [0,1]

21

1 1 t s (s,t) Use this color

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)

slide-22
SLIDE 22

Combining texture mapping and shading

22

slide-23
SLIDE 23

Combining Texture Mapping and Shading

  • Combine texture and OpenGL Phong shading
  • GL_MODULATE: Multiply texture and Phong
  • GL_BLEND: Linear combination of texture and Phong
  • GL_REPLACE: Use texture color only (ignore Phong)
  • Example:

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

23

slide-24
SLIDE 24

Outline

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

24

slide-25
SLIDE 25

Texture Mapping in OpenGL

  • Initialization:
  • 1. Read texture image from file into an array,
  • r generate texture using your program
  • 2. Specify texture mapping parameters: Wrapping, filtering, etc.
  • 3. Initialize and activate the texture
  • In display():
  • 1. Enable OpenGL texture mapping
  • 2. Draw objects: Assign texture coordinates to vertices
  • 3. Disable OpenGL texture mapping

25

slide-26
SLIDE 26

Initializing the Texture

  • For each texture image call glTexImage2D

[once during initialization]

  • The dimensions of texture images must be powers of 2
  • if not, rescale image or pad with zero
  • or can use OpenGL extensions
  • Can load textures dynamically if GPU memory is scarce

26

slide-27
SLIDE 27

glTexImage2D

glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, border, format, type, data)

  • GL_TEXTURE_2D: specifies that it is a 2D texture
  • level: used for specifying levels of detail for mipmapping (default:0)
  • internalFormat: Determines how texture is stored internally (often GL_RGB or GL_RGBA)
  • width, height: The size of the texture must be powers of 2
  • border: Often set to 0)
  • format, type
  • Specifies what the input data is (GL_RGB, GL_RGBA, …)
  • Specifies the input data type (GL_UNSIGNED_BYTE, GL_BYTE, …)
  • Regardless of format and type, OpenGL converts the data to internalFormat
  • data: pointer to the image buffer

27

slide-28
SLIDE 28

Enabling and Disabling Texture Mode

  • Before rendering primitives that are texture-mapped:

glEnable(GL_TEXTURE_2D) glDisable(GL_TEXTURE_2D)

  • Enable/disable texture mode to switch between drawing

textured/non-textured polygons

  • Changing textures:
  • Only one texture is active at any given time

(with OpenGL extensions, more than one can be used called multitexturing)

  • Use glBindTexture to select the active texture

28

slide-29
SLIDE 29

Outline

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

29

slide-30
SLIDE 30

Texture Interpolation

30

This photo is too small.

slide-31
SLIDE 31

Zooming

31

Consider a black and white image: Task: Blow up to poster size (zoom by a factor of 16)

slide-32
SLIDE 32

Interpolation

32

Given: The values of a function f at a few locations. f(1), f(2), f(3), … Compute: The values elsewhere What is f(1.5)? The challenge: Modeling how the function “should” behave.

slide-33
SLIDE 33

33

(s,t) may not land at an integer location!

When Does Interpolation Happen?

slide-34
SLIDE 34

Nearest Neighbor Interpolation

34

First try: Repeat each row 16 times, then each column 16 times

slide-35
SLIDE 35

35

Discontinuous!

  • We need a better way to find the in between values
  • For now, we’ll consider one horizontal slice through

the image (one scan line)

Nearest Neighbor Interpolation

slide-36
SLIDE 36

Linear Interpolation (LERP)

36

slide-37
SLIDE 37

Linear Interpolation (LERP)

37

Connect data points with straight lines

slide-38
SLIDE 38

Bilinear Interpolation

38

slide-39
SLIDE 39

Bilinear Interpolation

39

slide-40
SLIDE 40

Bilinear Interpolation

40

slide-41
SLIDE 41

Bilinear Interpolation

41

slide-42
SLIDE 42

Bilinear Interpolation

42

slide-43
SLIDE 43

Order Doesn’t Matter

43

slide-44
SLIDE 44

Bilinear Interpolation

44

Interpolate in x then in y (or vice versa!)

slide-45
SLIDE 45

Comparison

45

Nearest Neighbor Bilinear

slide-46
SLIDE 46

Texture Interpolation in OpenGL

Nearest-neighbor:

  • Faster, but worse quality

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

Linear:

  • Incorporate colors of several neighbors to determine color
  • Slower, better quality

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

46

slide-47
SLIDE 47

Opposite Issue: Minification

47

Large texture Small image

Adjacent rendered pixels are far apart in texture

slide-48
SLIDE 48

Why Do We Need to Filter?

  • Texture image is shrunk

in distant parts of the image

  • This leads to aliasing
  • Can be fixed with filtering
  • bilinear in space
  • trilinear in space and

level of detail (mipmapping)

48

slide-49
SLIDE 49

Mipmapping

  • Precompute texture at different scales and use the appropriate

texture at each distance

  • When rendering, choose scale to avoid having to minify on the fly

49

slide-50
SLIDE 50

Mipmapping

  • Each piece represents one level of detail (LOD)
  • Simplified by using powers of two

50

slide-51
SLIDE 51

Mipmapping in OpenGL

Generate all the mipmaps automatically: gluBuild2DMipmaps(GL_TEXTURE_2D, components, width, height, format, type, data) Tell OpenGL to use the mipmaps for the texture: glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST)

51

slide-52
SLIDE 52

Outline

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

52

slide-53
SLIDE 53

Complete example

void initTexture() { load image into memory; // Can use libjpeg, libtiff, or other image library. // Image should be stored as a sequence of bytes, usually 3 bytes per pixel (RGB), or 4 bytes (RGBA); image size is 4 * 256 * 256 bytes in this example. // We assume that the image data location is stored in pointer // “pointerToImage.” // create placeholder for texture // must declare a global variable in program header: GLUint texName glGenTextures(1, &texName); // make texture “texName” the currently active texture glBindTexture(GL_TEXTURE_2D, texName); (continues on next page)

53

slide-54
SLIDE 54

Complete example (part 2)

// specify texture parameters (they affect whatever texture is active) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // repeat pattern in s glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // repeat pattern in t // use linear filter both for magnification and minification glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // load image data stored at pointer “pointerToImage” into the currently active texture (“texName”) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pointerToImage); } // end init()

54

slide-55
SLIDE 55

Complete example (part 3)

void display() { … // no modulation of texture color with lighting; use texture color directly glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); // turn on texture mapping (this disables standard OpenGL lighting, unless in GL_MODULATE mode) glEnable(GL_TEXTURE_2D); (continues on next page)

55

slide-56
SLIDE 56

Complete example (part 4)

glBegin(GL_QUADS); // draw a textured quad glTexCoord2f(0.0,0.0); glVertex3f(-2.0,-1.0,0.0); glTexCoord2f(0.0,1.0); glVertex3f(-2.0,1.0,0.0); glTexCoord2f(1.0,0.0); glVertex3f(0.0,1.0,0.0); glTexCoord2f(1.0,1.0); glVertex3f(0.0,-1.0,0.0); glEnd(); // turn off texture mapping glDisable(GL_TEXTURE_2D); // draw some non-texture mapped objects (standard OpenGL lighting will be used if it is enabled) … // switch back to texture mode, etc. … } // end display()

56

slide-57
SLIDE 57

Outline

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

57

slide-58
SLIDE 58

Textures do not have to represent color

  • Specularity (patches of shininess)
  • Transparency (patches of clearness)
  • Normal vector changes (bump maps)
  • Reflected light (environment maps)
  • Shadows
  • Changes in surface height (displacement maps)

58

slide-59
SLIDE 59

Bump mapping

59

slide-60
SLIDE 60

Bump mapping

  • How do you make a surface look rough?

Option 1: model the surface with tiny polygons Option 2: perturb normal vectors before shading

  • Fakes small displacements above or below the true surface
  • The surface doesn’t actually change,

but shading makes it look like there are irregularities!

  • A texture stores information about the “fake” height of the

surface

60

slide-61
SLIDE 61

Bump mapping

  • Perturb normal without making any actual change to the shape
  • This illusion can be seen through:

How?

61

slide-62
SLIDE 62

Light Mapping

  • Quake used light maps in addition to texture maps

Texture maps add detail to surfaces Light maps store pre-computed illumination

  • Multiplied at runtime, cached for efficiency

62

Texture Map Only Texture + Light Map Light Map

slide-63
SLIDE 63

Summary

  • Introduction
  • Texture mapping in OpenGL
  • Filtering and Mipmaps
  • Example
  • Non-color texture maps

63

slide-64
SLIDE 64

http://cs420.hao-li.com

Thanks!

64