forked from LucasQAQ/Rtr-assignment5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.frag
More file actions
768 lines (654 loc) · 25.8 KB
/
Copy pathtask2.frag
File metadata and controls
768 lines (654 loc) · 25.8 KB
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
//============================================================================
// PROJECT ID: SWS3005_12
//
// GROUP NUMBER: 12
//
// STUDENT NAME: HU ZHANPENG
// NUS User ID.: t0930152
//
// STUDENT NAME: NING JUNTING
// NUS User ID.: t0930085
//
// STUDENT NAME: LUO TANGWEN
// NUS User ID.: t0930258
//
// COMMENTS TO GRADER:
//
//============================================================================
//============================================================================
// Constants.
//============================================================================
const float PI = 3.1415926536;
const vec3 BACKGROUND_COLOR = vec3( 0.1, 0.2, 0.6 );
// Vertical field-of-view angle of camera. In radians.
const float FOVY = 50.0 * PI / 180.0;
// Use this for avoiding the "epsilon problem" or the shadow acne problem.
const float DEFAULT_TMIN = 10.0e-4;
// Use this for tmax for non-shadow ray intersection test.
const float DEFAULT_TMAX = 10.0e6;
// Equivalent to number of recursion levels (0 means ray-casting only).
// We are using iterations to replace recursions.
const int NUM_ITERATIONS = 2;
// Constants for the scene objects.
const int NUM_LIGHTS = 3;
const int NUM_MATERIALS = 11;
const int NUM_SPHERES = 22;
const int NUM_CUBES = 7;
const int NUM_PLANES = 5;
//============================================================================
// Define new struct types.
//============================================================================
struct Ray_t {
vec3 o; // Ray Origin.
vec3 d; // Ray Direction. A unit vector.
};
struct Plane_t {
// The plane equation is Ax + By + Cz + D = 0.
float A, B, C, D;
int materialID;
};
struct Sphere_t {
vec3 center;
float radius;
int materialID;
};
struct Cube_t {
vec3 center;
vec3 size;
int materialID;
};
struct Light_t {
vec3 position; // Point light 3D position.
vec3 I_a; // For Ambient.
vec3 I_source; // For Diffuse and Specular.
};
struct Material_t {
vec3 k_a; // Ambient coefficient.
vec3 k_d; // Diffuse coefficient.
vec3 k_r; // Reflected specular coefficient.
vec3 k_rg; // Global reflection coefficient.
float n; // The specular reflection exponent. Ranges from 0.0 to 128.0.
};
//============================================================================
// Global scene data.
//============================================================================
Plane_t Plane[NUM_PLANES];
Sphere_t Sphere[NUM_SPHERES];
Light_t Light[NUM_LIGHTS];
Material_t Material[NUM_MATERIALS];
Cube_t Cube[NUM_CUBES];
/////////////////////////////////////////////////////////////////////////////
// Initializes the scene.
/////////////////////////////////////////////////////////////////////////////
void InitScene() {
// Bottom Plane
Plane[0].A = 0.0;
Plane[0].B = 1.0;
Plane[0].C = 0.0;
Plane[0].D = 10.0;
Plane[0].materialID = 10;
// Left Plane
Plane[1].A = 1.0;
Plane[1].B = 0.0;
Plane[1].C = 0.0;
Plane[1].D = 30.0;
Plane[1].materialID = 10;
// Right Plane
Plane[2].A = 1.0;
Plane[2].B = 0.0;
Plane[2].C = 0.0;
Plane[2].D = -30.0;
Plane[2].materialID = 10;
// Front Plane
Plane[3].A = 0.0;
Plane[3].B = 0.0;
Plane[3].C = 1.0;
Plane[3].D = -20.0;
Plane[3].materialID = 10;
// Back Plane
Plane[4].A = 0.0;
Plane[4].B = 0.0;
Plane[4].C = 1.0;
Plane[4].D = 20.0;
Plane[4].materialID = 10;
// Back Left Baffle
Cube[0].center = vec3(-8.75, 0.5, -9.5);
Cube[0].size = vec3(16.3, 1.0, 1.0);
Cube[0].materialID = 9;
// Back Right Baffle
Cube[1].center = vec3(8.75, 0.5, -9.5);
Cube[1].size = vec3(16.3, 1.0, 1.0);
Cube[1].materialID = 9;
// Front Left Baffle
Cube[2].center = vec3(-8.75, 0.5, 9.5);
Cube[2].size = vec3(16.3, 1.0, 1.0);
Cube[2].materialID = 9;
// Front Right Baffle
Cube[3].center = vec3(8.75, 0.5, 9.5);
Cube[3].size = vec3(16.3, 1.0, 1.0);
Cube[3].materialID = 9;
// Left Baffle
Cube[4].center = vec3(-18.5, 0.5, 0.0);
Cube[4].size = vec3(1.0, 1.0, 15.8);
Cube[4].materialID = 9;
// Right Baffle
Cube[5].center = vec3(18.5, 0.5, 0.0);
Cube[5].size = vec3(1.0, 1.0, 15.8);
Cube[5].materialID = 9;
// Table Plane
Cube[6].center = vec3(0.0, -0.05, 0.0);
Cube[6].size = vec3(38.0, 0.1, 20.0);
Cube[6].materialID = 8;
// Black Ball
Sphere[0].center = vec3(-15.0, 0.5, 0.0);
Sphere[0].radius = 0.5;
Sphere[0].materialID = 0;
// Red Ball * 15
for (int i = 1; i <= 15; i++) {
Sphere[i].radius = 0.5;
Sphere[i].materialID = 1;
}
Sphere[1].center = vec3(-12.0, 0.5, 2.0);
Sphere[2].center = vec3(-12.0, 0.5, 1.0);
Sphere[3].center = vec3(-12.0, 0.5, 0.0);
Sphere[4].center = vec3(-12.0, 0.5, -1.0);
Sphere[5].center = vec3(-12.0, 0.5, -2.0);
Sphere[6].center = vec3(-11.13, 0.5, 1.5);
Sphere[7].center = vec3(-11.14, 0.5, 0.5);
Sphere[8].center = vec3(-11.14, 0.5, -0.5);
Sphere[9].center = vec3(-11.14, 0.5, -1.5);
Sphere[10].center = vec3(-10.26, 0.5, 1.0);
Sphere[11].center = vec3(-10.28, 0.5, 0.0);
Sphere[12].center = vec3(-10.28, 0.5, -1.0);
Sphere[13].center = vec3(-9.39, 0.5, 0.5);
Sphere[14].center = vec3(-9.39, 0.5, -0.5);
Sphere[15].center = vec3(-8.52, 0.5, 0.0);
// Purple Ball
Sphere[16].center = vec3(-7.52, 0.5, 0.0);
Sphere[16].radius = 0.5;
Sphere[16].materialID = 2;
// Blue Ball
Sphere[17].center = vec3(0.0, 0.5, 0.0);
Sphere[17].radius = 0.5;
Sphere[17].materialID = 3;
// Green Ball
Sphere[18].center = vec3(11.0, 0.5, 2.5);
Sphere[18].radius = 0.5;
Sphere[18].materialID = 4;
// Brown Ball
Sphere[19].center = vec3(11.0, 0.5, 0.0);
Sphere[19].radius = 0.5;
Sphere[19].materialID = 5;
// Yellow Ball
Sphere[20].center = vec3(11.0, 0.5, -2.5);
Sphere[20].radius = 0.5;
Sphere[20].materialID = 6;
// White Ball
Sphere[21].center = vec3(-5.0, 0.5, 4.5);
Sphere[21].radius = 0.5;
Sphere[21].materialID = 7;
// Black Plastic Material
Material[0].k_d = vec3(0.0, 0.0, 0.0);
Material[0].k_a = 0.2 * Material[0].k_d;
Material[0].k_r = vec3(1.0, 1.0, 1.0);
Material[0].k_rg = 0.5 * Material[0].k_r;
Material[0].n = 128.0;
// Red Plastic Material
Material[1].k_d = vec3(0.89, 0.09, 0.05);
Material[1].k_a = 0.2 * Material[1].k_d;
Material[1].k_r = vec3(1.0, 1.0, 1.0);
Material[1].k_rg = 0.5 * Material[1].k_r;
Material[1].n = 128.0;
// Purple Plastic Material
Material[2].k_d = vec3(0.86, 0.44, 0.84);
Material[2].k_a = 0.2 * Material[2].k_d;
Material[2].k_r = vec3(1.0, 1.0, 1.0);
Material[2].k_rg = 0.5 * Material[2].k_r;
Material[2].n = 128.0;
// Blue Plastic Material
Material[3].k_d = vec3(0.0, 0.0, 1.0);
Material[3].k_a = 0.2 * Material[3].k_d;
Material[3].k_r = vec3(1.0, 1.0, 1.0);
Material[3].k_rg = 0.5 * Material[3].k_r;
Material[3].n = 128.0;
// Green Plastic Material
Material[4].k_d = vec3(0.13, 0.55, 0.13);
Material[4].k_a = 0.2 * Material[4].k_d;
Material[4].k_r = vec3(1.0, 1.0, 1.0);
Material[4].k_rg = 0.5 * Material[4].k_r;
Material[4].n = 128.0;
// Brown Plastic Material
Material[5].k_d = vec3(0.78, 0.38, 0.08);
Material[5].k_a = 0.2 * Material[5].k_d;
Material[5].k_r = vec3(1.0, 1.0, 1.0);
Material[5].k_rg = 0.5 * Material[5].k_r;
Material[5].n = 128.0;
// Yellow Plastic Material
Material[6].k_d = vec3(1.0, 1.0, 0.0);
Material[6].k_a = 0.2 * Material[6].k_d;
Material[6].k_r = vec3(1.0, 1.0, 1.0);
Material[6].k_rg = 0.5 * Material[6].k_r;
Material[6].n = 128.0;
// White Plastic Material
Material[7].k_d = vec3(1.0, 1.0, 1.0);
Material[7].k_a = 0.2 * Material[7].k_d;
Material[7].k_r = vec3(1.0, 1.0, 1.0);
Material[7].k_rg = 0.5 * Material[7].k_r;
Material[7].n = 128.0;
// Table Material
Material[8].k_d = vec3(0.42, 0.56, 0.14);
Material[8].k_a = 0.2 * Material[8].k_d;
Material[8].k_r = vec3(1.0, 1.0, 1.0);
Material[8].k_rg = 0.5 * Material[8].k_r;
Material[8].n = 128.0;
// Baffle Material
Material[9].k_d = vec3(0.18, 0.17, 0.23);
Material[9].k_a = vec3(0.05, 0.05, 0.07);
Material[9].k_r = vec3(0.33, 0.33, 0.35);
Material[9].k_rg = 0.5 * Material[9].k_r;
Material[9].n = 38.4;
// Room material.
Material[10].k_d = vec3( 0.5, 0.5, 0.5 );
Material[10].k_a = 0.2 * Material[10].k_d;
Material[10].k_r = 2.0 * Material[10].k_d;
Material[10].k_rg = 0.5 * Material[10].k_r;
Material[10].n = 64.0;
// Light 0
Light[0].position = vec3(9.0 * cos(iTime), 15.0, 9.0 * sin(iTime));
Light[0].I_a = vec3(0.1, 0.1, 0.1);
Light[0].I_source = vec3(1.0, 1.0, 1.0);
// Light 1
Light[1].position = vec3(-29.0, 12.0, 19.0);
Light[1].I_a = vec3(0.1, 0.1, 0.1);
Light[1].I_source = vec3(1.0, 1.0, 1.0);
// Light 2
Light[2].position = vec3(29.0, 12.0, -19.0);
Light[2].I_a = vec3(0.1, 0.1, 0.1);
Light[2].I_source = vec3(1.0, 1.0, 1.0);
}
/////////////////////////////////////////////////////////////////////////////
// Computes intersection between a plane and a ray.
// Returns true if there is an intersection where the ray parameter t is
// between tmin and tmax, otherwise returns false.
// If there is such an intersection, outputs the value of t, the position
// of the intersection (hitPos) and the normal vector at the intersection
// (hitNormal).
/////////////////////////////////////////////////////////////////////////////
bool IntersectPlane( in Plane_t pln, in Ray_t ray, in float tmin, in float tmax,
out float t, out vec3 hitPos, out vec3 hitNormal )
{
vec3 N = vec3( pln.A, pln.B, pln.C );
float NRd = dot( N, ray.d );
float NRo = dot( N, ray.o );
float t0 = (-pln.D - NRo) / NRd;
if ( t0 < tmin || t0 > tmax ) return false;
// We have a hit -- output results.
t = t0;
hitPos = ray.o + t0 * ray.d;
hitNormal = normalize( N );
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Computes intersection between a plane and a ray.
// Returns true if there is an intersection where the ray parameter t is
// between tmin and tmax, otherwise returns false.
/////////////////////////////////////////////////////////////////////////////
bool IntersectPlane( in Plane_t pln, in Ray_t ray, in float tmin, in float tmax )
{
vec3 N = vec3( pln.A, pln.B, pln.C );
float NRd = dot( N, ray.d );
float NRo = dot( N, ray.o );
float t0 = (-pln.D - NRo) / NRd;
if ( t0 < tmin || t0 > tmax ) return false;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Computes intersection between a sphere and a ray.
// Returns true if there is an intersection where the ray parameter t is
// between tmin and tmax, otherwise returns false.
// If there is one or two such intersections, outputs the value of the
// smaller t, the position of the intersection (hitPos) and the normal
// vector at the intersection (hitNormal).
/////////////////////////////////////////////////////////////////////////////
bool IntersectSphere( in Sphere_t sph, in Ray_t ray, in float tmin, in float tmax,
out float t, out vec3 hitPos, out vec3 hitNormal )
{
vec3 translateRay = ray.o;
translateRay -= sph.center;
float a = 1.0;
float b = 2.0 * dot(ray.d, translateRay);
float c = dot(translateRay, translateRay) - sph.radius * sph.radius;
float d = b * b - 4.0 * a * c;
if (d == 0.0) {
float t0 = - b / (2.0 * a);
if ( t0 < tmin || t0 > tmax ) return false;
t = t0;
hitPos = ray.o + t * ray.d;
hitNormal = normalize(translateRay + t * ray.d);
return true;
}
if (d < 0.0) {
return false;
}
if (d > 0.0) {
float t1 = (-b + sqrt(d)) / (2.0 * a);
float t2 = (-b - sqrt(d)) / (2.0 * a);
bool flag1 = true;
bool flag2 = true;
if ( t1 < tmin || t1 > tmax ){
flag1 = false;
}
if ( t2 < tmin || t2 > tmax ){
flag2 = false;
}
if (flag1 == false && flag2 == false) {
return false;
}
if (flag1 == false && flag2 == true) {
t = t2;
hitPos = ray.o + t * ray.d;
hitNormal = normalize(translateRay + t * ray.d);
return true;
}
if (flag1 == true && flag2 == false) {
t = t1;
hitPos = ray.o + t * ray.d;
hitNormal = normalize(translateRay + t * ray.d);
return true;
}
if (flag1 == true && flag2 == true) {
t = min(t1, t2);
hitPos = ray.o + t * ray.d;
hitNormal = normalize(translateRay + t * ray.d);
return true;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Computes intersection between a sphere and a ray.
// Returns true if there is an intersection where the ray parameter t is
// between tmin and tmax, otherwise returns false.
/////////////////////////////////////////////////////////////////////////////
bool IntersectSphere( in Sphere_t sph, in Ray_t ray, in float tmin, in float tmax )
{
vec3 translateRay = ray.o;
translateRay -= sph.center;
float a = 1.0;
float b = 2.0 * dot(ray.d, translateRay);
float c = dot(translateRay, translateRay) - sph.radius * sph.radius;
float d = b * b - 4.0 * a * c;
if (d == 0.0) {
float t0 = - b / (2.0 * a);
if ( t0 < tmin || t0 > tmax ) return false;
return true;
}
if (d < 0.0) {
return false;
}
if (d > 0.0) {
float t1 = (-b + sqrt(d)) / (2.0 * a);
float t2 = (-b - sqrt(d)) / (2.0 * a);
bool flag1 = true;
bool flag2 = true;
if ( t1 < tmin || t1 > tmax ){
flag1 = false;
}
if ( t2 < tmin || t2 > tmax ){
flag2 = false;
}
if (flag1 == false && flag2 == false) {
return false;
}
return true;
}
}
bool IntersectCube( in Cube_t cube, in Ray_t ray, in float tmin, in float tmax,
out float t, out vec3 hitPos, out vec3 hitNormal )
{
vec3 minBound = cube.center - (cube.size / 2.0); // Compute the minimum boundaries of the cube.
vec3 maxBound = cube.center + (cube.size / 2.0); // Compute the maximum boundaries of the cube.
// Compute the intersection of ray and planes of cube
vec3 t1 = (minBound - ray.o) / ray.d;
vec3 t2 = (maxBound - ray.o) / ray.d;
// Compute the smallest and largest t that intersects the cube
vec3 t3 = min(t1, t2);
vec3 t4 = max(t1, t2);
// Compute entry and exit points.
float tstart = max(max(t3.x, t3.y), max(t3.y, t3.z));
float tend = min(min(t4.x, t4.y), min(t4.y, t4.z));
if(tend < tstart || tstart > tmax || tend < tmin)
return false;
t = tstart < tmin ? tend : tstart;
// Compute the hit position
hitPos = ray.o + ray.d * t;
// Compute the length between the cube center and the hit position.
vec3 d = abs(hitPos - cube.center) / (cube.size / 2.0);
float maxD = max(max(d.x, d.y), d.z); // Maximum dimension
if(maxD == d.x)
hitNormal = vec3(sign(hitPos.x - cube.center.x), 0.0, 0.0);
else if(maxD == d.y)
hitNormal = vec3(0.0, sign(hitPos.y - cube.center.y), 0.0);
else
hitNormal = vec3(0.0, 0.0, sign(hitPos.z - cube.center.z));
hitNormal = normalize(hitNormal);
return true;
}
bool IntersectCube( in Cube_t cube, in Ray_t ray, in float tmin, in float tmax)
{
vec3 minBound = cube.center - (cube.size / 2.0);
vec3 maxBound = cube.center + (cube.size / 2.0);
vec3 t1 = (minBound - ray.o) / ray.d;
vec3 t2 = (maxBound - ray.o) / ray.d;
vec3 t3 = min(t1, t2);
vec3 t4 = max(t1, t2);
float tstart = max(max(t3.x, t3.y), max(t3.y, t3.z));
float tend = min(min(t4.x, t4.y), min(t4.y, t4.z));
if(tend < tstart || tstart > tmax || tend < tmin)
return false;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// Computes (I_a * k_a) + k_shadow * I_source * [ k_d * (N.L) + k_r * (R.V)^n ].
// Input vectors L, N and V are pointing AWAY from surface point.
// Assume all vectors L, N and V are unit vectors.
/////////////////////////////////////////////////////////////////////////////
vec3 PhongLighting( in vec3 L, in vec3 N, in vec3 V, in bool inShadow,
in Material_t mat, in Light_t light )
{
if ( inShadow ) {
return light.I_a * mat.k_a;
}
else {
vec3 R = reflect( -L, N );
float N_dot_L = max( 0.0, dot( N, L ) );
float R_dot_V = max( 0.0, dot( R, V ) );
float R_dot_V_pow_n = ( R_dot_V == 0.0 )? 0.0 : pow( R_dot_V, mat.n );
return light.I_a * mat.k_a +
light.I_source * (mat.k_d * N_dot_L + mat.k_r * R_dot_V_pow_n);
}
}
/////////////////////////////////////////////////////////////////////////////
// Casts a ray into the scene and returns color computed at the nearest
// intersection point. The color is the sum of light from all light sources,
// each computed using Phong Lighting Model, with consideration of
// whether the interesection point is being shadowed from the light.
// If there is no interesection, returns the background color, and outputs
// hasHit as false.
// If there is intersection, returns the computed color, and outputs
// hasHit as true, the 3D position of the intersection (hitPos), the
// normal vector at the intersection (hitNormal), and the k_rg value
// of the material of the intersected object.
/////////////////////////////////////////////////////////////////////////////
vec3 CastRay( in Ray_t ray,
out bool hasHit, out vec3 hitPos, out vec3 hitNormal, out vec3 k_rg )
{
// Find whether and where the ray hits some object.
// Take the nearest hit point.
bool hasHitSomething = false;
float nearest_t = DEFAULT_TMAX; // The ray parameter t at the nearest hit point.
vec3 nearest_hitPos; // 3D position of the nearest hit point.
vec3 nearest_hitNormal; // Normal vector at the nearest hit point.
int nearest_hitMatID; // MaterialID of the object at the nearest hit point.
float temp_t;
vec3 temp_hitPos;
vec3 temp_hitNormal;
bool temp_hasHit;
for (int i = 0; i < NUM_PLANES; i++) {
if (IntersectPlane(Plane[i], ray, DEFAULT_TMIN, nearest_t, temp_t, temp_hitPos, temp_hitNormal) == true) {
temp_hasHit = true;
if (temp_t < nearest_t) {
nearest_t = temp_t;
nearest_hitPos = temp_hitPos;
nearest_hitNormal = temp_hitNormal;
nearest_hitMatID = Plane[i].materialID;
hasHitSomething = temp_hasHit;
}
}
}
for (int i = 0; i < NUM_CUBES; i++) {
if (IntersectCube(Cube[i], ray, DEFAULT_TMIN, nearest_t, temp_t, temp_hitPos, temp_hitNormal) == true) {
temp_hasHit = true;
if (temp_t < nearest_t) {
nearest_t = temp_t;
nearest_hitPos = temp_hitPos;
nearest_hitNormal = temp_hitNormal;
nearest_hitMatID = Cube[i].materialID;
hasHitSomething = temp_hasHit;
}
}
}
for (int i = 0; i < NUM_SPHERES; i++) {
if (IntersectSphere(Sphere[i], ray, DEFAULT_TMIN, nearest_t, temp_t, temp_hitPos, temp_hitNormal) == true) {
temp_hasHit = true;
if (temp_t < nearest_t) {
nearest_t = temp_t;
nearest_hitPos = temp_hitPos;
nearest_hitNormal = temp_hitNormal;
nearest_hitMatID = Sphere[i].materialID;
hasHitSomething = temp_hasHit;
}
}
}
// One of the output results.
hasHit = hasHitSomething;
if ( !hasHitSomething ) return BACKGROUND_COLOR;
vec3 I_local = vec3( 0.0 ); // Result color will be accumulated here.
for (int i = 0; i < NUM_LIGHTS; i++) {
bool inShadow = false;
Ray_t shadowRay;
shadowRay.o = nearest_hitPos;
shadowRay.d = normalize(Light[i].position - shadowRay.o);
for (int j = 0; j < NUM_PLANES; j++) {
if (IntersectPlane(Plane[j], shadowRay, DEFAULT_TMIN, length(Light[i].position - shadowRay.o)) == true) {
inShadow = true;
}
}
for (int j = 0; j < NUM_SPHERES; j++) {
if (IntersectSphere(Sphere[j], shadowRay, DEFAULT_TMIN, length(Light[i].position - shadowRay.o)) == true) {
inShadow = true;
}
}
for (int j = 0; j < NUM_CUBES; j++) {
if (IntersectCube(Cube[j], shadowRay, DEFAULT_TMIN, length(Light[i].position - shadowRay.o)) == true) {
inShadow = true;
}
}
I_local += PhongLighting(shadowRay.d, nearest_hitNormal, -ray.d, inShadow, Material[nearest_hitMatID], Light[i]);
}
// Populate output results.
hitPos = nearest_hitPos;
hitNormal = nearest_hitNormal;
k_rg = Material[nearest_hitMatID].k_rg;
return I_local;
}
void CalcMove(in int sph) {
vec3 holePos = vec3(18.0, 0.5, -9.0);
vec3 centerPos = Sphere[sph].center;
vec3 TargetMove = holePos - centerPos;
float TargetMovex = abs(TargetMove.x);
float TargetMovez = abs(TargetMove.z);
float TargetMoveLength = sqrt(TargetMovex * TargetMovex + TargetMovez * TargetMovez);
float S = TargetMovez / TargetMoveLength;
float C = TargetMovex / TargetMoveLength;
vec3 arrivePos = vec3(centerPos.x - 1.0 * C, 0.5, centerPos.z + 1.0 * S);
vec3 HitMove = arrivePos - Sphere[21].center;
float HitMovex = abs(HitMove.x);
float HitMovez = abs(HitMove.z);
float HitMoveLength = sqrt(HitMovex * HitMovex + HitMovez * HitMovez);
float s = HitMovez / HitMoveLength;
float c = HitMovex / HitMoveLength;
float T = 3.0;
float t = mod(iTime, T);
float Speed = (HitMoveLength + 2.0 * TargetMoveLength) / T;
float t1 = HitMoveLength / Speed;
float xDir = 1.0;
if (Sphere[sph].center.x < Sphere[21].center.x) {
xDir = -1.0;
}
float zDir = 1.0;
if (Sphere[sph].center.z < Sphere[21].center.z) {
zDir = -1.0;
}
if (t <= t1) {
Sphere[21].center.x += xDir * Speed * t * c;
Sphere[21].center.z += zDir * Speed * t * s;
}
else {
Sphere[21].center = arrivePos;
float delta = t - t1;
Sphere[sph].center.x += Speed * 0.5 * delta * C;
Sphere[sph].center.z -= Speed * 0.5 * delta * S;
vec3 dir = HitMove - 0.5 * TargetMove;
float dirLength = sqrt(dir.x * dir.x + dir.z * dir.z);
float ss = dir.z / dirLength;
float cc = dir.x / dirLength;
Sphere[21].center.x += Speed * 0.15 * delta * cc;
Sphere[21].center.z += Speed * 0.15 * delta * ss;
}
}
/////////////////////////////////////////////////////////////////////////////
// Execution of fragment shader starts here.
// 1. Initializes the scene.
// 2. Compute a primary ray for the current pixel (fragment).
// 3. Trace ray into the scene with NUM_ITERATIONS recursion levels.
/////////////////////////////////////////////////////////////////////////////
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
InitScene();
float T = 3.0;
int turn = int(mod(iTime / T, 4.0));
CalcMove(17 + turn);
// Scale pixel 2D position such that its y coordinate is in [-1.0, 1.0].
vec2 pixel_pos = (2.0 * fragCoord.xy - iResolution.xy) / iResolution.y;
// Position the camera.
vec3 cam_pos = vec3( Sphere[21].center.x - 20.0, 10.0, Sphere[21].center.z + 15.0 );
vec3 cam_lookat = Sphere[21].center;
//vec3 cam_pos = vec3( 11.0, 3.0, -4.0 );
//vec3 cam_lookat = vec3( 15.0, 1.0, -9.0 );
vec3 cam_up_vec = vec3( 0.0, 1.0, 0.0 );
// Set up camera coordinate frame in world space.
vec3 cam_z_axis = normalize( cam_pos - cam_lookat );
vec3 cam_x_axis = normalize( cross(cam_up_vec, cam_z_axis) );
vec3 cam_y_axis = normalize( cross(cam_z_axis, cam_x_axis));
// Create primary ray.
float pixel_pos_z = -1.0 / tan(FOVY / 2.0);
Ray_t pRay;
pRay.o = cam_pos;
pRay.d = normalize( pixel_pos.x * cam_x_axis + pixel_pos.y * cam_y_axis + pixel_pos_z * cam_z_axis );
// Start Ray Tracing.
// Use iterations to emulate the recursion.
vec3 I_result = vec3( 0.0 );
vec3 compounded_k_rg = vec3( 1.0 );
Ray_t nextRay = pRay;
for ( int level = 0; level <= NUM_ITERATIONS; level++ )
{
bool hasHit;
vec3 hitPos, hitNormal, k_rg;
vec3 I_local = CastRay( nextRay, hasHit, hitPos, hitNormal, k_rg );
I_result += compounded_k_rg * I_local;
if ( !hasHit ) break;
compounded_k_rg *= k_rg;
nextRay = Ray_t( hitPos, normalize( reflect(nextRay.d, hitNormal) ) );
}
fragColor = vec4( I_result, 1.0 );
}