Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ichinii committed Oct 11, 2024
1 parent 38b14a6 commit 5ccfd2b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
10 changes: 5 additions & 5 deletions Intern/rayx-core/src/Shader/Efficiency.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ struct RotationBase {
// A convention for the up and right vectors is implemented, making this function well defined and for all forward directions
// TODO(Sven): this convention should be exchanged with one that does not branch
RAYX_FN_ACC
inline RotationBase forwardVectorToBaseConvention(glm::dvec3 forward) {
inline RotationBase forwardVectorToBaseConvention(const glm::dvec3 forward) {
auto up = glm::dvec3(0, 1, 0);
glm::dvec3 right;

Expand Down Expand Up @@ -246,22 +246,22 @@ inline glm::dmat3 rotationMatrix(const glm::dvec3 forward, const glm::dvec3 up)
}

RAYX_FN_ACC
inline ElectricField localToGlobalElectricField(LocalElectricField localField, glm::dvec3 forward) {
inline ElectricField localToGlobalElectricField(const LocalElectricField localField, const glm::dvec3 forward) {
return rotationMatrix(forward) * ElectricField(localField, complex::Complex{0, 0});
}

RAYX_FN_ACC
inline ElectricField localToGlobalElectricField(LocalElectricField localField, glm::dvec3 forward, glm::dvec3 up) {
inline ElectricField localToGlobalElectricField(const LocalElectricField localField, const glm::dvec3 forward, const glm::dvec3 up) {
return rotationMatrix(forward, up) * ElectricField(localField, complex::Complex{0, 0});
}

RAYX_FN_ACC
inline LocalElectricField globalToLocalElectricField(ElectricField field, glm::dvec3 forward) {
inline LocalElectricField globalToLocalElectricField(const ElectricField field, const glm::dvec3 forward) {
return glm::transpose(rotationMatrix(forward)) * field;
}

RAYX_FN_ACC
inline LocalElectricField globalToLocalElectricField(ElectricField field, glm::dvec3 forward, glm::vec3 up) {
inline LocalElectricField globalToLocalElectricField(const ElectricField field, const glm::dvec3 forward, const glm::vec3 up) {
return glm::transpose(rotationMatrix(forward, up)) * field;
}

Expand Down
93 changes: 75 additions & 18 deletions docs/src/Model/Efficiency.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
# Efficiency

# Efficiency

wiki for efficiency calculations

### Snell's law
A fraction of the light is reflected and another transmitted:

![refraction_fresnel](https://upload.wikimedia.org/wikipedia/commons/8/89/Fresnel1.svg)<br>
\\(\theta_i =\\) (normal) incidence angle <br>
\\(\theta_r =\\) (normal) reflection angle (same as \\(\theta_i\\))<br>
\\(\theta_t =\\) (normal) transmittance angle <br>
\\(N_1 =\\) refraction index of material from which the ray is coming (left in image)<br>
\\(N_2 =\\) refraction index of material into which the ray is going (right in image)<br>
$\theta_i =$ (normal) incidence angle
$\theta_r =$ (normal) reflection angle (same as $\theta_i$)
$\theta_t =$ (normal) transmittance angle
$N_1 =$ refraction index of material from which the ray is coming (left in image)
$N_2 =$ refraction index of material into which the ray is going (right in image)

all parameters are potentially complex numbers. The refractive indices are retrieved from files (Palik, Henke, Cromer..)
All parameters are potentially complex numbers.
The refractive indices $N_1$ and $N_2$ are retrieved from databases (Palik, Henke, Cromer..).

Snell's law:
\\[
$$
N_1 \sin \theta_i = N_2 \sin \theta_t \rightarrow \sin \theta_t = \frac{N_1}{N_2} \sin \theta_i
\\]
$$

\\(\theta_i\\), \\(N_1\\), \\(N_2\\) are known, we are looking for \\(\theta_t\\). <br>
We do not calculate the angle specifically but only the cosinus, which is sufficient for further calculations and more efficient/precise than calculating the angle itself because we do not need to use more trigonometric functions.
We can calculate the incidence angle \\(\theta_i\\) of each ray from its direction and the surface normal. Then we calculate \\(\cos(\theta_i)\\) and from that we can derive \\(\cos(\theta_t)\\) with snell's law:
With $\theta_i$, $\theta_r$, $N_1$, $N_2$ being known, we are looking for $\theta_t$.
We can calculate the incidence angle $\theta_i$ of each ray from its direction and the surface normal. Then we calculate $\cos(\theta_i)$ and from that we can derive $\cos(\theta_t)$ with snell's law:

\\[
$$
(\sin \theta_i)^2 = 1 - (\cos \theta_i)^2 \\\\
(\sin \theta_t)^2 = (\frac{N_1}{N_2})^2 (\sin \theta_i)^2 \\\\
\cos \theta_t = \sqrt{1 - (\sin \theta_t)^2} = \sqrt{1 - \Big(\frac{N_1}{N_2} \sin \theta_i\Big)^2}
\\]
$$

The cosine of both angles is then used in the Fresnel equations to calculate the s- and p-polarization

Expand All @@ -38,9 +39,65 @@ p-polarization (parallel, left image) lies parallel in the plane of incidence an
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4d/Polarisation_p.png" alt="ppol" width="200"/>
<img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Polarisation_s.png" alt="spol" width="200"/>

the reflectance of both polarizations is calculated with the fresnel equations:
the amplitude coefficient of a reflection for both polarizations is calculated with the fresnel equations:

$$r_s = \frac{N_1 \cdot \cos \theta_i - N_2 \cdot \cos \theta_t}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}$$
$$r_p = \frac{N_2 \cdot \cos \theta_i - N_1 \cdot \cos \theta_t}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}$$

the amplitude coefficient of a refraction for both polarizations is calculated with the fresnel equations:

$$t_s = \frac{2.0 \cdot N_1 \cdot \cos \theta_i}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}$$
$$t_p = \frac{2.0 \cdot N_1 \cdot \cos \theta_i}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}$$

### Mirror reflection

Reflecting a ray on a mirror involves polarization and phase changes of the incident electric field of the ray. The update of the electric field can be described by 3 steps:

1. Rotate incident electric field from global coordinates into local basis of incoming propagation vector $k$.
1. Multiply fresnel amplitude coefficients with components of the electric field.
1. Rotate resulting electric field back into global coordinates by rotating into basis of outgoing propagation vector $k'$.

In order to rotate the basis...

$$
\begin{aligned}
\vec{s}_{q} &= \frac{\vec{k}_{q-1} \times \vec{\eta}_{q}}{|\vec{k}_{q-1} \times \vec{\eta}_{q}|} \\
\quad \vec{p}_{q} &= \vec{k}_{q-1} \times \vec{s}_{q} \\
\vec{s}'_{q} &= \vec{s}_{q}, \quad \vec{p}'_{q} = \vec{k}_{q} \times \vec{s}_{q}
\end{aligned}
$$

Both incident vector $\vec{k}_{q-1}$ and reflected vector $\vec{k}_{q}$ lie within the plane of incidence and normal vector $\vec{\eta}_{q}$ is perpendicular and defining the front facing direction of the plane.
We define vectors $\vec{s}$ and $\vec{p}$ perpendicular to propagation vector \(k\), while \(s\) is perpendicular to the plane of incidence and \(k\) lies within the plane.

$$
\begin{aligned}
O_{q,in} &= \begin{pmatrix}
s'_{x,q} & p'_{x,q} & k_{x,q} \\
s'_{y,q} & p'_{y,q} & k_{y,q} \\
s'_{z,q} & p'_{z,q} & k_{z,q}
\end{pmatrix} \\
J_{q} &= \begin{pmatrix}
r_{s,q} & 0 & 0 \\
0 & r_{p,q} & 0 \\
0 & 0 & 1
\end{pmatrix} \\
O_{q,out} &= \begin{pmatrix}
s_{x,q} & s_{y,q} & s_{z,q} \\
p_{x,q} & p_{y,q} & p_{z,q} \\
k_{x,q-1} & k_{y,q-1} & k_{z,q-1}
\end{pmatrix} \\
P_{q} &= O_{q,in} \cdot J_{q} \cdot O_{q,out}
\end{aligned}
$$

All operations are composed into the Polarization Ray Tracing Matrix $P_{q}$, encorparating fresnel amplitude coefficients into $J_{q}$.

$$
E_{q} = P_q E_{q-1}
$$

\\[r_s = \frac{N_1 \cdot \cos \theta_i - N_2 \cdot \cos \theta_t}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}\\]
\\[r_p = \frac{N_2 \cdot \cos \theta_i - N_1 \cdot \cos \theta_t}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}\\]
$P_{q}$ is multiplied by the incident electric field $E_{q-1}$, resulting in the ray's a new electric field after the intercept with the mirror.

(The transmitted power is then "the rest": \\(t_s = 1 - r_s\\) and \\(t_p = 1 -r_p\\))
This code implements several formulas from the following book:
Russel A. Chipman, Wai-Sze Tiffany Lam, Garam Young "Polarized Light and Optical Systems" (2019).

0 comments on commit 5ccfd2b

Please sign in to comment.