-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.h
69 lines (51 loc) · 1.61 KB
/
Shape.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
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
//
//
//
#ifndef RAY_TRACER_SHAPE_H
#define RAY_TRACER_SHAPE_H
#include "Material.h"
#include "Transform.h"
#include <sstream>
enum class ShapeType {
Triangle,
Sphere
};
class Shape {
public:
Material material; // Material of the shape
ShapeType type;
Matrix4x4 transform; // Transform of the shape
Shape(const Material& material, ShapeType type) : material(material), type(type) {}
virtual bool intersect(const Ray& ray, float& t) const = 0; // Pure virtual method
virtual Vector3 normalAt(const Vector3& point) const = 0; // Pure virtual method to calculate the normal
virtual std::string toString() const {
std::ostringstream oss;
oss << "- Material properties: " << material << ",\n";
oss << "- Transform Matrix:\n" << transform; // Print the transform
return oss.str();
}
void setTransform(const Matrix4x4& t) {
// Only allow setTransform on Sphere objects
if (type == ShapeType::Sphere) {
transform = t;
}
}
const Matrix4x4& getTransform() const {
return transform;
}
Matrix4x4 getInverseTransform() const {
return transform.inverse();
}
virtual ~Shape() = default; // Virtual destructor
friend bool operator==(const Shape& lhs, const Shape& rhs);
};
bool operator==(const Shape& lhs, const Shape& rhs) {
return lhs.material == rhs.material &&
lhs.type == rhs.type &&
lhs.transform == rhs.transform;
}
std::ostream& operator<<(std::ostream& os, const Shape& shape) {
os << shape.toString();
return os;
}
#endif //RAY_TRACER_SHAPE_H