object.h 2.4 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
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
86
87
88
89
90
91
92
93
94
95
96

#pragma once

#include <variant>
#include "../lib/mathlib.h"
#include "../scene/object.h"

#include "trace.h"
#include "bvh.h"
#include "list.h"
#include "shapes.h"
#include "tri_mesh.h"

namespace PT {

class Object {
public:
	Object(Shape&& shape, Scene_ID id, unsigned int m = 0, const Mat4& T = Mat4::I) 
		: trans(T), itrans(T.inverse()), _id(id), material(m),
		  underlying(std::move(shape)) {
		has_trans = trans != Mat4::I;
	}
	Object(Tri_Mesh&& tri_mesh, Scene_ID id, unsigned int m = 0, const Mat4& T = Mat4::I) 
		: trans(T), itrans(T.inverse()), _id(id), material(m),
		  underlying(std::move(tri_mesh)) {
		has_trans = trans != Mat4::I;
	}
	Object(List<Object>&& list, Scene_ID id, unsigned int m = 0, const Mat4& T = Mat4::I) 
		: trans(T), itrans(T.inverse()), _id(id), material(m),
		  underlying(std::move(list)) {
		has_trans = trans != Mat4::I;
	}
	Object(BVH<Object>&& bvh, Scene_ID id, unsigned int m = 0, const Mat4& T = Mat4::I) 
		: trans(T), itrans(T.inverse()), _id(id), material(m),
		  underlying(std::move(bvh)) {
		has_trans = trans != Mat4::I;
	}

	Object(const Object& src) = delete;
	Object& operator=(const Object& src) = delete;
	Object& operator=(Object&& src) = default;
	Object(Object&& src) = default;

	BBox bbox() const {
		BBox box = std::visit(overloaded {
			[](const auto& o) {
				return o.bbox();
			}
		}, underlying);
		if(has_trans) box.transform(trans);
		return box;
	}

	Trace hit(Ray ray) const {
		if(has_trans) ray.transform(itrans);
		Trace ret = std::visit(overloaded {
			[&ray](const auto& o) {
				return o.hit(ray);
			}
		}, underlying);
		if(ret.hit) {
			ret.material = material;
			if(has_trans) ret.transform(trans, itrans.T());
		}
		return ret;
	}

	size_t visualize(GL::Lines& lines, GL::Lines& active, size_t level, const Mat4& vtrans) const {
		Mat4 next = has_trans ? vtrans * trans : vtrans;
		return std::visit(overloaded {
			[&](const BVH<Object>& bvh) {
				return bvh.visualize(lines, active, level, next);
			},
			[&](const Tri_Mesh& mesh) {
				return mesh.visualize(lines, active, level, next);
			},
			[](const auto&) { return size_t(0); }
		}, underlying);
	}

	Scene_ID id() const { return _id; }
	void set_trans(const Mat4& T) {
		trans = T;
		itrans = T.inverse();
		has_trans = trans != Mat4::I;
	}

private:
	bool has_trans;
	Mat4 trans, itrans;
	unsigned int material;
	Scene_ID _id;
	std::variant<Tri_Mesh, Shape, BVH<Object>, List<Object>> underlying;
};

}