object.h 2.68 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4
5

#pragma once

#include "../lib/mathlib.h"
#include "../scene/object.h"
TheNumbat's avatar
TheNumbat committed
6
#include <variant>
TheNumbat's avatar
TheNumbat committed
7
8
9
10

#include "bvh.h"
#include "list.h"
#include "shapes.h"
TheNumbat's avatar
TheNumbat committed
11
#include "trace.h"
TheNumbat's avatar
TheNumbat committed
12
13
14
15
16
17
#include "tri_mesh.h"

namespace PT {

class Object {
public:
TheNumbat's avatar
TheNumbat committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    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;
    }
TheNumbat's avatar
TheNumbat committed
34

TheNumbat's avatar
TheNumbat committed
35
36
37
38
    Object(const Object &src) = delete;
    Object &operator=(const Object &src) = delete;
    Object &operator=(Object &&src) = default;
    Object(Object &&src) = default;
TheNumbat's avatar
TheNumbat committed
39

TheNumbat's avatar
TheNumbat committed
40
41
42
43
44
45
    BBox bbox() const {
        BBox box = std::visit(overloaded{[](const auto &o) { return o.bbox(); }}, underlying);
        if (has_trans)
            box.transform(trans);
        return box;
    }
TheNumbat's avatar
TheNumbat committed
46

TheNumbat's avatar
TheNumbat committed
47
48
49
50
51
52
53
54
55
56
57
58
    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;
    }
TheNumbat's avatar
TheNumbat committed
59

TheNumbat's avatar
TheNumbat committed
60
61
62
63
64
65
66
67
68
    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);
    }
TheNumbat's avatar
TheNumbat committed
69

TheNumbat's avatar
TheNumbat committed
70
71
72
73
74
75
    Scene_ID id() const { return _id; }
    void set_trans(const Mat4 &T) {
        trans = T;
        itrans = T.inverse();
        has_trans = trans != Mat4::I;
    }
TheNumbat's avatar
TheNumbat committed
76
77

private:
TheNumbat's avatar
TheNumbat committed
78
79
80
81
82
    bool has_trans;
    Mat4 trans, itrans;
    unsigned int material;
    Scene_ID _id;
    std::variant<Tri_Mesh, Shape, BVH<Object>, List<Object>> underlying;
TheNumbat's avatar
TheNumbat committed
83
84
};

TheNumbat's avatar
TheNumbat committed
85
} // namespace PT