1 of 168

Nima Kalantari

CSCE 441 - Computer Graphics

Shading and Texture Mapping

Some slides from Ren Ng and Scott Schaefer

2 of 168

So far

3 of 168

Realistic Lighting

Image taken from http://graphics.ucsd.edu/~henrik/images/cbox.html

4 of 168

Rasterization Pipeline (covered)

Vertex Processing

Triangle Processing

Rasterization

Fragment Processing

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

z

x

y

Modeling & viewing transforms

5 of 168

Rasterization Pipeline (covered)

Vertex Processing

Triangle Processing

Rasterization

Fragment Processing

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

Sampling triangle coverage

6 of 168

Rasterization Pipeline (not covered)

Triangle Processing

Rasterization

Fragment Processing

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

+ Specular

Phong Reflection

=

Ambient

Diffuse

+

Evaluating shading functions

Vertex Processing

7 of 168

Rasterization Pipeline (not covered)

Triangle Processing

Rasterization

Fragment Processing

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

Texture mapping

Vertex Processing

8 of 168

Rasterization Pipeline (covered)

Vertex Processing

Triangle Processing

Rasterization

Fragment Processing

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

Z-Buffer Visibility Tests

9 of 168

Lighting/Illumination

  • Color is a function of how light reflects from surfaces to the eye

10 of 168

Reflection Models

11 of 168

Types of Reflection Functions

  • Ideal Specular
    • Reflection Law
    • Mirror
  • Ideal Diffuse
    • Lambert’s Law
    • Matte
  • Specular
    • Glossy
    • Directional diffuse

12 of 168

Illumination Types

  • Local illumination only accounts for light that directly hits a surface and is transmitted to the eye
  • Global illumination accounts for light from all sources as it is transmitted throughout the environment

13 of 168

Realistic Lighting (global illumination)

Image taken from http://graphics.ucsd.edu/~henrik/images/cbox.html

would be completely black with local ilumination

14 of 168

Outline

  • Phong reflection model
  • Shading methods
  • GLSL
  • Texture mapping
  • Advanced texturing methods

15 of 168

Phong reflection model

  • A local shading model: simple, per-pixel, fast
  • Based on perceptual observations, not physics

16 of 168

Perceptual Observations

Photo credit: Jessica Andrews, flickr

Diffuse reflection

Specular highlights

Ambient lighting

17 of 168

Illumination Model

  • Ambient Light
    • Uniform light caused by secondary reflections
  • Diffuse Light
    • Light scattered equally in all directions
  • Specular Light
    • Highlights on shiny surfaces

18 of 168

Ambient Light

  • intensity of ambient light
  • ambient reflection coefficient
  • Really 3 equations! (Red, Green, Blue)
  • Accounts for indirect illumination

19 of 168

Example

20 of 168

Diffuse Light

21 of 168

Diffuse Light

  • Assumes that light is reflected equally in all directions

Surface

22 of 168

Diffuse Ball Example

akimeta.com

23 of 168

Lambert’s Law

Surface

Beam of Light

24 of 168

Lambert’s Law

Surface

Beam of Light

25 of 168

Lambert’s Law

Surface

Beam of Light

26 of 168

Diffuse Light

  • intensity of point light source
  • diffuse reflection coefficient
  • angle between normal and direction to light

Surface

27 of 168

Example

28 of 168

Example

29 of 168

Specular Light

30 of 168

Perceptual Observations

Photo credit: Jessica Andrews, flickr

Diffuse reflection

Specular highlights

Ambient lighting

31 of 168

Specular Light

  • Perfect, mirror-like reflection of light from surface
  • Forms highlights on shiny objects (metal, plastic)

Surface

32 of 168

Specular Light

  • intensity of point light source
  • specular reflection coefficient
  • angle between reflected vector ( ) and eye ( )
  • specular exponent

Surface

33 of 168

Cosine Power Plots

  • Increasing n narrows the reflection lobe

[Foley et al.]

34 of 168

Finding the Reflected Vector

Surface

35 of 168

Finding the Reflected Vector

Surface

36 of 168

Finding the Reflected Vector

Surface

37 of 168

Finding the Reflected Vector

Surface

38 of 168

Finding the Reflected Vector

Surface

39 of 168

Total Illumination

40 of 168

Total Illumination

41 of 168

Total Illumination

42 of 168

Total Illumination

43 of 168

Multiple Light Sources

  • Only one ambient term no matter how many lights
  • Light is additive; add contribution of multiple lights (diffuse/specular components)

44 of 168

Total Illumination

45 of 168

Total Illumination

46 of 168

Light Falloff

  • Intensity proportional to power per unit area

47 of 168

Spot Lights

48 of 168

Spot Lights

  • Eliminate light contribution outside of a cone

Surface

49 of 168

Spot Lights

  • Eliminate light contribution outside of a cone

Surface

50 of 168

Spot Lights

51 of 168

Spot Lights

Small α

52 of 168

Spot Lights

Large α

53 of 168

Outline

  • Phong reflection model
  • Shading methods
  • GLSL
  • Texture mapping
  • Advanced texturing methods

54 of 168

Shading Methods

  • Flat Shading (shade each triangle)
  • Gouraud Shading (shade each vertex)
  • Phong Shading (shade each pixel)

55 of 168

Shading Frequency: Triangle, Vertex or Pixel

  • Shade each triangle (flat shading)
    • Triangle face is flat — one normal vector
    • Not good for smooth surfaces
  • Shade each vertex (“Gouraud” shading)
    • Interpolate colors from vertices across triangle
    • Each vertex has a normal vector

56 of 168

Defining Per-Vertex Normal Vectors

  • Best to get vertex normals from the underlying

geometry

    • e.g. consider a sphere
  • Otherwise have to infer vertex normals from triangle faces
    • Simple scheme: average �surrounding face normals

 

 

57 of 168

Shading Frequency: Triangle, Vertex or Pixel

  • Shade each triangle (flat shading)
    • Triangle face is flat — one normal vector
    • Not good for smooth surfaces
  • Shade each vertex (“Gouraud” shading)
    • Interpolate colors from vertices across triangle
    • Each vertex has a normal vector
  • Shade each pixel (“Phong” shading)
    • Interpolate normal vectors across each triangle
    • Compute full shading model at each pixel

58 of 168

Defining Per-Pixel Normal Vectors

  • Barycentric interpolation of vertex normals

Problem: length of vectors?

59 of 168

Shading Frequency: Face, Vertex or Pixel

Image credit: Happyman, http://cg2010studio.com/

Num�Vertices

Face�Flat

Vertex

Gouraud

Pixel

Phong

Shading freq. :�Shading type :

60 of 168

Implementation Notes

  • Computation can be done in
    • World space
    • Camera space
  • Transforming normals

61 of 168

Careful: Transforming Normal Vectors

Original object�& normal vectors

Transforming vertices & �normals with matrix M

Incorrect normals!

62 of 168

Careful: Transforming Normal Vectors

Original object�& normal vectors

Transforming vertices & �normals with matrix M

Incorrect normals!

Transforming

normals with M–T

Correct normals

63 of 168

Careful: Transforming Normal Vectors

  • Transforming surface normal vectors:
    • Must use inverse transpose matrix
  • Derivation:

64 of 168

Outline

  • Phong reflectance model
  • Shading methods
  • GLSL
  • Texture mapping
  • Advanced texturing methods

65 of 168

Rasterization Pipeline

Vertex Processing

Triangle Processing

Rasterization

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

Fragment Processing

66 of 168

Rasterization Pipeline

Vertex Processing

Triangle Processing

Rasterization

Framebuffer Operations

Shaded Fragments

Fragment Stream

Triangle Stream

Vertex Stream

Display

Application

Fragment Processing

Vertex Shader

Fragment or Pixel Shader

67 of 168

Shading Language

  • Assembly
  • Nvidia’s CG
  • DirectX High Level Shader (HLSL)
  • OpenGL Shading Language (GLSL)

68 of 168

Vertex Information

float posBuff[] = {

px1, py1, pz1,

px2, py2, pz2,

px3, py3, pz3,

.

.

.

};

float colBuff[] = {

cr1, cg1, cb1,

cr2, cg2, cb2,

cr3, cg3, cb3,

.

.

.

};

float norBuff[] = {

nx1, ny1, nz1,

nx2, ny2, nz2,

nx3, ny3, nz3,

.

.

.

};

69 of 168

Example

  • Draw the triangles in the posBuff using the color in the colBuff
    • Transform vertices based on model, view, and projection matrices (vertex shader)
    • Rasterize the triangles
    • Shade each fragment (fragment shader)
    • Perform the visibility test

70 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

71 of 168

Variables

  • Attribute
    • Vertex information (position, normal, color, texture coordinates, etc.)
    • Passed from CPU to vertex shader

72 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

73 of 168

Variables

  • Attribute
    • Vertex information (position, normal, color, texture coordinates, etc.)
    • Passed from CPU to vertex shader
  • Uniform
    • The same for all the vertices or fragments
    • Passed from CPU to vertex and fragment shaders

74 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

75 of 168

Variables

  • Attribute
    • Vertex information (position, normal, color, texture coordinates, etc.)
    • Passed from CPU to vertex shader
  • Uniform
    • The same for all the vertices or fragments
    • Passed from CPU to vertex and fragment shaders
  • Varying
    • Declare in vertex shader - Passed to fragment shader

76 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

77 of 168

Variables

  • Attribute
    • Vertex information (position, normal, color, texture coordinates, etc.)
    • Passed from CPU to vertex shader
  • Uniform
    • The same for all the vertices or fragments
    • Passed from CPU to vertex and fragment shaders
  • Varying
    • Declare in vertex shader - Passed to fragment shader
  • Pre-defined
    • gl_Position: output of vertex shader
    • gl_FragColor: output of fragment shader

78 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

79 of 168

Example Fragment Shader

varying vec3 color; // passed from the vertex shader

void main()

{

gl_FragColor = vec4(color, 1.0);

}

80 of 168

Vertex Shader

  • Every vertex passes through the vertex shader (in parallel)

Position0

Position1

Position2

Color 0

Color 1

Color 2

Attribute variables

Vertex buffer

Vertex Shader

Varying variables

gl_Position,

Uniform variables

Rasterizer

81 of 168

Fragment (Pixel) Shader

  • Every fragment passes through the fragment shader (in parallel)

Fragment Shader

Varying variables

Uniform variables

gl_FragColor

Rasterizer

82 of 168

Pipeline

Position 0

Position 1

Position 2

Color 0

Color 1

Color 2

Attribute variables

Vertex buffer

Vertex Shader

Varying variables

gl_Position,

Uniform variables

Fragment Shader

Varying variables

gl_FragColor

Rasterizer

Uniform variables

83 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

84 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

85 of 168

Load, Compile, and Link

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);

GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

vsText = ReadShader(vertexShaderFileName);

fsText = ReadShader(fragmentShaderFileName);

glShaderSource(vertShader, 1, &vsText, 0);

glShaderSource(fragShader, 1, &fsText, 0);

glCompileShader(vertShader);

glCompileShader(fragShader);

programID = glCreateProgram();

glAttachShader(programID, vertShader);

glAttachShader(programID, fragShader);

glLinkProgram(programID);

86 of 168

Load, Compile, and Link

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);

GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

vsText = ReadShader(vertexShaderFileName);

fsText = ReadShader(fragmentShaderFileName);

glShaderSource(vertShader, 1, &vsText, 0);

glShaderSource(fragShader, 1, &fsText, 0);

glCompileShader(vertShader);

glCompileShader(fragShader);

programID = glCreateProgram();

glAttachShader(programID, vertShader);

glAttachShader(programID, fragShader);

glLinkProgram(programID);

87 of 168

Vertex and Fragment Shaders

Shader.vert

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

------------------------------------------------------------------

Shader.frag

varying vec3 color; // passed from the vertex shader

void main()

{

gl_FragColor = vec4(color, 1.0);

}

88 of 168

Load, Compile, and Link

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);

GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

vsText = ReadShader(vertexShaderFileName);

fsText = ReadShader(fragmentShaderFileName);

glShaderSource(vertShader, 1, &vsText, 0);

glShaderSource(fragShader, 1, &fsText, 0);

glCompileShader(vertShader);

glCompileShader(fragShader);

programID = glCreateProgram();

glAttachShader(programID, vertShader);

glAttachShader(programID, fragShader);

glLinkProgram(programID);

Shader.vert

Shader.frag

89 of 168

Load, Compile, and Link

GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);

GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

vsText = ReadShader(vertexShaderFileName);

fsText = ReadShader(fragmentShaderFileName);

glShaderSource(vertShader, 1, &vsText, 0);

glShaderSource(fragShader, 1, &fsText, 0);

glCompileShader(vertShader);

glCompileShader(fragShader);

programID = glCreateProgram();

glAttachShader(programID, vertShader);

glAttachShader(programID, fragShader);

glLinkProgram(programID);

number of

string arrays

Length of

each string

90 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

91 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

92 of 168

Pass Attributes

GLuint vPosID;

glGenBuffers(1, &vPosID);

glBindBuffer(GL_ARRAY_BUFFER, vPosID);

glBufferData(GL_ARRAY_BUFFER, sizeof(posBuff), posBuff, GL_STATIC_DRAW);

GLint aLoc = glGetAttribLocation(programID, “vPos”);

glEnableVertexAttribArray(aLoc);

glVertexAttribPointer(aLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);

93 of 168

Vertex Information

float posBuff[] = {

px1, py1, pz1,

px2, py2, pz2,

px3, py3, pz3,

.

.

.

};

float colBuff[] = {

cr1, cg1, cb1,

cr2, cg2, cb2,

cr3, cg3, cb3,

.

.

.

};

94 of 168

Pass Attributes

GLuint vPosID;

glGenBuffers(1, &vPosID);

glBindBuffer(GL_ARRAY_BUFFER, vPosID);

glBufferData(GL_ARRAY_BUFFER, sizeof(posBuff), posBuff, GL_STATIC_DRAW);

GLint aLoc = glGetAttribLocation(programID, “vPos”);

glEnableVertexAttribArray(aLoc);

glVertexAttribPointer(aLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);

95 of 168

Example Vertex Shader

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

96 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

97 of 168

Shader Programs

  • Initialization
    • Load, Compile, and Link
    • Pass attributes
  • Run-time
    • Pass uniform variables and draw

98 of 168

Vertex and Fragment Shaders

Shader.vert

attribute vec3 vPos; // posBuff content passed from CPU

attribute vec3 vCol; // colBuff content passed from CPU

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPos, 1.0f);

color = vCol; // passed to fragment shader

}

------------------------------------------------------------------

Shader.frag

varying vec3 color; // passed from the vertex shader

void main()

{

gl_FragColor = vec4(color, 1.0);

}

99 of 168

Run-Time (Display code)

glm::mat4 projectionMatrix = glm::perspective(…);

glm::mat4 viewMatrix = glm::lookAt(…);

glm::mat4 modelMatrix(1.0f);

glUseProgram(programID);

GLint uLoc;

uLoc = glGetUniformLocation(programID, “projection”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &projectionMatrix[0][0]);

uLoc = glGetUniformLocation(programID, “view”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &viewMatrix[0][0]);

uLoc = glGetUniformLocation(programID, “model”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &modelMatrix[0][0]);

glDrawArrays(GL_TRIANGLES, 0, NUM_TRIANGLES);

glUseProgram(0);

100 of 168

Run-Time (Display code)

glm::mat4 projectionMatrix = glm::perspective(…);

glm::mat4 viewMatrix = glm::lookAt(…);

glm::mat4 modelMatrix(1.0f);

glUseProgram(programID);

GLint uLoc;

uLoc = glGetUniformLocation(programID, “projection”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &projectionMatrix[0][0]);

uLoc = glGetUniformLocation(programID, “view”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &viewMatrix[0][0]);

uLoc = glGetUniformLocation(programID, “model”);

glUniformMatrix4fv(uLoc, 1, GL_FALSE, &modelMatrix[0][0]);

glDrawArrays(GL_TRIANGLES, 0, NUM_TRIANGLES);

glUseProgram(0);

number of

matrices

transpose?

101 of 168

Assignment 4

  • Gouraud Shading
  • Phong Shading
  • Silhouette Shading

102 of 168

Assignment 4

103 of 168

Gouraud Shading

  • Shade each vertex (in vertex shader)
  • Interpolate the color of three vertices to obtain the color of fragment (automatically done during rasterization)

104 of 168

Vertex Shader

attribute vec3 vPositionModel; // in object space

attribute vec3 vNormalModel; // in object space

uniform mat4 model;

uniform mat4 view;

uniform mat4 projection;

struct lightStruct

{

vec3 position;

vec3 color;

};

#define NUM_LIGHTS 2

uniform lightStruct lights[NUM_LIGHTS];

uniform vec3 ka;

uniform vec3 kd;

uniform vec3 ks;

uniform float s;

varying vec3 color;

void main()

{

gl_Position = projection * view * model * vec4(vPositionModel, 1.0);

color = vec3(1.0f, 0.0f, 0.0f);

}

105 of 168

Outline

  • Phong reflectance model
  • Shading methods
  • GLSL
  • Texture mapping
  • Advance texturing methods

106 of 168

Texture Mapping Has Many Uses

Pattern on ball

Wood grain on floor

107 of 168

Three Spaces

  • Surface lives in 3D world space
  • Every 3D surface point also has a place where it goes�in the 2D image and in the 2D texture.

Screen space

Texture space

World space

108 of 168

Image Texture Applied to Surface

Rendering with texture

Rendering without texture

Texture image

Zoom

Each triangle “copies” a piece of the texture image back to the surface.

109 of 168

Texture Mapping

110 of 168

Texture Mapping

111 of 168

Texture Mapping

112 of 168

Texture Mapping

113 of 168

Texture Mapping

  • Assume texture parameterized by u, v

114 of 168

Texture Mapping

  • Any u, v coordinate maps to a point on the image

115 of 168

Texture Mapping

  • Associate texture coordinates with each vertex on the surface

116 of 168

Texture Mapping

  • Lookup diffuse color from texture using interpolated texture coord.

 

117 of 168

Texture Mapping

118 of 168

Sampling Issues in Texture Mapping

119 of 168

Applying Textures is Sampling!

for each rasterized screen sample (x,y):

(u,v) = evaluate texcoord value at (x,y)

float3 texcolor = texture.sample(u,v);

set sample’s color to texcolor;

120 of 168

Point Sampling Textures

Point sampling

Jaggies

Moire

Source image: 1280x1280 pixels

256x256 pixels

High-res reference

121 of 168

Triangle Footprint in Texture

Screen Space

World Space

Texture Space

122 of 168

Triangle Footprint in Texture

Screen Space

World Space

Texture Space

123 of 168

Triangle Footprint in Texture

Screen Space

Texture Space

World Space

124 of 168

Triangle Footprint in Texture

Screen Space

World Space

Texture Space

125 of 168

Triangle Footprint in Texture

Screen Space

World Space

Texture Space

126 of 168

Screen Pixel Footprint in Texture

Screen space

Texture space

127 of 168

Screen Pixel Footprint in Texture

Upsampling

(Magnification)

Downsampling

(Minification)

128 of 168

Screen Pixel Area vs Texel Area

  • Texel: A texture pixel
  • At optimal viewing size:
    • 1:1 mapping between pixel sampling rate and �texel sampling rate
  • When texel is larger (magnification)
    • Multiple pixel samples per texel sample
  • When texel is smaller (minification)
    • One pixel sample per multiple texel samples

129 of 168

Texture Magnification

  • Generally don’t want this — insufficient resolution

130 of 168

Triangle Footprint in Texture

Screen Space

World Space

Texture Space

131 of 168

Texture sampling

Want to sample texture value f(u,v) at red point ��Black points indicate texture sample locations

132 of 168

Nearest Neighbor

Want to sample texture value f(u,v) at red point ��Black points indicate texture sample locations

133 of 168

Texture Magnification

  • Generally don’t want this — insufficient resolution

Nearest

134 of 168

Bilinear Filtering

Take 4 nearest sample locations, with texture values as labeled.

135 of 168

Bilinear Filtering

Linear interpolation (1D)

136 of 168

Bilinear Filtering

Linear interpolation (1D)

137 of 168

Bilinear Filtering

Linear interpolation (1D)

Two helper lerps (horizontal)

138 of 168

Bilinear Filtering

Linear interpolation (1D)

Two helper lerps

Final vertical lerp, to get result:

u

139 of 168

Texture Magnification

  • Generally don’t want this — insufficient resolution

Nearest

Bilinear

Bicubic

140 of 168

Texture Minification

141 of 168

Triangle Footprint in Texture

Screen Space

Texture Space

World Space

142 of 168

Triangle Footprint in Texture

Screen Space

Texture Space

World Space

143 of 168

Point Sampling Textures Causes Aliasing

High-res reference

Point sampling

144 of 168

Will Supersampling Antialias?

512x supersampling

High-res reference

Point sampling

145 of 168

Texture Antialiasing

  • Will supersampling work?
    • Yes, high quality, but costly
    • When highly minified, many texels in pixel footprint
  • Goal: efficient texture antialiasing
    • How? Antialiasing = filtering before sampling!

146 of 168

Triangle Footprint in Texture

Screen Space

Texture Space

Texture Space

Lower Resolution

147 of 168

Level 0 - Full Resolution Texture

Aliasing

OK

148 of 168

Level 2 - Downsample 4x4

Aliasing

Blurring

149 of 168

Level 4 - Downsample 16x16

OK

Blurring

150 of 168

Texture Minification

  • Idea:
    • Low-pass filter and downsample texture file, and store successively lower resolutions
    • For each sample, use the texture file whose resolution approximates the screen sampling rate

151 of 168

Mipmap (L. Williams 83)

Level 2 = 32x32

Level 3 = 16x16

Level 4 = 8x8

Level 5 = 4x4

Level 1 = 64x64

Level 0 = 128x128

Level 6 = 2x2

Level 7 = 1x1

“Mip” comes from the Latin “multum in parvo", meaning a multitude in a small space

152 of 168

Triangle Footprint in Texture

Screen Space

Texture Space

D = 4, L = 2

D = 2, L = 1

153 of 168

Mipmap (L. Williams 83)

Williams’ original proposed mipmap layout

“Mip hierarchy”

level = D

u

v

What is the storage overhead of a mipmap?

D

154 of 168

Mipmap Limitation

Point sampling

155 of 168

Mipmap Limitation

Point sampling

Supersampling 512x

156 of 168

Mipmap Limitation

Point sampling

Mipmap trilinear sampling

157 of 168

Mipmal Limitation

  • Overblurs the texture because max length in two directions is chosen as D

158 of 168

Anisotropic Filtering

  • Ripmaps and summed area tables
    • Can look up axis-aligned rectangular zones
    • Diagonal footprints still a problem

Wikipedia

159 of 168

Anisotropic Filtering

Elliptical weighted average (EWA) filtering

160 of 168

Supersampling (reference)

Point sampling

Supersampling 512x

161 of 168

Outline

  • Phong reflectance model
  • Shading methods
  • GLSL
  • Texture mapping
  • Advance texturing methods

162 of 168

Bump Mapping

163 of 168

Bump Mapping

  • Use texture to perturb surface normal
  • Shade using the perturbed normal

After bump mapping

164 of 168

Bump/Normal Mapping Example

165 of 168

Bump Mapping

  • Problem: silhouette of the objects and shadows wrong

Geometry

Bump mapping

Perturbs normals

166 of 168

Displacement Mapping

  • Problem: needs high resolution geometry, costly

Geometry

Bump mapping

Displacement mapping

Perturbs normals

Perturbs positions

167 of 168

Bump/Normal Mapping Example

168 of 168

Displacement Mapping Example