Skip to content

Shading

这节课

  • Barycentric coordinates (重心坐标)
  • Texture queries (纹理查询)
  • Applications of textures (应用)

Interpolation Across Triangles: Barycentric Coordinates (重心坐标)

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 (α,β,γ)

What’s the barycentric coordinate of A?

$A,B,C$ 是坐标向量, 例A: (1, 2)

Geometric viewpoint — proportional areas (几何视点-比例面积)

What’s the barycentric coordinate of the centroid? (质心的重心坐标是什么?)

Formulas

Using Barycentric Coordinates

Linearly interpolate values at vertices (在顶点处线性插值)

However, barycentric coordinates are not invariant under projection! (然而,质心坐标在投影下不是不变的!)

Applying Textures

Simple Texture Mapping: Diffuse Color (简单纹理映射:漫反射颜色)

for each rasterized screen sample (x,y): -> Usually a pixel's center

  • (u,v) = evaluate texture coordinate at (x,y) -> Using barycentric coordinates!

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

  • set sample’s color to texcolor; -> Usually the diffuse albedo Kd(recall the Blinn-Phong reflectance model)

Texture Magnification (What if the texture is too small?)

Easy Case

Generally don’t want this — insufficient texture resolution

A pixel on a texture — a texel(纹理元素、纹素)

Bilinear Interpolation (双线性插值)

Want to sample texture value f(x,y) at red point (想要取样吗? 纹理值f(x,y)在红点)

Black points indicate texture sample locations (黑点表示纹理样本的位置)

Take 4 nearest sample locations, with texture values as labeled. (取4个最近的样本位置,标记纹理值)

And fractional offsets, (s,t) as shown (分数偏移量, (s,t)如图所示)

Linear interpolation (1D)

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

Two helper lerps (horizontal)

u0=lerp(s,u00,u10)

u1=lerp(s,u01,u11)

Final vertical lerp, to get result:

f(x,y)=lerp(t,u0,u1)

Bilinear interpolation usually gives pretty good results at reasonable costs (双线性插值通常以合理的成本给出相当好的结果)

Texture Magnification (What if the texture is too large?)

Hard Case

Point Sampling Textures — Problem (点采样纹理-问题)

Screen Pixel "Footprint" in Texture (纹理中的屏幕像素"足迹")

Will Supersampling Do Antialiasing? (超采样会做抗锯齿吗?)

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

Different Pixels -> Different-Sized Footprints(不同像素->不同大小的足迹)

Mipmap (Allowing (fast, approx., square) range queries)

"Mip" comes from the Latin "multum in parvo", meaning a multitude in a small space ("Mip"来自拉丁语"multum in parvo",意思是一个小空间里的许多人)

"Mip hierarchy"

level = D

What is the storage overhead of a mipmap?

Computing Mipmap Level D (计算Mipmap级别D)

Estimate texture footprint using texture coordinates of neighboring screen samples (使用相邻屏幕样本的纹理坐标估计纹理占用)

D=log2L

L=max((dudx)2+(dvdx)2,(dudy)2+(dvdy)2)

Visualization of Mipmap Level (Mipmap级别的可视化)

D rounded to nearest integer level

Trilinear Interpolation (三维插值)

Visualization of Mipmap Level

Trilinear filtering: visualization of continuous D

Mipmap Limitations

过度模糊

Anisotropic Filtering (各向异性过滤:一种3D显示技术,通过对周围各个方向上的像素进行取样计算后映射到目标像素上,以提高图像的精度和逼真度。)

Irregular Pixel Footprint in Texture (纹理中的不规则像素足迹)

Anisotropic Filtering

Ripmaps and summed area tables

  • Can look up axis-aligned rectangular zones
  • Diagonal footprints still a problem

EWA filtering

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

Applications of textures (应用)

In modern GPUs, texture = memory + range query (filtering) (在现代gpu中,纹理=内存+范围查询(过滤))

  • General method to bring data to fragment calculations (将数据带入片段计算的一般方法)

Many applications

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

Environment Map (环境映射)

Environmental Lighting

Environment map (left) used to render realistic lighting (环境图(左)用于渲染逼真的照明)

Spherical Environment Map (球面环境映射)

Spherical Map — Problem (问题)

Prone to distortion (top and bottom parts)! (容易变形(上下部分)!)

Cube Map (立方体映射)

为了解决球面扭曲的问题,使用立方体来进行环境光照的纹理映射。 A vector maps to cube point along that direction. (一个矢量沿着这个方向映射到一个立方体点。)

The cube is textured with 6 square texture maps.(立方体有6个正方形纹理贴图。)

Textures can affect shading! (纹理可以影响阴影!)

  • Textures doesn't have to only represent colors
    • What if it stores the height / normal? (如果它存储高度/法线呢?)
    • Bump / normal mapping (凹凸/法线映射)
    • Fake the detailed geometry (假的 详细的几何结构)

Bump/Normal Mapping (凹凸/法线贴图)

Adding surface detail without adding more triangles (添加表面细节而不添加更多三角形)

  • Perturb surface normal per pixel (for shading computations only) (每像素的扰动表面法线 (只适用于阴影计算))
  • "Height shift" per texel defined by a texture (每个纹理定义的"高度移动")
  • How to modify normal vector? (如何修改法向量?)

How to perturb the normal (in flatland) (计算法线贴图(一维))

  • Original surface normal n(p) = (0, 1)
  • Derivative at p is dp = c * [h(p+1) - h(p)]
  • Perturbed normal is then n(p) = (-dp, 1).normalized() (切线旋转90°得到)

How to perturb the normal (in 3D)

  • Original surface normal n(p) = (0, 0, 1)
  • Derivatives at p are
    • dp/du = c1 * [h(u+1) - h(u)]
    • dp/dv = c2 * [h(v+1) - h(v)]
  • Perturbed normal is n = (-dp/du, -dp/dv, 1).normalized()
  • Note that this is in local coordinate! More will be elaborated in FAQ of HW3

Displacement mapping — a more advanced approach (位移映射 -一种更高级的方法)

  • Uses the same texture as in bumping mapping (使用与凹凸贴图相同的纹理)
  • Actually moves the vertices (实际上移动了顶点位置)
  • 代价: 本身要求模型足够细致
    • DirectX 提供了曲面细分来实现

三维纹理

3D Procedural Noise + Solid Modeling (3D程序噪声+实体建模)

Provide Precomputed Shading (提供预先计算的阴影)

3D Textures and Volume Rendering (3D纹理和体渲染)