-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraycpp.cpp
643 lines (626 loc) · 15 KB
/
raycpp.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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
* Copyright(c) 2016-2019 Nicolas Sauzede ([email protected])
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include "vec.h"
#include "veccpp.h"
enum { OT_NONE = -1, OT_SPHERE = 0, OT_PLANE };
class CObject {
public:
CObject( int type = OT_NONE, int len = 0) : m_type( type), m_len( len), m_flags( 0), m_hollow(0) {
}
virtual ~CObject() {
}
// returns intersection distance (HUGE_VAL => no intersection)
virtual double Intersec( const v3& e, const v3& v) const = 0;
virtual v3 Normal( const v3& vint) const = 0;
virtual void SetColor( const double *color) {
m_color = color;
}
virtual const v3& Color() const {
return m_color;
}
virtual v3& Center() {
return m_c;
}
virtual const v3& Center() const {
return m_c;
}
virtual void SetHollow( const int& hollow) {
m_hollow = hollow;
}
const int& Hollow() const {
return m_hollow;
}
virtual std::ostream& Serialize( std::ostream& out) const {
out << m_type << " " << m_len << std::endl;
return out;
}
friend std::ostream& operator<<( std::ostream& out, const CObject& ob) {
return ob.Serialize( out);
}
virtual void Json( std::ostream& out) const = 0;
protected:
int m_type;
int m_len; // for serialization : total number of double excluding type and len
//---------------
int m_flags;
v3 m_color;
int m_hollow;
v3 m_c;
};
typedef class CSphere CLamp;
class CSphere : public CObject {
public:
enum {
FLAGS, COLOR, COLOR_RED = COLOR, COLOR_GREEN, COLOR_BLUE, CENTER, CENTER_X = CENTER, CENTER_Y, CENTER_Z, RADIUS,
// A0, B0, C0, D0, E0, F0,
MAX
};
CSphere( const double *params) :
CObject( OT_SPHERE, MAX),
m_r( params[RADIUS]) {
m_c = ¶ms[CENTER];
SetColor( ¶ms[COLOR]);
// std::cout << *this << std::endl;
}
const double& Radius() const {
return m_r;
}
std::ostream& Serialize( std::ostream& out) const {
CObject::Serialize( out);
out << m_flags << std::endl;
out << m_color << std::endl;
out << m_c << std::endl;
out << m_r << std::endl;
return out;
}
void Json( std::ostream& out) const {
out << "{'type':'sphere', 'data': [";
m_c.Print( out);
out << ",\t";
out << m_r;
out << ",\t";
m_color.Print( out);
out << "]}";
}
double Intersec( const v3 &e, const v3 &v) const {
double result = HUGE_VAL;
double sr2 = m_r * m_r;
double a, b, c;
v3 t = e - m_c;
a = v % v;
b = 2 * (v % t);
c = (t % t) - sr2;
double t1 = 0, t2 = 0;
int sol = solvetri( a, b, c, &t1, &t2);
if (sol >= 1) {
if (sol > 1) {
if (t1 < t2) {
result = t1;
} else {
result = t2;
}
}
else {
result = t1;
}
}
return result;
}
v3 Normal( const v3& vint) const {
v3 nv = ~(vint - Center());
return nv;
}
friend std::ostream& operator<<( std::ostream& out, const CSphere& sp) {
out << "sphere: c=";
out << sp.Center();
out << " r=";
out << sp.Radius();
out << " col=";
out << sp.Color();
return out;
}
private:
double m_r;
};
class CPlane : public CObject {
public:
enum {
FLAGS, COLOR, COLOR_RED = COLOR, COLOR_GREEN, COLOR_BLUE,
LOC0, LOC0_X = LOC0, LOC0_Y, LOC0_Z,
LOC1, LOC1_X = LOC1, LOC1_Y, LOC1_Z,
LOC2, LOC2_X = LOC2, LOC2_Y, LOC2_Z,
MAX
};
CPlane( const double *params) :
CObject( OT_PLANE, MAX),
m_loc0( ¶ms[LOC0]),
m_loc1( ¶ms[LOC1]),
m_loc2( ¶ms[LOC2])
{
SetColor( ¶ms[COLOR]);
std::cout << *this << std::endl;
}
std::ostream& Serialize( std::ostream& out) const {
CObject::Serialize( out);
out << m_flags << std::endl;
out << m_color << std::endl;
out << m_loc0 << std::endl;
out << m_loc1 << std::endl;
out << m_loc2 << std::endl;
return out;
}
void Json( std::ostream& out) const {
out << "'plane': [";
m_loc0.Print( out);
out << ",\t";
m_loc1.Print( out);
out << ",\t";
m_loc2.Print( out);
out << ",\t";
m_color.Print( out);
out << "]";
}
double Intersec( const v3 &oi, const v3 &vi) const {
double res = HUGE_VAL;
v3 n = Normal( m_loc0);
v3 p = m_loc0 - oi;
double num = p % n;
double den = vi % n;
if (den == 0) {
if (num == 0) {
printf( "all intersections\n");
}
else {
printf( "no intersections\n");
}
}
else {
// printf( "to be computed\n");
double det;
double a, b, c, d, e, f, g, h, i;
v3 lb = oi + vi;
a = oi[0] - lb[0];
b = m_loc1[0] - m_loc0[0];
c = m_loc2[0] - m_loc0[0];
d = oi[1] - lb[1];
e = m_loc1[1] - m_loc0[1];
f = m_loc2[1] - m_loc0[1];
g = oi[2] - lb[2];
h = m_loc1[2] - m_loc0[2];
i = m_loc2[2] - m_loc0[2];
det = a * (e * i - f * h) - b * (i * d - f * g) + c * (d * h - e * g);
if (det > 0.001) { // matrix is invertible
double A, B, C, D, E, F, G, H, I;
A = (e * i - f * h);
B = -(d * i - f * g);
C = (d * h - e * g);
D = -(b * i - c * h);
E = (a * i - c * g);
F = -(a * h - b * g);
G = (b * f - c * e);
H = -(a * f - c * d);
I = (a * e - b * d);
double t, u = 0, v = 0;
// p=p0+u*v1+v*v2
t = (A * (oi[0] - m_loc0[0]) + D * (oi[1] - m_loc0[1]) + G * (oi[2] - m_loc0[2])) / det;
u = (B * (oi[0] - m_loc0[0]) + E * (oi[1] - m_loc0[1]) + H * (oi[2] - m_loc0[2])) /det;
v = (C * (oi[0] - m_loc0[0]) + F * (oi[1] - m_loc0[1]) + I * (oi[2] - m_loc0[2])) / det;
if ((u >= 0 && u <= 1) && (v >= 0 && v <= 1) && ((u + v) <= 1)) {
if (t > 1) {
res = 1;
}
}
}
}
return res;
}
v3 Normal( const v3& vint) const {
v3 v1 = m_loc1 - vint;
v3 v2 = m_loc2 - vint;
v3 n;
n = ~(v1 ^ v2);
return n;
}
friend std::ostream& operator<<( std::ostream& out, const CPlane& pl) {
out << "plane: loc0=";
out << pl.m_loc0;
out << " loc1=";
out << pl.m_loc1;
out << " loc2=";
out << pl.m_loc2;
out << " col=";
out << pl.Color();
return out;
}
private:
v3 m_loc0, m_loc1, m_loc2;
};
class CRealist {
public:
void JsonScene( std::ostream& out) const {
out << "{" << std::endl;
out << "'screen': { 'w':" << m_w << ", 'h':" << m_h << ", 'ratiox':1, 'ratioy':1 }," << std::endl;
out << "'camera': { ";
out << "'loc':";
m_e.Json( out);
out << ", 'front':";
m_f.Json( out);
out << ", 'up':";
m_u.Json( out);
out << "}," << std::endl;
out << "'objects': [" << std::endl;
for (unsigned ii = 0; ii < m_objs.size(); ii++) {
if (ii > 0)
out << "," << std::endl;
(m_objs.at( ii))->Json( out);
}
out << std::endl << "]" << std::endl;
out << "}" << std::endl;
}
int LoadScene( const char *scene_file) {
int result = -1;
std::ifstream ifs( scene_file);
if (!ifs.is_open())
return result;
for (unsigned ii = 0; ii < 3; ii++) {
ifs >> m_e[ii];
}
for (unsigned ii = 0; ii < 3; ii++) {
ifs >> m_f[ii];
}
for (unsigned ii = 0; ii < 3; ii++) {
ifs >> m_u[ii];
}
while (!ifs.eof()) {
unsigned type;
unsigned len;
ifs >> type;
if (ifs.eof())
break;
result = -1;
ifs >> len;
// printf( "read type=%u len=%u\n", type, len);
double *data = new double[len];
for (unsigned ii = 0; ii < len; ii++) {
if (ifs.eof())
break;
ifs >> data[ii];
}
if (ifs.eof())
break;
CObject *obj = 0;
switch (type) {
case OT_SPHERE:
obj = new CSphere( data);
break;
}
if (obj)
m_objs.push_back( obj);
delete data;
result = 0;
}
// JsonScene( std::cout);
return result;
}
void OutScene( std::ostream& out) const {
out << m_e << std::endl;
out << m_f << std::endl;
out << m_u << std::endl;
out << std::endl;
for (unsigned ii = 0; ii < m_objs.size(); ii++) {
out << *(m_objs.at( ii)) << std::endl;
}
}
int SaveScene( const char *scene_file) const {
std::ofstream f( scene_file);
OutScene( f);
return 0;
}
#define W 1024
#define H 768
CRealist( const char *scene_file = 0):
m_w(W),
m_h(H) {
// std::cout << "# initial #objects: " << m_objs.size() << std::endl;
const char *wfile_name = 0;
if (scene_file) {
if (LoadScene( scene_file)) {
printf( "failed to load\n");
exit( 1);
}
}
else {
#if 1
// camera
v3 cam[] = {
{ 0.4, 0, 0.4}, // eye
{ -1, 0, -1}, // front towards screen
{ -0.707107, 0, 0.707107}, // up along screen
};
// scene
//FLAGS, COLOR_RED, COLOR_GREEN, COLOR_BLUE, CENTER_X, CENTER_Y, CENTER_Z, RADIUS,
double sph[][CSphere::MAX] = {
{0, 0.8, 0.8, 0.8, 0, -0.1, 0, 0.05},
{0, 0.8, 0.8, 0.8, 0, 0, 0, 0.05 },
{0, 0.8, 0.8, 0.8, 0, 0.1, 0, 0.05},
{0, 0.8, 0, 0, 0.1, -0.05, 0, 0.05},
{0, 0, 0, 0.8, 0.1, 0.05, 0, 0.05},
{0, 0, 0.8, 0, 0.2, 0, 0, 0.05},
{0, 0.8, 0.8, 0.8, 0.05, -0.05, 0.1, 0.05},
{0, 0.8, 0.8, 0.8, 0.05, 0.05, 0.1, 0.05},
{0, 1, 1, 0, 0, -0.5, 0.5, 0.02},
};
int i = 0;
m_e = cam[i++];
m_f = cam[i++];
m_u = cam[i++];
for (unsigned ii = 0; ii < (sizeof(sph) / sizeof(sph[0])); ii++) {
m_objs.push_back( new CSphere( sph[ii]));
}
#else
// default scene : origins (unit vectors)
// camera
v3 cam[] = {
#define ED 3
{ ED, ED, 1*ED}, // eye
{ -1, -1, 1*-1}, // front towards screen
{ 0, 0, 1}, // up along screen
};
// scene
double sph[][CSphere::MAX] = {
#define SR 0.1
{ 0, 1, 1, 1, 0, 0, 0, SR},
{ 0, 1, 0, 0, 1, 0, 0, SR},
{ 0, 0, 1, 0, 0, 1, 0, SR},
{ 0, 0, 0, 1, 0, 0, 1, SR},
};
int i = 0;
m_e = cam[i++];
m_f = cam[i++];
m_u = cam[i++];
for (unsigned ii = 0; ii < (sizeof(sph) / sizeof(sph[0])); ii++) {
m_objs.push_back( new CSphere( sph[ii]));
}
double pl[][CPlane::MAX] = {
{
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0,
},
{
0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
},
{
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
},
};
for (unsigned ii = 0; ii < (sizeof(pl) / sizeof(pl[0])); ii++) {
m_objs.push_back( new CPlane( pl[ii]));
}
#endif
}
if (m_r[0] == 0 && m_r[1] == 0 && m_r[2] == 0) {
m_u = ~m_u;
m_r = m_f ^ m_u; // compute right
m_u = m_r ^ m_f; // re-compute up
m_u = ~m_u;
m_r = ~m_r;
}
double slamp[] = {
0,
#define LAMP_DIST 0.5
#define LAMP_RAD 0.02
#define LAMP_COL 1.0
1.0 * LAMP_COL, 1.0 * LAMP_COL, 0.0 * LAMP_COL,
0.0 * LAMP_DIST, -1.0 * LAMP_DIST, 1.0 * LAMP_DIST,
LAMP_RAD,
};
CSphere *lamp = new CLamp( slamp);
lamp->SetHollow( 1);
m_lamps.push_back( lamp);
m_objs.push_back( lamp);
if (wfile_name) {
// write scene file here
SaveScene( wfile_name);
}
}
#define MAX_DEPTH 3
void Trace( int depth, const v3 &o, const v3 &v, v3 &color) const {
if (depth > MAX_DEPTH)
return;
double tmin = HUGE_VAL;
CObject *omin = 0;
// unsigned imin = 0;
for (unsigned ii = 0; ii < m_objs.size(); ii++) {
double tres = m_objs.at( ii)->Intersec( o, v);
if ((tres > 0) && (tres < tmin)) {
tmin = tres;
omin = m_objs.at( ii);
// imin = ii;
}
}
double def_color = 0;
color *= def_color;
if (tmin < HUGE_VAL) {
double energy = 0;
// ambient
#if defined USE_FLASH || defined USE_REFL || defined USE_LAMP
energy += 0.2;
v3 vint;
v3 nv;
#else
energy += 1.0;
#endif
// printf( "[HIT %u]", imin);
// intersected object color
color = omin->Color() * 1.0;
#if defined USE_FLASH || defined USE_REFL || defined USE_LAMP
// coords of intersec
vint = o + v * tmin;
// normal at intersec
nv = ~omin->Normal( vint);
#endif
#ifdef USE_FLASH
// camera flash
#define MAX_FLASH 0.1
double flash_nrj = 1 - energy;
if (flash_nrj > MAX_FLASH)
flash_nrj = MAX_FLASH;
double dist = !(vint - m_e);
#define LAMP_FLOOR 0.4
if (dist < LAMP_FLOOR)
dist = LAMP_FLOOR;
energy += flash_nrj / dist / dist;
#endif
#ifdef USE_LAMP
// is there any object intersection between vint and a lamp ?
for (unsigned jj = 0; jj < m_lamps.size(); jj++) {
if (omin == m_lamps.at( jj)) // skip current lamp==intersected object
continue;
v3 plamp = m_lamps.at( jj)->Center();
v3 vlamp = plamp - vint;
// is normal dot vlamp <= 0 (surface not exposed to light)
if ((vlamp % nv) <= 0)
continue;
double dlamp = !vlamp;
int shadowed = 0;
for (unsigned ii = 0; ii < m_objs.size(); ii++) {
if (omin == m_objs.at( ii)) // skip current object=intersected object
continue;
double tres = m_objs.at( ii)->Intersec( vint, vlamp);
if ((tres > 0) && (tres < tmin)) {
v3 vint2 = vint + vlamp * tres;
double dint = !(vint2 - vint);
if ((dint < dlamp) && !m_objs.at( ii)->Hollow()) {
shadowed = 1;
break;
}
}
}
if (!shadowed) {
double nrj = 0.1 * 1.0 / dlamp / dlamp;
if (nrj > 1.0)
nrj = 1.0;
energy += nrj;
}
}
#endif
color *= energy;
#ifdef USE_REFL
if (!omin->Hollow()) {
// reflection
double dot = 2 * (v % nv);
v3 vrefl = v - nv * dot;
v3 refl_color = { 0, 0, 0};
Trace( depth + 1, vint, vrefl, refl_color);
double refl_att = 0.2;
color *= (1 - refl_att);
color += refl_color * refl_att;
}
#endif
}
}
void Render( unsigned w = 0, unsigned h = 0, char *fnameout = 0) {
if (w && h) {
m_w = w;
m_h = h;
}
// screen
m_ww = 1;
m_hh = m_ww * m_h / m_w;
FILE *fout = stdout;
unsigned char *bytes = 0;
size_t nbytes = 0;
if (fnameout) {
fout = fopen( fnameout, "wb");
fprintf( fout, "P6\n");
nbytes = 3 * h * w;
bytes = (unsigned char *)malloc(nbytes);
} else {
fprintf( fout, "P3\n");
}
fprintf( fout, "%d %d\n", m_w, m_h);
int max = 255;
fprintf( fout, "%d\n", max);
// ray
v3 v;
// printf( "tr=%f\n", tr);
for (unsigned jj = 0; jj < m_h; jj++) {
v3 vu;
vu = m_u * ((double)m_h - (double)jj - 1.0 - (double)m_h / 2) / (double)m_h * m_hh;
// vprint( "raycpp vu", vu);
// getchar();
for (unsigned ii = 0; ii < m_w; ii++) {
v3 vr;
vr = m_r * ((double)ii - m_w / 2) / m_w * m_ww;
v = ~(m_f + vu + vr);
v3 color = { 1, 1, 1};
Trace( 0, m_e, v, color);
if (fnameout) {
bytes[(jj * w + ii) * 3 + 0] = max*color[0];
bytes[(jj * w + ii) * 3 + 1] = max*color[1];
bytes[(jj * w + ii) * 3 + 2] = max*color[2];
} else {
fprintf( fout, "%2.lf %2.lf %2.lf ", max*color[0], max*color[1], max*color[2]);
}
}
if (!fnameout) {
fprintf( fout, "\n");
}
}
if (fnameout) {
fwrite(bytes, nbytes, 1, fout);
fclose( fout);
free(bytes);
}
for (unsigned ii = 0; ii < m_objs.size(); ii++) {
delete m_objs.at( ii);
m_objs.at( ii) = 0;
}
}
private:
unsigned m_w, m_h; // screen pixel dimensions
std::vector<CObject *> m_objs;
std::vector<CLamp *> m_lamps;
v3 m_e; // eye position
v3 m_f; // front towards screen
v3 m_u; // up along screen
v3 m_r; // right along screen (computed)
double m_ww, m_hh; // screen dimensions (space)
};
int main( int argc, char *argv[]) {
unsigned w = 0, h = 0;
char *fnameout = 0;
char *scene = 0;
int arg = 1;
if (arg < argc) {
sscanf( argv[arg++], "%d", &w);
if (arg < argc) {
sscanf( argv[arg++], "%d", &h);
if (arg < argc) {
fnameout = argv[arg++];
if (arg < argc) {
scene = argv[arg++];
}
}
}
}
CRealist r( scene);
r.Render( w, h, fnameout);
return 0;
}