This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
398 lines (311 loc) · 8.78 KB
/
main.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
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
#include <X11/Xlib.h>
#include <math.h>
#include <vector>
#include <string>
struct Vec3
{
double x, y, z;
Vec3(){};
Vec3(double x, double y, double z) : x(x), y(y), z(z)
{
}
Vec3 operator+(const Vec3 &v) const
{
return Vec3(x + v.x, y + v.y, z + v.z);
}
Vec3 operator+(const int num) const
{
return Vec3(x + num, y + num, z + num);
}
Vec3 operator-(const Vec3 &v) const
{
return Vec3(x - v.x, y - v.y, z - v.z);
}
Vec3 operator-(const double val) const
{
return Vec3(x - val, y - val, z - val);
}
Vec3 operator*(double mul) const
{
return Vec3(x * mul, y * mul, z * mul);
}
Vec3 operator*(const Vec3 v) const
{
return Vec3(v.x * x, v.y * y, v.z * z);
}
Vec3 operator/(double div) const
{
return Vec3(x / div, y / div, z / div);
}
Vec3 operator/(const Vec3 &div) const
{
return Vec3(x / div.x, y / div.y, z / div.z);
}
bool operator==(const Vec3 &v1)
{
if (v1.x == x && v1.y == y && v1.z == z)
return true;
return false;
}
bool operator!=(const Vec3 &v1)
{
if (v1.x != x && v1.y != y && v1.z != z)
return true;
return false;
}
bool operator>(const Vec3 &v1)
{
if (v1.x > x && v1.y > y && v1.z > z)
return true;
return false;
}
bool operator<(const Vec3 &v1)
{
if (v1.x < x && v1.y < y && v1.z < z)
return true;
return false;
}
Vec3 Normalize() const
{
double magnitude = sqrt(x * x + y * y + z * z);
return Vec3(x / magnitude, y / magnitude, z / magnitude);
}
};
inline double dot(const Vec3 &vec1, const Vec3 &vec2)
{
return (vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z);
}
struct Ray
{
Vec3 origin, destination;
Ray(const Vec3 &org, const Vec3 &dest) : origin(org), destination(dest)
{
}
};
struct Sphere
{
Vec3 center;
double radius;
Vec3 clr;
std::string name;
Sphere(const Vec3 &ctr, double rad, Vec3 &clr) : center(ctr), radius(rad), clr(clr)
{
}
Sphere(const Vec3 &ctr, double rad, Vec3 &clr, std::string name) : center(ctr), radius(rad), clr(clr), name(name)
{
}
Vec3 GetNormal(const Vec3 &pi) const
{
return (pi - center) / radius;
}
bool Intersects(const Ray &ray, double &t) const
{
const Vec3 origin = ray.origin;
const Vec3 dest = ray.destination;
const Vec3 orgCtr = origin - this->center;
const double b = 2 * dot(orgCtr, dest);
const double center = dot(orgCtr, orgCtr) - radius * radius;
double discrim = b * b - 4 * center;
if (discrim < 1e-4)
return false;
discrim = sqrt(discrim);
const double t0 = -b - discrim;
const double t1 = -b + discrim;
t = (t0 < t1) ? t0 : t1;
return true;
}
bool operator!=(const Sphere &s) const
{
if (s.center.x == center.x && s.center.y == center.y && s.center.z == center.z)
return false;
return true;
}
bool operator==(const Sphere &s) const
{
if (s.center.x == center.x && s.center.y == center.y && s.center.z == center.z)
return true;
return false;
}
};
struct Colors
{
Vec3 white = {255, 255, 255};
Vec3 yellow = {255, 255, 0};
Vec3 black = {0, 0, 0};
Vec3 red = {255, 0, 110};
Vec3 green = {110, 255, 0};
Vec3 blue = {0, 110, 255};
};
void ColorBoundary(Vec3 &clr)
{
clr.x = (clr.x > 255) ? 255 : (clr.x < 0) ? 0 : clr.x;
clr.y = (clr.y > 255) ? 255 : (clr.y < 0) ? 0 : clr.y;
clr.z = (clr.z > 255) ? 255 : (clr.z < 0) ? 0 : clr.z;
}
void Reflection(const std::vector<Sphere *> &objects, const Vec3 &pointOfIntersection, const Vec3 &normal, Vec3 &pixclr)
{
Colors color;
double reflectionIntensity = 2.5;
for (auto &sphereB : objects)
{
const Ray ray2(pointOfIntersection, normal * 40);
double t2;
if (sphereB->Intersects(ray2, t2))
{
const Vec3 pointOfIntersection2 = ray2.origin + ray2.destination * t2;
const Vec3 len2 = pointOfIntersection - pointOfIntersection2;
const Vec3 normal2 = sphereB->GetNormal(pointOfIntersection2);
const double dotP2 = dot(len2.Normalize(), normal2.Normalize());
Vec3 pixclrBounce = sphereB->clr * dotP2 * reflectionIntensity;
if (sphereB->name == "world")
pixclrBounce = pixclrBounce - color.white / 1.5;
ColorBoundary(pixclrBounce);
pixclr = pixclrBounce + pixclr;
}
}
}
void Shadow(const std::vector<Sphere *> &objects, const Vec3 &pointOfIntersection, const Vec3 &normal, Vec3 &pixclr,
const Sphere &light)
{
Colors color;
double shadowIntensity = 2.3;
for (auto &sphereB : objects)
{
const Ray ray2(pointOfIntersection, light.GetNormal(sphereB->center) * -3);
double t2;
if (sphereB->Intersects(ray2, t2))
{
const Vec3 pointOfIntersection2 = ray2.origin + ray2.destination * t2;
const Vec3 len2 = pointOfIntersection - pointOfIntersection2;
const Vec3 normal2 = sphereB->GetNormal(pointOfIntersection2);
const double dotP2 = dot(len2.Normalize(), normal2.Normalize());
Vec3 shadow = color.black + 1 * shadowIntensity;
Vec3 pixclrBounce = pixclr * dotP2 * shadow;
ColorBoundary(pixclrBounce);
pixclr = pixclr - pixclrBounce;
}
}
}
Vec3 Trace(const Ray &ray, const Sphere &light, const std::vector<Sphere *> &objects)
{
Colors color;
Vec3 pixclr = color.black;
for (auto &sphere : objects)
{
double t;
double lightIntensity = 0.62;
if (sphere->Intersects(ray, t))
{
const Vec3 pointOfIntersection = ray.origin + ray.destination * t;
const Vec3 len = light.center - pointOfIntersection;
const Vec3 normal = sphere->GetNormal(pointOfIntersection);
const double dotProduct = dot(len.Normalize(), normal.Normalize());
pixclr = (sphere->clr + light.clr / 2 * dotProduct) * lightIntensity;
if (sphere->name != "world")
Reflection(objects, pointOfIntersection, normal, pixclr);
Shadow(objects, pointOfIntersection, normal, pixclr, light);
ColorBoundary(pixclr);
}
}
return pixclr;
}
struct ScreenData
{
Colors &color;
int x, y;
Sphere &light;
std::vector<Sphere *> &objList;
};
Vec3 RenderPixel(const ScreenData &sd)
{
Vec3 pixclr = sd.color.black;
//~ Vec3 pixclrBounce = sd.color.black;
const Ray ray(Vec3(sd.x, sd.y, -3), Vec3(0, 0, 1));
pixclr = Trace(ray, sd.light, sd.objList);
return pixclr;
}
void HandleEvents(XEvent &event, Sphere &light, Sphere &world)
{
if (event.type == MotionNotify)
{
light.center.x = event.xmotion.x;
light.center.y = event.xmotion.y;
}
if (event.type == ButtonPress && event.xbutton.button == 4 && world.center.z > 580)
{
world.center.z -= 5;
world.center.y -= 5;
light.center.z -= 30;
}
if (event.type == ButtonPress && event.xbutton.button == 5 && world.center.z < 600)
{
world.center.z += 5;
world.center.y += 5;
light.center.z += 30;
}
}
inline unsigned long _RGB(int r, int g, int b)
{
return b + (g << 8) + (r << 16);
}
int main()
{
const int W = 300;
const int H = 300;
//
Display *display = XOpenDisplay(NULL);
Window rWin = XDefaultRootWindow(display);
Window win = XCreateSimpleWindow(display, rWin, 0, 0, W, H, 0, 0, 0);
XStoreName(display, win, "Ray Tracer");
unsigned long eventmask = PointerMotionMask | ButtonPressMask;
XSelectInput(display, win, eventmask);
XMapWindow(display, win);
// TODO: render style
GC render = XCreateGC(display, win, 0, NULL);
XSync(display, False);
// WORLD
Colors color;
Sphere sphere(Vec3(W * 0.5, H * 0.5, 40), 30, color.white);
Sphere sphere1(Vec3(W * 0.3, H * 0.5, 20), 15, color.red);
Sphere sphere2(Vec3(W * 0.25, H * 0.35, 30), 20, color.green);
Sphere sphere3(Vec3(W * 0.85, H * 0.65, 40), 35, color.blue);
Vec3 worldColor = Vec3(94, 0, 182);
Sphere world(Vec3(W * 0.5, H * 3.8, 650), 1100, worldColor, "world");
Sphere light(Vec3(W * 0.1, H * 0.5, 0), 40, color.white);
std::vector<Sphere *> objList = {&world, &sphere, &sphere1, &sphere2, &sphere3};
double theta = 0.0;
std::vector<Vec3> background;
for (int y = 0; y < H * 2; ++y)
for (int x = 0; x < W * 2; ++x)
background.emplace_back(Vec3(122, 11, 133) - (x + y) / 5);
// WORLD LOOP
XEvent event;
while (1)
{
if (!XCheckMaskEvent(display, eventmask, &event))
event.type = 0;
HandleEvents(event, light, world);
for (int y = 0; y < H; ++y)
for (int x = 0; x < W; ++x)
{
ScreenData scrnData = {color, x, y, light, objList};
Vec3 pixclr = RenderPixel(scrnData);
ColorBoundary(pixclr);
XSetForeground(display, render, _RGB((int)pixclr.x, (int)pixclr.y, (int)pixclr.z));
XDrawPoint(display, win, render, x, y);
}
int startIdx = 1;
objList[startIdx]->center.x = (W >> 1) + (W / 6) * sin(theta);
objList[startIdx]->center.y = H / 2.8 + (H / 6) * cos(theta / 2);
objList[startIdx + 1]->center.x = objList[startIdx + 1]->center.x + 2 * sin(theta);
objList[startIdx + 1]->center.y = objList[startIdx + 1]->center.y + 2 * cos(theta);
objList[startIdx + 2]->center.y = objList[startIdx + 2]->center.y + 2 * sin(theta);
objList[startIdx + 3]->center.y = objList[startIdx + 3]->center.y + 2 * -sin(theta);
// objList[startIdx + 3]->center.z = 50 + 10 * sin(theta);
theta += 0.1;
XFlush(display);
XSync(display, False);
}
XCloseDisplay(display);
return 0;
}