-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother-reference.cpp
366 lines (332 loc) · 10.7 KB
/
other-reference.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
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
/*
// Copyright (c) www.scratchapixel.com August, 2007
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// c++ -o render main.cpp -O3 -Wall
*/
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cassert>
typedef struct Normal
{
Normal() : x(0), y(0), z(0) {}
Normal(float xx) : x(xx), y(xx), z(xx) {}
Normal(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
Normal operator * (const float &r) const
{ return Normal(x*r, y*r, z*r); }
Normal operator - () const
{ return Normal(-x, -y, -z); }
float x, y, z;
};
typedef struct Vector
{
Vector() : x(0), y(0), z(0) {}
Vector(float xx) : x(xx), y(xx), z(xx) {}
Vector(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
Vector(const Normal &n) : x(n.x), y(n.y), z(n.z) {}
float length() const { return sqrtf(x*x+y*y+z*z); }
float dot(const Vector &v) const { return x*v.x+y*v.y+z*v.z; }
Vector operator * (const float &r) const
{ return Vector(x*r, y*r, z*r); }
float x, y, z;
Vector operator - (const Vector &v) const
{ return Vector(x - v.x, y - v.y, z - v.z); }
Vector operator + (const Vector &v) const
{ return Vector(x + v.x, y + v.y, z + v.z); }
Vector operator - () const
{ return Vector(-x, -y, -z); }
};
typedef struct Point
{
Point() : x(0), y(0), z(0) {}
Point(float xx) : x(xx), y(xx), z(xx) {}
Point(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
Vector operator - (const Point &p) const
{ return Vector(x - p.x, y - p.y, z - p.z); }
Point operator - (const Vector &v) const
{ return Point(x - v.x, y - v.y, z - v.z); }
Point operator + (const Vector &v) const
{ return Point(x+v.x, y+v.y, z+v.z); }
float x, y, z;
};
void Normalize(Vector *v)
{
float len2, lenInv;
len2 = v->x*v->x + v->y*v->y + v->z*v->z;
if (len2) {
lenInv = 1.f/sqrtf(len2);
v->x *= lenInv;
v->y *= lenInv;
v->z *= lenInv;
}
}
typedef struct Color
{
Color() : r(0), g(0), b(0) {}
Color(float rr) : r(rr), g(rr), b(rr) {}
Color(float rr, float gg, float bb) : r(rr), g(gg), b(bb) {}
Color operator * (const float &f) const { return Color(r * f, g * f, b * f); }
Color operator + (const Color &c) const
{ return Color(r + c.r, g + c.g, b + c.b); }
Color operator * (const Color &c) const
{ return Color(r * c.r, g * c.g, b * c.b); }
Color& operator *= (const Color &c)
{ r *= c.r, g *= c.g, b *= c.b; return *this;}
Color& operator *= (const float &f)
{ r *= f, g *= f, b *= f; return *this;}
float r, g, b;
};
typedef struct Ray
{
Vector direction;
Point origin;
};
typedef enum MATERIAL_TYPE { MATTE, DIFFUSE, GLASS };
typedef struct Object
{
Object(const Point &c, const float &r,
const Color &col = Color(1), MATERIAL_TYPE matType = MATTE,
float l = 0, float ior = 1.3) :
center(c), radius(r), radius2(r*r), color(col), materialType(matType),
isLight(l), indexOfRefraction(ior) {}
Point center;
float radius, radius2;
Color color;
MATERIAL_TYPE materialType;
int isLight;
float indexOfRefraction;
};
typedef struct Light
{
const Object *object;
};
static void computePrimRay(const int &i, const int &j, const int &imageWidth,
const int &imageHeight, const float &frameAspectRatio, Ray *ray)
{
float fov = 45;
float angle = tan(fov * 0.5f * M_PI / 180.0f);
ray->origin = Point(0);
float dx = 2 * frameAspectRatio/(float)imageWidth;
float dy = 2 / (float)imageHeight;
ray->direction.x = angle * ((i + 0.5) * dx - frameAspectRatio);
ray->direction.y = angle * (-(j + 0.5) * dy + 1);
ray->direction.z = 1;
Normalize(&ray->direction);
}
int Intersect(const Object *object, const Ray *ray, float *t)
{
// use geometric method
Vector oc = object->center - ray->origin;
// square distance to center of sphere
float oc2 = oc.dot(oc);
// distance to point on ray closest to sphere center
float tca = oc.dot(ray->direction);
bool outside = oc2 > object->radius2;
if (tca < 0 && outside) return 0;
// square distance from sphere center to closest point on ray
float d2 = oc2 - tca*tca;
// square distance from perpendicular bisector to center
float thc = object->radius2 - d2;
if (thc < 0)
return 0;
if (outside)
*t = tca - sqrtf(thc);
else
*t = tca + sqrtf(thc);
if (*t < 0) return 0;
return 1;
}
#ifdef INFINITY
#undef INFINITY
#endif
#define INFINITY 1e6
#define MAX_DEPTH_LEVEL 3
void snell(
const Vector &incidentRay,
const Normal &surfaceNormal,
const double &n1,
const double &n2,
Vector *reflectionDir,
Vector *refractionDir )
{
float n1n2 = n1 / n2;
float cost1 = incidentRay.dot(surfaceNormal);
float cost2 = sqrt(1.0 - (n1n2 * n1n2) * (1.0 - cost1 * cost1));
*reflectionDir = incidentRay - surfaceNormal * (2 * cost1);
*refractionDir = incidentRay * n1n2 + surfaceNormal * (cost2 - n1n2 * cost1);
}
void fresnel( const float &etai, const float &etat, const float &cosi,
const float &cost, float *Kr )
{
float Rp = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost));
float Rs = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost));
*Kr = ( Rp*Rp + Rs*Rs ) * .5;
}
static Color Trace(
const Ray *ray,
const int &depth,
const Object **objects,
const int &numObjects,
const Light *light,
const Color &bgColor)
{
if (depth > MAX_DEPTH_LEVEL) {
return bgColor;
}
float bias = 0.001;
const Object *object = NULL;
float minDistance = INFINITY;
Point pHit;
Normal nHit;
float t;
for (int k = 0; k < numObjects; k++) {
if (Intersect(objects[k], ray, &t)) {
if (t < minDistance) {
minDistance = t;
object = objects[k];
}
}
}
if (object == NULL)
return bgColor;
pHit = ray->origin + ray->direction * minDistance;
nHit.x = pHit.x - object->center.x;
nHit.y = pHit.y - object->center.y;
nHit.z = pHit.z - object->center.z;
Normalize((Vector*)&nHit);
if (object->materialType == GLASS) {
// compute reflection and refraction direction
Ray reflectionRay, refractionRay;
reflectionRay.origin = pHit + nHit * bias;
refractionRay.origin = pHit - nHit * bias;
snell(ray->direction, -nHit, 1.0, object->indexOfRefraction,
&reflectionRay.direction, &refractionRay.direction);
Normalize(&reflectionRay.direction);
Normalize(&refractionRay.direction);
float cosi = ray->direction.dot(-nHit);
float cost = refractionRay.direction.dot(-nHit);
float Kr;
fresnel(1.0, object->indexOfRefraction, cosi, cost, &Kr);
if (Kr < 0)
Kr = 0;
if (Kr > 1)
Kr = 1;
Color reflectionColor = Trace(&reflectionRay, depth + 1, objects,
numObjects, light, bgColor);
Color refractionColor = Trace(&refractionRay, depth + 1, objects,
numObjects, light, bgColor);
return object->color * refractionColor * (1-Kr) + reflectionColor * Kr;
}
if (object->materialType == MATTE)
return object->color;
Ray shadowRay;
int isInShadow = 0;
shadowRay.origin.x = pHit.x + nHit.x * bias;
shadowRay.origin.y = pHit.y + nHit.y * bias;
shadowRay.origin.z = pHit.z + nHit.z * bias;
shadowRay.direction = light->object->center - pHit;
float len = shadowRay.direction.length();
Normalize(&shadowRay.direction);
float LdotN = shadowRay.direction.dot(nHit);
if (LdotN < 0)
return 0;
Color lightColor = light->object->color;
for (int k = 0; k < numObjects; k++) {
if (Intersect(objects[k], &shadowRay, &t) && !objects[k]->isLight) {
if (objects[k]->materialType == GLASS)
lightColor *= objects[k]->color; // attenuate light color by glass color
else
isInShadow = 1;
break;
}
}
lightColor *= 1.f/(len*len);
return (isInShadow) ? 0 : object->color * lightColor * LdotN;
}
#define MAX_OBJECTS 4
static const Object* createNewObject(const Point &p, const float &r,
const Color &col, MATERIAL_TYPE matType, int isLight, Object **&objects,
int &numObjects)
{
assert(numObjects <= MAX_OBJECTS);
Object *object = new Object(p, r, col, matType, isLight);
objects[numObjects] = object;
numObjects++;
return object;
}
int main (int argc, char * const argv[])
{
static const int imageWidth = 640;
static const int imageHeight = 480;
// scene data (objects/light)
Object **objects = new Object*[MAX_OBJECTS];
int numObjects = 0;
createNewObject(
Point(0, 0, 15), 3, Color(.5, .7, .5), GLASS, 0, objects, numObjects);
createNewObject(
Point(-4.5, 4.5, 17), 1.8, Color(1, .3, .3), DIFFUSE, 0, objects, numObjects);
createNewObject(
Point(2, -2, 19), 2, Color(.3, 1, .3), DIFFUSE, 0, objects, numObjects);
Light light;
light.object = createNewObject(
Point(3, 3, 13), .5, Color(50), MATTE, 1, objects, numObjects);
// main render loop
assert(imageWidth >= imageHeight);
float frameAspectRatio = imageWidth/(float)imageHeight;
Color **pixels;
pixels = new Color*[imageWidth];
Color bgColor(0.5f, 0.62f, 0.78f);
for (int i = 0; i < imageWidth; ++i)
pixels[i] = new Color[imageHeight];
for (int j = 0; j < imageHeight; ++j) {
for (int i = 0; i < imageWidth; ++i) {
// compute primary ray direction
Ray primRay;
computePrimRay(i, j, imageWidth, imageHeight, frameAspectRatio, &primRay);
pixels[i][j] = Trace(&primRay, 0, (const Object**)objects, numObjects,
&light, bgColor);
// clamp
if (pixels[i][j].r > 1.f) pixels[i][j].r = 1.f;
if (pixels[i][j].g > 1.f) pixels[i][j].g = 1.f;
if (pixels[i][j].b > 1.f) pixels[i][j].b = 1.f;
}
}
// save to disk
FILE *fp;
fp = fopen("./raytrace.ppm", "w");
if (fp != NULL) {
fprintf(fp, "%s\n", "P6");
fprintf(fp, "%d %d\n", imageWidth, imageHeight);
fprintf(fp, "%d\n", 255);
char r, g, b;
float gamma = 1; // mac
for (int j = 0; j < imageHeight; ++j) {
for (int i = 0; i < imageWidth; ++i) {
pixels[i][j].r = 255 * powf(pixels[i][j].r, gamma);
pixels[i][j].g = 255 * powf(pixels[i][j].g, gamma);
pixels[i][j].b = 255 * powf(pixels[i][j].b, gamma);
r = (pixels[i][j].r > 255) ? 255 : pixels[i][j].r;
g = (pixels[i][j].g > 255) ? 255 : pixels[i][j].g;
b = (pixels[i][j].b > 255) ? 255 : pixels[i][j].b;
fprintf(fp, "%c%c%c", r, g, b);
}
}
fclose(fp);
}
// free memory
for (int i = 0; i < imageWidth; ++i)
delete [] pixels[i];
delete [] pixels;
for (int i = 0; i < numObjects; ++i)
delete objects[i];
delete [] objects;
return 0;
}