-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathray.h
More file actions
85 lines (64 loc) · 1.81 KB
/
ray.h
File metadata and controls
85 lines (64 loc) · 1.81 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once
#include "vector.h"
#include "material.h"
constexpr float shadow_epsilon = 1.e-4f;
struct Ray
{
Ray(const Vector& o = Vector(), const Vector& dir = Vector()) : origin(o), direction(dir) {};
Vector GetPoint(float t) const { return origin + t * direction; }
void OffsetOrigin(const Vector& n)
{ origin += shadow_epsilon * n; }
Vector origin;
Vector direction;
};
// Information about intersections
struct HitInfo
{
HitInfo() : hitP(), incident(), normal(), rayT(INF), ior(), material(nullptr) {};
HitInfo(Vector hitP, Vector incident, Vector normal, float rayT, float ior, Material* material) :
hitP(hitP), incident(incident),
normal(normal),
rayT(rayT), ior(ior), material(material)
{}
HitInfo& operator=(const HitInfo&) = default;
Ray GetReflectedRay() const
{
if (!cachedReflected)
{
cachedReflected = true;
Vector i_n = normal * (incident * normal);
reflected.origin = hitP;
reflected.direction = incident - 2.f * i_n;
reflected.OffsetOrigin(normal);
}
return reflected;
}
Ray GetRefractedRay() const
{
if (!cachedRefracted)
{
cachedRefracted = true;
float ior2 = (ior == material->GetRefrIndex()) ? 1.f : material->GetRefrIndex();
Vector i_n = normal * (incident * normal);
Vector i_t = incident - i_n;
Vector t_t = (ior / ior2) * i_t;
float norm_magn_sq = 1 - t_t.LengthSquared();
refracted.origin = hitP;
refracted.OffsetOrigin(-normal);
if (norm_magn_sq <= 0.f) refracted.direction = Vector(0.f, 0.f, 0.f);
else refracted.direction = t_t - sqrtf(norm_magn_sq) * normal;
}
return refracted;
}
Vector hitP;
Vector incident;
mutable Ray reflected;
mutable Ray refracted;
Vector normal;
float rayT;
float ior;
Material* material;
private:
mutable bool cachedReflected = false;
mutable bool cachedRefracted = false;
};