-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstance.cpp
39 lines (33 loc) · 908 Bytes
/
Instance.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
#include "Instance.h"
#include "ONB.h"
Instance::Instance(Matrix trans, Matrix trans_inverse, Shape *prim)
: M(trans), N(trans_inverse), prim(prim)
{ }
Instance::Instance(Matrix trans, Shape *prim)
: M(trans), N(trans), prim(prim)
{
N.Invert();
}
bool Instance::Hit(const Ray &r, float tmin, float tmax, float time, HitRecord &rec) const
{
Vector3 no = TransformLoc(N, r.o);
Vector3 nd = TransformVec(N, r.d);
Ray tray(no, nd);
if (prim->Hit(tray, tmin, tmax, time, rec))
{
rec.pos = TransformLoc(M, rec.pos);
Vector3 normal = TransformVec(N.GetTranspose(), rec.uvw.w);
ONB uvw;
uvw.InitFromW(normal);
rec.uvw = uvw;
return true;
}
else return false;
}
bool Instance::ShadowHit(const Ray &r, float tmin, float tmax, float time) const
{
Vector3 no = TransformLoc(N, r.o);
Vector3 nd = TransformVec(N, r.d);
Ray tray(no, nd);
return (prim->ShadowHit(tray, tmin, tmax, time));
}