Skip to content

Commit

Permalink
ADD: ray tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
CatOnly committed Jul 27, 2021
1 parent 7f5d786 commit 53f342c
Show file tree
Hide file tree
Showing 20 changed files with 312 additions and 159 deletions.
2 changes: 1 addition & 1 deletion DigitalImageProcessing/Part1_Filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ for (int i=0; i < HISTOGRAM_SIZE; ++i )
- 空间滤波器(模版):通过特定的方法得到的对应像素的权重集合
- 线性空间滤波器:执行特定的方法是线性操作的空间滤波器(对于边缘像素的计算结果,根据纹理的环绕方式的不同而不同)
![](images/spaceFilter.png)
空间相关/卷积:滤波器移过图像,计算每个位置乘积之和得出对应像素值的处理方法
空间相关/卷积:滤波器移过图像,计算每个位置乘积之和得出对应像素值的处理方法
> 离散单位冲激:包含单个 1 其余都是 0 的函数/矩阵
Expand Down
6 changes: 4 additions & 2 deletions LinearAlgebra/Part2_Quaternion.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,11 @@ $$

三次贝塞尔曲线:

![](images/Bezier_3.gif)
![](images/bezier.gif)

![](images/Bezier3.png)
![](images/bezier2.png)

![](images/bezier3.png)

插值方式可以用 lerp、Slerp 等方式,上图采用 de Casteljau 算法构造贝塞尔曲线
上图采用 lerp 方式插值,插值方程为:
Expand Down
7 changes: 6 additions & 1 deletion LinearAlgebra/Part3_Triangles.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ $$

根据划分条件的不同,可以采用不同的划分结构

![](./images/spatial_partition.png)

## 1. 划分条件

### 1.1 传统划分
Expand All @@ -264,11 +266,14 @@ $$

### 2.1 BVH 划分

划分物体
![](./images/spatial_partition3.png)

### 2.2 K-D 树划分

### 2.3 Uniform Grids 划分


![](./images/spatial_partition2.png)



Expand Down
File renamed without changes
Binary file added LinearAlgebra/images/bezier2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LinearAlgebra/images/probability.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LinearAlgebra/images/spatial_partition.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LinearAlgebra/images/spatial_partition2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LinearAlgebra/images/spatial_partition3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 129 additions & 0 deletions OpenGL/EXT3_Tracing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
[TOC]



# 一、概率论 Probability

**随机变量** $X$​:可能取很多不同值的变量

**随机变量分布函数** $X \sim p(x)$​​​​​​​:
连续的分布函数又称**概率密度函数** Probability Density Function(PDF),指不同概率事件下随机变量和概率的映射关系

某一个随机变量 $x$ 对应的概率 $P$​
$$
\begin{align}
离散:P &= p(x), & dx = 1\\
连续:P &= p(x)dx \\
\\
所有概率和:\sum p(x) &= 1
\end{align}
$$
**均值**:统计所有数据得到的结果

**期望** $E$​​:
抽取部分数据得到的**平均概率值**,无限接近于均值
$$
\begin{align}
\lim_{x \to \infty} E[X]&= \bar X \\
离散: E[X] &= \sum _{i=1}^{n} x_ip(x_i),p(x) \geq 0\\
连续: E[X] &= \int_1^n xp(x)dx \\
\\
对于随机变量X,Y \\
Y &= f(X) \\
E[Y] &= E[f(x)] \\
&= \int f(x)p(x)dx
\end{align}
$$
**方差 Variance**
用来度量随机变量和其期望(即均值)之间的**分散程度**,波动越大,方差越大
$$
\begin{align}
Var(x)
&= s^2 \\
&= \sum _{i=1}^n(x_i - \bar x)^2f(x) \\
&=E((x - \bar x)^2) \\
&=E(x^2 - 2x\bar x + \bar x^2) \\
&=E(x^2) - 2E(x \bar x) + E(\bar x^2) \\
&=E(x)^2 - 2 \sum x \bar x p(x) + \sum \bar x^2 p(x) \\
&=E(x)^2 - 2 \bar x \sum xp(x) + \bar x^2 \sum p(x) \\
&=E(x)^2 - 2 \bar xE(x) + \bar x^2 \\
&=E(x)^2 - 2E(x)E(x) + (E(x))^2 \\
&=E(x)^2 - (E(x))^2
\end{align}
$$
**协方差**
衡量两个变量之间的变化方向关系
$$
cov(X,Y) = E(XY) - E(X)E(Y)
$$


# 二、光线追踪 Ray Tracing

优点:真实,多用于离线渲染
缺点:计算量大

前提:

- **假设**光线近似直线传播

- **假设**光线交叉后仍然互不影响

- 光路可逆:从光源到人眼的路径 == 从人眼到光源



## 1. Whitted-Style Ray Tracing

方法

1. 从相机出发,向场景投射光线
2. 将场景进行合理分割,方便快速找到光线与物体的相交点
3. 判断光线与距离相机最近的地方相交(反射),在相交处计算物体颜色
4. 光线会折射多次,在每一次折射点计算颜色值
![](./images/ray_tracing.png)



## 2. 渲染方程推导

![](./images/ray_tracing_rendering_equation.png)

折射点渲染方程推导:

1. 考虑**自发光物体 Emission** 的光照

2. 考虑多个光源的光照

3. 考虑到面光源,将**累加 sum** 替换为**积分 integral** 更准确

4. 考虑到其他物体反射的光线(**间接光照 inter reflection**

5. 渲染方程化简
$$
\begin{align}
设:\\
E &= L_e(x, \omega_r)\\
L &= L_r(x, \omega_r) =L_i(x, \omega_i)\\
K &= \int_{\Omega}f(x,\omega_i, \omega_r) \cos \theta_i d\omega_i \\
则 \space 渲染方程简化为:\\
L &= E + KL \\
L - KL &= E \\
(I - K)L &= E \\
L &= (I - K)^{-1} E \\
L &= (I + K + K^2 + K^3 + ...)E \\
L &= E + KE + K^2E+ K^3E + ... \\
其中:\\
直接光照 &= KE \\
间接光照 &= K^2E \\
二次间接光照 &= K^3E \\
...
\end{align}
$$





# 三、路径追踪 Path Tracing

6 changes: 3 additions & 3 deletions OpenGL/Part0_BaseInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ std::uint8_t checkViewCut(const glm::vec4& v)
{
auto ret = (std::uint8_t)0;

if (v.x < -v.w) ret |= 1;
if (v.x < -v.w) ret |= 1;
else if (v.x > v.w) ret |= 2;
if (v.y < -v.w) ret |= 4;
if (v.y < -v.w) ret |= 4;
else if (v.y > v.w) ret |= 8;
if (v.z < -v.w) ret |= 16;
if (v.z < -v.w) ret |= 16;
else if (v.z > v.w) ret |= 32;

return ret;
Expand Down
Loading

0 comments on commit 53f342c

Please sign in to comment.