CS 6958 LECTURE 7 TRIANGLES, BVH January 29, 2014 Triangles 2 - - PowerPoint PPT Presentation
CS 6958 LECTURE 7 TRIANGLES, BVH January 29, 2014 Triangles 2 - - PowerPoint PPT Presentation
CS 6958 LECTURE 7 TRIANGLES, BVH January 29, 2014 Triangles 2 Lets try to derive an intersection test Barycentric Coordinates 3 1 2 3 0 1 , 2 , 3 1 1 1 + 2 + 3 = 1 2
Triangles
Let’s try to derive an intersection test
2
Barycentric Coordinates
3
0 ≤ 𝑐1, 𝑐2, 𝑐3 ≤ 1 𝑐1 + 𝑐2 + 𝑐3 = 1 𝑄 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 𝑐3𝑄3
𝑄 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 1 − 𝑐1 − 𝑐2 𝑄3
𝑄2 𝑄3 𝑄
1
𝑄 𝑐3 𝑐2 𝑐1
Barycentric Coordinates
4
0 ≤ 𝑐1, 𝑐2, 𝑐3 ≤ 1 𝑐1 + 𝑐2 + 𝑐3 = 1 𝑄 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 𝑐3𝑄3
𝑄 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 1 − 𝑐1 − 𝑐2 𝑄3
𝑃 + 𝑢𝑊 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 1 − 𝑐1 − 𝑐2 𝑄3
𝑄2 𝑄3 𝑄
1
𝑄 𝑐3 𝑐2 𝑐1
𝑃 + 𝑢𝑊 = 𝑐1𝑄
1 + 𝑐2𝑄2 + 1 − 𝑐1 − 𝑐2 𝑄3
−𝑢𝑊 + 𝑐1 𝑄
1 − 𝑄3 + 𝑐2 𝑄2 − 𝑄3 = 𝑃 − 𝑄3
𝑓1 = 𝑄
1 − 𝑄3
𝑓2 = 𝑄2 − 𝑄3 𝑡 = 𝑃 − 𝑄3 −𝑊
𝑦
𝑓1𝑦 𝑓2𝑦 −𝑊
𝑧
𝑓1𝑧 𝑓2𝑧 −𝑊
𝑨
𝑓1𝑨 𝑓2𝑨 𝑢 𝑐1 𝑐2 = 𝑡𝑦 𝑡𝑧 𝑡𝑨
Barycentric Coordinates
5
𝑄2 𝑄3 𝑄
1
𝑄 𝑐3 𝑐2 𝑐1
𝑢 =
𝑡𝑦 𝑓1𝑦 𝑓2𝑦 𝑡𝑧 𝑓1𝑧 𝑓2𝑧 𝑡𝑨 𝑓1𝑨 𝑓2𝑨 −𝑊
𝑦
𝑓1𝑦 𝑓2𝑦 −𝑊
𝑧
𝑓1𝑧 𝑓2𝑧 −𝑊
𝑨
𝑓1𝑨 𝑓2𝑨
, 𝑐1 =
−𝑊
𝑦
𝑡𝑦 𝑓2𝑦 −𝑊
𝑧
𝑡𝑧 𝑓2𝑧 −𝑊
𝑨
𝑡𝑨 𝑓2𝑨 −𝑊
𝑦
𝑓1𝑦 𝑓2𝑦 −𝑊
𝑧
𝑓1𝑧 𝑓2𝑧 −𝑊
𝑨
𝑓1𝑨 𝑓2𝑨
,𝑐2 =
−𝑊
𝑦
𝑓1𝑦 𝑡𝑦 −𝑊
𝑧
𝑓1𝑧 𝑡𝑧 −𝑊
𝑨
𝑓1𝑨 𝑡𝑨 −𝑊
𝑦
𝑓1𝑦 𝑓2𝑦 −𝑊
𝑧
𝑓1𝑧 𝑓2𝑧 −𝑊
𝑨
𝑓1𝑨 𝑓2𝑨
In reality too slow for intersections, but we can
do better!
Solution – Cramer’s Rule
6
Scalar Triple Product
Scalar triple product
𝐵 ∙ 𝐶 × 𝐷 = 𝐶 ∙ 𝐷 × 𝐵 = 𝐷 ∙ 𝐵 × 𝐶
Also expressed as a determinant
𝐵𝑦 𝐵𝑧 𝐵𝑨 𝐶𝑦 𝐶𝑧 𝐶𝑨 𝐷𝑦 𝐷𝑧 𝐷𝑨
This comes in handy
𝐵𝑈 = 𝐵
7
Faster Solution
𝑒𝑓𝑜𝑝𝑛 = −𝑊
𝑦
−𝑊
𝑦
−𝑊
𝑦
𝑓1𝑦 𝑓1𝑧 𝑓1𝑨 𝑓2𝑦 𝑓2𝑧 𝑓2𝑨 = −𝑊 ∙ 𝑓1 × 𝑓2 = −𝑓1 ∙ 𝑓2 × 𝑊 = 𝑓1 ∙ 𝑊 × 𝑓2 𝑒𝑓𝑜𝑝𝑛 = 𝑓1 ∙ 𝑊 × 𝑓2 𝑢 = 𝑓2 ∙ 𝑡 × 𝑓1 𝑒𝑓𝑜𝑝𝑛 𝑐1 = 𝑡 ∙ 𝑊 × 𝑓2 𝑒𝑓𝑜𝑝𝑛 𝑐1 = 𝑊 ∙ 𝑡 × 𝑓1 𝑒𝑓𝑜𝑝𝑛
8
Ray-Triangle Intersection
𝑓1 = 𝑄
1 − 𝑄3
𝑓2 = 𝑄2 − 𝑄3 𝑠
1 = 𝑊 × 𝑓2
𝑒𝑓𝑜𝑝𝑛 = 𝑓1 ∙ 𝑠
1
if(𝑏𝑐𝑡 𝑒𝑓𝑜𝑝𝑛 < 𝜗) miss, return;
9
Ray-Triangle Intersection
𝑓1 = 𝑄
1 − 𝑄3
𝑓2 = 𝑄2 − 𝑄3 𝑠
1 = 𝑊 × 𝑓2
𝑒𝑓𝑜𝑝𝑛 = 𝑓1 ∙ 𝑠
1
if(𝑏𝑐𝑡 𝑒𝑓𝑜𝑝𝑛 < 𝜗) miss, return; 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 = 1 𝑒𝑓𝑜𝑝𝑛 s = 𝑃 − 𝑄3 𝑐1 = 𝑡 ∙ 𝑠
1 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛
if(𝑐1 < 0 ∥ 𝑐1 > 1) miss, return;
10
Ray-Triangle Intersection
𝑓1 = 𝑄
1 − 𝑄3
𝑓2 = 𝑄2 − 𝑄3 𝑠
1 = 𝑊 × 𝑓2
𝑒𝑓𝑜𝑝𝑛 = 𝑓1 ∙ 𝑠
1
if(𝑏𝑐𝑡 𝑒𝑓𝑜𝑝𝑛 < 𝜗) miss, return; 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 = 1 𝑒𝑓𝑜𝑝𝑛 s = 𝑃 − 𝑄3 𝑐1 = 𝑡 ∙ 𝑠
1 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛
if(𝑐1 < 0 ∥ 𝑐1 > 1) miss, return; 𝑠2 = 𝑡 × 𝑓1 𝑐2 = 𝑊 ∙ 𝑠2 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 if(𝑐2 < 0 ∥ 𝑐1 + 𝑐2 > 1) miss, return; t = 𝑓2 ∙ 𝑠2 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 hit! save 𝑐1 and 𝑐2 for interpolation
11
12
Operation add / sub / mult compare divide 𝑓1 = 𝑄
1 − 𝑄3
3 𝑓2 = 𝑄2 − 𝑄3 3 𝑠
1 = 𝑊 × 𝑓2
9 𝑒𝑓𝑜𝑝𝑛 = 𝑓1 ∙ 𝑠
1
5 if(𝑏𝑐𝑡 𝑒𝑓𝑜𝑝𝑛 < 𝜗) miss, return; 2 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 = 1 𝑒𝑓𝑜𝑝𝑛 1 s = 𝑃 − 𝑄3 3 𝑐1 = 𝑡 ∙ 𝑠
1 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛
6 if(𝑐1 < 0 ∥ 𝑐1 > 1) miss, return; 2 𝑠
2 = 𝑡 × 𝑓1
9 𝑐2 = 𝑊 ∙ 𝑠2 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 6 if(𝑐2 < 0 ∥ 𝑐1 + 𝑐2 > 1) miss, return; 1 2 t = 𝑓2 ∙ 𝑠2 𝑗𝑜𝑤𝐸𝑓𝑜𝑝𝑛 6 hit! save 𝑐1 and 𝑐2 for interpolation 2 total 20 / 29 / 45 / 51 2 / 4 / 6 / 8 0 / 1 / 1 / 1
Normals
Flat shaded triangles
𝑂 = 𝑓1 × 𝑓2
Smooth shaded (per-vertex normals)
𝑂 = 𝑐1𝑂1 + 𝑐2𝑂2 + 1 − 𝑐1 − 𝑐2 𝑂3
13
Updates to Hitrecord
include barycentric coordinates include computed normal
alternatively, save per-vertex normals and
interpolate before shading
14
Which Operation Most Costly?
foreach frame foreach pixel foreach sample generate ray intersect ray with objects shade intersection point
15
End
16