-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
executable file
·507 lines (411 loc) · 10.7 KB
/
util.cpp
File metadata and controls
executable file
·507 lines (411 loc) · 10.7 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/***********************************************************
Starter code for Assignment 3
Implementations of util.h
***********************************************************/
#include "util.h"
Point3D::Point3D() {
m_data[0] = 0.0;
m_data[1] = 0.0;
m_data[2] = 0.0;
}
Point3D::Point3D(double x, double y, double z) {
m_data[0] = x;
m_data[1] = y;
m_data[2] = z;
}
Point3D::Point3D(const Point3D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
}
Point3D& Point3D::operator =(const Point3D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
return *this;
}
double& Point3D::operator[](int i) {
return m_data[i];
}
double Point3D::operator[](int i) const {
return m_data[i];
}
Vector3D Point3D::ToVector() {
return Vector3D(m_data[0], m_data[1], m_data[2]);
}
Vector3D::Vector3D() {
m_data[0] = 0.0;
m_data[1] = 0.0;
m_data[2] = 0.0;
}
Vector3D::Vector3D(double x, double y, double z) {
m_data[0] = x;
m_data[1] = y;
m_data[2] = z;
}
Vector3D::Vector3D(const Vector3D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
}
Vector3D& Vector3D::operator =(const Vector3D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
return *this;
}
double& Vector3D::operator[](int i) {
return m_data[i];
}
double Vector3D::operator[](int i) const {
return m_data[i];
}
double Vector3D::length() const
{
return sqrt(dot(*this));
}
double Vector3D::normalize() {
double denom = 1.0;
double x = (m_data[0] > 0.0) ? m_data[0] : -m_data[0];
double y = (m_data[1] > 0.0) ? m_data[1] : -m_data[1];
double z = (m_data[2] > 0.0) ? m_data[2] : -m_data[2];
if(x > y) {
if(x > z) {
if(1.0 + x > 1.0) {
y = y / x;
z = z / x;
denom = 1.0 / (x * sqrt(1.0 + y*y + z*z));
}
} else { /* z > x > y */
if(1.0 + z > 1.0) {
y = y / z;
x = x / z;
denom = 1.0 / (z * sqrt(1.0 + y*y + x*x));
}
}
} else {
if(y > z) {
if(1.0 + y > 1.0) {
z = z / y;
x = x / y;
denom = 1.0 / (y * sqrt(1.0 + z*z + x*x));
}
} else { /* x < y < z */
if(1.0 + z > 1.0) {
y = y / z;
x = x / z;
denom = 1.0 / (z * sqrt(1.0 + y*y + x*x));
}
}
}
if(1.0 + x + y + z > 1.0) {
m_data[0] *= denom;
m_data[1] *= denom;
m_data[2] *= denom;
return 1.0 / denom;
}
return 0.0;
}
double Vector3D::dot(const Vector3D& other) const
{
return m_data[0]*other.m_data[0] +
m_data[1]*other.m_data[1] +
m_data[2]*other.m_data[2];
}
Vector3D Vector3D::cross(const Vector3D& other) const
{
return Vector3D(
m_data[1]*other[2] - m_data[2]*other[1],
m_data[2]*other[0] - m_data[0]*other[2],
m_data[0]*other[1] - m_data[1]*other[0]);
}
Vector3D operator *(double s, const Vector3D& v)
{
return Vector3D(s*v[0], s*v[1], s*v[2]);
}
Vector3D operator +(const Vector3D& u, const Vector3D& v)
{
return Vector3D(u[0]+v[0], u[1]+v[1], u[2]+v[2]);
}
Point3D operator +(const Point3D& u, const Vector3D& v)
{
return Point3D(u[0]+v[0], u[1]+v[1], u[2]+v[2]);
}
Vector3D operator -(const Point3D& u, const Point3D& v)
{
return Vector3D(u[0]-v[0], u[1]-v[1], u[2]-v[2]);
}
Vector3D operator -(const Vector3D& u, const Vector3D& v)
{
return Vector3D(u[0]-v[0], u[1]-v[1], u[2]-v[2]);
}
Vector3D operator -(const Vector3D& u)
{
return Vector3D(-u[0], -u[1], -u[2]);
}
Point3D operator -(const Point3D& u, const Vector3D& v)
{
return Point3D(u[0]-v[0], u[1]-v[1], u[2]-v[2]);
}
Vector3D cross(const Vector3D& u, const Vector3D& v)
{
return u.cross(v);
}
std::ostream& operator <<(std::ostream& s, const Point3D& p)
{
return s << "p(" << p[0] << "," << p[1] << "," << p[2] << ")";
}
std::ostream& operator <<(std::ostream& s, const Vector3D& v)
{
return s << "v(" << v[0] << "," << v[1] << "," << v[2] << ")";
}
Color::Color() {
m_data[0] = 0.0;
m_data[1] = 0.0;
m_data[2] = 0.0;
}
Color::Color(double r, double g, double b) {
m_data[0] = r;
m_data[1] = g;
m_data[2] = b;
}
Color::Color(const Color& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
}
Color& Color::operator =(const Color& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
return *this;
}
Color Color::operator *(const Color& other) {
return Color(m_data[0]*other.m_data[0],
m_data[1]*other.m_data[1],
m_data[2]*other.m_data[2]);
}
double& Color::operator[](int i) {
return m_data[i];
}
double Color::operator[](int i) const {
return m_data[i];
}
void Color::clamp() {
for (int i = 0; i < 3; i++) {
if (std::isnan(m_data[i])) m_data[i] = 0.0;
if (m_data[i] > 1.0) m_data[i] = 1.0;
if (m_data[i] < 0.0) m_data[i] = 0.0;
}
}
Color operator *(double s, const Color& c)
{
return Color(s*c[0], s*c[1], s*c[2]);
}
Color operator +(const Color& u, const Color& v)
{
return Color(u[0]+v[0], u[1]+v[1], u[2]+v[2]);
}
std::ostream& operator <<(std::ostream& s, const Color& c)
{
return s << "c(" << c[0] << "," << c[1] << "," << c[2] << ")";
}
Vector4D::Vector4D() {
m_data[0] = 0.0;
m_data[1] = 0.0;
m_data[2] = 0.0;
m_data[3] = 0.0;
}
Vector4D::Vector4D(double w, double x, double y, double z) {
m_data[0] = w;
m_data[1] = x;
m_data[2] = y;
m_data[3] = z;
}
Vector4D::Vector4D(const Vector4D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
m_data[3] = other.m_data[3];
}
Vector4D& Vector4D::operator =(const Vector4D& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
m_data[3] = other.m_data[3];
return *this;
}
double& Vector4D::operator[](int i) {
return m_data[i];
}
double Vector4D::operator[](int i) const {
return m_data[i];
}
Matrix4x4::Matrix4x4() {
for (int i = 0; i < 16; i++)
m_data[i] = 0.0;
m_data[0] = 1.0;
m_data[5] = 1.0;
m_data[10] = 1.0;
m_data[15] = 1.0;
}
Matrix4x4::Matrix4x4(const Matrix4x4& other) {
for (int i = 0; i < 16; i++)
m_data[i] = other.m_data[i];
}
Matrix4x4& Matrix4x4::operator=(const Matrix4x4& other) {
for (int i = 0; i < 16; i++)
m_data[i] = other.m_data[i];
return *this;
}
Vector4D Matrix4x4::getRow(int row) const {
return Vector4D(m_data[4*row], m_data[4*row+1], m_data[4*row+2],
m_data[4*row+3]);
}
double* Matrix4x4::getRow(int row) {
return (double*)m_data + 4*row;
}
Vector4D Matrix4x4::getColumn(int col) const {
return Vector4D(m_data[col], m_data[4+col], m_data[8+col],
m_data[12+col]);
}
Vector4D Matrix4x4::operator[](int row) const {
return getRow(row);
}
double* Matrix4x4::operator[](int row) {
return getRow(row);
}
Matrix4x4 Matrix4x4::transpose() const {
Matrix4x4 M;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
M[i][j] = (*this)[j][i];
}
}
return M;
}
Matrix4x4 operator *(const Matrix4x4& a, const Matrix4x4& b) {
Matrix4x4 ret;
for(size_t i = 0; i < 4; ++i) {
Vector4D row = a.getRow(i);
for(size_t j = 0; j < 4; ++j) {
ret[i][j] = row[0] * b[0][j] + row[1] * b[1][j] +
row[2] * b[2][j] + row[3] * b[3][j];
}
}
return ret;
}
Vector3D operator *(const Matrix4x4& M, const Vector3D& v) {
return Vector3D(
v[0] * M[0][0] + v[1] * M[0][1] + v[2] * M[0][2],
v[0] * M[1][0] + v[1] * M[1][1] + v[2] * M[1][2],
v[0] * M[2][0] + v[1] * M[2][1] + v[2] * M[2][2]);
}
Point3D operator *(const Matrix4x4& M, const Point3D& p) {
return Point3D(
p[0] * M[0][0] + p[1] * M[0][1] + p[2] * M[0][2] + M[0][3],
p[0] * M[1][0] + p[1] * M[1][1] + p[2] * M[1][2] + M[1][3],
p[0] * M[2][0] + p[1] * M[2][1] + p[2] * M[2][2] + M[2][3]);
}
Vector3D transNorm(const Matrix4x4& M, const Vector3D& n) {
return Vector3D(
n[0] * M[0][0] + n[1] * M[1][0] + n[2] * M[2][0],
n[0] * M[0][1] + n[1] * M[1][1] + n[2] * M[2][1],
n[0] * M[0][2] + n[1] * M[1][2] + n[2] * M[2][2]);
}
std::ostream& operator <<(std::ostream& os, const Matrix4x4& M) {
return os << "[" << M[0][0] << " " << M[0][1] << " "
<< M[0][2] << " " << M[0][3] << "]" << std::endl
<< "[" << M[1][0] << " " << M[1][1] << " "
<< M[1][2] << " " << M[1][3] << "]" << std::endl
<< "[" << M[2][0] << " " << M[2][1] << " "
<< M[2][2] << " " << M[2][3] << "]" << std::endl
<< "[" << M[3][0] << " " << M[3][1] << " "
<< M[3][2] << " " << M[3][3] << "]";
}
int SolveQuadratic(double a, double b, double c, double *intersections) {
double thing = (b*b) - (4.0 * a * c);
if (thing < 0)
return 0;
if (thing > -QUADRATIC_EPSILON && thing < QUADRATIC_EPSILON) {
intersections[0] = (-b) / (2.0 * a);
return 1;
}
intersections[0] = (-b + sqrt(thing))/ (2.0 * a);
intersections[1] = (-b - sqrt(thing))/ (2.0 * a);
return 2;
}
/*
* Light direction is from the intersection point to the light
* Eye direction is from the eye to the point.
*/
Color Phong(Vector3D& _lightDirection, Vector3D& _normal, Vector3D _eyeDirection,
Material* mat, Color& col_ambient, Color& col_diffuse, Color& col_specular) {
_lightDirection.normalize();
double diffuseIntensity =
std::max(0.0, _normal.dot(_lightDirection));
Vector3D mirrorRay = (
(2.0 * _normal.dot(_lightDirection) * _normal)
- _lightDirection
);
mirrorRay.normalize();
// Just in case
_eyeDirection.normalize();
double specularIntensity = pow(
std::max(0.0, _eyeDirection.dot(-1 * mirrorRay)),
mat -> specular_exp
);
Color diffuseColor = diffuseIntensity * col_diffuse;
diffuseColor.clamp();
Color specularColor = specularIntensity * col_specular;
specularColor.clamp();
Color c(
(col_ambient * mat -> ambient) +
(diffuseColor * mat -> diffuse) +
(specularColor * mat -> specular)
);
c.clamp();
return c;
}
int Vector3D::isZero() const {
return (this -> m_data[0] < EPSILON &&
this -> m_data[1] < EPSILON &&
this -> m_data[2] < EPSILON);
}
Vector3D GetArbitraryTangentFromNorm(Vector3D& n) {
Vector3D test(1.0, 1.0, 1.0);
Vector3D crossRes = n.cross(test);
if (crossRes.isZero()) {
test[0] = 0.0;
crossRes = n.cross(test);
}
crossRes.normalize();
return crossRes;
}
unsigned char* readBMP(char* filename) {
/*
* Note that this is taken from StackOverflow @9296059 as a helper class for
* loading bitmaps.
*
*/
FILE* f = fopen(filename, "rb");
unsigned char info[122];
fread(info, sizeof(unsigned char), 122, f); // Read 122-byte header.
// This might be sketchy since the header size could change.
// TODO make this static.
int width = *(int*)&info[18];
int height = *(int*)&info[22];
int size = 3 * width * height; // 24 bit color depth
unsigned char* data = new unsigned char[size];
fread(data, sizeof(unsigned char), size, f);
fclose(f);
/*
* Convert from BGR to RGB?
*/
for (int i = 0; i < size; i += 3) {
unsigned char tmp = data[i];
data[i] = data[i+2];
data[i + 2] = tmp;
}
return data;
}