-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.hh
104 lines (72 loc) · 2.09 KB
/
Triangle.hh
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
#ifndef TRIANGULE_HXX
#define TRIANGULE_HXX
#include "Basic.hh"
class Triangule : public Primitive
{
private:
Vec3f a, b, c;
Vec3f normal;
bool PointInTriangle(Vec3f &pt, Ray &ray);
public:
Triangule(Vec3f a, Vec3f b, Vec3f c, Shader *s);
~Triangule() {};
virtual Box CalcBounds()
{
Box bounds;
bounds.Extend(a);
bounds.Extend(b);
bounds.Extend(c);
return bounds;
};
virtual bool Intersect(Ray &ray)
{
//// the ray hit the backside of the triangle ..
//// we count that as not hit
//if( ray.dir.dot( GetNormal(ray) ) < 0 )
// return false;
const Vec3f edge1 = b-a;
const Vec3f edge2 = c-a;
const Vec3f pvec = Cross(ray.dir,edge2);
const float det = Dot(edge1, pvec);
if (fabs(det) < Epsilon) return false;
const float inv_det = 1.0f / det;
const Vec3f tvec = ray.org-a;
float lambda = Dot(tvec, pvec);
lambda *= inv_det;
if (lambda < 0.0f || lambda > 1.0f) return false;
const Vec3f qvec = Cross(tvec,edge1);
float mue = Dot(ray.dir, qvec);
mue *= inv_det;
if (mue < 0.0f || mue+lambda > 1.0f) return false;
float f = Dot(edge2, qvec);
f = f * inv_det;// - Epsilon;
//f = f * inv_det + Epsilon;
if (ray.t <= f || f < Epsilon ) return false;
ray.u = lambda;
ray.v = mue;
ray.t = f;
ray.hit = this;
return true;
};
//! Get triangle's normal
virtual Vec3f GetNormal( Ray &ray )
{
Vec3f e1 = b - a;
Vec3f e2 = c - a;
Vec3f normal = Cross(e1,e2);
Normalize(normal);
return normal;
};
virtual void GetUV(const Ray &ray, float &u, float &v) const {
//float alpha = 1 - ray.u - ray.v;
//Vec3f interpolated(alpha * ray.getHitPoint(0) + ray.u * ray.getHitPoint(0) + ray.v * ray.getHitPoint(0));
//Vec3f bar = ray.getHitPoint(0);
//u = bar.x*u + bar.y*u + bar.z*u;
//v = bar.x*v + bar.y*v + bar.z*v;
u = ray.u;
v = ray.v;
//u = interpolated.x;
//v = interpolated.y;
}
};
#endif