(hopefully) finished Box::Clip()
[MicroTrace.git] / Triangle.cxx
1 #include "Triangle.hxx"
2
3 Triangle::Triangle(const Vec3f& a,
4 const Vec3f& b,
5 const Vec3f& c,
6 Shader* shader)
7 : Primitive(shader),
8 m_a(a),
9 m_b(b),
10 m_c(c),
11 m_n( (b-a).cross(c-a) )
12 {
13 m_n.normalize();
14 }
15
16 Triangle::~Triangle()
17 {
18 }
19
20 bool
21 Triangle::Intersect(Ray& ray)
22 {
23 const Vec3f edge1 = m_b-m_a;
24 const Vec3f edge2 = m_c-m_a;
25
26 const Vec3f pvec = ray.direction().cross(edge2);
27
28 const float det = edge1.dot(pvec);
29 if (fabs(det) < Epsilon) return false;
30
31 const float inv_det = 1.0f / det;
32
33 const Vec3f tvec = ray.origin()-m_a;
34 float lambda = tvec.dot( pvec );
35 lambda *= inv_det;
36
37 if (lambda < 0.0f || lambda > 1.0f)
38 return false;
39
40 const Vec3f qvec = tvec.cross(edge1);
41 float mue = ray.direction().dot(qvec);
42 mue *= inv_det;
43
44 if (mue < 0.0f || mue+lambda > 1.0f)
45 return false;
46
47 float f = edge2.dot(qvec);
48 f *= inv_det;
49 if (ray.t() <= f || f < Epsilon )
50 return false;
51
52 ray.setT(f);
53 ray.setHit(this);
54
55 return true;
56 }
57
58 Vec3f
59 Triangle::GetNormal(Ray& ray)
60 {
61 return m_n;
62 }
63
64 Box
65 Triangle::CalcBounds()
66 {
67 Box bounds;
68 bounds.Extend(m_a);
69 bounds.Extend(m_b);
70 bounds.Extend(m_c);
71 return Box();
72 }
73
74 bool
75 Triangle::InVoxel(const Box& box)
76 {
77 return false;
78 }
This page took 0.052435 seconds and 5 git commands to generate.