-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathGeometry.h
395 lines (343 loc) · 10.5 KB
/
Geometry.h
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
//--------------------------------------------------------------------------//
// Geometry.h - geometric objects needed for the ray tracer
//--------------------------------------------------------------------------//
#ifndef __GEOMETRY__
#define __GEOMETRY__
#include <vector>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define EPSILON 0.001f
double TIME(void);
class Point3;
class BoundingBox;
class Material;
class DirectionalLight;
class Ray;
class Intersection;
class Triangle;
class TriangleMesh;
class Scene;
class Camera;
class LBVH;
typedef LBVH* LBVHptr;
using namespace std;
//--------------------------------------------------------------------------//
// Point3 - a point class made of 3 floats (also used for colors)
//--------------------------------------------------------------------------//
class Point3
{
public:
float A[3];
public:
// CONSTRUCTORS
Point3() { A[0]=0.0f; A[1]=0.0f; A[2]=0.0f; }
Point3(const float *X) { A[0]=X[0]; A[1]=X[1]; A[2]=X[2]; }
Point3(float x, float y, float z) { A[0]=x; A[1]=y; A[2]=z; }
Point3(const Point3 &P) { A[0]=P[0]; A[1]=P[1]; A[2]=P[2]; }
//
~Point3() {}
// SETTERS
void set(float x, float y, float z) { A[0]=x; A[1]=y; A[2]=z; }
void set(Point3 &P) { A[0]=P[0]; A[1]=P[1]; A[2]=P[2]; }
// MEMBER ACCESS FUNCTIONS
float &operator[](int i) { return A[i]; }
const float &operator[](int i) const { return A[i]; }
float &x(void) { return A[0]; }
float &y(void) { return A[1]; }
float &z(void) { return A[2]; }
// OPERATORS
void operator =(const Point3 &P) { A[0]=P[0]; A[1]=P[1]; A[2]=P[2]; }
void operator +=(const Point3 &P) { A[0]+=P[0]; A[1]+=P[1]; A[2]+=P[2]; }
const Point3 operator + (const Point3 &P) const { Point3 S(*this); S+=P; return S; }
void operator -=(const Point3 &P) { A[0]-=P[0]; A[1]-=P[1]; A[2]-=P[2]; }
const Point3 operator - (const Point3 &P) const { Point3 S(*this); S-=P; return S; }
const Point3 operator - (void) const { return Point3(-A[0], -A[1], -A[2]); }
void operator *=(float s) { A[0]*=s; A[1]*=s; A[2]*=s; }
const Point3 operator * (const float s) const { Point3 P(*this); P*=s; return P; }
friend const Point3 operator *(float s, const Point3 &P) { Point3 Q(P); Q*=s; return Q; }
const Point3 operator * (const Point3 &P) const { Point3 Q(*this); Q*=P; return Q; }
void operator *=(const Point3 &P) { A[0]*=P[0]; A[1]*=P[1]; A[2]*=P[2]; }
// OTHER FUNCTIONS
float length2(void) const {
return A[0]*A[0] + A[1]*A[1] + A[2]*A[2];
}
float length(void) const {
return sqrtf(A[0]*A[0] + A[1]*A[1] + A[2]*A[2]);
}
void normalize(void) {
float d = length();
if (d > 0.0f) (*this) *= (1.0f/d);
}
float dot(const Point3 &P) const {
return( A[0]*P[0] + A[1]*P[1] + A[2]*P[2] );
}
const Point3 cross(const Point3 &P) const {
return Point3( A[1]*P[2]-A[2]*P[1], A[2]*P[0]-A[0]*P[2], A[0]*P[1]-A[1]*P[0] );
}
const Point3 reflect(const Point3 &N) const {
float NdotD = 2.0f * (N.dot(*this));
return Point3( N[0]*NdotD - A[0], N[1]*NdotD - A[1], N[2]*NdotD - A[2] );
}
friend const Point3 minComponents(const Point3 &A, const Point3 &B) {
return Point3( MIN(A[0],B[0]), MIN(A[1],B[1]), MIN(A[2],B[2]) );
}
friend const Point3 maxComponents(const Point3 &A, const Point3 &B) {
return Point3( MAX(A[0],B[0]), MAX(A[1],B[1]), MAX(A[2],B[2]) );
}
void clamp(float min, float max) {
A[0] = MIN(max, MIN(max, A[0]));
A[1] = MIN(max, MIN(max, A[1]));
A[2] = MIN(max, MIN(max, A[2]));
}
void print(void) {
printf("(%0.4f, %0.4f, %0.4f)", A[0], A[1], A[2]);
}
};
//--------------------------------------------------------------------------//
// BoundingBox - a floating point bounding box
//--------------------------------------------------------------------------//
class BoundingBox {
public:
Point3 min, max;
BoundingBox() {init();}
~BoundingBox() {}
void init(void) {
min.set(FLT_MAX, FLT_MAX, FLT_MAX);
max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
}
void set(Point3 &minPoint, Point3 &maxPoint) {
min = minPoint;
max = maxPoint;
}
void expand(float x) {
min -= Point3(x,x,x);
max += Point3(x,x,x);
}
void expand(const Point3 &P) {
min = minComponents(min,P);
max = maxComponents(max,P);
}
void expand(const BoundingBox &BB) {
min = minComponents(min, BB.min);
max = maxComponents(max, BB.max);
}
const int getMajorAxis(void) const {
Point3 P=max-min;
if (P[0]>=P[1] && P[0]>=P[2]) return 0;
if (P[1]>P[2]) return 1;
else return 2;
}
void print(void) {
min.print();
printf(" - ");
max.print();
}
};
//--------------------------------------------------------------------------//
// Material - material properties
//--------------------------------------------------------------------------//
class Material
{
public:
Point3 diffuse, specular;
float exponent;
Material() {
diffuse.set(1,1,1);
specular.set(0,0,0);
exponent = 10.0f;
}
~Material() {}
};
//--------------------------------------------------------------------------//
// DirectionalLight
//--------------------------------------------------------------------------//
class DirectionalLight
{
public:
Point3 direction;
Point3 color;
void set(const Point3 &d, const Point3 &c) {
color=c;
direction=d;
direction.normalize();
}
};
//--------------------------------------------------------------------------//
// Ray - a ray (origin and direction)
//--------------------------------------------------------------------------//
class Ray
{
public:
Point3 origin;
Point3 direction;
void set(const Point3 &o, const Point3 &d) {
origin=o;
direction=d;
direction.normalize();
}
};
//--------------------------------------------------------------------------//
// Intersection - encapsulates an intersection
//--------------------------------------------------------------------------//
class Intersection
{
public:
float tval;
Point3 location;
Point3 normal;
Material *material;
void operator =(Intersection &I) {
tval = I.tval;
location = I.location;
normal = I.normal;
material = I.material;
}
void set(float t, Point3 &loc, Point3 &norm, Material *mat) {
tval = t;
location = loc;
normal = norm;
material = mat;
}
const Point3 evaluateLighting(const Point3 &D, DirectionalLight &L) {
Point3 val;
float NdotL = normal.dot(L.direction);
if (NdotL > 0.0f) {
Point3 diffuse(L.color);
diffuse *= material->diffuse;
diffuse *= NdotL;
val += diffuse;
Point3 R = D.reflect(normal);
float RdotL = R.dot(L.direction);
if (RdotL > 0.0f) {
Point3 specular(L.color);
specular *= material->specular;
specular *= powf(RdotL, material->exponent);
val += specular;
}
}
return val;
}
};
//--------------------------------------------------------------------------//
// Triangle - a triangle used in conjunction with TriangleMesh
//--------------------------------------------------------------------------//
class Triangle
{
public:
int v0, v1, v2;
public:
Triangle() {}
~Triangle() {};
void setVertices(int vert0, int vert1, int vert2) { v0=vert0; v1=vert1; v2=vert2; }
void getNormal(Point3 &N, float a, float b, float c, TriangleMesh &mesh);
Point3 &vertex(int i, TriangleMesh &mesh);
void getBounds(BoundingBox &BB, TriangleMesh &mesh);
void operator =(const Triangle &T) { v0=T.v0; v1=T.v1; v2=T.v2; }
void print(void) { printf("[%d, %d, %d]", v0,v1,v2); }
bool intersectRay(Ray &ray, TriangleMesh &mesh, Intersection *intersection, float maxT);
};
//--------------------------------------------------------------------------//
// TriangleMesh - a goup of Triangles
//--------------------------------------------------------------------------//
class TriangleMesh
{
public:
Material material;
vector<Point3> vertices;
vector<Point3> normals;
//vector<Point3> texCoords; // not supporting textures
vector<Triangle> triangles;
TriangleMesh() {}
~TriangleMesh() {}
bool intersectTriangle(int i, Ray &ray, Intersection *intersection, float maxT) {
return triangles[i].intersectRay(ray, *this, intersection, maxT);
}
void getBounds(BoundingBox &BB) {
for (int i=(int)vertices.size()-1; i>=0; i--) BB.expand(vertices[i]);
}
void getTriangleBounds(BoundingBox &BB, int i) {
if (i < (int)triangles.size()) triangles[i].getBounds(BB, *this);
else BB.init();
}
int size(void) {
return (int)triangles.size();
}
bool hasNormals(void) {
return (normals.size() > 0);
}
void scale(Point3 &S) {
for (int i=(int)vertices.size()-1; i>=0; i--) vertices[i]*=S;
}
void translate(Point3 &T) {
for (int i=(int)vertices.size()-1; i>=0; i--) vertices[i]+=T;
}
};
//--------------------------------------------------------------------------//
// Scene
//--------------------------------------------------------------------------//
class Scene
{
public:
Point3 backgroundColor;
Point3 ambientLight;
vector<DirectionalLight*> lights;
vector<LBVHptr> meshes;
LBVH *rootBoundingVolume;
Scene() {
rootBoundingVolume=NULL;
backgroundColor.set(0,0,0);
ambientLight.set(0,0,0);
}
~Scene();
};
//--------------------------------------------------------------------------//
// Camera
//--------------------------------------------------------------------------//
class Camera
{
public:
Point3 lookFrom;
Point3 lookAt;
Point3 viewUp;
float fieldOfView;
int imageWidth, imageHeight;
Scene *scene;
Camera() {
lookFrom = Point3(0,0,10);
lookAt = Point3(0,0,0);
viewUp = Point3(0,1,0);
fieldOfView = 0.5f;
imageWidth = 256;
imageHeight = 256;
}
~Camera() {}
Point3 traceRay(Ray &ray);
void captureImage(Scene *scene, char *imageName);
};
//--------------------------------------------------------------------------//
// Triangle function implementations
//--------------------------------------------------------------------------//
inline Point3& Triangle::vertex(int i, TriangleMesh &mesh)
{
return mesh.vertices[(&v0)[i]];
}
//--------------------------------------------------------------------------//
inline void Triangle::getNormal(Point3 &N, float a, float b, float c, TriangleMesh &mesh)
{
if (mesh.hasNormals()) {
N = (a*mesh.normals[v0]) + (b*mesh.normals[v1]) + (c*mesh.normals[v2]);
N.normalize();
} else {
N = (mesh.vertices[v1]-mesh.vertices[v0]).cross(mesh.vertices[v2]-mesh.vertices[v0]);
N.normalize();
}
}
//--------------------------------------------------------------------------//
inline void Triangle::getBounds(BoundingBox &BB, TriangleMesh &mesh)
{
BB.set(mesh.vertices[v0], mesh.vertices[v0]);
BB.expand(mesh.vertices[v1]);
BB.expand(mesh.vertices[v2]);
}
//--------------------------------------------------------------------------//
#endif