Nima Kalantari
CSCE 441 - Computer Graphics
Shading and Texture Mapping
Some slides from Ren Ng and Scott Schaefer
So far
Realistic Lighting
Image taken from http://graphics.ucsd.edu/~henrik/images/cbox.html
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
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
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
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
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
Lighting/Illumination
Reflection Models
Types of Reflection Functions
Illumination Types
Realistic Lighting (global illumination)
Image taken from http://graphics.ucsd.edu/~henrik/images/cbox.html
would be completely black with local ilumination
Outline
Phong reflection model
Perceptual Observations
Photo credit: Jessica Andrews, flickr
Diffuse reflection
Specular highlights
Ambient lighting
Illumination Model
Ambient Light
Example
Diffuse Light
Diffuse Light
Surface
Diffuse Ball Example
akimeta.com
Lambert’s Law
Surface
Beam of Light
Lambert’s Law
Surface
Beam of Light
Lambert’s Law
Surface
Beam of Light
Diffuse Light
Surface
Example
Example
Specular Light
Perceptual Observations
Photo credit: Jessica Andrews, flickr
Diffuse reflection
Specular highlights
Ambient lighting
Specular Light
Surface
Specular Light
Surface
Cosine Power Plots
[Foley et al.]
Finding the Reflected Vector
Surface
Finding the Reflected Vector
Surface
Finding the Reflected Vector
Surface
Finding the Reflected Vector
Surface
Finding the Reflected Vector
Surface
Total Illumination
Total Illumination
Total Illumination
Total Illumination
Multiple Light Sources
Total Illumination
Total Illumination
Light Falloff
Spot Lights
Spot Lights
Surface
Spot Lights
Surface
Spot Lights
Spot Lights
Small α
Spot Lights
Large α
Outline
Shading Methods
Shading Frequency: Triangle, Vertex or Pixel
Defining Per-Vertex Normal Vectors
geometry
Shading Frequency: Triangle, Vertex or Pixel
Defining Per-Pixel Normal Vectors
Problem: length of vectors?
Shading Frequency: Face, Vertex or Pixel
Image credit: Happyman, http://cg2010studio.com/
Num�Vertices
Face�Flat
Vertex
Gouraud
Pixel
Phong
Shading freq. :�Shading type :
Implementation Notes
Careful: Transforming Normal Vectors
Original object�& normal vectors
Transforming vertices & �normals with matrix M
Incorrect normals!
Careful: Transforming Normal Vectors
Original object�& normal vectors
Transforming vertices & �normals with matrix M
Incorrect normals!
Transforming
normals with M–T
Correct normals
Careful: Transforming Normal Vectors
Outline
Rasterization Pipeline
Vertex Processing
Triangle Processing
Rasterization
Framebuffer Operations
Shaded Fragments
Fragment Stream
Triangle Stream
Vertex Stream
Display
Application
Fragment Processing
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
Shading Language
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,
.
.
.
};
Example
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
}
Variables
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
}
Variables
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
}
Variables
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
}
Variables
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
}
Example Fragment Shader
varying vec3 color; // passed from the vertex shader
void main()
{
gl_FragColor = vec4(color, 1.0);
}
Vertex Shader
Position0
Position1
Position2
…
Color 0
Color 1
Color 2
…
Attribute variables
Vertex buffer
Vertex Shader
Varying variables
…
gl_Position,
…
Uniform variables
Rasterizer
Fragment (Pixel) Shader
Fragment Shader
Varying variables
Uniform variables
gl_FragColor
Rasterizer
…
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
Shader Programs
Shader Programs
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);
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);
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);
}
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
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
Shader Programs
Shader Programs
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);
Vertex Information
float posBuff[] = {
px1, py1, pz1,
px2, py2, pz2,
px3, py3, pz3,
.
.
.
};
float colBuff[] = {
cr1, cg1, cb1,
cr2, cg2, cb2,
cr3, cg3, cb3,
.
.
.
};
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);
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
}
Shader Programs
Shader Programs
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);
}
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);
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?
Assignment 4
Assignment 4
Gouraud Shading
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);
}
Outline
Texture Mapping Has Many Uses
Pattern on ball
Wood grain on floor
Three Spaces
Screen space
Texture space
World space
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.
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Texture Mapping
Sampling Issues in Texture Mapping
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;
Point Sampling Textures
Point sampling
Jaggies
Moire
Source image: 1280x1280 pixels
256x256 pixels
High-res reference
Triangle Footprint in Texture
Screen Space
World Space
Texture Space
Triangle Footprint in Texture
Screen Space
World Space
Texture Space
Triangle Footprint in Texture
Screen Space
Texture Space
World Space
Triangle Footprint in Texture
Screen Space
World Space
Texture Space
Triangle Footprint in Texture
Screen Space
World Space
Texture Space
Screen Pixel Footprint in Texture
Screen space
Texture space
Screen Pixel Footprint in Texture
Upsampling
(Magnification)
Downsampling
(Minification)
Screen Pixel Area vs Texel Area
Texture Magnification
Triangle Footprint in Texture
Screen Space
World Space
Texture Space
Texture sampling
Want to sample texture value f(u,v) at red point ��Black points indicate texture sample locations
Nearest Neighbor
Want to sample texture value f(u,v) at red point ��Black points indicate texture sample locations
Texture Magnification
Nearest
Bilinear Filtering
Take 4 nearest sample locations, with texture values as labeled.
Bilinear Filtering
Linear interpolation (1D)
Bilinear Filtering
Linear interpolation (1D)
Bilinear Filtering
Linear interpolation (1D)
Two helper lerps (horizontal)
Bilinear Filtering
Linear interpolation (1D)
Two helper lerps
Final vertical lerp, to get result:
u
Texture Magnification
Nearest
Bilinear
Bicubic
Texture Minification
Triangle Footprint in Texture
Screen Space
Texture Space
World Space
Triangle Footprint in Texture
Screen Space
Texture Space
World Space
Point Sampling Textures Causes Aliasing
High-res reference
Point sampling
Will Supersampling Antialias?
512x supersampling
High-res reference
Point sampling
Texture Antialiasing
Triangle Footprint in Texture
Screen Space
Texture Space
Texture Space
Lower Resolution
Level 0 - Full Resolution Texture
Aliasing
OK
Level 2 - Downsample 4x4
Aliasing
Blurring
Level 4 - Downsample 16x16
OK
Blurring
Texture Minification
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
Triangle Footprint in Texture
Screen Space
Texture Space
D = 4, L = 2
D = 2, L = 1
Mipmap (L. Williams 83)
Williams’ original proposed mipmap layout
“Mip hierarchy”
level = D
u
v
What is the storage overhead of a mipmap?
D
Mipmap Limitation
Point sampling
Mipmap Limitation
Point sampling
Supersampling 512x
Mipmap Limitation
Point sampling
Mipmap trilinear sampling
Mipmal Limitation
Anisotropic Filtering
Wikipedia
Anisotropic Filtering
Elliptical weighted average (EWA) filtering
Supersampling (reference)
Point sampling
Supersampling 512x
Outline
Bump Mapping
Bump Mapping
After bump mapping
Bump/Normal Mapping Example
Bump Mapping
Geometry
Bump mapping
Perturbs normals
Displacement Mapping
Geometry
Bump mapping
Displacement mapping
Perturbs normals
Perturbs positions
Bump/Normal Mapping Example
Displacement Mapping Example