tri_mesh.cpp 2.07 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2

#include "../rays/tri_mesh.h"
TheNumbat's avatar
TheNumbat committed
3
#include "debug.h"
TheNumbat's avatar
TheNumbat committed
4
5
6
7
8
9
10

namespace PT {

BBox Triangle::bbox() const {

    // TODO (PathTracer): Task 2
    // compute the bounding box of the triangle
TheNumbat's avatar
TheNumbat committed
11

TheNumbat's avatar
TheNumbat committed
12
13
14
    // Beware of flat/zero-volume boxes! You may need to 
    // account for that here, or later on in BBox::intersect

TheNumbat's avatar
TheNumbat committed
15
16
17
18
    BBox box;
    return box;
}

TheNumbat's avatar
TheNumbat committed
19
Trace Triangle::hit(const Ray &ray) const {
TheNumbat's avatar
TheNumbat committed
20
21
22
23
24

    // Vertices of triangle - has postion and surface normal
    Tri_Mesh_Vert v_0 = vertex_list[v0];
    Tri_Mesh_Vert v_1 = vertex_list[v1];
    Tri_Mesh_Vert v_2 = vertex_list[v2];
TheNumbat's avatar
TheNumbat committed
25
26
27
    (void)v_0;
    (void)v_1;
    (void)v_2;
TheNumbat's avatar
TheNumbat committed
28

TheNumbat's avatar
TheNumbat committed
29
    // TODO (PathTracer): Task 2
TheNumbat's avatar
TheNumbat committed
30
    // Intersect this ray with a triangle defined by the three above points.
TheNumbat's avatar
TheNumbat committed
31
32
33
34
35
36
37
38

    Trace ret;
    ret.hit = false;       // was there an intersection?
    ret.time = 0.0f;       // at what time did the intersection occur?
    ret.position = Vec3{}; // where was the intersection?
    ret.normal = Vec3{};   // what was the surface normal at the intersection?
                           // (this should be interpolated between the three vertex normals)
    return ret;
TheNumbat's avatar
TheNumbat committed
39
40
}

TheNumbat's avatar
TheNumbat committed
41
Triangle::Triangle(Tri_Mesh_Vert *verts, unsigned int v0, unsigned int v1, unsigned int v2)
TheNumbat's avatar
TheNumbat committed
42
43
    : vertex_list(verts), v0(v0), v1(v1), v2(v2) {}

TheNumbat's avatar
TheNumbat committed
44
void Tri_Mesh::build(const GL::Mesh &mesh) {
TheNumbat's avatar
TheNumbat committed
45
46
47

    verts.clear();
    triangles.clear();
TheNumbat's avatar
TheNumbat committed
48
49

    for (const auto &v : mesh.verts()) {
TheNumbat's avatar
TheNumbat committed
50
51
        verts.push_back({v.pos, v.norm});
    }
TheNumbat's avatar
TheNumbat committed
52
53

    const auto &idxs = mesh.indices();
TheNumbat's avatar
TheNumbat committed
54
55

    std::vector<Triangle> tris;
TheNumbat's avatar
TheNumbat committed
56
57
    for (size_t i = 0; i < idxs.size(); i += 3) {
        tris.push_back(Triangle(verts.data(), idxs[i], idxs[i + 1], idxs[i + 2]));
TheNumbat's avatar
TheNumbat committed
58
59
60
61
62
    }

    triangles.build(std::move(tris), 4);
}

TheNumbat's avatar
TheNumbat committed
63
Tri_Mesh::Tri_Mesh(const GL::Mesh &mesh) { build(mesh); }
TheNumbat's avatar
TheNumbat committed
64

TheNumbat's avatar
TheNumbat committed
65
BBox Tri_Mesh::bbox() const { return triangles.bbox(); }
TheNumbat's avatar
TheNumbat committed
66

TheNumbat's avatar
TheNumbat committed
67
68
69
70
Trace Tri_Mesh::hit(const Ray &ray) const { 
    Trace t = triangles.hit(ray); 
    return t;
}
TheNumbat's avatar
TheNumbat committed
71

TheNumbat's avatar
TheNumbat committed
72
73
size_t Tri_Mesh::visualize(GL::Lines &lines, GL::Lines &active, size_t level,
                           const Mat4 &trans) const {
TheNumbat's avatar
TheNumbat committed
74
75
76
    return triangles.visualize(lines, active, level, trans);
}

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