Newer
Older
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
#pragma once
#include "TriangleMesh.h"
#include <sstream>
#include <string>
#include "../tools/ObjectLoader.h"
namespace shapes {
TriangleMesh::TriangleMesh(std::vector<Triangle> triangles)
: triangles(triangles),
material(nullptr),
hierarchy(Group(util::identity())) {
}
TriangleMesh::TriangleMesh(std::istream& in,
const std::shared_ptr<material::Material>& material)
: material(material), hierarchy(Group(util::identity())) {
triangles = util::loadObj(in, material);
}
std::optional<cam::Hit> TriangleMesh::intersect(const cam::Ray& r) const {
std::optional<cam::Hit> result = std::nullopt;
for (auto tri : triangles) {
// if (tri.bounds().intersects(r)) {
std::optional<cam::Hit> temp = tri.intersect(r);
if (temp) {
if (r.in_range(temp->scalar())) {
if (!result) {
result = temp;
} else if (result->scalar() > temp->scalar()) {
result = temp;
}
}
}
//}
}
return result;
}
util::AxisAlignedBoundingBox TriangleMesh::bounds() const {
return util::AxisAlignedBoundingBox(util::Vec3(-2), util::Vec3(2));
}
util::SurfacePoint TriangleMesh::sampleLight() const {
return util::SurfacePoint(util::Vec3(), 0, material);
}
util::Vec3 TriangleMesh::calculateLightEmission(const util::SurfacePoint& p,
const util::Vec3& d) const {
return util::Vec3();
}
util::AxisAlignedBoundingBox TriangleMesh::initBB() {
util::AxisAlignedBoundingBox init = triangles[0].bounds();
for (auto tri : triangles) init + tri.bounds();
return init;
}
} // namespace shapes