games101-note-L16

Monte Carlo Integration

Why: we want to solve an integral, but it can be too difficult to solve analytically.

1717159620443

难以用解析法求解

数值解法:

1.黎曼积分:用很多小矩形近似

2.蒙特卡洛积分:多次采样近似

1717160122259

Example: Uniform Monte Carlo Estimator

1717160165980

Some notes:
• The more samples, the less variance.
• Sample on x, integrate on x.

Path Tracing

Motivation: Whitted-Style Ray Tracing

Whitted-style ray tracing:

-Always perform specular reflections / refractions

-Stop bouncing at diffuse surfaces

Are these simplifications reasonable?

Whitted-Style Ray Tracing: Problem 1

Where should the ray be reflected for glossy materials?

1717160516256

Whitted-Style Ray Tracing: Problem 2

No reflections between diffuse materials?

1717160607141

Whitted-Style Ray Tracing is Wrong

But the rendering equation is correct

1717160752115

But it involves

  • Solving an integral over the hemisphere, and

  • Recursive execution

How do you solve an integral numerically?

A Simple Monte Carlo Solution for direct illumination

Suppose we want to render one pixel (point) in the following scene for direct illumination only

1717160889039

Abuse the concept of Reflection Equation a little bit

1717160993144

(assume all directions are pointing outwards

Fancy as it is, it’s still just an integration over directions
So, of course we can solve it using Monte Carlo integration!

1717161269869

So, in general

1717161396240

What does it mean?
A correct shading algorithm for direct illumination!
Code:

1717161469162

Introducing Global Illumination

One more step forward: what if a ray hits an object?

1717161859991

Q also reflects light to P! How much? The dir. illum. at Q!

假设P点有个相机,计算Q点的直接光照

代码修改:

1717161945872

Problem 1: Explosion of #rays as #bounces go up:

#rays = N#bounces

1717163192180

Key observation: #rays will not explode iff N = ???????

From now on, we always assume that only 1 ray is traced at each shading point:

1717163442209

This is path tracing! (FYI, Distributed Ray Tracing if N != 1)

But this will be noisy!

Ray Generation

No problem, just trace more paths through each pixel and average their radiance!

1717163530467

Very similar to ray casting in ray tracing

1717163661186

Problem 2: The recursive algorithm will never stop!

Dilemma: the light does not stop bouncing indeed!

Cutting #bounces == cutting energy!

Solution: Russian Roulette (RR) 俄罗斯轮盘赌

Russian Roulette is all about probability
With probability 0 < P < 1, you are fine
With probability 1 - P, otherwise

1717163882142

code:

1717164152445

Problem of sampling: low efficiency

Now we already have a correct version of path tracing!
But it’s not really efficient

1717164321859

(SPP the number of ray genetating at each pixel)

Sampling the Light

Understanding the reason of being inefficient

1717164449030

there will be 1 ray hitting the light. So a lot of rays are “wasted”if we uniformly sample the hemisphere at the shading point

想要在光源上采样,需要修改到在光源上积分。

Monte Carlo methods allows any sampling methods, so we can sample the light (therefore no rays are “wasted”)

Assume uniformly sampling on the light:
pdf = 1 / A (because ∫pdf dA = 1)

But the rendering equation integrates
on the solid angle: Lo = ∫Li fr cos dω.

Recall Monte Carlo Integration:
Sample on x & integrate on x

Since we sample on the light, can we integrate on the light?

Need to make the rendering equation as an integral of dA
Need the relationship between dω and dA 寻找两者关系

Easy! Recall the alternative def. of solid angle:
Projected area on the unit sphere 投影到单位球面上

1717164964688

Then we can rewrite the rendering equation as

1717165066956

Now an integration on the light!
Monte Carlo integration:
“f(x)”: everything inside Pdf: 1 / A

Summary

Previously, we assume the light is “accidentally” shot by
uniform hemisphere sampling

分为2部分
Now we consider the radiance coming from two parts:

–light source (direct, no need to have RR) 直接来自光线,无需反射,即无需RR

–other reflectors (indirect, RR)

code:

1717165260742

One final thing: how do we know if the sample on the light is not blocked or not?

code:

1717165400017

Now path tracing is finally done!

点光源很难,可以做成很小的面积光源

Some Side Notes

• Path tracing (PT) is indeed difficult

-Consider it the most challenging in undergrad CS

-Why: physics, probability, calculus, coding

-Learning PT will help you understand deeper in these

• Is it still “Introductory”?

-Not really, but it’s “modern” :)

-And so learning it will be rewarding also because …

Ray tracing: Previous vs. Modern Concepts

• Previous

-Ray tracing == Whitted-style ray tracing
• Modern (the teacher’s definition)

-The general solution of light transport, including

-(Unidirectional & bidirectional) path tracing

-Photon mapping

-Metropolis light transport

-VCM / UPBP…

Things we haven’t covered / won’t cover

• Uniformly sampling the hemisphere

-How? And in general, how to sample any function? (sampling)

• Monte Carlo integration allows arbitrary pdfs

-What’s the best choice? (importance sampling)

• Do random numbers matter?

-Yes! (low discrepancy sequences)

• I can sample the hemisphere and the light

-Can I combine them? Yes! (multiple imp. sampling)

• The radiance of a pixel is the average of radiance on all paths passing through it

-Why? (pixel reconstruction filter)

• Is the radiance of a pixel the color of a pixel?

-No. (gamma correction, curves, color space)

• Asking again, is path tracing still “Introductory”?

-This time, yes. Fear the science, my friends

games101-note-L15 Ray Tracing 3 (Light Transport & Global Illumination)

Radiometry

Irradiance

Definition: The irradiance is the power per unit area incident on a surface point.

1717154765390

A指的是投影的面积

Radiance

Radiance is the fundamental field quantity that describes the distribution of light in an environment

• Radiance is the quantity associated with a ray
• Rendering is all about computing radiance

Definition: The radiance (luminance) is the power emitted, reflected, transmitted or received by a surface,

per unit solid angle, per projected unit area.

1717155199774

Recall
• Irradiance: power per projected unit area
• Intensity: power per solid angle
So
• Radiance: Irradiance per solid angle
• Radiance: Intensity per projected unit area

Incident Radiance

Incident radiance is the irradiance per unit solid angle arriving at the surface.

1717155606769

i.e. it is the light arriving at the surface along a given ray
(point on surface and incident direction).

Exiting Radiance

Exiting surface radiance is the intensity per unit projected area leaving the surface.

1717155635712

e.g. for an area light it is the light emitted along a given ray
(point on surface and exit direction).

Irradiance vs. Radiance

1717155724492

Light transport

Bidirectional Reflectance Distribution Function (BRDF)

Radiance from direction ωi turns into the power E that dA receives
Then power E will become the radiance to any other direction ω

1717156055163

1717156129189

f_r 即BRDF 定义了光从一个方向到达接触点后,向另一个方向的反射的强度

The Reflection Equation

1717156429171

Challenge: Recursive Equation

光线会弹射多次

Reflected radiance depends on incoming radiance

But incoming radiance depends on reflected radiance (at another point in the scene)

The Rendering Equation

1717156673951

Note: now, we assume that all directions are pointing outwards

Understanding the rendering equation

一个点光源

1717156862885

多个点光源

1717156896586

面光源:

1717156969968

面光源带反射光源:

1717157005405

Rendering Equation as Integral Equation

1717157407925

Linear Operator Equation

1717157436878

1717157449108

Global illumination

1717157573507

分解为不同的弹射次数,得到全局光照(不同弹射次数光照的集合)

games101-note-L14 Ray Tracing 2 (Acceleration & Radiometry)

Uniform Spatial Partitions (Grids)

Preprocess – Build Acceleration Grid

  1. Find bounding box
  2. Create grid
  3. Store each object
    in overlapping cells

1717145643371

Grid Resolution?

One cell
• No speedup

Too many cells
• Inefficiency due to extraneous grid traversal

需要平衡格子数量

Grids work well on large collections of objects that are distributed evenly in size and space

在分布均匀的物体上,格子方法效果不错

Spatial Partitions

空间划分

Spatial Partitioning Examples

1717145954424

Oct-tree:对空间不断八分 (2维是4分)

KD-tree:不断沿着轴方向二分

BSP-tree:不是横平竖直来划分,斜着

Data Structure for KD-Trees

Internal nodes store
• split axis: x-, y-, or z-axis
• split position: coordinate of split plane along axis
• children: pointers to child nodes
• No objects are stored in internal nodes

中间节点不存储物体,只存在叶子节点(不再划分的节点)上
Leaf nodes store
• list of objects

存在的问题:

1 物体可能存在多个格子里

2 难以判断三角形是否在一个格子里(如格子插入三角形的情况,无法只用顶点来判断)

Object Partitions & Bounding Volume Hierarchy (BVH)

1717149544260

不断划分物体

• Find bounding box
• Recursively split set of objects in two subsets
• Recompute the bounding box of the subsets
• Stop when necessary (例如三角形足够少)
• Store objects in each leaf node

Building BVHs

How to subdivide a node? 小技巧
• Choose a dimension to split
• Heuristic #1: Always choose the longest axis in node
• Heuristic #2: Split node at location of median object 让分割后两部分物体数量差不多 (可以使用快速划分算法)O(n)
Termination criteria?
• Heuristic: stop when node contains few elements

Data Structure for BVHs

Internal nodes store
• Bounding box
• Children: pointers to child nodes
Leaf nodes store
• Bounding box
• List of objects
Nodes represent subset of primitives in scene
• All objects in subtree

Spatial vs Object Partitions

Spatial partition (e.g.KD-tree)
• Partition space into
non-overlapping regions
• An object can be contained in multiple regions

Object partition (e.g. BVH)
• Partition set of objects into disjoint subsets
• Bounding boxes for each set may overlap in space

Radiometry

Measurement system and units for illumination
Accurately measure the spatial properties of light

-New terms: Radiant flux, intensity, irradiance, radiance
Perform lighting calculations in a physically correct manner

Radiometry — Motivation

Observation
• In assignment 3, we implement the Blinn-Phong model
• Light intensity I is 10, for example
• But 10 what?
Do you think Whitted style ray tracing gives you CORRECT results?
All the answers can be found in radiometry
• Also the basics of “Path Tracing”

Radiant Energy and Flux (Power)

Definition: Radiant energy is the energy of electromagnetic radiation.

It is measured in units of joules, and denoted by the symbol:

1717150732749

Definition: Radiant flux (power) is the energy emitted, reflected, transmitted or received, per unit time.

1717150795454

Important Light Measurements of Interest

1717150977591

Radiant Intensity

Definition: The radiant (luminous) intensity is the power per unit solid angle (立体角) emitted by a point light source.

1717151078680

The candela is one of the seven SI base units.

Angles and Solid Angles

1717151237941

Differential Solid Angles 单位/微分立体角

1717151311113

Isotropic Point Source 各向同性光源

1717151493311

games101-note-L13 Ray Tracing 1 (Whitted-Style Ray Tracing)

Basic Ray-Tracing Algorithm

Light Rays

Three ideas about light rays

  1. Light travels in straight lines (though this is wrong)
  2. Light rays do not “collide” with each other if they cross (though this is still wrong)
  3. Light rays travel from the light sources to the eye (but the physics is invariant under path reversal - reciprocity).

Ray Casting

Appel 1968 - Ray casting

  1. Generate an image by casting one ray per pixel
  2. Check for shadows by sending a ray to the light

Ray Casting - Generating Eye Rays

Shading Pixels (Local Only)

Pinhole Camera Model

Pinhole Camera Model

每个像素投射出一条光线,求与物体相交最近的点,将该点用于计算光照着色

Recursive (Whitted-Style) Ray Tracing

Recursive (Whitted-Style) Ray Tracing

每个像素投射出一条光线,在物体上进行折射和反射,然后计算折射和反射后光线与物体的所有交点,

然后计算每个交点的光照着色,再根据一个权重加到像素颜色上

Ray-Surface Intersection

Ray Equation

Ray is defined by its origin and a direction vector

Ray Equation

Ray Intersection With Sphere

相交点满足光线和圆的两个方程

Ray Intersection With Sphere  相交点满足光线和圆的两个方程

Ray Intersection With Sphere

Ray Intersection With Implicit Surface

1717143369704

需要t大于等于0 且解是实数

Ray Intersection With Triangle Mesh

Triangle is in a plane
• Ray-plane intersection
• Test if hit point is inside triangle
Many ways to optimize…

先判断和平面是否相交,再判断交点是否在三角形内、

Plane Equation

1717143544043

Ray Intersection With Plane

1717143748013

下面有更简单的方法,直接求出光线和三角形的交点

Möller Trumbore Algorithm

A faster approach, giving barycentric coordinate directly Derivation in the discussion section!

用重心坐标来描述三角形内的点(公式右边)

1717143828344

解出来后需要判断 t非负且重心坐标三个坐标非负

Accelerating Ray-Surface Intersection

Ray Tracing – Performance Challenges

Simple ray-scene intersection
• Exhaustively test ray-intersection with every triangle
• Find the closest hit (i.e. minimum t)
Problem:
• Naive algorithm = #pixels ⨉ # traingles (⨉ #bounces)
• Very slow!

For generality, we use the term objects instead of triangles later (but doesn’t necessarily mean entire objects)

Bounding Volumes 包围盒

Quick way to avoid intersections: bound complex object with a simple volume
• Object is fully contained in the volume
• If it doesn’t hit the volume, it doesn’t hit the object
• So test BVol first, then test object if it hits

1717144441364

Ray-Intersection With Box

Understanding: box is the intersection of 3 pairs of slabs(3个对面)

1717144508447

Specifically:
We often use an Axis-Aligned Bounding Box (AABB) (轴对⻬包围盒)
i.e. any side of the BB is along either x, y, or z axis

Ray Intersection with Axis-Aligned Box

2D example; 3D is the same!

Compute intersections with slabs and take intersection of t_min/t_max intervals

1717144667444

计算t的交集

从3D角度看:

• Recall: a box (3D) = three pairs of infinitely large slabs

• Key ideas

-The ray enters the box only when it enters all pairs of slabs

-The ray exits the box as long as itexitsany pair of slabs

具体计算方法:

For each pair, calculate the tmin and tmax (negative is fine)

For the 3D box, t_enter = max{t_min}, t_exit = min{t_max}

If t_enter < t_exit, we know the ray stays a while in the box (so they must intersect!)

• However, ray is not a line

-Should check whether t is negative for physical correctness!

• What if t_exit < 0?

-The box is “behind” the ray — no intersection!

• What if t_exit >= 0 and t_enter < 0?

-The ray’s origin is inside the box — have intersection!

• In summary, ray and AABB intersect iff (iff指的是当且仅当)

-t_enter < t_exit && t_exit >= 0 最终判断条件

Why Axis-Aligned?

1717145096303

计算简单很多,只需要差值除以对应分量

games101-note-L12 Geometry 3

Mesh Operations: Geometry Processing

• Mesh subdivision
• Mesh simplification
• Mesh regularization

Mesh Subdivision (upsampling)

Mesh Subdivision

Increase resolution

Loop Subdivision

Loop 是作者的名字

• Split each triangle into four

Loop Subdivision

• Assign new vertex positions according to weights - New / old vertices updated differently

Loop Subdivision

Loop Subdivision — Update

新的点:

for new vertices

已有的点:

for old vertices

Catmull-Clark Subdivision (General Mesh)

Each subdivision step:
Add vertex in each face
Add midpoint on each edge
Connect all new vertices

一次细分后,增加了非四边形数量的奇异点(因为四边形中心加入的点不是奇异点),之后再也没有改变奇异点的数量

Extraordinary vertex (degree != 4)

Catmull-Clark Subdivision

Mesh Simplification

Goal: reduce number of mesh elements
while maintaining the overall shape

Mesh Simplification

Collapsing An Edge

• Suppose we simplify a mesh using edge collapsing

Collapsing An Edge

Quadric Error Metrics (⼆次误差度量)

• How much geometric error is introduced by simplification?
• Not a good idea to perform local averaging of vertices
• Quadric error: new vertex should minimize its sum of square distance (L2 distance) to previously related triangle planes!

点距离和它相关的边的距离

Simplification via Quadric Error

由于一条边的坍缩会影响其他边的二次度量误差

利用堆结构每次坍缩二次度量误差最小的边

Iteratively collapse edges
Which edges? Assign score with quadric error metric*
• approximate distance to surface as sum of distances to
planes containing triangles
• iteratively collapse edge with smallest score
• greedy algorithm… great results!

example

Shadows

How to draw shadows using rasterization? - Shadow mapping!

Shadow Mapping

在很多早期电影和游戏里的主要技术

• An Image-space Algorithm

-no knowledge of scene’s geometry during shadow computation

-must deal with aliasing artifacts

• Key idea:

  • the points NOT in shadow must be seen both by the light and by the camera

在点光源下使用

Pass 1: Render from Light

• Depth image from light source 记录光源看到的深度图

Render from Light

Pass 2A: Render from Eye

Standard image (with depth) from eye

Pass 2B: Project to light

Project visible points in eye view back to light source

比较实际深度和记录深度(shadow map),深度相等,光线能照射到,反之则光线被阻挡

(Reprojected) depths match for light and eye. VISIBLE

visible

(Reprojected) depths from light and eye are not the same. BLOCKED!!

BLOCKED

会存在的问题:

1.数值精度:会比较实际深度和记录深度,会有浮点误差

2.会记录光源看到的shadow map:shadow map的分辨率有需求,太低的话会导致阴影又锯齿

• Hard shadows (point lights only)
• Quality depends on shadow map resolution (general problem with image-based techniques)
• Involves equality comparison of floating point depth values means issues of scale, bias, tolerance

games101-note-L11 Geometry 2 (Curves and Surfaces)

Explicit Representations in Computer Graphics

Many Explicit Representations in Graphics

triangle meshes
Bezier surfaces
subdivision surfaces
NURBS
point clouds

Point Cloud (Explicit)

Easiest representation: list of points (x,y,z)
Easily represent any kind of geometry
Useful for LARGE datasets (>>1 point/pixel)
Often converted into polygon mesh
Difficult to draw in undersampled regions

Point Cloud

Polygon Mesh (Explicit)

Store vertices & polygons (often triangles or quads)
Easier to do processing / simulation, adaptive sampling
More complicated data structures
Perhaps most common representation in graphics

The Wavefront Object File (.obj) Format

Commonly used in Graphics research
Just a text file that specifies vertices, normals, texture coordinates and their connectivities
The Wavefront Object File (.obj) Format

v 顶点坐标 vt 纹理坐标 vn 法线方向

f 三角形顶点选择 5/1/1 —> v/vt/vn

Curves

Bézier Curves

Consider three points (quadratic Bezier) – de Casteljau Algorithm

定义起点在时间0,终点在时间1,在b0、b1间取得t时刻的点b01

类比在b1和b2间也取得t时刻的点b11,最后在b01和b11间取得t时刻的点b02

最后Run the same algorithm for every t in [0,1]

quadratic Bezier

Four input points in total

Same recursive linear interpolations

Four input points in total

Bézier Curve – General Algebraic Formula

Bernstein form of a Bézier curve of order n:

Bézier Curve – General Algebraic Formula

伯恩斯坦多项式:i –> 0~n 的求和为1 括号(n i)表达的是C(i n),组合的结果

Properties of Bézier Curves

Interpolates endpoints
• For cubic Bézier:
b(0) = b0; b(1) = b3
Tangent to end segments 相切

• Cubic case:
b0(0) = 3(b1 b0); b0(1) = 3(b3 b2)

Affine transformation property
• Transform curve by transforming control points

Convex hull property
• Curve is within convex hull of control points

Piecewise Bézier Curves 逐段贝塞尔曲线

高阶贝塞尔曲线很难控制,不够直观

Instead, chain many low-order Bézier curve
Piecewise cubic Bézier the most common technique

Piecewise Bézier Curves

Widely used (fonts, paths, Illustrator, Keynote, …)

Piecewise Bézier Curve – Continuity

C0连续

1716895874754

C1连续
1716895887215

C1 continuity

Other types of splines 样条

• Spline

A continuous curve constructed so as to pass through a given set of points and have a certain number of continuous derivatives.

In short, a curve under control

• B-splines 相比贝塞尔曲线具有局部性(一个控制点的变化只会影响局部曲线)

-Short for basis splines

-Require more information than Bezier curves

-Satisfy all important properties that Bézier curves have (i.e. superset)

Surfaces

Bézier Surfaces

Extend Bézier curves to surfaces

Bicubic Bézier Surface Patch

Evaluating Surface Position For Parameters (u,v)

 bi-cubic Bezier surface

Method: Separable 1D de Casteljau Algorithm

Goal: Evaluate surface position corresponding to (u,v)
(u,v)-separable application of de Casteljau algorithm

• Use de Casteljau to evaluate point u on each of the 4 Bezier curves in u. This gives 4 control points for the “moving” Bezier curve
• Use 1D de Casteljau to evaluate point v on the “moving” curve

Separable 1D de Casteljau Algorithm

Mesh Operations: Geometry Processing

• Mesh subdivision
• Mesh simplification
• Mesh regularization

games101-note-L10 Geometry 1 (Introduction)

Application of textures 贴图的应用

In modern GPUs, texture = memory + range query (filtering)

• General method to bring data to fragment calculations

Many applications
• Environment lighting
• Store microgeometry
• Procedural textures
• Solid modeling
• Volume rendering

Environment Map 环境贴图

Environment Map

Spherical Map

spherical map

问题:Prone to distortion (top and bottom parts)!

Cube Map

Cube map

A vector maps to cube point along that direction.
The cube is textured with 6 square texture maps.

Much less distortion!
Need dir->face computation

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 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?

Bump Mapping

Perturb the normal(in flatland)计算法线的扰动

perturb the normal (in flatland)

• Original surface normal n(p) = (0, 1)
• Derivative at p is dp = c * [h(p+1) - h(p)] 计算斜率向量(1,dp)
• Perturbed normal is then n(p) = (-dp, 1).normalized() 逆时针旋转90度得到法线向量(-dp,1)

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 (认为局部地方的法线为(0,0,1))

Displacement mapping 位移贴图- a more advanced approach

使用和凹凸贴图相同的贴图

Actually moves the vertices

Displacement mapping VS Bump Mapping

需要模型的三角形足够细致

3D Procedural Noise + Solid Modeling

利用三维噪声来生成外表

3D Procedural Noise + Solid Modeling

(Perlin noise, Ken Perlin)

Provide Precomputed Shading

Provide Precomputed Shading

3D Textures and Volume Rendering

3D texture

Introduction to geometry

Examples of geometry

各种几何图形

Various representations of geometry

Implicit
• algebraic surface
• level sets
• distance functions
• …

Explicit
• point cloud
• polygon mesh
• subdivision, NURBS
• …

Each choice best suited to a different task/type of geometry

Implicit Representation of Geometry

Based on classifying points
• Points satisfy some specified relationship
E.g. sphere: all points in 3D, where x^2+y^2+z2 = 1
More generally, f(x,y,z) = 0

容易用于判断点在范围内外,但是很难直接得到面上所有的点。

Implicit surface

Sampling Can Be Hard

Sampling Can Be Hard

Explicit Representation of Geometry

f(u,v) = ((2 +cosu)cosv,(2+cosu)sinv,sinu)

Explicit surface

Sampling is easy!但是很难判断一个点在表面内外

1716881924130

Many Implicit Representations in Graphics

Algebraic Surfaces (Implicit)

Algebraic Surfaces (Implicit)

Constructive Solid Geometry (Implicit)

Combine implicit geometry via Boolean operations

Combine implicit geometry via Boolean operations

Distance Functions (Implicit)

Instead of Booleans, gradually blend surfaces together using
Distance functions:
giving minimum distance (could be signed distance) from anywhere to object

An Example: Blending (linear interp.) a moving boundary

Distance Functions (Implicit)

SDF = signed distance function

Level Set Methods (Also implicit)

Closed-form equations are hard to describe complex shapes
Alternative: store a grid of values approximating function

Surface is found where interpolated values equal zero 用插值求到所需的表面
Provides much more explicit control over shape (like a texture)

Fractals (Implicit)

Exhibit self-similarity, detail at all scales
“language” for describing natural phenomena
Hard to control shape!
Fractals

Implicit Representations - Pros & Cons

Pros:
• compact description (e.g., a function)
• certain queries easy (inside object, distance to surface)
• good for ray-to-surface intersection (more later)
• for simple shapes, exact description / no sampling error
• easy to handle changes in topology (e.g., fluid)
Cons:
• difficult to model complex shapes

games101-note-L9 Shading 3 (Texture Mapping cont.)

Shading 3

Barycenric Coordinates 重心坐标

Interpolation Across Triangles 在三角形内部插值

原因:

指定顶点的值后能够获得三角形内部的平滑的变化值

Specify values at vertices

Obtain smoothly varying values across triangles

可以用于:

Texture coordinates, colors, normal vectors, …

Barycenric Coordinates

计算重心坐标

面积又可以用叉积进行计算 S=1/2 || aXb ||

cross product

问题:

做投影变换时,重心坐标会发生发生变化。

所以三维空间中的属性需要在三维空间进行插值后,再投影变换到二维空间中去。

Applying texture

Applying texture

认为纹理值是漫反射系数

会出现的问题:Texture Magnification 纹理放大

Generally don’t want this — insufficient texture resolution 纹理分辨率不足

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

1716744895709

Nearest:一定pixel范围内使用相同的texel

Bilinear:双线性插值

Bilinear Interpolation

双线性插值

Bicubic:取周围16个点来进行插值。

问题2:纹理缩小 Texture minification(hard case)

会产生走样

alias

downsampling 造成了问题

超采样可以解决问题,但是会消耗很多算力

一个像素的信号频率太高

更好的方法(范围查询):

避免采样,直接获取一定区域的平均值

Point Query vs. (Avg.) Range Query

Mipmap:allowing (fast, approx., square) range queries

Mipmap

也叫Mip hierarchy

Mip hierarchy

存储开销:

只多了1/3的存储量(每级缩小1/4)

计算Mipmap的Level D:

Computing Mipmap Level D

查询1.8层,在两层间进行一次插值。也就是三线性插值

Trilinear Interpolation

Mipmap的缺陷:

在远处会出现过度模糊。此处主要因为不满足square,不是方形区域的缩小

Overblur

Anisotropic Filtering 各向异性过滤:

Anisotropic Filtering 各向异性过滤

存储开销:3倍

各向异性多少x(例如2x,4x,16x)代表了这里的图片压缩多少次,如2x,压缩一次,取左上2x2区域内4个图,4x就是压缩3次,3x3区域内9个图。

Can look up axis-aligned rectangular zones

Diagonal footprints still a problem

可部分解决该问题,相当于使用矩形,而不只是正方形来获取平均值。但是对于倾斜的区域还是不太好用,如下图最左上角的区域。

Irregular Pixel Footprint in Texture

质量更好的方法:

EWA filtering

EWA filtering

EWA使用多个区域来取平均。但是存储开销更大。

games101-note-L8 Shading 2 (Shading, Pipeline and Texture Mapping)

Shading 2

Specular Term (Blinn-Phong)

V close to mirror direction ⇔ half vector near normal

Specular Term

Increasing p narrows the reflection lobe
指数p越大镜面反射区域越小

一般p取到100~200

Ambient Term

Shading that does not depend on anything
• Add constant color to account for disregarded illumination and fill in black shadows
• This is approximate / fake!

Ambient Term

Shading Frequencies

Shade each triangle (flat shading)

Flat shading 逐平面
• Triangle face is flat — one normal vector
• Not good for smooth surfaces

Shade each triangle (flat shading)

Shade each vertex (Gouraud shading)

Gouraud shading 逐顶点
• Interpolate colors from vertices across triangle
• Each vertex has a normal vector (how?)

Shade each vertex (Gouraud shading)

Shade each pixel (Phong shading)

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

对比

Defining Per-Vertex Normal Vectors

Best to get vertex normals from the underlying geometry
• e.g. consider a sphere

get vertex normals from the underlying geometry

Otherwise have to infer vertex normals from triangle faces

从这个顶点相邻的几个面的法线来求平均值

infer vertex normals from triangle facesinfer vertex normals from triangle faces

Defining Per-Pixel Normal Vectors

Barycentric interpolation (introducing soon) of vertex normals

Barycentric interpolation

Don’t forget to normalize the interpolated directions

Graphics (Real-time Rendering) Pipeline 图形管线

各个部分内容所对应的渲染管线的流程

Pipeline

mvp transforms

rasterization

Z-Buffer

shading

texture

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).

Surfaces are 2D

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

Each triangle vertex is assigned a texture coordinate (u,v)
每个三角形的顶点都被映射到纹理坐标上

Each triangle vertex is assigned a texture coordinate

纹理可以被多次使用(上下相连的tiled)

games101-note-L7 Shading1 (Illumination, Shading and Graphics Pipeline)

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 -> closer, larger z -> further)

Z-Buffer

Complexity 复杂度
• O(n) for n triangles (assuming constant coverage)
• How is it possible to sort n triangles in linear time?

Drawing triangles in different orders?

和三角形绘制顺序没有关系

Most important visibility algorithm
• Implemented in hardware for all GPUs

Q&A:
对于MSAA来说,每一个采样点都会利用Z-Buffer进行判断
对于透明的物体而言,需要特殊处理

Shading 着色

Shading:The process of applying a material to an object

Blinn-Phong Reflectance Model

Compute light reflected toward cameraat a specific shading point

Inputs:

• Viewer direction, v

• Surface normal, n

• Light direction, l (for each of many lights)

• Surface parameters (color, shininess, …)

此处没有生成阴影(shadow)shading ≠ shadow

Diffuse Reflection

• Light is scattered uniformly in all directions

–Surface color is the same for all viewing directions

Diffuse Reflection

• But how much light (energy) is received?

–Lambert’s cosine law

Lambert’s cosine law

Lambertian (Diffuse) Shading

Lambertian (Diffuse) Shading

为什么要使用max?

当向量点乘为负数的时,没有物理意义,对亮度没有贡献。

漫反射系数:

kd = 0 完全吸收

kd = 1 完全不吸收