-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonMath.cpp
42 lines (34 loc) · 1.03 KB
/
CommonMath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Created by Comp on 10.03.2021.
//
#include "CommonMath.h"
#include <stdlib.h>
#include <cmath>
float randomFloat() {
return rand() / (RAND_MAX + 1.0f);
}
float randomFloat(float min, float max) {
return min + (max - min) * randomFloat();
}
Vector3D randomInUnitSphere() {
while (true) {
Vector3D p = random(-1,1);
if (p.lengthSquared() >= 1) continue;
return p;
}
}
Vector3D random(float min, float max) {
return Vector3D(randomFloat(min, max), randomFloat(min, max), randomFloat(min, max));
}
Vector3D randomUnitVector() {
return randomInUnitSphere().normalized();
}
Vector3D reflect(const Vector3D& v, const Vector3D& n) {
return v - 2 * Vector3D::dotProduct(v, n) * n;
}
Vector3D refract(const Vector3D& uv, const Vector3D& n, float etaiOverEtat) {
float cosTheta = fminf(Vector3D::dotProduct(-uv, n), 1.0f);
Vector3D rOutPerp = etaiOverEtat * (uv + cosTheta * n);
Vector3D rOutParallel = -sqrt(fabs(1.0f - rOutPerp.lengthSquared())) * n;
return rOutPerp + rOutParallel;
}