Skip to content

Shading is the process of applying a material to an object. It involves determining how light interacts with the surface of the object, including its color, texture, and reflectivity. The shading process is crucial for creating realistic images in computer graphics.


05-CG-Shading

Visibility / Occlusion

  • Z-buffering

Shading

  • Blinn-Phong reflectance Model
  • Shading Models / frequencies
  • Graphics Pipeline
  • Texture Mapping
  • Barycentric Coordinates
  • Texture Queries
  • Applications of Textures

Visibility / occlusion

Painter's Algorithm

Inspired by how painters paint Paint form back to front, overwrite in the frame buffer

画家算法:由远及近

Requires sorting in depth (O(n logn) for n triangles) Can have unresolvable depth order ri9viu

Z-Buffer

This is the algorithm that eventually won.

Idea:

  • Store current min. z-value for each sample (pixel)
  • Needs an additional buffer for depth values
    • frame buffer stores color values
    • depth buffer (z-buffer) stores depth

+ IMPORTANT: For simplicity we suppose: *z is always positive*

  • smaller z means closer to the camera.
  • larger z means farther from the camera.

i6vYj7

Z-Buffer Algorithm

Initialize depth buffer to During rasterization:

cpp
for (each triangle T)
	for (each sample (x,y,z) in T)
		if (z < zbuffer[x,y]) // closest sample so far
			framebuffer[x,y] = rgb; // update color
			zbuffer[x,y] = z; // update depth
		else ;// do nothing, this sample is occluded

ZpavJh

Complexity

  • O(n) for n triangles (assuming constant coverage)

Most important visibility algorithm

  • Implemented in hardware for all GPUs

Shading

The darkening or coloring of an illustration or diagram with parallel lines or a block of color.

In Computer Graphics:

  • Shading is the process of applying a material to an object.

A Simple Shading Model: (Blinn-Phong Reflection Model)

Perceptual Observations

  • Specular Highlights 镜面高光
  • Diffuse Reflection 漫反射
  • Ambient Lighting 环境光

rL8xKj

Shading is Local

阴影是局部的

Compute light reflected toward camera at a specific shading point

Inputs: All of the following is unit vectors

  • Viewer direction, v
  • Surface normal, n
  • Light direction, I (for each of many lights)
  • Surface parameters (color, shininess, ...)

WTha9K

No Shadows will be generated! (Shading Shadow)

Diffuse Reflection

Light is scattered uniformly in all directions

  • Surface color is the same for all viewing directions

How much light (energy) is received?

  • Lambert’s cosine law

6M5oYM

  • Lighting Falloff

YldksD

Shading independent of view direction 在任意方向观察,颜色都一样

aKXnrn

Energy arrived at the shading point

Ld=kd(Ir2)max(0,nl)
  • kd: diffuse coefficient (color)
  • (Ir2): energy received by the shading point
  • Ld: diffusely reflected light
  • nl: cosine of angle between light direction and surface normal

OFTTWy

Specular Reflection

Intensity depends on view direction

  • Bright near mirror reflection direction

JiybPV

V close to mirror direction half vector near normal 角平分线

  • Measure "near" by dot product of unit vectors

MbOza8

(半程向量)

h=bisector(v,l)h=v+lv+lLs=ks(Ir2)max(0,cosα)p=ks(Ir2)max(0,nh)p
  • ks: specular coefficient
  • Ls: specularly reflected light
  • p: specular exponent (shininess)

p used to control the size of the highlight, usually p is set to 100-200.

  • Increasing p narrows the reflection lobe.

5Dvhuy

SXJyZ4

Ambient Lighting

Shading that does not depend on anything

  • Add constant color to account for disregarded illumination and fill in black shadows
  • This is approximate / fake!

K0ZoB9

保证没有地方是黑的

La=kaIa
  • ka: ambient coefficient
  • La: reflected ambient light

Blinn-Phong Reflection Model

1jgAXJ

L=La+Ld+Ls

   =kaIa+kd(Ir2)max(0,nl)+ks(Ir2)max(0,nh)p

Shading Frequencies

QSpDda

  • Flat shading: Shade each triangle
  • Gouraud shading: Shade each vertex
  • Phong Shading: Shade each pixel

Flat Shading

  • Triangle face is flat — one normal vector
  • Not good for smooth surfaces VdewHx

Gouraud shading

  • Interpolate colors from vertices across triangle
  • Each vertex has a normal vector UURAyE

Phong Shading

  • Interpolate normal vectors across each triangle
  • Compute full shading model at each pixel
  • Not the Blinn-Phong Reflectance Model

Rtro7p

CKSHML

当渲染图形较为简单时,可以采用着色频率较低的着色方法,来提高渲染速度。

Defining Per-Vertex Normal Vectors

Best to get vertex normals from the underlying geometry

  • e.g. consider a sphere JDnh9q

Otherwise have to infer vertex normals from triangle faces

  • Simple scheme: average surrounding face normals
Nv=iNiiNi

s6ZSxN

Defining Per-Pixel Normal Vectors

Barycentric interpolation of vertex normals

nJLNuS

Don’t forget to normalize the interpolated directions.

Graphics Pipeline

(Real-time Rendering)

VnNXeh

VU61ms

y17lxT

uIjRch

5BHXax

DIOVpJ

Shader Programs

  • Program vertex and fragment processing stages
  • Describe operation on a single vertex (or fragment)

Example GLSL fragment shader program

cpp
uniform sampler2D myTexture; // program parameter
uniform vec3 lightDir;// program parameter
varying vec2 uv; // per fragment value (interp. by rasterizer)
varying vec3 norm; // per fragment value (interp. by rasterizer)

void diffuseShader(){
	vec3 kd;
	kd = texture2d(myTexture, uv); // material color from texture
	kd *= clamp(dot(-lightDir, norm), 0.0, 1.0); // Lambertian shading model
	gl_FragColor = vec4(kd, 1.0); // output fragment color
}
  • Shader function executes once per fragment.
  • Outputs color of surface at the current fragment's screen sample position.
  • This shader performs a texture lookup to obtain the surface's material color at this point, then performs a diffuse lighting calculation.

Texture Mapping

Surfaces are 2D

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

tw6Uez

Visualization of Texture Coordinates

Each triangle vertex is assigned a texture coordinate (u,v) u,v[0,1].

gVF8L4

Textures can be used multiple times!

QhexYI

Interpolation Across Triangles

Why do we want to interpolate?

  • Specify values at vertices
  • Obtain smoothly varying values across triangles.

What do we want to interpolate?

  • Texture coordinates, colors, normal vectors,

How do we interpolate?

  • Barycentric Coordinates 重心坐标

Barycentric Coordinates

A coordinate system for triangles (α,β,γ)

(x,y)=αA+βB+γCα+β+γ=1

eCxs59

Eample: Point A:

α,β,γ=(1,0,0)(x,y)=αA+βB+γC=A

Geometric viewpoint - proportional areas

α=AAAA+AB+ACβ=ABAA+AB+ACγ=ACAA+AB+AC

LKGJbv

What is the barycentric coordinates of the centroid? hwCHiT

Barycentric Coordinates: Formulas

ZeWNAE

α=(xxB)(yCyB)+(yyB)(xCxB)(xAxB)(yCyB)+(yAyB)(xCxB)β=(xxC)(yAyC)+(yyC)(xAxC)(xBxC)(yAyC)+(yByC)(xAxC)γ=1αβ

Using Barycentric Coordinates

Linearly interpolate values at vertices

V=αVA+βVB+γVC

V could be positions, texture coordinates, color, normal, depth, material attributes,

YtJeb6

However, barycentric coordinates are not invariant under projection!

Applying Textures

Simple Texture Mapping: Diffuse Color

python
for each rasterized screen sample (x,y):
	(u,v) = evaluate texture coordinate at (x,y)
	texcolor = texture.sample(u,v);
	set sample's color to texcolor;
  • (x,y): Usually a pixel's center
  • (u,v): using barycenteric coordinates
  • set sample's color to texcolor: Usually the diffuse albedo Kd (Bling-Phong reflectance model)

Texture Magnification

What if the texture is too small?

Generally don't want this - Insufficient texture resolution A pixel on a texture - a texel (纹理元素,纹素)

Bilinear interpolation

NrUGR7

Take 4 nearest sample locations, with texture values as labeled. And fractional offsets, (s,t) as shown

Linear interpolation (1D)

lerp(x,v0,v1)=v0+x(v1v0)

GAAIeQ

Two helper lerps (horizontal)

μ0=lerp(s,μ00,μ10)μ1=lerp(s,μ01,μ11)

h9kYON

Final vertical lerp, to get result:

f(x,y)=lerp(t,μ0,μ1)

Bilinear Interpolation

Bicubic interpolation: 取邻近 16 个 pixel 插值

YlTh26

What if the texture is too large?

Pe3n54

Antialiasing — Supersampling?

Will supersampling work?

  • Yes, high quality, but costly
  • When highly minified, many texels in pixel footprint
  • Signal frequency too large in a pixel
  • Need even higher sampling frequency

Let's understand this problem in another way

  • What if we don't sample?
  • Just need to get the average value within a range!
Point Query vs. (Avg.) Range Query

I4CY0Z

Mipmap

Allowing (fast, approx.,square) range queries

Ehgotg

Wwjbp0

7Dm7PW

BqU2D5

在第D层查询该值 D=log2L

**Example: **

EluYBy 颜色越红,层数越低

Trilinear Interpolation

8dzsdg

2Dl9SO

Mipmap Limitations: Overblur

3TTihB

ClRF3x

Y63S2n

Anisotropic Filtering / Ripmap

各向异性过滤

引入矩形查询,但无法解决平行四边形的查询问题

Hcuj3w

Reason: Irregular Pixel Footprint in Texture

n8QsHB

3bu2bS

EWA filtering
  • Use multiple lookups
  • Weighted average
  • Mipmap hierarchy still helps
  • Can handle irregular footprints

RKoy2R

Textures Application

  • Environment Lighting
  • Store microgeometry
  • Procedural textures
  • Solid modeling
  • Volume rendering
  • ...