author | Radek Brich <radek.brich@devl.cz> |
Fri, 28 Mar 2008 00:53:20 +0100 | |
branch | pyrit |
changeset 49 | 558fde7da82a |
parent 47 | 320d5d466864 |
child 53 | 228cb8bfdd54 |
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 |
/* |
44 | 2 |
* scene.cc: classes for objects in scene |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
6 |
* Copyright 2006, 2007, 2008 Radek Brich |
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
|
7 |
* |
44 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
10 |
* in the Software without restriction, including without limitation the rights |
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
|
13 |
* furnished to do so, subject to the following conditions: |
|
14 |
* |
|
15 |
* The above copyright notice and this permission notice shall be included in |
|
16 |
* all copies or substantial portions of the Software. |
|
17 |
* |
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 |
* THE SOFTWARE. |
|
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
|
25 |
*/ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
#include <math.h> |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
28 |
|
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
29 |
#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
|
30 |
#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
|
31 |
|
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
32 |
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:
15
diff
changeset
|
33 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
34 |
/* |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
35 |
//non-optimized |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
36 |
Quaternion res; |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
37 |
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:
15
diff
changeset
|
38 |
u = res.toVector(); |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
39 |
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:
15
diff
changeset
|
40 |
v = res.toVector(); |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
41 |
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:
15
diff
changeset
|
42 |
p = res.toVector(); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
43 |
*/ |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
44 |
|
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
45 |
// optimized |
22 | 46 |
Float t2 = q.a*q.b; |
47 |
Float t3 = q.a*q.c; |
|
48 |
Float t4 = q.a*q.d; |
|
49 |
Float t5 = -q.b*q.b; |
|
50 |
Float t6 = q.b*q.c; |
|
51 |
Float t7 = q.b*q.d; |
|
52 |
Float t8 = -q.c*q.c; |
|
53 |
Float t9 = q.c*q.d; |
|
54 |
Float t10 = -q.d*q.d; |
|
55 |
Float x,y,z; |
|
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
56 |
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:
15
diff
changeset
|
57 |
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:
15
diff
changeset
|
58 |
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:
15
diff
changeset
|
59 |
p = Vector3(x,y,z); |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
60 |
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:
15
diff
changeset
|
61 |
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:
15
diff
changeset
|
62 |
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:
15
diff
changeset
|
63 |
u = Vector3(x,y,z); |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
64 |
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:
15
diff
changeset
|
65 |
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:
15
diff
changeset
|
66 |
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:
20
diff
changeset
|
67 |
v = Vector3(x,y,z); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
68 |
p.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
69 |
u.normalize(); |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
70 |
v.normalize(); |
20
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
71 |
} |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
72 |
|
22 | 73 |
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:
15
diff
changeset
|
74 |
{ |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
75 |
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:
15
diff
changeset
|
76 |
} |
f22952603f29
new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents:
15
diff
changeset
|
77 |
|
46 | 78 |
Ray Camera::makeRay(Sample *samp) |
79 |
{ |
|
49
558fde7da82a
workaround for divide by zero bug in octree
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
80 |
Vector3 dir = p + (u*samp->x - v*samp->y)*F; |
46 | 81 |
dir.normalize(); |
82 |
return Ray(eye, dir); |
|
83 |
} |
|
84 |
||
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
85 |
/* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */ |
22 | 86 |
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:
8
diff
changeset
|
87 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
88 |
register Float tnear = -Inf; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
89 |
register Float tfar = Inf; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
90 |
register Float t1, t2; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
91 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
92 |
for (int i = 0; i < 3; i++) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
93 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
94 |
if (ray.dir[i] == 0) { |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
95 |
/* ray is parallel to these planes */ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
96 |
if (ray.o[i] < L[i] || ray.o[i] > H[i]) |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
97 |
return false; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
98 |
} else |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
99 |
{ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
100 |
/* compute the intersection distance of the planes */ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
101 |
t1 = (L[i] - ray.o[i]) / ray.dir[i]; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
102 |
t2 = (H[i] - ray.o[i]) / ray.dir[i]; |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
103 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
104 |
if (t1 > t2) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
105 |
swap(t1, t2); |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
106 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
107 |
if (t1 > tnear) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
108 |
tnear = t1; /* want largest Tnear */ |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
109 |
if (t2 < tfar) |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
110 |
tfar = t2; /* want smallest Tfar */ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
111 |
if (tnear > tfar || tfar < 0) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
112 |
return false; /* box missed; box is behind ray */ |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
113 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
114 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
115 |
|
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
116 |
a = tnear; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
117 |
b = tfar; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
118 |
return true; |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
119 |
} |
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
120 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
121 |
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
|
122 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
123 |
Vector3 V = ray.o - center; |
42 | 124 |
register Float d = dot(V, ray.dir); |
22 | 125 |
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
|
126 |
if (Det > 0) { |
42 | 127 |
d = -d - sqrtf(Det); |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
128 |
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
|
129 |
{ |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
130 |
dist = d; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
131 |
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
|
132 |
} |
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 |
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
|
135 |
} |
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 |
|
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
137 |
/* if there should be CSG sometimes, this may be needed... */ |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
138 |
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
|
139 |
{ |
22 | 140 |
//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
|
141 |
|
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
142 |
Vector3 V = ((Ray)ray).o - center; |
22 | 143 |
Float Vd = - dot(V, ray.dir); |
144 |
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
|
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 |
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
|
147 |
Det = sqrtf(Det); |
22 | 148 |
Float t1 = Vd - Det; |
149 |
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
|
150 |
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
|
151 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
152 |
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
|
153 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
154 |
// 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
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
159 |
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
|
160 |
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
|
161 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
162 |
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
|
163 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
164 |
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
|
165 |
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
|
166 |
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
|
167 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
168 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
169 |
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
|
170 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
171 |
|
38
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
172 |
bool Sphere::intersect_bbox(const BBox &bbox) const |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
173 |
{ |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
174 |
register float dmin = 0; |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
175 |
for (int i = 0; i < 3; i++) |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
176 |
{ |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
177 |
if (center[i] < bbox.L[i]) |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
178 |
dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]); |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
179 |
else |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
180 |
if (center[i] > bbox.H[i]) |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
181 |
dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]); |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
182 |
} |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
183 |
if (dmin <= sqr_radius) |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
184 |
return true; |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
185 |
return false; |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
186 |
}; |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
187 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
188 |
BBox Sphere::get_bbox() const |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
189 |
{ |
38
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
190 |
return BBox(center - radius, 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:
7
diff
changeset
|
191 |
} |
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
192 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
193 |
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
|
194 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
195 |
register Float tnear = -Inf; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
196 |
register Float tfar = Inf; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
197 |
register Float t1, t2; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
198 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
199 |
for (int i = 0; i < 3; i++) |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
200 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
201 |
if (ray.dir[i] == 0) { |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
202 |
/* ray is parallel to these planes */ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
203 |
if (ray.o[i] < L[i] || ray.o[i] > H[i]) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
204 |
return false; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
205 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
206 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
207 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
208 |
/* compute the intersection distance of the planes */ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
209 |
t1 = (L[i] - ray.o[i]) / ray.dir[i]; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
210 |
t2 = (H[i] - ray.o[i]) / ray.dir[i]; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
211 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
212 |
if (t1 > t2) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
213 |
swap(t1, t2); |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
214 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
215 |
if (t1 > tnear) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
216 |
tnear = t1; /* want largest Tnear */ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
217 |
if (t2 < tfar) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
218 |
tfar = t2; /* want smallest Tfar */ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
219 |
if (tnear > tfar || tfar < 0) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
220 |
return false; /* box missed; box is behind ray */ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
221 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
222 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
223 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
224 |
if (tnear < dist) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
225 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
226 |
dist = tnear; |
21
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
227 |
return true; |
79b516a3803d
naive color driven sub-sampling
Radek Brich <radek.brich@devl.cz>
parents:
20
diff
changeset
|
228 |
} |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
229 |
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
|
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 |
|
38
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
232 |
bool Box::intersect_bbox(const BBox &bbox) const |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
233 |
{ |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
234 |
return ( |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
235 |
H.x > bbox.L.x && L.x < bbox.H.x && |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
236 |
H.y > bbox.L.y && L.y < bbox.H.y && |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
237 |
H.z > bbox.L.z && L.z < bbox.H.z); |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
238 |
} |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
239 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
240 |
const Vector3 Box::normal(const Vector3 &P) const |
12
f4fcabf05785
kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
241 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
242 |
register Vector3 l = P - L; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
243 |
register Vector3 h = H - P; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
244 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
245 |
if (l.x < h.x) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
246 |
h.x = -1; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
247 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
248 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
249 |
l.x = h.x; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
250 |
h.x = +1; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
251 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
252 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
253 |
if (l.y < h.y) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
254 |
h.y = -1; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
255 |
else |
14
fc18ac4833f2
replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
256 |
{ |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
257 |
l.y = h.y; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
258 |
h.y = +1; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
259 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
260 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
261 |
if (l.z < h.z) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
262 |
h.z = -1; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
263 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
264 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
265 |
l.z = h.z; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
266 |
h.z = +1; |
15
a0a3e334744f
C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
267 |
} |
40
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
268 |
|
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
269 |
if (l.x > l.y) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
270 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
271 |
h.x = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
272 |
if (l.y > l.z) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
273 |
h.y = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
274 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
275 |
h.z = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
276 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
277 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
278 |
{ |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
279 |
h.y = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
280 |
if (l.x > l.z) |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
281 |
h.x = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
282 |
else |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
283 |
h.z = 0; |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
284 |
} |
929aad02c5f2
Makefile: added help and distclean target, plus small fixes
Radek Brich <radek.brich@devl.cz>
parents:
38
diff
changeset
|
285 |
return h; |
8
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
286 |
} |
e6567b740c5e
fixed virtual method get_bbox() for all shapes, default thread num changed to 4
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
287 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
288 |
#ifdef TRI_PLUCKER |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
289 |
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:
24
diff
changeset
|
290 |
{ |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
291 |
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:
24
diff
changeset
|
292 |
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:
24
diff
changeset
|
293 |
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:
24
diff
changeset
|
294 |
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:
24
diff
changeset
|
295 |
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:
24
diff
changeset
|
296 |
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:
24
diff
changeset
|
297 |
} |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
298 |
|
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
299 |
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:
24
diff
changeset
|
300 |
{ |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
301 |
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:
24
diff
changeset
|
302 |
} |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
303 |
#endif |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
304 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
305 |
Triangle::Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial) |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
306 |
: 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
|
307 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
308 |
material = amaterial; |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
309 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
310 |
const Vector3 c = B->P - A->P; |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
311 |
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:
24
diff
changeset
|
312 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
313 |
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:
24
diff
changeset
|
314 |
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
|
315 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
316 |
#ifdef TRI_PLUCKER |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
317 |
Plucker(B->P,C->P,pla); |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
318 |
Plucker(C->P,A->P,plb); |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
319 |
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:
24
diff
changeset
|
320 |
#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
|
321 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
322 |
#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
|
323 |
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
|
324 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
} |
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 |
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
|
331 |
{ |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
332 |
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
|
333 |
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
|
334 |
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
|
335 |
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
|
336 |
} |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
337 |
#endif |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
338 |
#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
|
339 |
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
|
340 |
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
|
341 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
342 |
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
|
343 |
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
|
344 |
nv = N[v] * krec; |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
345 |
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
|
346 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
347 |
// first line equation |
22 | 348 |
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
|
349 |
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
|
350 |
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
|
351 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
352 |
// second line equation |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
353 |
cnu = -c[u] * reci; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
354 |
cnv = c[v] * reci; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
355 |
#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
|
356 |
} |
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
357 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
358 |
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
|
359 |
{ |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
360 |
#ifdef TRI_PLUCKER |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
361 |
Float plr[6]; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
362 |
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:
24
diff
changeset
|
363 |
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:
24
diff
changeset
|
364 |
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:
24
diff
changeset
|
365 |
if (side0 != side1) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
366 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
367 |
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:
24
diff
changeset
|
368 |
if (side0 != side2) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
369 |
return false; |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
370 |
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:
24
diff
changeset
|
371 |
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:
24
diff
changeset
|
372 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
373 |
dist = t; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
374 |
return true; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
375 |
#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
|
376 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
377 |
#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:
24
diff
changeset
|
378 |
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:
24
diff
changeset
|
379 |
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:
24
diff
changeset
|
380 |
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:
24
diff
changeset
|
381 |
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:
24
diff
changeset
|
382 |
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:
24
diff
changeset
|
383 |
#endif |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
384 |
#ifdef TRI_BARI_PRE |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
385 |
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
|
386 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
387 |
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
|
388 |
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
|
389 |
|
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
390 |
const Float hu = O[u] + t * D[u] - A->P[u]; |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
391 |
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:
24
diff
changeset
|
392 |
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
|
393 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
394 |
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
|
395 |
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
|
396 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
397 |
const Float gamma = hu * cnv + hv * cnu; |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
398 |
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
|
399 |
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
|
400 |
|
3547b885df7e
initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
401 |
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
|
402 |
return true; |
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
403 |
#endif |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
404 |
|
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
405 |
#ifdef TRI_BARI |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
406 |
// 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:
24
diff
changeset
|
407 |
// 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:
24
diff
changeset
|
408 |
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:
24
diff
changeset
|
409 |
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:
24
diff
changeset
|
410 |
// distance test |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
411 |
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:
24
diff
changeset
|
412 |
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:
24
diff
changeset
|
413 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
414 |
|
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
415 |
// calc hitpoint |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
416 |
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:
24
diff
changeset
|
417 |
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:
24
diff
changeset
|
418 |
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:
24
diff
changeset
|
419 |
if (beta < 0) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
420 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
421 |
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:
24
diff
changeset
|
422 |
if (gamma < 0) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
423 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
424 |
if (beta+gamma > 1) |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
425 |
return false; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
426 |
dist = t; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
427 |
return true; |
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
428 |
#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
|
429 |
} |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
430 |
|
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
431 |
bool Triangle::intersect_bbox(const BBox &bbox) const |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
432 |
{ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
433 |
const Vector3 boxcenter = (bbox.L+bbox.H)*0.5; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
434 |
const Vector3 boxhalfsize = (bbox.H-bbox.L)*0.5; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
435 |
const Vector3 v0 = A->P - boxcenter; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
436 |
const Vector3 v1 = B->P - boxcenter; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
437 |
const Vector3 v2 = C->P - boxcenter; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
438 |
const Vector3 e0 = v1-v0; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
439 |
const Vector3 e1 = v2-v1; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
440 |
const Vector3 e2 = v0-v2; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
441 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
442 |
Float fex = fabsf(e0.x); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
443 |
Float fey = fabsf(e0.y); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
444 |
Float fez = fabsf(e0.z); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
445 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
446 |
Float p0,p1,p2,min,max,rad; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
447 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
448 |
p0 = e0.z*v0.y - e0.y*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
449 |
p2 = e0.z*v2.y - e0.y*v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
450 |
if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
451 |
rad = fez * boxhalfsize.y + fey * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
452 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
453 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
454 |
p0 = -e0.z*v0.x + e0.x*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
455 |
p2 = -e0.z*v2.x + e0.x*v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
456 |
if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
457 |
rad = fez * boxhalfsize.x + fex * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
458 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
459 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
460 |
p1 = e0.y*v1.x - e0.x*v1.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
461 |
p2 = e0.y*v2.x - e0.x*v2.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
462 |
if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
463 |
rad = fey * boxhalfsize.x + fex * boxhalfsize.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
464 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
465 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
466 |
fex = fabsf(e1.x); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
467 |
fey = fabsf(e1.y); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
468 |
fez = fabsf(e1.z); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
469 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
470 |
p0 = e1.z*v0.y - e1.y*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
471 |
p2 = e1.z*v2.y - e1.y*v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
472 |
if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
473 |
rad = fez * boxhalfsize.y + fey * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
474 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
475 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
476 |
p0 = -e1.z*v0.x + e1.x*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
477 |
p2 = -e1.z*v2.x + e1.x*v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
478 |
if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
479 |
rad = fez * boxhalfsize.x + fex * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
480 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
481 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
482 |
p0 = e1.y*v0.x - e1.x*v0.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
483 |
p1 = e1.y*v1.x - e1.x*v1.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
484 |
if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
485 |
rad = fey * boxhalfsize.x + fex * boxhalfsize.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
486 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
487 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
488 |
fex = fabsf(e2.x); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
489 |
fey = fabsf(e2.y); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
490 |
fez = fabsf(e2.z); |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
491 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
492 |
p0 = e2.z*v0.y - e2.y*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
493 |
p1 = e2.z*v1.y - e2.y*v1.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
494 |
if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
495 |
rad = fez * boxhalfsize.y + fey * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
496 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
497 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
498 |
p0 = -e2.z*v0.x + e2.x*v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
499 |
p1 = -e2.z*v1.x + e2.x*v1.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
500 |
if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
501 |
rad = fez * boxhalfsize.x + fex * boxhalfsize.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
502 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
503 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
504 |
p1 = e2.y*v1.x - e2.x*v1.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
505 |
p2 = e2.y*v2.x - e2.x*v2.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
506 |
if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
507 |
rad = fey * boxhalfsize.x + fex * boxhalfsize.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
508 |
if(min>rad || max<-rad) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
509 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
510 |
/* test overlap in the {x,y,z}-directions */ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
511 |
/* test in X-direction */ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
512 |
min = v0.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
513 |
if (v1.x < min) min = v1.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
514 |
if (v2.x < min) min = v2.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
515 |
max = v0.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
516 |
if (v1.x > max) max = v1.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
517 |
if (v2.x > max) max = v2.x; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
518 |
if(min>boxhalfsize.x || max<-boxhalfsize.x) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
519 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
520 |
/* test in Y-direction */ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
521 |
min = v0.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
522 |
if (v1.y < min) min = v1.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
523 |
if (v2.y < min) min = v2.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
524 |
max = v0.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
525 |
if (v1.y > max) max = v1.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
526 |
if (v2.y > max) max = v2.y; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
527 |
if(min>boxhalfsize.y || max<-boxhalfsize.y) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
528 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
529 |
/* test in Z-direction */ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
530 |
min = v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
531 |
if (v1.z < min) min = v1.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
532 |
if (v2.z < min) min = v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
533 |
max = v0.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
534 |
if (v1.z > max) max = v1.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
535 |
if (v2.z > max) max = v2.z; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
536 |
if(min>boxhalfsize.z || max<-boxhalfsize.z) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
537 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
538 |
/* test if the box intersects the plane of the triangle */ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
539 |
Vector3 vmin,vmax; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
540 |
Float v; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
541 |
for(int q=0;q<3;q++) |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
542 |
{ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
543 |
v=v0[q]; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
544 |
if(N[q]>0.0f) |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
545 |
{ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
546 |
vmin.cell[q]=-boxhalfsize[q] - v; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
547 |
vmax.cell[q]= boxhalfsize[q] - v; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
548 |
} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
549 |
else |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
550 |
{ |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
551 |
vmin.cell[q]= boxhalfsize[q] - v; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
552 |
vmax.cell[q]=-boxhalfsize[q] - v; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
553 |
} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
554 |
} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
555 |
if(dot(N,vmin)>0.0f) return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
556 |
if(dot(N,vmax)>=0.0f) return true; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
557 |
|
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
558 |
return false; |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
559 |
} |
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
560 |
|
25
b8232edee786
tuned ray-triangle intersection, now there are three algorithms to choose from:
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
561 |
BBox Triangle::get_bbox() const |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
562 |
{ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
563 |
BBox bbox = BBox(); |
28
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
564 |
bbox.L = A->P; |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
565 |
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:
25
diff
changeset
|
566 |
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:
25
diff
changeset
|
567 |
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:
25
diff
changeset
|
568 |
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:
25
diff
changeset
|
569 |
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:
25
diff
changeset
|
570 |
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:
25
diff
changeset
|
571 |
bbox.H = A->P; |
ffe83ca074f3
smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents:
25
diff
changeset
|
572 |
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:
25
diff
changeset
|
573 |
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:
25
diff
changeset
|
574 |
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:
25
diff
changeset
|
575 |
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:
25
diff
changeset
|
576 |
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:
25
diff
changeset
|
577 |
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:
0
diff
changeset
|
578 |
return bbox; |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
579 |
}; |