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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#include <iostream>
#include "raytracer/raytracer.h"
/**
* @brief This source file handles intersection calculations to be used by the ray tracer.
* The implementation for findIntersection in the RayTracer namespace is at the end of the file.
*/
// TODO: implement mesh
//updated to handle intersection in 4d
glm::vec4 intersectCircle(
glm::vec4 p,
glm::vec4 d,
const RenderShapeData& shape,
bool &isHit
)
{
isHit = false;
// implicit: x^2 + y^2 + z^2 - r^2 = 0, all directions
float radius = 0.5f;
float a = d.x*d.x + d.y*d.y + d.z*d.z + d[3] * d[3];
float b = 2.f * (p.x*d.x + p.y*d.y + p.z*d.z + p[3]*d[3]);
float c = p.x*p.x + p.y*p.y + p.z*p.z + p[3] * p[3] - radius*radius;
float discriminant = b*b - 4*a*c;
if (discriminant < 0) // no solution
{
return glm::vec4(0.f);
}
float t1 = (-b - std::sqrt(discriminant)) / (2.f*a);
float t2 = (-b + std::sqrt(discriminant)) / (2.f*a);
if (t1 <= 0 && t2 <= 0) // both behind camera
{
return glm::vec4(0.f);
} else if (t1 <= 0) // t2 in front of camera
{
isHit = true;
return p + t2*d;
} else if (t2 <= 0) // t1 in front of camera
{
isHit = true;
return p + t1*d;
} else {
isHit = true;
float t = std::min(t1, t2);
return p + t*d; // want best intersection point
}
}
glm::vec4 intersectCone(
glm::vec4 p,
glm::vec4 d,
const RenderShapeData& shape,
bool &isHit)
{
isHit = false;
float t = FINF;
// updated to 4d
// x^2 + z^2 - y^2 - w^2= r^2, conic top
float radius = 0.5f;
float a = d.x*d.x + d.z*d.z - .25f*(d.y*d.y) - .25f*(d[3]*d[3]);
float b = 2.f*(p.x*d.x + p.z*d.z) - .5f*(p.y*d.y) + .25f*d.y - .5f*(p[3]*d[3]) + .25f*d[3];
float c = p.x*p.x + p.z*p.z - .25f*(p.y*p.y) + .25f*p.y - .25f*(p[3]*p[3]) + .25f*p[3] - 1/16.f;
float discriminant = b*b - 4*a*c;
if (discriminant >= 0)
{
float t1 = (-b - std::sqrt(discriminant)) / (2.f*a);
float t2 = (-b + std::sqrt(discriminant)) / (2.f*a);
auto p1Top = p + t1 * d;
if (
t1 > 0 &&
p1Top.y >= -.5f && p1Top.y <= .5f &&
p1Top[3] >= -.5f && p1Top[3] <= .5f)
{
t = std::min(t1, t);
}
auto p2Top = p + t2 * d;
if (
t2 > 0 &&
p2Top.y >= -.5f && p2Top.y <= .5f &&
p2Top[3] >= -.5f && p2Top[3] <= .5f)
{
t = std::min(t2, t);
}
}
// x^2 + y^2 - z^2 = 0, base w.r.t. w axis
float twBase = (- .5f - p[3]) / d[3];
auto pwBase = p + twBase * d;
if (
twBase > 0 &&
pwBase.x*pwBase.x + pwBase.z*pwBase.z <= pwBase.y*pwBase.y + .25f &&
pwBase.y >= -.5f && pwBase.y <= .5f
)
{
t = std::min(t, twBase);
}
// x^2 + w^2 - z^2 = 0, base w.r.t. y axis
float tyBase = (- .5f - p.y) / d.y;
auto pyBase = p + tyBase * d;
if (
tyBase > 0 &&
pyBase.x*pyBase.x + pyBase.z*pyBase.z <= pyBase[3]*pyBase[3] + .25f &&
pyBase[3] >= -.5f && pyBase[3] <= .5f
)
{
t = std::min(t, tyBase);
}
if (t == FINF)
{
return glm::vec4(0.f);
} else {
isHit = true;
return p + t*d;
}
}
glm::vec4 intersectCylinder(
glm::vec4 p,
glm::vec4 d,
const RenderShapeData& shape,
bool &isHit)
{
isHit = false;
float t = FINF;
// implicit: x^2 + z^2 - w/2 = r^2, y + w between -.5, .5
float radius = 0.5f;
float a = d.x*d.x + d.z*d.z;
float b = 2.f * (p.x*d.x + p.z*d.z) - .5f * d[3];
float c = p.x*p.x + p.z*p.z - .5f * p[3] - radius*radius;
float discriminant = b*b - 4*a*c;
if (discriminant >= 0)
{
float t1 = (-b - std::sqrt(discriminant)) / (2.f*a);
float t2 = (-b + std::sqrt(discriminant)) / (2.f*a);
auto p1Top = p + t1 * d;
if (
t1 > 0 &&
p1Top.y + p1Top[3] >= -.5f && p1Top.y - p1Top[3] <= .5f &&
p1Top[3] >= -.5f && p1Top[3] <= .5f
)
{
t = std::min(t1, t);
}
auto p2Top = p + t2 * d;
if (
t2 > 0 &&
p2Top.y + p2Top[3] >= -.5f && p2Top.y - p2Top[3] <= .5f &&
p2Top[3] >= -.5f && p2Top[3] <= .5f)
{
t = std::min(t2, t);
}
}
// implicit y - w = .5f, top base
float tTop = (.5f - p.y + p[3]) / (d.y - d[3]);
auto pTop = p + tTop * d;
if (
tTop > 0 &&
pTop.x*pTop.x + pTop.z*pTop.z <= radius*radius + pTop[3]/2.f &&
pTop[3] >= -.5f && pTop[3] <= .5f
)
{
t = std::min(t, tTop);
}
// implicit y + w = -.5f, Bottom base
float tBase = (-.5f - p.y - p[3]) / (d[3] + d.y);
auto pBase = p + tBase * d;
if (
tBase > 0 &&
pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius + pBase[3]/2.f &&
pBase[3] >= -.5f && pBase[3] <= .5f
)
{
t = std::min(t, tBase);
}
if (t == FINF)
{
return glm::vec4(0.f);
} else {
isHit = true;
return p + t*d;
}
}
glm::vec4 intersectCube (
glm::vec4 p,
glm::vec4 d,
const RenderShapeData& shape,
bool &isHit)
{
isHit = false;
// float t = FINF;
float apothem = .5f;
// start with x-dir
float tmin = (-apothem - p.x) / d.x;
float tmax = (apothem - p.x) / d.x;
// see if it hits top or bottom
if (tmin > tmax)
{
std::swap(tmin, tmax);
}
// y-dir
float tymin = (-apothem - p.y) / d.y;
float tymax = (apothem - p.y) / d.y;
if (tymin > tymax)
{
std::swap(tymin, tymax);
}
if ((tmin > tymax) || (tymin > tmax))
{ // no hit
return glm::vec4(0.f);
}
if (tymin > tmin)
{
tmin = tymin;
}
if (tymax < tmax)
{
tmax = tymax;
}
// z-dir
float tzmin = (-apothem - p.z) / d.z;
float tzmax = (apothem - p.z) / d.z;
if (tzmin > tzmax)
{
std::swap(tzmin, tzmax);
}
if ((tmin > tzmax) || (tzmin > tmax))
{ // no hit
return glm::vec4(0.f);
}
if (tzmin > tmin)
{
tmin = tzmin;
}
if (tzmax < tmax)
{
tmax = tzmax;
}
// w-dir
float twmin = (-apothem - p[3]) / d[3];
float twmax = (apothem - p[3]) / d[3];
if (twmin > twmax)
{
std::swap(twmin, twmax);
}
if ((tmin > twmax) || (twmin > tmax))
{ // no hit
return glm::vec4(0.f);
}
if (twmin > tmin)
{
tmin = twmin;
}
if (twmax < tmax)
{
tmax = twmax;
}
if (tmin <= 0 && tmax <= 0) // both behind camera
{
return glm::vec4(0.f);
} else if (tmin > 0) // tmin in front of camera
{
isHit = true;
return p + tmin*d;
} else if (tmin <= 0) // tmax in front of camera
{
isHit = true;
return p + tmax*d;
}
return glm::vec4(0.f);
}
/**
* @brief Finds the intersection point of a ray and a shape.
* The ray and shape should be in the same space for this function to work properly.
* This function does not check if the intersection point is in front of the camera.
* @param p, the point of the ray
* @param d, the direction of the space
* @param shape, the shape to be intersected with the ray
* @return the intersection point as a vec4. If there exists no intersection, returns vec4(0.f).
*/
glm::vec4 RayTracer::findIntersection(
glm::vec4 p,
glm::vec4 d,
const RenderShapeData& shape,
bool &isHit
)
{
switch(shape.primitive.type) {
case PrimitiveType::PRIMITIVE_SPHERE:
return intersectCircle(p, d, shape, isHit);
case PrimitiveType::PRIMITIVE_CONE:
return intersectCone(p, d, shape, isHit);
case PrimitiveType::PRIMITIVE_CYLINDER:
return intersectCylinder(p, d, shape, isHit);
case PrimitiveType::PRIMITIVE_CUBE:
return intersectCube(p, d, shape, isHit);
case PrimitiveType::PRIMITIVE_MESH:
break;
}
return glm::vec4(0.f);
}
|