-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHelpers.cs
More file actions
71 lines (62 loc) · 2.18 KB
/
Copy pathHelpers.cs
File metadata and controls
71 lines (62 loc) · 2.18 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
namespace _12_Dielectrics
{
public class Helpers
{
// Constants
public static float Infinity = float.MaxValue;
public static Random random = new Random();
// Utility Functions
public static float Degress_to_radians(float degrees)
{
return degrees * (float)Math.PI / 180.0f;
}
public static float RandomFloat(float min, float max)
{
return (float)(random.NextDouble() * (max - min) + min);
}
public static Vector3 RandomVector3(float min, float max)
{
return new Vector3(RandomFloat(min, max), RandomFloat(min, max), RandomFloat(min, max));
}
public static Vector3 Random_in_unit_sphere()
{
while (true)
{
Vector3 p = RandomVector3(-1, 1);
if (p.LengthSquared() >= 1)
continue;
return p;
}
}
public static Vector3 Random_unit_Vector()
{
float a = RandomFloat(0, 2.0f * (float)Math.PI);
float z = RandomFloat(-1, 1);
float r = (float)Math.Sqrt(1 - z * z);
return new Vector3(r * (float)Math.Cos(a), r * (float)Math.Sin(a), z);
}
public static Vector3 Random_in_hemisphere(Vector3 normal)
{
Vector3 in_unit_sphere = Random_unit_Vector();
if (Vector3.Dot(in_unit_sphere, normal) > 0.0) // In the same hemisphere as the normal
return in_unit_sphere;
else
return -in_unit_sphere;
}
public static Vector3 Reflect(Vector3 v, Vector3 n)
{
return v - 2 * Vector3.Dot(v, n) * n;
}
public static Vector3 Refract(Vector3 uv, Vector3 n, float etai_over_etat)
{
float cos_theta = Vector3.Dot(-uv, n);
Vector3 r_out_parallel = etai_over_etat * (uv + cos_theta * n);
Vector3 r_out_perp = -(float)Math.Sqrt(1.0f - r_out_parallel.LengthSquared()) * n;
return r_out_parallel + r_out_perp;
}
}
}