-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ray.h
37 lines (27 loc) · 995 Bytes
/
Ray.h
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
//
//
//
#ifndef RAY_TRACER_RAY_H
#define RAY_TRACER_RAY_H
#include "Matrix4x4.h"
class Ray {
public:
Vector3 origin;
Vector3 direction; // Normalized direction of the ray
Ray(const Vector3& origin, const Vector3& direction) : origin(origin), direction(direction.normalize()) {}
Ray transformedBy(const Matrix4x4& matrix) const {
// Transform the origin as a point
Vector3 transformedOrigin = matrix * origin;
// Transform the direction as a vector (ignoring translations)
Vector3 transformedDirection = matrix * (origin + direction) - transformedOrigin;
return Ray(transformedOrigin, transformedDirection);
}
};
bool operator==(const Ray& lhs, const Ray& rhs) {
return lhs.origin == rhs.origin && lhs.direction == rhs.direction;
}
std::ostream& operator<<(std::ostream& os, const Ray& ray) {
os << "Ray(Origin: " << ray.origin << ", Direction: " << ray.direction << ")";
return os;
}
#endif //RAY_TRACER_RAY_H