-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndt.c
2105 lines (1868 loc) · 70.3 KB
/
ndt.c
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ndt.c
* ndt: n-dimensional tracer
*
* Copyright (c) 2014-2021 Bryan Franklin. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <dlfcn.h>
#ifdef WITH_VALGRIND
#include <valgrind/valgrind.h>
#endif /* WITH_VALGRIND */
#ifdef WITH_MPI
#include <mpi.h>
#endif /* WITH_MPI */
#include "vectNd.h"
#include "image.h"
#include "object.h"
#include "scene.h"
#include "timing.h"
#ifndef MIN
#define MIN(x,y) (((x)>(y))?(y):(x))
#endif /* MIN */
#ifndef MAX
#define MAX(x,y) (((x)<(y))?(y):(x))
#endif /* MAX */
#if 1
#define WITH_SPECULAR
#endif /* 0 */
#ifdef WITH_SPECULAR
int specular_enabled = 1;
#endif /* WITH_SPECULAR */
int recursive_aa = 0;
typedef enum stereo_mode_t {
MONO, SIDE_SIDE_3D, OVER_UNDER_3D, ANAGLYPH_3D, HIDEF_3D
} stereo_mode;
#ifdef WITH_MPI
typedef enum mpi_mode {
MPI_MODE_NONE,
MPI_MODE_PIXEL, /* not supported, yet! */
MPI_MODE_ROW,
MPI_MODE_FRAME, /* rank 0 just manages other ranks */
MPI_MODE_FRAME2, /* rank 0 also renders frames */
} mpi_mode_t;
static int mpiRank = -1;
static int mpiSize = -1;
static mpi_mode_t mpi_mode = MPI_MODE_NONE;
int mpi_collect_image(image_t*);
int mpi_broadcast_image(image_t *img, int source_rank);
#endif /* WITH_MPI */
#ifndef WITHOUT_KDTREE
kd_tree_t kdtree;
#endif /* !WITHOUT_KDTREE */
static inline int apply_lights(scene *scn, int dim, object *obj_ptr, vectNd *src, vectNd *look, vectNd *hit, vectNd *hit_normal, dbl_pixel_t *color) {
dbl_pixel_t clr;
double hit_r, hit_g, hit_b;
vectNd rev_view, rev_light, light_vec, light_hit, light_hit_normal;
vectNd lgt_pos, near_pos;
/* get color of object */
obj_ptr->get_color(obj_ptr, hit, &hit_r, &hit_g, &hit_b);
#ifdef WITH_SPECULAR
double hitr_r = 0.0, hitr_g = 0.0, hitr_b = 0.0;
if( specular_enabled ) {
/* get reflectivity of object */
obj_ptr->get_reflect(obj_ptr, hit, &hitr_r, &hitr_g, &hitr_b);
}
#endif /* WITH_SPECULAR */
memset(&clr,'\0',sizeof(clr));
clr.r = hit_r * scn->ambient.red;
clr.g = hit_g * scn->ambient.green;
clr.b = hit_b * scn->ambient.blue;
clr.a = 1.0;
/* all of these are initialized within the loop */
vectNd_alloc(&rev_view,dim);
vectNd_alloc(&rev_light,dim);
vectNd_alloc(&light_vec,dim);
vectNd_alloc(&light_hit,dim);
vectNd_alloc(&light_hit_normal,dim);
vectNd_alloc(&lgt_pos,dim);
vectNd_alloc(&near_pos,dim);
int i=0;
for(i=0; i<scn->num_lights; ++i) {
light_type lgt_type = scn->lights[i]->type;
if( lgt_type == LIGHT_AMBIENT ) {
clr.r += hit_r * scn->lights[i]->red;
clr.g += hit_g * scn->lights[i]->green;
clr.b += hit_b * scn->lights[i]->blue;
continue;
}
/* copy location of actual light */
vectNd_copy(&lgt_pos,&scn->lights[i]->pos);
if( lgt_type == LIGHT_DISK ||
lgt_type == LIGHT_RECT ) {
/* move light to a random point on areal light */
double x,y;
vectNd temp;
vectNd_alloc(&temp,scn->lights[i]->pos.n);
if( scn->lights[i]->prepared == 0 ) {
scene_prepare_light(scn->lights[i]);
}
double radius = scn->lights[i]->radius;
/* get one random sample */
/* re-sampling happens at the pixel level */
do {
x = 2 * drand48() - 1.0;
y = 2 * drand48() - 1.0;
/* reject any samples outside unit circle */
/* see: Ray Tracing From The Ground Up, p. 120 */
} while (lgt_type == LIGHT_DISK && x*x + y*y > 1.0 );
/* map point onto light surface */
vectNd_scale(&scn->lights[i]->u1,x*radius,&temp);
vectNd_add(&lgt_pos,&temp,&lgt_pos);
vectNd_scale(&scn->lights[i]->v1,y*radius,&temp);
vectNd_add(&lgt_pos,&temp,&lgt_pos);
/* treat sampled point as a regular point light source */
lgt_type = LIGHT_POINT;
vectNd_free(&temp);
}
if( lgt_type == LIGHT_POINT ||
lgt_type == LIGHT_DIRECTIONAL ||
lgt_type == LIGHT_SPOT ) {
/* determine if light is in correct direction */
if( lgt_type == LIGHT_POINT ||
lgt_type == LIGHT_SPOT ) {
vectNd_sub(&lgt_pos, hit, &rev_light);
} else if( scn->lights[i]->type == LIGHT_DIRECTIONAL ) {
vectNd_scale(&scn->lights[i]->dir, -1, &rev_light);
}
vectNd_unitize(&rev_light);
vectNd_sub(src, hit, &rev_view);
double dotRev1, dotRev2;
vectNd_dot(&rev_light, hit_normal,&dotRev1);
vectNd_dot(&rev_view, hit_normal,&dotRev2);
if( (dotRev1*dotRev2) <= 0 ) {
/* light and observer are on opposite sides of the surface */
/*printf("light on the wrong side of surface\n");*/
continue;
}
}
if( lgt_type == LIGHT_POINT
|| lgt_type == LIGHT_SPOT
|| lgt_type == LIGHT_DIRECTIONAL ) {
object *light_obj_ptr=NULL;
int got_hit = 0;
/* dist_limit meanings:
* <0 check all objects
* 0 stop if any hit found
* >0 stop if hit found withing limit
*/
double dist_limit = -1.0;
if( lgt_type == LIGHT_DIRECTIONAL ) {
dist_limit = 0.0;
} else if( lgt_type == LIGHT_POINT
|| lgt_type == LIGHT_SPOT ) {
vectNd_dist(hit,&lgt_pos,&dist_limit);
dist_limit += EPSILON;
}
double ldist2=1.0;
if( lgt_type == LIGHT_POINT ||
lgt_type == LIGHT_SPOT ) {
vectNd_sub(hit, &lgt_pos, &light_vec);
/* get distance squared, needed for diffuse lighting */
vectNd_dot(&light_vec,&light_vec,&ldist2);
vectNd_unitize(&light_vec);
/* check that hit point is within cone of light for
* spotlight, skip tracing path if not */
if( lgt_type == LIGHT_SPOT ) {
double angle = -1;
vectNd_angle(&scn->lights[i]->dir,&light_vec,&angle);
if( (angle*180.0/M_PI) > scn->lights[i]->angle ) {
continue;
}
}
/* trace from light to object */
#ifndef WITHOUT_KDTREE
got_hit = trace_kd(&lgt_pos, &light_vec, &kdtree,
&light_hit, &light_hit_normal, &light_obj_ptr, dist_limit);
#else
got_hit = trace(&lgt_pos, &light_vec, scn->object_ptrs, scn->num_objects,
&light_hit, &light_hit_normal, &light_obj_ptr, dist_limit);
#endif /* !WITHOUT_KDTREE */
if( !got_hit || light_obj_ptr != obj_ptr ) {
/* light didn't hit the target object */
continue;
}
double dist = 1000;
vectNd_dist(hit,&light_hit,&dist);
/* verify we hit the right point with the light */
if( dist > EPSILON ) {
/* light didn't hit the same point on the object */
continue;
}
} else if( lgt_type == LIGHT_DIRECTIONAL ) {
/* trace from object towards light */
vectNd_copy(&near_pos,&scn->lights[i]->dir);
vectNd_unitize(&near_pos);
vectNd_scale(&near_pos,-EPSILON,&near_pos);
vectNd_add(&near_pos,hit,&near_pos);
vectNd_scale(&scn->lights[i]->dir, -1.0, &light_vec);
#ifndef WITHOUT_KDTREE
got_hit = trace_kd(&near_pos, &rev_light, &kdtree,
&light_hit, &light_hit_normal, &light_obj_ptr, 0.0);
#else
got_hit = trace(&near_pos, &rev_light, scn->object_ptrs, scn->num_objects,
&light_hit, &light_hit_normal, &light_obj_ptr, 0.0);
#endif /* !WITHOUT_KDTREE */
/* success is not hitting anything */
if( got_hit ) {
/* something between object and infinity */
continue;
}
/* prep vectors for remainder of computations */
vectNd_copy(&light_vec, &scn->lights[i]->dir);
vectNd_copy(&light_hit, hit);
vectNd_copy(&light_hit_normal, hit_normal);
light_obj_ptr = obj_ptr;
/* distance is irrelevant for diffuse lighting */
ldist2 = 1;
}
double angle = -1;
double light_scale = 1;
vectNd_angle(hit_normal,&light_vec,&angle);
/* diffuse lighting */
if( angle > M_PI / 2.0 )
angle = M_PI - angle;
light_scale = cos(angle) / ldist2;
if( !obj_ptr->transparent ) {
clr.r += hit_r * scn->lights[i]->red * light_scale;
clr.g += hit_g * scn->lights[i]->green * light_scale;
clr.b += hit_b * scn->lights[i]->blue * light_scale;
}
}
#ifdef WITH_SPECULAR
if( lgt_type == LIGHT_POINT ||
lgt_type == LIGHT_SPOT ||
lgt_type == LIGHT_DIRECTIONAL ) {
if( specular_enabled ) {
/* specular highlighting */
/* see: http://en.wikipedia.org/wiki/Specular_highlight */
/* note: wikipedia doesn't mention that these have to be unit vectors */
/* slide 28 of
* http://www.eng.utah.edu/~cs5600/slides/Wk%2013%20Ray%20Tracing.pdf
* looks interesting. */
vectNd light_ref;
vectNd_alloc(&light_ref,dim);
vectNd_reflect(&light_vec,&light_hit_normal,&light_ref,0.5);
double rv;
vectNd rev_look;
vectNd_alloc(&rev_look,look->n);
vectNd_unitize(&light_ref);
vectNd_scale(look,-1,&rev_look);
vectNd_unitize(&rev_look);
vectNd_dot(&light_ref,&rev_look,&rv);
vectNd_free(&rev_look);
rv = MAX(0,rv);
double rvn = pow(rv,50);
double max_light = MAX(scn->lights[i]->red, MAX(scn->lights[i]->green, scn->lights[i]->blue));
clr.r += hitr_r * scn->lights[i]->red/max_light * rvn;
clr.g += hitr_g * scn->lights[i]->green/max_light * rvn;
clr.b += hitr_b * scn->lights[i]->blue/max_light * rvn;
clr.a = 1.0;
vectNd_free(&light_ref);
}
}
#else
#warning "Specular highlighting not enabled."
#endif /* WITH_SPECULAR */
}
vectNd_free(&lgt_pos);
vectNd_free(&near_pos);
vectNd_free(&light_vec);
vectNd_free(&light_hit);
vectNd_free(&light_hit_normal);
vectNd_free(&rev_light);
vectNd_free(&rev_view);
memcpy(color,&clr,sizeof(clr));
return 0;
}
/* get color of ray r,g,b \in [0,1] */
int get_ray_color(vectNd *src, vectNd *unit_look, scene *scn, dbl_pixel_t *pixel,
double pixel_frac, double *depth, int max_depth)
{
int ret = 0;
pixel->r = pixel->g = pixel->b = 0.0;
pixel->a = 1.0;
if( pixel_frac < (1.0/512.0) )
return 1;
//printf("pixel_frac = %g\n", pixel_frac);
if( max_depth <= 0 )
return 1;
vectNd hit;
vectNd hit_normal;
object *obj_ptr=NULL;
int dim = src->n;
dbl_pixel_t clr;
memset(&clr,'\0',sizeof(clr));
/* trace from camera to possible object */
vectNd_calloc(&hit,dim);
vectNd_calloc(&hit_normal,dim);
obj_ptr = NULL;
#ifndef WITHOUT_KDTREE
trace_kd(src, unit_look, &kdtree, &hit, &hit_normal, &obj_ptr, -1.0);
#else
trace(src, unit_look, scn->object_ptrs, scn->num_objects, &hit, &hit_normal, &obj_ptr, -1.0);
#endif /* !WITHOUT_KDTREE */
/* record depth for depth maps */
double trace_dist = -1;
if( obj_ptr != NULL || depth != NULL ) {
vectNd_dist(&hit,src,&trace_dist);
if( depth != NULL ) {
/* record distance to hit */
if( trace_dist > EPSILON )
*depth = 1.0/trace_dist;
}
}
if( obj_ptr == NULL && depth != NULL )
*depth = 0.0;
/* apply light */
if( obj_ptr != NULL && trace_dist > EPSILON )
{
apply_lights(scn,dim,obj_ptr,src,unit_look,&hit,&hit_normal,&clr);
#if 1
/* get reflectivity of object */
double hitr_r, hitr_g, hitr_b;
obj_ptr->get_reflect(obj_ptr, &hit, &hitr_r, &hitr_g, &hitr_b);
/* compute reflection and refraction */
/* see:
* http://www.unc.edu/~marzuola/Math547_S13/Math547_S13_Projects/P_Smith_Section001_RayTracing.pdf
*/
dbl_pixel_t ref;
vectNd new_ray;
vectNd_alloc(&new_ray,dim);
double contrib = MAX(hitr_r,MAX(hitr_g,hitr_b));
if(contrib > 0 ) {
/* apply reflectivity */
if( hitr_r != 0.0 || hitr_g != 0.0 || hitr_b != 0.0 ) {
vectNd_reflect(unit_look,&hit_normal,&new_ray,1.0);
vectNd_unitize(&new_ray);
/* set color based on actual reflection */
get_ray_color(&hit,&new_ray,scn,&ref, contrib*pixel_frac, NULL, max_depth-1);
#ifdef WITH_SPECULAR
if( specular_enabled ) {
clr.r = (1-hitr_r)*(clr.r)+(hitr_r)*ref.r;
clr.g = (1-hitr_g)*(clr.g)+(hitr_g)*ref.g;
clr.b = (1-hitr_b)*(clr.b)+(hitr_b)*ref.b;
clr.a = 1.0;
} else {
#endif /* WITH_SPECULAR */
clr.r += hitr_r*ref.r;
clr.g += hitr_g*ref.g;
clr.b += hitr_b*ref.b;
clr.a = 1.0;
#ifdef WITH_SPECULAR
}
#endif /* WITH_SPECULAR */
}
}
/* apply transparency */
if( obj_ptr->transparent ) {
vectNd_refract(unit_look,&hit_normal,&new_ray,obj_ptr->refract_index);
vectNd_unitize(&new_ray);
get_ray_color(&hit,&new_ray,scn,&ref, (1-contrib)*pixel_frac, NULL, max_depth-1);
clr.r += (1.0-hitr_r)*ref.r;
clr.g += (1.0-hitr_g)*ref.g;
clr.b += (1.0-hitr_b)*ref.b;
clr.a = 1.0;
}
vectNd_free(&new_ray);
#endif /* 1 */
ret = 1;
} else {
/* didn't hit an object, so use background color */
clr.r = scn->bg_red;
clr.g = scn->bg_green;
clr.b = scn->bg_blue;
clr.a = scn->bg_alpha;
ret = 0;
}
memcpy(pixel,&clr,sizeof(clr));
vectNd_free(&hit);
vectNd_free(&hit_normal);
return ret;
}
typedef enum camera_mode_t {
CAM_LEFT, CAM_CENTER, CAM_RIGHT
} camera_mode;
int get_pixel_color(scene *scn, int width, int height, double x, double y,
dbl_pixel_t *clr, int samples, camera_mode mode, double *depth,
int max_optic_depth)
{
vectNd look;
vectNd pixel;
vectNd virtCam;
vectNd temp;
int dim = scn->cam.pos.n;
/* compute look vector */
/* pixelPos = cam.imgOrig + x*cam.dirX + y*cam.dirY */
/* look = pixelPos - cam.pos */
vectNd_alloc(&pixel,dim);
vectNd_alloc(&virtCam,dim);
vectNd_alloc(&look,dim);
vectNd_alloc(&temp,dim);
int min_samples = samples;
int max_samples = 10000;
double max_diff = 1.0/256.0;
double clr_diff = 256;
dbl_pixel_t t_clr;
t_clr.r = t_clr.g = t_clr.b = t_clr.a = 0.0;
int t_samples = 0;
int i=0;
double pixel_width = 1.0/width;
double pixel_height = 1.0/height;
double orig_x, orig_y;
orig_x = x;
orig_y = y;
for(i=0; i<min_samples || (i<max_samples && clr_diff > max_diff); ++i) {
dbl_pixel_t l_clr;
switch(mode) {
case CAM_LEFT:
vectNd_copy(&virtCam,&scn->cam.leftEye);
break;
case CAM_RIGHT:
vectNd_copy(&virtCam,&scn->cam.rightEye);
break;
case CAM_CENTER:
default:
vectNd_copy(&virtCam,&scn->cam.pos);
break;
}
/* apply pixel sampling for anti-aliasing */
if( recursive_aa == 0 && samples > 1 ) {
double dx,dy;
/* perturb look vector slightly */
/* see: Ray Tracing From The Ground Up, p. 172 */
dx = drand48();
dy = drand48();
x = orig_x + dx * pixel_width;
y = orig_y + dy * pixel_height;
}
double focal_dist = scn->cam.focal_distance;
camera_target_point(&scn->cam, x, y, focal_dist, &pixel);
if( scn->cam.type == CAMERA_VR || scn->cam.type == CAMERA_PANO ) {
/* For VR, rotate camera around CAM_CENTER point */
if( mode != CAM_CENTER ) {
double azi = x * scn->cam.hFov;
vectNd_rotate2(&virtCam,&scn->cam.pos,&scn->cam.localX,&scn->cam.localZ,azi,&virtCam);
}
}
/* apply aperture sampling for depth of field */
if( recursive_aa != 0 || samples > 1 ) {
double x,y;
/* perturb look vector slightly */
/* see: Ray Tracing From The Ground Up, p. 171 */
do {
x = 2 * drand48() - 1.0;
y = 2 * drand48() - 1.0;
/* reject any samples outside unit circle */
/* see: Ray Tracing From The Ground Up, p. 120 */
} while (x*x + y*y > 1.0 );
vectNd_scale(&scn->cam.localX, x*scn->cam.aperture_radius, &temp);
vectNd_add(&virtCam, &temp, &virtCam);
vectNd_scale(&scn->cam.localY, y*scn->cam.aperture_radius, &temp);
vectNd_add(&virtCam, &temp, &virtCam);
}
/* compute primary ray to use */
vectNd_sub(&pixel, &virtCam, &look);
l_clr.r = l_clr.g = l_clr.b = 0.0;
l_clr.a = 1.0;
vectNd_unitize(&look);
get_ray_color(&virtCam, &look, scn, &l_clr, 1.0, depth, max_optic_depth);
if( i > 1 ) {
clr_diff = MAX( fabs(t_clr.r / (i-1) - (t_clr.r+l_clr.r) / i),
MAX( fabs(t_clr.g / (i-1) - (t_clr.g+l_clr.g) / i),
fabs(t_clr.b / (i-1) - (t_clr.b+l_clr.b) / i) ) );
}
t_clr.r += l_clr.r;
t_clr.g += l_clr.g;
t_clr.b += l_clr.b;
t_clr.a += l_clr.a;
t_samples += 1;
}
clr->r = t_clr.r/t_samples;
clr->g = t_clr.g/t_samples;
clr->b = t_clr.b/t_samples;
clr->a = t_clr.a/t_samples;
vectNd_free(&temp);
vectNd_free(&look);
vectNd_free(&pixel);
vectNd_free(&virtCam);
return 1;
}
int render_pixel(scene *scn, int width, double x_scale, int height, double y_scale, double i, double j, stereo_mode mode, int samples, dbl_pixel_t *clr, double *depth, int max_optic_depth)
{
double ip = 0;
double jp = 0;
camera_mode cam_mode = CAM_CENTER;
double x = 0.0;
double y = 0.0;
ip = i;
jp = j;
cam_mode = CAM_CENTER;
if( mode == SIDE_SIDE_3D ) {
if( i < width/2 ) {
// left image
ip = ip/x_scale;
cam_mode = CAM_LEFT;
} else {
// right image
ip = (ip-width/2)/x_scale;
cam_mode = CAM_RIGHT;
}
}
if( mode == OVER_UNDER_3D ) {
if( j < height/2 ) {
// top image
jp = jp/y_scale;
cam_mode = CAM_LEFT;
} else {
// bottom image
jp = (jp-height/2)/y_scale;
cam_mode = CAM_RIGHT;
}
}
if( mode == HIDEF_3D ) {
if( j < 1080 ) {
// left image
cam_mode = CAM_LEFT;
} else if( j > (1080+45) ) {
// bottom image
jp = j - (1080+45);
cam_mode = CAM_RIGHT;
} else {
// some wierd blanking thing
// see:
// http://www.tomshardware.com/reviews/blu-ray-3d-3d-video-3d-tv,2632-6.html
clr->r = clr->g = clr->b = 0;
return 0;
}
x = ip/(double)width - 0.5;
y = -(jp/1080.0 - 0.5);
} else {
x = ip/(double)width - 0.5;
y = -(jp/(double)height - 0.5);
}
if( mode == ANAGLYPH_3D ) {
dbl_pixel_t left_clr;
dbl_pixel_t right_clr;
get_pixel_color(scn, width, height, x, y, &left_clr, samples, CAM_LEFT, depth, max_optic_depth);
get_pixel_color(scn, width, height, x, y, &right_clr, samples, CAM_RIGHT, NULL, max_optic_depth);
/* true anaglyph */
clr->r = 0.299*left_clr.r+0.587*left_clr.g+0.114*left_clr.b;
clr->g = 0;
clr->b = 0.299*right_clr.r+0.587*right_clr.g+0.114*right_clr.b;
clr->a = 1.0;
} else {
get_pixel_color(scn, width, height, x, y, clr, samples, cam_mode, depth, max_optic_depth);
}
return 0;
}
int recursive_resample(scene *scn, int width, double x_scale,
int height, double y_scale, double x, double y,
int samples, int aa_diff, int aa_depth, stereo_mode mode, double step,
dbl_pixel_t *p1, dbl_pixel_t *p2, dbl_pixel_t *p3, dbl_pixel_t *p4,
dbl_pixel_t *res, int max_optic_depth)
{
dbl_pixel_t p5, p6, p7, p8, p9;
if( aa_depth<=0 || step < 1.0/(2<<(aa_depth-1)) ) {
image_avg_dbl_pixels4(p1,p2,p3,p4,res,NULL);
return 0;
}
double hs=step/2;
/* center */
render_pixel(scn, width, x_scale, height, y_scale, x+hs, y+hs, mode, samples, &p5, NULL, max_optic_depth);
/* top middle */
render_pixel(scn, width, x_scale, height, y_scale, x+hs, y, mode, samples, &p6, NULL, max_optic_depth);
/* left edge */
render_pixel(scn, width, x_scale, height, y_scale, x, y+hs, mode, samples, &p7, NULL, max_optic_depth);
/* right edge */
render_pixel(scn, width, x_scale, height, y_scale, x+step, y+hs, mode, samples, &p8, NULL, max_optic_depth);
/* bottom middle */
render_pixel(scn, width, x_scale, height, y_scale, x+hs, y+step, mode, samples, &p9, NULL, max_optic_depth);
/* compute 4 sub-pixels */
dbl_pixel_t sp1, sp2, sp3, sp4;
double var1=0, var2=0, var3=0, var4=0, var5;
double threshold = aa_diff/255.0;
/* top left */
image_avg_dbl_pixels4(p1,&p6,&p7,&p5,&sp1,&var1);
if( var1 > threshold )
recursive_resample(scn,width,x_scale,height,y_scale,x,y,samples,aa_diff,aa_depth,mode,hs,p1,&p6,&p7,&p5,&sp1,max_optic_depth);
/* top right */
image_avg_dbl_pixels4(p2,&p6,&p8,&p5,&sp2,&var2);
if( var2 > threshold )
recursive_resample(scn,width,x_scale,height,y_scale,x+hs,y,samples,aa_diff,aa_depth,mode,hs,&p6,p2,&p5,&p8,&sp2,max_optic_depth);
/* bottom left */
image_avg_dbl_pixels4(p3,&p9,&p7,&p5,&sp3,&var3);
if( var3 > threshold )
recursive_resample(scn,width,x_scale,height,y_scale,x,y+hs,samples,aa_diff,aa_depth,mode,hs,&p7,&p5,p3,&p9,&sp3,max_optic_depth);
/* bottom right */
image_avg_dbl_pixels4(p4,&p9,&p8,&p5,&sp4,&var4);
if( var4 > threshold )
recursive_resample(scn,width,x_scale,height,y_scale,x+hs,y+hs,samples,aa_diff,aa_depth,mode,hs,&p5,&p8,&p9,p4,&sp4,max_optic_depth);
image_avg_dbl_pixels4(&sp1,&sp2,&sp3,&sp4,res,&var5);
return var5;
}
int resample_pixel(scene *scn, int width, double x_scale, int height, double y_scale, int i, int j, stereo_mode mode, int samples, int aa_diff, int aa_depth, image_t *img, dbl_pixel_t *clr, int max_optic_depth) {
dbl_pixel_t p1, p2, p3, p4;
double var = 0.0;
int ret = 0;
memset(&p1, '\0', sizeof(p1));
memset(&p2, '\0', sizeof(p2));
memset(&p3, '\0', sizeof(p3));
memset(&p4, '\0', sizeof(p4));
dbl_image_get_pixel(img,i,j,&p1);
dbl_image_get_pixel(img,i+1,j,&p2);
dbl_image_get_pixel(img,i,j+1,&p3);
dbl_image_get_pixel(img,i+1,j+1,&p4);
image_avg_dbl_pixels4(&p1,&p2,&p3,&p4,clr,&var);
/* resample pixel if needed */
if( var > aa_diff/255.0 ) {
ret = 1;
recursive_resample(scn,width+1,x_scale,height+1,y_scale,
i,j,samples,aa_diff,aa_depth,mode,1.0, &p1, &p2, &p3, &p4, clr, max_optic_depth);
}
return ret;
}
int render_line(scene *scn, int width, double x_scale, int height, double y_scale, int j, stereo_mode mode, int samples, image_t *img, image_t *depth_map, int max_optic_depth)
{
dbl_pixel_t clr;
dbl_pixel_t depth_clr;
double depth;
int i=0;
depth_clr.a = 1.0;
int row_start = 0;
int row_step = 1;
#ifdef WITH_MPI
if( mpi_mode == MPI_MODE_PIXEL && mpiSize > 0 ) {
row_start = (width * j + mpiRank) % mpiSize;
row_step = mpiSize;
}
#endif /* WITH_MPI */
for(i=row_start; i<width; i+=row_step) {
render_pixel(scn,width,x_scale,height,y_scale,i,j,mode,samples,&clr, &depth, max_optic_depth);
dbl_image_set_pixel(img,i,j,&clr);
if( depth_map != NULL ) {
depth_clr.r = depth_clr.g = depth_clr.b = depth;
dbl_image_set_pixel(depth_map,i,j,&depth_clr);
}
}
return 0;
}
int resample_line(scene *scn, int width, double x_scale, int height, double y_scale, int j, stereo_mode mode, int samples, int aa_diff, int aa_depth, image_t *img, image_t *actual_img, int max_optic_depth)
{
dbl_pixel_t clr;
int ret = 0;
int i=0;
int row_start = 0;
int row_step = 1;
#ifdef WITH_MPI
if( mpi_mode == MPI_MODE_PIXEL && mpiSize > 0 ) {
row_start = (width * j + mpiRank) % mpiSize;
row_step = mpiSize;
}
#endif /* WITH_MPI */
for(i=row_start; i<width; i+=row_step) {
ret += resample_pixel(scn, width, x_scale, height, y_scale, i, j, mode, samples, aa_diff, aa_depth, img, &clr, max_optic_depth);
dbl_image_set_pixel(actual_img,i,j,&clr);
}
return ret;
}
struct thr_info {
image_t *img;
image_t *actual_img;
image_t *depth_map;
scene *scn;
char *name;
int width;
int height;
double x_scale;
double y_scale;
int samples;
int aa_diff;
int aa_depth;
stereo_mode mode;
int threads;
int thr_offset;
int pixel_count;
int max_optic_depth;
};
void *render_lines_thread(void *arg)
{
struct thr_info info;
memcpy(&info,arg,sizeof(info));
int j=0;
struct timeval timer;
if( info.thr_offset==0 )
timer_start(&timer);
int row_start = info.thr_offset;
int row_step = info.threads;
#ifdef WITH_MPI
if( mpi_mode == MPI_MODE_ROW ) {
row_start += mpiRank*info.threads;
row_step *= mpiSize;
}
#endif /* WITH_MPI */
for(j=row_start; j<info.height; j+=row_step) {
render_line(info.scn, info.width, info.x_scale,
info.height, info.y_scale, j, info.mode, info.samples,
info.img, info.depth_map, info.max_optic_depth);
if( info.thr_offset==0 && (j%10) == 0 ) {
int num = image_active_saves();
double remaining = timer_remaining(&timer, j, info.height+1);
#ifdef WITH_MPI
if( mpiRank == 0 || (mpiRank == 1 && mpi_mode == MPI_MODE_FRAME) ) {
#endif /* WITH_MPI */
if( num <= 0 ) {
char remainStr[32] = "";
if( remaining >= 0 )
snprintf(remainStr, sizeof(remainStr), " (%.2fs remaining)", remaining);
printf(" \r% 6.2f%%%s", 100.0*j/(info.height+1),remainStr);
} else {
printf(" \r% 6.2f%% (%i active save%s)", 100.0*j/info.height, num, (num==1)?"":"s");
}
fflush(stdout);
#ifdef WITH_MPI
}
#endif /* WITH_MPI */
}
}
memcpy(arg,&info,sizeof(info));
return 0;
}
void *resample_lines_thread(void *arg)
{
struct thr_info info;
memcpy(&info,arg,sizeof(info));
int j=0;
struct timeval timer;
if( info.thr_offset==0 )
timer_start(&timer);
int row_start = info.thr_offset;
int row_step = info.threads;
#ifdef WITH_MPI
if( mpi_mode == MPI_MODE_ROW ) {
row_start += mpiRank*info.threads;
row_step *= mpiSize;
}
#endif /* WITH_MPI */
for(j=row_start; j<info.height; j+=row_step) {
info.pixel_count += resample_line(info.scn, info.width, info.x_scale,
info.height, info.y_scale, j, info.mode, info.samples,
info.aa_diff, info.aa_depth, info.img, info.actual_img,
info.max_optic_depth);
if( info.thr_offset==0 && (j%10) == 0 ) {
int num = image_active_saves();
double remaining = timer_remaining(&timer, j, info.height+1);
#ifdef WITH_MPI
if( mpiRank == 0 || (mpiRank == 1 && mpi_mode == MPI_MODE_FRAME) ) {
#endif /* WITH_MPI */
if( num <= 0 ) {
char remainStr[32] = "";
if( remaining >= 0 )
snprintf(remainStr, sizeof(remainStr), " (%.2fs remaining)", remaining);
printf(" \r% 6.2f%%%s", 100.0*j/(info.height+1),remainStr);
} else {
printf(" \r% 6.2f%% (%i active save%s)", 100.0*j/info.height, num, (num==1)?"":"s");
}
fflush(stdout);
#ifdef WITH_MPI
}
#endif /* WITH_MPI */
}
}
memcpy(arg,&info,sizeof(info));
return 0;
}
int render_image(scene *scn, char *name, char *depth_name, int width, int height, int samples, stereo_mode mode, int threads, int aa_diff, int aa_depth, int max_optic_depth, image_t *img_copy, image_t *depth_copy)
{
image_t *img = NULL;
image_t *actual_img = NULL;
image_t *depth_map = NULL;
double x_scale = 1.0;
double y_scale = 1.0;
struct timeval timer;
double seconds;
int aa_pad = 0;
printf("using %i thread%s to render image\n", threads, threads!=1?"s":"");
if( mode == SIDE_SIDE_3D )
x_scale = 0.5;
if( mode == OVER_UNDER_3D )
y_scale = 0.5;
/* create output image */
img = calloc(1,sizeof(image_t));
dbl_image_init(img);
/* make image one larger in each dimension (Whitted’s method) */
if( recursive_aa )
aa_pad = 1;
image_set_size(img,width+aa_pad,height+aa_pad);
if( mode != HIDEF_3D ) {
vectNd_scale(&scn->cam.dirX,width/(double)height,&scn->cam.dirX);
} else {
vectNd_scale(&scn->cam.dirX,width/(double)1080,&scn->cam.dirX);
}
/* create depth-map image, if requested */
if( depth_name != NULL ) {
depth_map = calloc(1,sizeof(image_t));
dbl_image_init(depth_map);
image_set_size(depth_map,width,height);
}
actual_img = calloc(1,sizeof(image_t));
image_init(actual_img);
image_set_size(actual_img,width,height);
struct thr_info *info;
pthread_t *thr;
info = calloc(threads,sizeof(struct thr_info));
thr = calloc(threads,sizeof(pthread_t));
timer_start(&timer);
int i=0;
for(i=0; i<threads; ++i) {
info[i].img = img;
info[i].scn = scn;
info[i].name = name;
info[i].width = width+aa_pad;
info[i].height = height+aa_pad;
info[i].x_scale = x_scale;
info[i].y_scale = y_scale;
info[i].samples = samples;
info[i].mode = mode;
info[i].threads = threads;
info[i].thr_offset = i;
info[i].actual_img = actual_img;
info[i].depth_map = depth_map;
info[i].max_optic_depth = max_optic_depth;
if( threads > 1 )
pthread_create(&thr[i],NULL,render_lines_thread,&info[i]);
else
render_lines_thread(&info[i]);
}
if( threads > 1 ) {
for(i=0; i<threads; ++i) {
pthread_join(thr[i],NULL);
}
}
double initial_time = -1.0;
timer_elapsed(&timer,&seconds);
initial_time = seconds;
#ifdef WITH_MPI
if( mpiRank == 0 ) {
#endif /* WITH_MPI */
printf("\r \r");
printf("rendering took %.3fs\n", seconds);
#ifdef WITH_MPI
}
#endif /* WITH_MPI */
/* write initial image */
if( name != NULL ) {
#ifdef WITH_MPI
if( !img_copy ) {
mpi_collect_image(img);
if( depth_name )
mpi_collect_image(depth_map);
}
if( mpiRank == 0 ) {
#endif /* WITH_MPI */