-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionTester.cpp
More file actions
211 lines (185 loc) · 5.95 KB
/
IntersectionTester.cpp
File metadata and controls
211 lines (185 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "IntersectionTester.h"
#pragma region BASE_INTERSECTION_TESTER
IntersectionTester::~IntersectionTester() {
delete sampler;
}
void IntersectionTester::basicIntersectionTest(const Ray &ray, const F& face) {
const Vector3R &p0 = scenePtr->vertices[face.v[0]].v, &p1 = scenePtr->vertices[face.v[1]].v, &p2 = scenePtr->vertices[face.v[2]].v;
Vector3R E1 = p1 - p0;
Vector3R E2 = p2 - p0;
Vector3R T = ray.source - p0;
Vector3R P = ray.direction.cross(E2);
Real det = P.dot(E1);
if (abs(det) <= Limit::Epsilon) {
return;
}
Real u = P.dot(T) / det;
if (u > 1 + Limit::Epsilon || u < -Limit::Epsilon) {
return;
}
Vector3R Q = T.cross(E1);
Real v = Q.dot(ray.direction) / det, w;
if (v < -Limit::Epsilon || v > 1 + Limit::Epsilon || (w = (1 - u - v)) < -Limit::Epsilon) {
return;
}
Real t = Q.dot(E2) / det;
if (t < Limit::Epsilon || t > cacheDistance) {
return;
}
cacheIntersected = true;
cacheDistance = t;
if (visibilityTestMode) {
return;
}
Vector3R n =
scenePtr->normals[face.vn[0]].v * w + scenePtr->normals[face.vn[1]].v * u + scenePtr->normals[face.vn[2]].v * v;
cacheNormal = n.normalized();
cacheMPtr = face.materialPtr;
if (!cacheMPtr->isTextured() && !cacheMPtr->isBumped()) {
return;
}
else {
// compute normal shift
Vector3R &t0 = scenePtr->textures[face.vt[0]].v, &t1 = scenePtr->textures[face.vt[1]].v, &t2 = scenePtr->textures[face.vt[2]].v;
Vector3R T1 = t1 - t0, T2 = t2 - t0;
Real v11 = E1.squaredNorm(), v12 = E1.dot(E2), v22 = E2.squaredNorm();
Real det2 = v11 * v22 - v12 * v12;
Real k11 = (T2[0] * v22 - T1[0] * v12) / det2;
Real k12 = (v11 * T2[0] - v12 * T1[0]) / det2;
Real k21 = (T2[1] * v22 - T1[1] * v12) / det2;
Real k22 = (v11 * T2[1] - v12 * T1[1]) / det2;
Vector3R shiftH = k11 * E1 + k12 * E2;
Vector3R shiftV = k21 * E1 + k22 * E2;
Vector3R textureCoordinate = t0 * w + t1 * u + t2 * v;
int tI = std::min((int)(textureCoordinate[0] * cacheMPtr->texturePtr->W), cacheMPtr->texturePtr->W - 2);
int tJ = std::min((int)(textureCoordinate[1] * cacheMPtr->texturePtr->H), cacheMPtr->texturePtr->H - 2);
if (cacheMPtr->isBumped()) {
/*Real shiftX = cacheMPtr->texturePtr->bump[tI + 1][tJ] - cacheMPtr->texturePtr->bump[tI][tJ];
Real shiftY = cacheMPtr->texturePtr->bump[tI][tJ + 1] - cacheMPtr->texturePtr->bump[tI][tJ];
cacheNormal = (cacheNormal + 10 * (shiftH * shiftX + shiftV * shiftY)).normalized();*/
Real shiftX = 2 * uniform_01(gen) - 1;
shiftX = (shiftX / abs(shiftX)) * pow(abs(shiftX), 1.7);
Vector3R pos = ray.source + cacheDistance * ray.direction;
Vector3R shift = (pos - Vector3R(0.3, 0.3, 0)).normalized();
//Vector3R shift = Vector3R(0, 1, 0);
cacheNormal = 2 * shift * shiftX + sqrt(1 - shiftX * shiftX) * cacheNormal;
cacheNormal.normalize();
return;
}
textureFilterR = cacheMPtr->texturePtr->texture[tI][tJ][0];
textureFilterG = cacheMPtr->texturePtr->texture[tI][tJ][1];
textureFilterB = cacheMPtr->texturePtr->texture[tI][tJ][2];
return;
}
}
void IntersectionTester::basicIntersectionTest(const Ray &ray, const Sphere& sphere) {
Real B = 2 * (ray.direction.dot(ray.source - sphere.center));
Real C = (ray.source - sphere.center).squaredNorm() - sphere.r * sphere.r;
Real delta;
if ((delta = (B*B - 4 * C)) < Limit::Epsilon) {
return;
}
Real t1 = (-B - sqrt(delta)) / 2;
if (t1 > cacheDistance) {
return;
}
if (t1 < Limit::Epsilon) {
t1 = (-B + sqrt(delta)) / 2;
if (t1 < Limit::Epsilon || t1 > cacheDistance) {
return;
}
}
cacheIntersected = true;
cacheDistance = t1;
cacheNormal = (ray.source + cacheDistance * ray.direction - sphere.center).normalized();
cacheMPtr = sphere.materialPtr;
}
bool SimpleIntersectionTester::visible(const Vector3R &pos1, const Vector3R &pos2) {
visibilityTestMode = true;
Ray ray = Ray::fromPoints(pos1, pos2);
Real d = (pos2 - pos1).norm();
cacheDistance = d;
cacheIntersected = false;
cacheNormal = Vector3R(1, 0, 0);
cacheMPtr = nullptr;
int n = scenePtr->faces.size();
for (int i = 0; i < n; i++) {
basicIntersectionTest(ray, scenePtr->faces[i]);
if (cacheDistance < d - Limit::Epsilon) {
visibilityTestMode = false;
return false;
}
}
n = scenePtr->spheres.size();
for (int i = 0; i < n; i++) {
basicIntersectionTest(ray, scenePtr->spheres[i]);
if (cacheDistance < d - Limit::Epsilon) {
visibilityTestMode = false;
return false;
}
}
visibilityTestMode = false;
return true;
}
void IntersectionTester::clearCache() {
cacheDistance = Limit::Infinity;
cacheIntersected = false;
cacheNormal = Vector3R(1, 0, 0);
cacheMPtr = nullptr;
cacheU = 0;
cacheV = 0;
}
#pragma endregion
#pragma region SIMPLE_TESTER
SimpleIntersectionTester::SimpleIntersectionTester(Scene * _scenePtr) {
scenePtr = _scenePtr;
sampler = new Sampler3D();
if (scenePtr->scatterMtl != nullptr) {
exponential_scatter = std::exponential_distribution<Real>(scenePtr->scatterMtl->n);
scatterMode = true;
}
else {
scatterMode = false;
}
}
void SimpleIntersectionTester::intersectionTest(const Ray &ray, bool &intersected, Real &distance, Vector3R &normal, MaterialPtr &mPtr) {
clearCache();
int n = scenePtr->faces.size();
for (int i = 0; i < n; i++) {
basicIntersectionTest(ray, scenePtr->faces[i]);
}
n = scenePtr->spheres.size();
for (int i = 0; i < n; i++) {
basicIntersectionTest(ray, scenePtr->spheres[i]);
}
if (scatterMode) {
bool ls = false;
if (cacheMPtr != nullptr) {
if (cacheMPtr->isLightSource()) {
ls = true;
}
}
Real d = exponential_scatter(gen);
if (d < cacheDistance && !ls) {
normal = sampler->sample_Diffuse_P(-ray.direction);
intersected = true;
mPtr = scenePtr->scatterMtl;
distance = d;
return;
}
}
intersected = cacheIntersected;
distance = cacheDistance;
normal = cacheNormal;
mPtr = cacheMPtr;
if (cacheMPtr != nullptr) {
// memory leak fixed
if (mPtr->isTextureKd()) {
mPtr = new Material(*cacheMPtr);
mPtr->Kd[0] *= textureFilterR;
mPtr->Kd[1] *= textureFilterG;
mPtr->Kd[2] *= textureFilterB;
}
}
}
#pragma endregion