-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
executable file
·110 lines (87 loc) · 2.84 KB
/
Triangle.cpp
File metadata and controls
executable file
·110 lines (87 loc) · 2.84 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "Triangle.h"
#include "TriangleMesh.h"
#include "Ray.h"
int Triangle::intersectionNumber = 0;
Triangle::Triangle(TriangleMesh * m, unsigned int i) :
m_mesh(m), m_index(i)
{
}
Triangle::~Triangle()
{
}
int
Triangle::getIntersectionNumber()
{
return intersectionNumber;
}
void
Triangle::resertIntersectionNumber()
{
intersectionNumber = 0;
}
void
Triangle::addIntersectionNumber()
{
intersectionNumber++;
}
void
Triangle::renderGL()
{
TriangleMesh::TupleI3 ti3 = m_mesh->vIndices()[m_index];
const Vector3 & v0 = m_mesh->vertices()[ti3.x]; //vertex a of triangle
const Vector3 & v1 = m_mesh->vertices()[ti3.y]; //vertex b of triangle
const Vector3 & v2 = m_mesh->vertices()[ti3.z]; //vertex c of triangle
glBegin(GL_TRIANGLES);
glVertex3f(v0.x, v0.y, v0.z);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glEnd();
}
Vector3
Triangle::getPoint() const
{
TriangleMesh::TupleI3 ti3 = m_mesh->vIndices()[m_index];
const Vector3 & v0 = m_mesh->vertices()[ti3.x]; //vertex a of triangle
const Vector3 & v1 = m_mesh->vertices()[ti3.y]; //vertex b of triangle
const Vector3 & v2 = m_mesh->vertices()[ti3.z]; //vertex c of triangle
return (v0 + v1 + v2) / 3;
}
std::vector<Vector3>
Triangle::getVertices() const
{
std::vector<Vector3> vertices;
TriangleMesh::TupleI3 ti3 = m_mesh->vIndices()[m_index];
const Vector3 & v0 = m_mesh->vertices()[ti3.x]; //vertex a of triangle
const Vector3 & v1 = m_mesh->vertices()[ti3.y]; //vertex b of triangle
const Vector3 & v2 = m_mesh->vertices()[ti3.z]; //vertex c of triangle
vertices.push_back(v0);
vertices.push_back(v1);
vertices.push_back(v2);
return vertices;
}
bool
Triangle::intersect(HitInfo& result, const Ray& r,float tMin, float tMax)
{
TriangleMesh::TupleI3 ti3 = m_mesh->vIndices()[m_index];
const Vector3 & v0 = m_mesh->vertices()[ti3.x]; //vertex a of triangle
const Vector3 & v1 = m_mesh->vertices()[ti3.y]; //vertex b of triangle
const Vector3 & v2 = m_mesh->vertices()[ti3.z]; //vertex c of triangle
// init
float delta = dot(cross(v0-v1, v0-v2), r.d);
float delta1 = dot(cross(v0-r.o, v0-v2), r.d);
float delta2 = dot(cross(v0-v1, v0-r.o), r.d);
float delta3 = dot(cross(v0-v1, v0-v2), v0-r.o);
float beta = delta1 / delta;
float gamma = delta2 / delta;
float t = delta3 / delta;
if ((beta < 0) || (beta > 1) || (gamma < 0) || (gamma > 1) || ((1-beta-gamma) < 0) || ((1-beta-gamma) > 1) || t < tMin+epsilon || t > tMax)
return false;
result.t = t;
result.P = r.o + result.t * r.d;
TriangleMesh::TupleI3 tin3 = m_mesh->nIndices()[m_index];
result.N = m_mesh->normals()[tin3.x] * (1-beta-gamma) + m_mesh->normals()[tin3.y] * beta + m_mesh->normals()[tin3.z] * gamma;
result.N.normalize();
result.material = this->m_material;
this->addIntersectionNumber();
return true;
}