| author | Radek Brich <radek.brich@devl.cz> | 
| Wed, 12 Dec 2007 19:59:19 +0100 | |
| branch | pyrit | 
| changeset 35 | fb170fccb19f | 
| parent 34 | 28f6e8b9d5d1 | 
| child 36 | b490093b0ac3 | 
| permissions | -rw-r--r-- | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 1 | /* | 
| 34 
28f6e8b9d5d1
quaternion moved to extra header file
 Radek Brich <radek.brich@devl.cz> parents: 
31diff
changeset | 2 | * Pyrit Ray Tracer | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 3 | * file: scene.cc | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 4 | * | 
| 34 
28f6e8b9d5d1
quaternion moved to extra header file
 Radek Brich <radek.brich@devl.cz> parents: 
31diff
changeset | 5 | * Radek Brich, 2006-2007 | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 6 | */ | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 7 | |
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 8 | #include <math.h> | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 9 | |
| 14 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 10 | #include "common.h" | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 11 | #include "scene.h" | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 12 | |
| 20 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 13 | void Camera::rotate(const Quaternion &q) | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 14 | {
 | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 15 | /* | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 16 | //non-optimized | 
| 20 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 17 | Quaternion res; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 18 | res = q * Quaternion(u) * conjugate(q); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 19 | u = res.toVector(); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 20 | res = q * Quaternion(v) * conjugate(q); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 21 | v = res.toVector(); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 22 | res = q * Quaternion(p) * conjugate(q); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 23 | p = res.toVector(); | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 24 | */ | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 25 | |
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 26 | // optimized | 
| 22 | 27 | Float t2 = q.a*q.b; | 
| 28 | Float t3 = q.a*q.c; | |
| 29 | Float t4 = q.a*q.d; | |
| 30 | Float t5 = -q.b*q.b; | |
| 31 | Float t6 = q.b*q.c; | |
| 32 | Float t7 = q.b*q.d; | |
| 33 | Float t8 = -q.c*q.c; | |
| 34 | Float t9 = q.c*q.d; | |
| 35 | Float t10 = -q.d*q.d; | |
| 36 | Float x,y,z; | |
| 20 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 37 | x = 2*( (t8 + t10)*p.x + (t6 - t4)*p.y + (t3 + t7)*p.z ) + p.x; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 38 | y = 2*( (t4 + t6)*p.x + (t5 + t10)*p.y + (t9 - t2)*p.z ) + p.y; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 39 | z = 2*( (t7 - t3)*p.x + (t2 + t9)*p.y + (t5 + t8)*p.z ) + p.z; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 40 | p = Vector3(x,y,z); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 41 | x = 2*( (t8 + t10)*u.x + (t6 - t4)*u.y + (t3 + t7)*u.z ) + u.x; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 42 | y = 2*( (t4 + t6)*u.x + (t5 + t10)*u.y + (t9 - t2)*u.z ) + u.y; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 43 | z = 2*( (t7 - t3)*u.x + (t2 + t9)*u.y + (t5 + t8)*u.z ) + u.z; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 44 | u = Vector3(x,y,z); | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 45 | x = 2*( (t8 + t10)*v.x + (t6 - t4)*v.y + (t3 + t7)*v.z ) + v.x; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 46 | y = 2*( (t4 + t6)*v.x + (t5 + t10)*v.y + (t9 - t2)*v.z ) + v.y; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 47 | z = 2*( (t7 - t3)*v.x + (t2 + t9)*v.y + (t5 + t8)*v.z ) + v.z; | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 48 | v = Vector3(x,y,z); | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 49 | p.normalize(); | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 50 | u.normalize(); | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 51 | v.normalize(); | 
| 20 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 52 | } | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 53 | |
| 22 | 54 | void Camera::move(const Float fw, const Float left, const Float up) | 
| 20 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 55 | {
 | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 56 | eye = eye + fw*p + left*u + up*v; | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 57 | } | 
| 
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
 Radek Brich <radek.brich@devl.cz> parents: 
15diff
changeset | 58 | |
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 59 | /* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */ | 
| 22 | 60 | bool BBox::intersect(const Ray &ray, Float &a, Float &b) | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 61 | {
 | 
| 22 | 62 | Float tnear = -FLT_MAX; | 
| 63 | Float tfar = FLT_MAX; | |
| 64 | Float t1, t2; | |
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 65 | |
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 66 | for (int i = 0; i < 3; i++) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 67 | 	{
 | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 68 | 		if (ray.dir.cell[i] == 0) {
 | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 69 | /* ray is parallel to these planes */ | 
| 15 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 70 | if (ray.o.cell[i] < L.cell[i] || ray.o.cell[i] > H.cell[i]) | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 71 | return false; | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 72 | } else | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 73 | 		{
 | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 74 | /* compute the intersection distance of the planes */ | 
| 15 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 75 | t1 = (L.cell[i] - ray.o.cell[i]) / ray.dir.cell[i]; | 
| 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 76 | t2 = (H.cell[i] - ray.o.cell[i]) / ray.dir.cell[i]; | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 77 | |
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 78 | if (t1 > t2) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 79 | swap(t1, t2); | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 80 | |
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 81 | if (t1 > tnear) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 82 | tnear = t1; /* want largest Tnear */ | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 83 | if (t2 < tfar) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 84 | tfar = t2; /* want smallest Tfar */ | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 85 | if (tnear > tfar) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 86 | return false; /* box missed */ | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 87 | if (tfar < 0) | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 88 | return false; /* box is behind ray */ | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 89 | } | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 90 | } | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 91 | |
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 92 | a = tnear; | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 93 | b = tfar; | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 94 | return true; | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 95 | } | 
| 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 96 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 97 | bool Sphere::intersect(const Ray &ray, Float &dist) const | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 98 | {
 | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 99 | Vector3 V = ray.o - center; | 
| 22 | 100 | register Float d = -dot(V, ray.dir); | 
| 101 | register Float Det = d * d - (dot(V,V) - sqr_radius); | |
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 102 | 	if (Det > 0) {
 | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 103 | d -= sqrtf(Det); | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 104 | if (d > 0 && d < dist) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 105 | 		{
 | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 106 | dist = d; | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 107 | return true; | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 108 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 109 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 110 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 111 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 112 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 113 | bool Sphere::intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 114 | {
 | 
| 22 | 115 | //allts = new vector<Float>(); | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 116 | |
| 15 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 117 | Vector3 V = ((Ray)ray).o - center; | 
| 22 | 118 | Float Vd = - dot(V, ray.dir); | 
| 119 | Float Det = Vd * Vd - (dot(V,V) - sqr_radius); | |
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 120 | |
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 121 | 	if (Det > 0) {
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 122 | Det = sqrtf(Det); | 
| 22 | 123 | Float t1 = Vd - Det; | 
| 124 | Float t2 = Vd + Det; | |
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 125 | if (t1 < 0) | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 126 | 		{
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 127 | if (t2 > 0) | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 128 | 			{
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 129 | // ray from inside of the sphere | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 130 | allts.push_back(0.0); | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 131 | allts.push_back(t2); | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 132 | return true; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 133 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 134 | else | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 135 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 136 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 137 | else | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 138 | 		{
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 139 | allts.push_back(t1); | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 140 | allts.push_back(t2); | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 141 | return true; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 142 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 143 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 144 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 145 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 146 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 147 | BBox Sphere::get_bbox() const | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 148 | {
 | 
| 8 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 149 | BBox bbox = BBox(); | 
| 14 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 150 | bbox.L = center - radius; | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 151 | bbox.H = center + radius; | 
| 8 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 152 | return bbox; | 
| 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 153 | } | 
| 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 154 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 155 | bool Box::intersect(const Ray &ray, Float &dist) const | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 156 | {
 | 
| 22 | 157 | Float a,b; | 
| 21 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 158 | bool res = get_bbox().intersect(ray, a, b); | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 159 | if (res && a < dist) | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 160 | 	{
 | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 161 | dist = a; | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 162 | return true; | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 163 | } | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 164 | else | 
| 
79b516a3803d
naive color driven sub-sampling
 Radek Brich <radek.brich@devl.cz> parents: 
20diff
changeset | 165 | return false; | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 166 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 167 | |
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 168 | const Vector3 Box::normal(const Vector3 &P) const | 
| 12 
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
 Radek Brich <radek.brich@devl.cz> parents: 
8diff
changeset | 169 | {
 | 
| 15 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 170 | Vector3 N; | 
| 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 171 | for (int i = 0; i < 3; i++) | 
| 14 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 172 | 	{
 | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 173 | if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps) | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 174 | //if (P.cell[i] == L.cell[i]) | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 175 | 		{
 | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 176 | N.cell[i] = -1.0; | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 177 | break; | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 178 | } | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 179 | if (P.cell[i] >= H.cell[i]-Eps && P.cell[i] <= H.cell[i]+Eps) | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 180 | //if (P.cell[i] == H.cell[i]) | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 181 | 		{
 | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 182 | N.cell[i] = +1.0; | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 183 | break; | 
| 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 184 | } | 
| 15 
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
 Radek Brich <radek.brich@devl.cz> parents: 
14diff
changeset | 185 | } | 
| 14 
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
 Radek Brich <radek.brich@devl.cz> parents: 
12diff
changeset | 186 | return N; | 
| 8 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 187 | } | 
| 
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
 Radek Brich <radek.brich@devl.cz> parents: 
7diff
changeset | 188 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 189 | #ifdef TRI_PLUCKER | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 190 | inline void Plucker(const Vector3 &p, const Vector3 &q, Float* pl) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 191 | {
 | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 192 | pl[0] = p.x*q.y - q.x*p.y; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 193 | pl[1] = p.x*q.z - q.x*p.z; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 194 | pl[2] = p.x - q.x; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 195 | pl[3] = p.y*q.z - q.y*p.z; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 196 | pl[4] = p.z - q.z; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 197 | pl[5] = q.y - p.y; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 198 | } | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 199 | |
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 200 | inline Float Side(const Float* pla, const Float* plb) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 201 | {
 | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 202 | return pla[0]*plb[4] + pla[1]*plb[5] + pla[2]*plb[3] + pla[4]*plb[0] + pla[5]*plb[1] + pla[3]*plb[2]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 203 | } | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 204 | #endif | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 205 | |
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 206 | Triangle::Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial) | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 207 | : smooth(false), A(aA), B(aB), C(aC) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 208 | {
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 209 | material = amaterial; | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 210 | |
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 211 | const Vector3 c = B->P - A->P; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 212 | const Vector3 b = C->P - A->P; | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 213 | |
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 214 | N = cross(c, b); | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 215 | N.normalize(); | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 216 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 217 | #ifdef TRI_PLUCKER | 
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 218 | Plucker(B->P,C->P,pla); | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 219 | Plucker(C->P,A->P,plb); | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 220 | Plucker(A->P,B->P,plc); | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 221 | #endif | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 222 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 223 | #if defined(TRI_BARI) || defined(TRI_BARI_PRE) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 224 | if (fabsf(N.x) > fabsf(N.y)) | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 225 | 	{
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 226 | if (fabsf(N.x) > fabsf(N.z)) | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 227 | k = 0; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 228 | else | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 229 | k = 2; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 230 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 231 | else | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 232 | 	{
 | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 233 | if (fabsf(N.y) > fabsf(N.z)) | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 234 | k = 1; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 235 | else | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 236 | k = 2; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 237 | } | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 238 | #endif | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 239 | #ifdef TRI_BARI_PRE | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 240 | int u = (k + 1) % 3; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 241 | int v = (k + 2) % 3; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 242 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 243 | Float krec = 1.0 / N[k]; | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 244 | nu = N[u] * krec; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 245 | nv = N[v] * krec; | 
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 246 | nd = dot(N, A->P) * krec; | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 247 | |
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 248 | // first line equation | 
| 22 | 249 | Float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]); | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 250 | bnu = b[u] * reci; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 251 | bnv = -b[v] * reci; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 252 | |
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 253 | // second line equation | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 254 | cnu = -c[u] * reci; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 255 | cnv = c[v] * reci; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 256 | #endif | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 257 | } | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 258 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 259 | bool Triangle::intersect(const Ray &ray, Float &dist) const | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 260 | {
 | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 261 | #ifdef TRI_PLUCKER | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 262 | Float plr[6]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 263 | Plucker(ray.o, ray.o+ray.dir, plr); | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 264 | const bool side0 = Side(plr, pla) >= 0.0; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 265 | const bool side1 = Side(plr, plb) >= 0.0; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 266 | if (side0 != side1) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 267 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 268 | const bool side2 = Side(plr, plc) >= 0.0; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 269 | if (side0 != side2) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 270 | return false; | 
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 271 | const Float t = - dot( (ray.o - A->P), N) / dot(ray.dir,N); | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 272 | if(t <= Eps || t >= dist) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 273 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 274 | dist = t; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 275 | return true; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 276 | #endif | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 277 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 278 | #if defined(TRI_BARI) || defined(TRI_BARI_PRE) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 279 | 	static const int modulo3[5] = {0,1,2,0,1};
 | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 280 | const Vector3 &O = ray.o; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 281 | const Vector3 &D = ray.dir; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 282 | register const int u = modulo3[k+1]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 283 | register const int v = modulo3[k+2]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 284 | #endif | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 285 | #ifdef TRI_BARI_PRE | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 286 | const Float t = (nd - O[k] - nu * O[u] - nv * O[v]) / (D[k] + nu * D[u] + nv * D[v]); | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 287 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 288 | if (t >= dist || t < Eps) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 289 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 290 | |
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 291 | const Float hu = O[u] + t * D[u] - A->P[u]; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 292 | const Float hv = O[v] + t * D[v] - A->P[v]; | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 293 | const Float beta = hv * bnu + hu * bnv; | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 294 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 295 | if (beta < 0.) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 296 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 297 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 298 | const Float gamma = hu * cnv + hv * cnu; | 
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 299 | if (gamma < 0. || beta + gamma > 1.) | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 300 | return false; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 301 | |
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 302 | dist = t; | 
| 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 303 | return true; | 
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 304 | #endif | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 305 | |
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 306 | #ifdef TRI_BARI | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 307 | // original barycentric coordinates based intesection | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 308 | // not optimized, just for reference | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 309 | const Vector3 c = B - A; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 310 | const Vector3 b = C - A; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 311 | // distance test | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 312 | const Float t = - dot( (O-A), N) / dot(D,N); | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 313 | if (t < Eps || t > dist) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 314 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 315 | |
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 316 | // calc hitpoint | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 317 | const Float Hu = O[u] + t * D[u] - A[u]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 318 | const Float Hv = O[v] + t * D[v] - A[v]; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 319 | const Float beta = (b[u] * Hv - b[v] * Hu) / (b[u] * c[v] - b[v] * c[u]); | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 320 | if (beta < 0) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 321 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 322 | const Float gamma = (c[v] * Hu - c[u] * Hv) / (b[u] * c[v] - b[v] * c[u]); | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 323 | if (gamma < 0) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 324 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 325 | if (beta+gamma > 1) | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 326 | return false; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 327 | dist = t; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 328 | return true; | 
| 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 329 | #endif | 
| 0 
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
 Radek Brich <radek.brich@devl.cz> parents: diff
changeset | 330 | } | 
| 7 
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
 Radek Brich <radek.brich@devl.cz> parents: 
0diff
changeset | 331 | |
| 25 
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
 Radek Brich <radek.brich@devl.cz> parents: 
24diff
changeset | 332 | BBox Triangle::get_bbox() const | 
| 7 
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
 Radek Brich <radek.brich@devl.cz> parents: 
0diff
changeset | 333 | {
 | 
| 
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
 Radek Brich <radek.brich@devl.cz> parents: 
0diff
changeset | 334 | BBox bbox = BBox(); | 
| 28 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 335 | bbox.L = A->P; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 336 | if (B->P.x < bbox.L.x) bbox.L.x = B->P.x; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 337 | if (C->P.x < bbox.L.x) bbox.L.x = C->P.x; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 338 | if (B->P.y < bbox.L.y) bbox.L.y = B->P.y; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 339 | if (C->P.y < bbox.L.y) bbox.L.y = C->P.y; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 340 | if (B->P.z < bbox.L.z) bbox.L.z = B->P.z; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 341 | if (C->P.z < bbox.L.z) bbox.L.z = C->P.z; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 342 | bbox.H = A->P; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 343 | if (B->P.x > bbox.H.x) bbox.H.x = B->P.x; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 344 | if (C->P.x > bbox.H.x) bbox.H.x = C->P.x; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 345 | if (B->P.y > bbox.H.y) bbox.H.y = B->P.y; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 346 | if (C->P.y > bbox.H.y) bbox.H.y = C->P.y; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 347 | if (B->P.z > bbox.H.z) bbox.H.z = B->P.z; | 
| 
ffe83ca074f3
smooth triangles (aka Phong shading)
 Radek Brich <radek.brich@devl.cz> parents: 
25diff
changeset | 348 | if (C->P.z > bbox.H.z) bbox.H.z = C->P.z; | 
| 7 
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
 Radek Brich <radek.brich@devl.cz> parents: 
0diff
changeset | 349 | return bbox; | 
| 
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
 Radek Brich <radek.brich@devl.cz> parents: 
0diff
changeset | 350 | }; |