-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primitives.cpp
341 lines (281 loc) · 8.42 KB
/
Primitives.cpp
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
340
341
#include "precomp.h"
Material::Material(vec4 color, MaterialType type)
{
this->color = color;
this->type = type;
}
Primitive::Primitive(Material* material)
{
this->material = material;
}
// -------------------- SPHERE ------------------------------------
Sphere::Sphere(Material* material, vec3 position, float radius) : Primitive(material)
{
this->position = position;
this->radius = radius;
this->radius2 = radius * radius;
this->boundingBox = new BoundingBox(this->position - this->radius, this->position + this->radius);
}
void Sphere::intersect(Ray* ray)
{
vec3 c = this->position - ray->origin;
float t = dot(c, ray->direction);
if (t < 0) return;
vec3 q = c - t * ray->direction;
float p2 = dot(q, q);
if (p2 > this->radius2) return;
t -= sqrt(this->radius2 - p2);
if (t < ray->t && t >= EPSILON)
{
ray->t = t;
ray->intersectedObjectId = this->id;
}
}
vec3 Sphere::getNormal(vec3 point)
{
return normalize(point - this->position);
}
void Sphere::translate(vec3 vector)
{
this->position += vector;
this->boundingBox->translate(vector);
}
// -------------------- TRIANGLE ------------------------------------
Triangle::Triangle(Material* material, vec3 a, vec3 b, vec3 c) : Primitive(material)
{
this->a = a;
this->b = b;
this->c = c;
float minX = MIN(MIN(this->a.x, this->b.x), this->c.x);
float minY = MIN(MIN(this->a.y, this->b.y), this->c.y);
float minZ = MIN(MIN(this->a.z, this->b.z), this->c.z);
float maxX = MAX(MAX(this->a.x, this->b.x), this->c.x);
float maxY = MAX(MAX(this->a.y, this->b.y), this->c.y);
float maxZ = MAX(MAX(this->a.z, this->b.z), this->c.z);
this->boundingBox = new BoundingBox(vec3(minX, minY, minZ), vec3(maxX, maxY, maxZ));
this->normal = normalize(
cross(this->a - this->b, this->b - this->c)
);
}
void Triangle::intersect(Ray* ray)
{
float t, u, v;
vec3 ab = this->b - this->a;
vec3 ac = this->c - this->a;
vec3 pvec = ray->direction.cross(ac);
float det = ab.dot(pvec);
float invDet = 1 / det;
vec3 tvec = ray->origin - a;
u = tvec.dot(pvec) * invDet;
if (u < 0 || u > 1) return;
vec3 qvec = tvec.cross(ab);
v = ray->direction.dot(qvec) * invDet;
if (v < 0 || u + v > 1) return;
t = ac.dot(qvec) * invDet;
if (t < ray->t && t >= EPSILON)
{
ray->t = t;
ray->intersectedObjectId = this->id;
}
}
vec3 Triangle::getNormal(vec3 point)
{
return this->normal;
}
void Triangle::translate(vec3 vector)
{
this->a += vector;
this->b += vector;
this->c += vector;
this->boundingBox->translate(vector);
}
// -------------------- PLANE ------------------------------------
Plane::Plane(Material* material, vec3 position, vec3 direction, float size) : Primitive(material)
{
this->position = position;
this->direction = direction;
this->boundingBox = new BoundingBox(this->position - size, this->position + size);
}
void Plane::intersect(Ray* ray)
{
float denominator = dot(this->direction, ray->direction);
if (abs(denominator) > EPSILON) {
float t = dot(this->position - ray->origin, this->direction) / denominator;
if (t < ray->t && t >= EPSILON)
{
ray->t = t;
ray->intersectedObjectId = this->id;
}
}
}
vec3 Plane::getNormal(vec3 point)
{
return normalize(this->direction);
}
void Plane::translate(vec3 vector)
{
this->position += vector;
this->boundingBox->translate(vector);
}
// -------------------- CYLINDER ------------------------------------
Cylinder::Cylinder(Material* material, vec3 position, vec3 upVector, float radius, float height) : Primitive(material)
{
this->position = position;
this->upVector = upVector;
this->radius = radius;
this->height = height;
float boundingRadius = MAX(this->radius, this->height);
vec3 size;
size.x = upVector.x * height + 2 * radius * sqrt(1 - upVector.x * upVector.x);
size.y = upVector.y * height + 2 * radius * sqrt(1 - upVector.y * upVector.y);
size.z = upVector.z * height + 2 * radius * sqrt(1 - upVector.z * upVector.z);
this->boundingBox = new BoundingBox(this->position - size, this->position + size);
}
void Cylinder::intersect(Ray* ray)
{
vector<float> points;
vec3 alpha = upVector * ray->direction.dot(upVector);
vec3 deltaPosition = (ray->origin - this->position);
vec3 beta = upVector * deltaPosition.dot(upVector);
vec3 center2 = this->position + upVector * height;
float a = (ray->direction - alpha).sqrLentgh();
float b = 2 * ((ray->direction - alpha).dot(deltaPosition - beta));
float c = (deltaPosition - beta).sqrLentgh() - radius*radius;
float discriminant = b * b - 4 * a * c;
if (discriminant < 0) { return; }
else
{
discriminant = sqrt(discriminant);
float t1 = ((-1 * b) + discriminant) / (2 * a);
float t2 = ((-1 * b) - discriminant) / (2 * a);
if (t1 >= 0)
{
if (upVector.dot((ray->origin - this->position) + ray->direction * t1) > 0 && upVector.dot((ray->origin - center2) + ray->direction * t1) < 0)
{
points.push_back(t1);
}
}
if (t2 >= 0)
{
if (upVector.dot((ray->origin - this->position) + ray->direction * t2) > 0 && upVector.dot((ray->origin - center2) + ray->direction * t2) < 0)
{
points.push_back(t2);
}
}
}
float denominator = ray->direction.dot(upVector);
if (denominator > EPSILON)
{
vec3 co = this->position - ray->origin;
float t3 = co.dot(upVector) / denominator;
if (t3 > 0 && (ray->direction * t3 - co).sqrLentgh() <= radius*radius)
{
points.push_back(t3);
}
}
else if (denominator < EPSILON)
{
vec3 co2 = center2 - ray->origin;
float t4 = co2.dot(upVector) / denominator;
if (t4 > 0 && (ray->direction * t4 - co2).sqrLentgh() <= radius*radius)
{
points.push_back(t4);
}
}
float minT = INFINITY;
bool intersects = false;
for (int i = 0; i<points.size(); i++)
{
if (minT > points[i] && points[i] >= EPSILON)
{
minT = points[i];
intersects = true;
}
}
if (intersects && minT < ray->t)
{
ray->t = minT / ray->direction.length();
ray->intersectedObjectId = this->id;
}
}
vec3 Cylinder::getNormal(vec3 point)
{
vec3 co = point - this->position;
vec3 co2 = co - upVector * height;
if (co.sqrLentgh() <= radius * radius)
{
return this->upVector;
}
if (co2.sqrLentgh() <= radius * radius)
{
return this->upVector;
}
return normalize(co - co.project(upVector));
}
void Cylinder::translate(vec3 vector)
{
this->position += vector;
this->boundingBox->translate(vector);
}
// -------------------- TORUS ------------------------------------
Torus::Torus(Material* material, float R, float r, vec3 position, vec3 axis) : Primitive(material)
{
this->position = position;
this->R = R;
this->r = r;
this->axis = normalize(axis);
this->boundingBox = new BoundingBox(this->position - (this->R + this->r), this->position + (this->R + this->r));
}
void Torus::intersect(Ray* ray)
{
vec3 centerToRayOrigin = ray->origin - position;
long double centerToRayOriginDotDirectionSquared = dot(centerToRayOrigin, centerToRayOrigin);
long double r2 = r * r;
long double R2 = R * R;
long double axisDotCenterToRayOrigin = dot(axis, centerToRayOrigin);
long double axisDotRayDirection = dot(axis, ray->direction);
long double a = 1 - axisDotRayDirection * axisDotRayDirection;
long double b = 2 * (dot(centerToRayOrigin, ray->direction) - axisDotCenterToRayOrigin * axisDotRayDirection);
long double c = centerToRayOriginDotDirectionSquared - axisDotCenterToRayOrigin * axisDotCenterToRayOrigin;
long double d = centerToRayOriginDotDirectionSquared + R2 - r2;
// Solve quartic equation with coefficients A, B, C, D and E
long double A = 1;
long double B = 4 * dot(ray->direction, centerToRayOrigin);
long double C = 2 * d + B * B * 0.25f - 4 * R2 * a;
long double D = B * d - 4 * R2 * b;
long double E = d * d - 4 * R2 * c;
// Maximum number of roots is 4
QuarticEquation equation(A, B, C, D, E);
const int maxRootsCount = 4;
double roots[maxRootsCount] = { -1.0, -1.0, -1.0, -1.0 };
int rootsCount = equation.Solve(roots);
if (rootsCount == 0) { return; }
// Find closest to zero positive solution
float closestRoot = INFINITY;
for (int i = 0; i < maxRootsCount; ++i)
{
if (roots[i] >= EPSILON && roots[i] < closestRoot)
{
closestRoot = roots[i];
}
}
if (closestRoot < ray->t)
{
ray->t = closestRoot;
ray->intersectedObjectId = this->id;
}
}
vec3 Torus::getNormal(vec3 point)
{
vec3 P = (point - this->position);
float A = (this->R - this->r) / 2 + this->r;
float alpha = A / (sqrtf(P.x * P.x + P.y * P.y));
vec3 Q = vec3(alpha * P.x, alpha * P.y, 0);
vec3 N = normalize(P - Q);
return N;
}
void Torus::translate(vec3 vector)
{
this->position += vector;
this->boundingBox->translate(vector);
}