-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcRay21qb64v2.txt
More file actions
3221 lines (2745 loc) · 125 KB
/
TcRay21qb64v2.txt
File metadata and controls
3221 lines (2745 loc) · 125 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
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
'TCRAY21 by Jark 2003
'Ported to QB64PE by Antoni Gual 2026
' Shared variables for SVGA management
Dim Shared xPlotMin%, xPlotMax%
Dim Shared yPlotMin%, yPlotMax%
Common Shared SizeScreen As Integer
Common Shared ScreenRes$, Name$
Common Shared ScrWidth As Long, ScrHeight As Long
Common Shared Red%, Green%, Blue%
Common Shared xScrCenter%, yScrCenter%
Const eps = 9.98012604599318D-322 ' See Antoni's version
'CONST eps = 1.401298E-45 ' Avoids logarithm overflow
' Constants for the TC-Lib programs
Const SaveDir$ = "" ' For files management
Const Pi = 3.14159265358979# ' For angles management
Const SQR12 = .7071068 'SQR(1/2)
Const SQR16 = .4082483 'SQR(1/6)
Const SQR23 = .8164966 'SQR(2/3)
Const SatCoeff = 84.51663 '100/ATN(SQR(6))
Const SatCoef = 1.183199E-02 '1/SatCoeff
Const PiOver180 = .0174533
Const PiOver2 = 1.5707963267949#
Const Gamma = 1.4, Aperture = 1
Dim Shared xCam(3), xLight(3), xCenter(3), UScreen(3), VScreen(3)
Type shapetyp
Degree As Integer
ShapeType As String * 2
NumType As Integer
HueRef As Single
SatRef As Single
x0 As Single
y0 As Single
z0 As Single
dRef1 As Single
dRef2 As Single
Brilliance As Single
Reflect As Single
Texture As Integer
Bump As Integer
End Type
Dim Shared NbObjects%
Dim Shared shape(64) As shapetyp, Object(64, 35), Matrix(64, 3, 3)
Dim Shared cReflect, cShadow, AntiAlias
Dim Shared t!
' Shared variables for the Perlin noises
Const octaves% = 7
Dim Shared Seed&, Noise%(129, 129), Persistence!, Span%, Amplitude!(16)
' Powers of two precalculation
Dim Shared Power2%(15)
For k% = 0 To 14: Power2%(k%) = 2 ^ k%: Next k%: Power2%(15) = &H8000
Clear
SizeScreen = 2
top:
NbObjects% = 0
Erase shape
ScrWidth = 1024
ScrHeight = 768
Screen _NewImage(ScrWidth, ScrHeight, 32)
_ScreenMove _Middle
_Title "Jark's TCRay Raytracer"
xPlotMin% = 0
xPlotMax% = ScrWidth - 1
yPlotMin% = 0
yPlotMax% = ScrHeight - 1
xScrCenter% = Int((xPlotMin% + xPlotMax%) / 2 + .5)
yScrCenter% = Int((yPlotMin% + yPlotMax%) / 2 + .5)
_Font _LoadFont("cour.ttf", 24)
Menu
t! = Timer
Draw.Space
t! = Timer - t!
Sleep
Do While Len(InKey$): Loop
GoTo top:
EndProg
Function Acos (x#)
' Returns the ArcCosinus of a real number
If x# = 0 Then Acos = PiOver2: Exit Function
If x# > 0 Then
Acos = Atn(Sqr(1 - x# * x#) / x#)
Else
Acos = Pi + Atn(Sqr(1 - x# * x#) / x#)
End If
End Function
Function Arg (xa, ya)
' Returns the argument (in radians) of a point in the (x,y) plan
' Ranges from -Pi to Pi
If xa = 0 And ya = 0 Then Arg = 0: Exit Function
If xa = 0 And ya >= 0 Then Arg = Pi / 2: Exit Function
If ya = 0 And xa < 0 Then Arg = Pi: Exit Function
If xa = 0 And ya < 0 Then Arg = -Pi / 2: Exit Function
If xa > 0 Then Arg = Atn(ya / xa): Exit Function
If xa < 0 And ya >= 0 Then Arg = Pi - Atn(-ya / xa): Exit Function
If xa < 0 And ya < 0 Then Arg = -Pi + Atn(ya / xa): Exit Function
End Function
Function CosD (A) Static
' Corrects the wrong values provided by thea standard COS function
' a is in degrees
A = A - Int(A / 360 + .5) * 360
If A >= 0 And A <= 90 Then
x# = A / 180 * Pi
If Cos(x#) < .5 Then
CosD = Sin(Pi / 2 - x#)
Else
CosD = Cos(x#)
End If
Exit Function
End If
If A < 0 And A >= -90 Then
x# = -A / 180 * Pi
If Cos(x#) < .5 Then
CosD = Sin(Pi / 2 - x#)
Else
CosD = Cos(x#)
End If
Exit Function
End If
If A > 90 And A <= 180 Then
x# = (180 - A) / 180 * Pi
If Cos(x#) < .5 Then
CosD = Sin(x# - Pi / 2)
Else
CosD = -Cos(x#)
End If
Exit Function
End If
If A < -90 And A >= -180 Then
x# = (A - 180) / 180 * Pi
If Cos(x#) < .5 Then
CosD = -Sin(Pi / 2 - x#)
Else
CosD = -Cos(x#)
End If
Exit Function
End If
End Function
Sub EndProg
' Quit the program
Cls
Print
Print
Print
Print
Print " ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"
Print " º Thanks for using the TC-Lib º"
Print " º Please report any bug, remark, comment... to º"
Print " º mandelbrot.dazibao@free.fr º"
Print " º and feel free to leave your footprint on º"
Print " º http://mandelbrot.dazibao.free.fr º"
Print " ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ"
_Display
Sleep
End
End Sub
Sub FullScreen
' The Pset24 routine can plot any pixel on the screen
xPlotMin% = 0
xPlotMax% = ScrWidth - 1
yPlotMin% = 0
yPlotMax% = ScrHeight - 1
xScrCenter% = Int((xPlotMin% + xPlotMax%) / 2 + .5)
yScrCenter% = Int((yPlotMin% + yPlotMax%) / 2 + .5)
End Sub
Function Gamma.Correction (Value) Static
' Corrects the monitor rendering for dark pixels
' The Gamma correction coefficient should be set once and for all
' for you own computer monitor
' Gamma = 1 > no correction
Gamma.Correction = ((Value / 100) ^ (1 / Gamma)) * 100
End Function
Sub HSVtoRGB (Hue, Sat, Value) Static
' ======================================================================
' Optimised version (35% faster than the previous version of the TC-Lib)
' ======================================================================
' Converts a HSV colour definition into RGB values via exact
' trigonometry calculations. Refer to HSVtoRGB1 for a simplified,
' faster but poorer conversion algorithm.
' Hue ranges from 0 to 360 degrees. Red corresponds to Hue = 0
' Saturation ranges from 0 to 100
' Value ranges from 0 to 100
' Saturation = 0 provides grey tones, from black (Value = 0)
' to white (Value = 100).
' Red%, Green% and Blue% are shared variables
' The routine uses 4 constants:
' SQR(1/2) = .7071068
' SQR(1/6) = .4082483
' ATN(SQR(6)/100 = 1.183199E-02
' Pi/180 = .0174533
Angle = (Hue - 150) * PiOver180
Ur = Value * 2.55
Radius = Ur * Tan(Sat * SatCoef)
Vr = Radius * Cos(Angle) * SQR12
Wr = Radius * Sin(Angle) * SQR16
Red% = Ur - Vr - Wr
Green% = Ur + Vr - Wr
Blue% = Ur + Wr + Wr
If Red% < 0 Then
Rdim = Ur / (Vr + Wr)
Red% = 0
Green% = Ur + (Vr - Wr) * Rdim
Blue% = Ur + 2 * Wr * Rdim
GoTo Ctrl255
End If
If Green% < 0 Then
Rdim = -Ur / (Vr - Wr)
Red% = Ur - (Vr + Wr) * Rdim
Green% = 0
Blue% = Ur + 2 * Wr * Rdim
GoTo Ctrl255
End If
If Blue% < 0 Then
Rdim = -Ur / (Wr + Wr)
Red% = Ur - (Vr + Wr) * Rdim
Green% = Ur + (Vr - Wr) * Rdim
Blue% = 0
GoTo Ctrl255
End If
Ctrl255:
If Red% > 255 Then
Rdim = (Ur - 255) / (Vr + Wr)
Red% = 255
Green% = Ur + (Vr - Wr) * Rdim
Blue% = Ur + 2 * Wr * Rdim
End If
If Green% > 255 Then
Rdim = (255 - Ur) / (Vr - Wr)
Red% = Ur - (Vr + Wr) * Rdim
Green% = 255
Blue% = Ur + 2 * Wr * Rdim
End If
If Blue% > 255 Then
Rdim = (255 - Ur) / (Wr + Wr)
Red% = Ur - (Vr + Wr) * Rdim
Green% = Ur + (Vr - Wr) * Rdim
Blue% = 255
End If
End Sub
Function Module (x, y, z)
' Returns the module of a 3D vector
Module = Sqr(x * x + y * y + z * z)
End Function
Sub RGBtoHSV (Hue, Saturation, Value) Static
' Returns the HSV codes of the current RGB combination
Temp = (Red% + Green% + Blue%) / 3
xa = (Green% - Red%) * SQR12
ya = (Blue% + Blue% - Red% - Green%) * SQR16
Hue = Arg(xa, ya) / PiOver180 + 150
Saturation = Arg(Temp, Module(Red% - Temp, Green% - Temp, Blue% - Temp)) * SatCoeff
Value = Temp / 2.55
If Saturation = 0 Or Value = 0 Then Hue = 0
If Hue < 0 Then Hue = Hue + 360
If Hue >= 360 Then Hue = Hue - 360
End Sub
Function ScalProd (u(), v())
' Returns the scalar product of two vectors
ScalProd = u(1) * v(1) + u(2) * v(2) + u(3) * v(3)
End Function
Function SinD (A)
' Correct Sine value for an angle in degrees
SinD = CosD(90 - A)
End Function
Function Sqr3 (x#)
' Cubic root of a double length real number
If x# = 0 Then Sqr3 = 0: Exit Function
If x# > 0 Then
Sqr3 = Exp(Log(x#) / 3)
Else
Sqr3 = -Exp(Log(-x#) / 3)
End If
End Function
'
' ==============================================================
Sub Calc.Normal (Id%, xh!(), xn!(), Ray!()) Static
' Get the normal vector xn on object number Id%
' The hit point is xh is the object axis system
' The normal vector is returned in the main axis system for brilliance calculation
' xo is the same vector, but in the object axis system
' The ray is defined in the main axis system
Dim xo!(3)
'=======================================================
' Calculate the normal vector in object axis system
Select Case shape(Id%).Degree
Case 1
xo!(1) = Object(Id%, 32)
xo!(2) = Object(Id%, 33)
xo!(3) = Object(Id%, 34)
Case 2
xo!(1) = (2 * Object(Id%, 26) * xh!(1) + Object(Id%, 29) * xh!(2) + Object(Id%, 31) * xh!(3) + Object(Id%, 32))
xo!(2) = (2 * Object(Id%, 27) * xh!(2) + Object(Id%, 29) * xh!(1) + Object(Id%, 30) * xh!(3) + Object(Id%, 33))
xo!(3) = (2 * Object(Id%, 28) * xh!(3) + Object(Id%, 30) * xh!(2) + Object(Id%, 31) * xh!(3) + Object(Id%, 34))
Case 3
xo!(1) = 3 * Object(Id%, 17) * xh!(1) ^ 2 + Object(Id%, 16) * xh!(2) * xh!(3) + 2 * Object(Id%, 20) * xh!(1) * xh!(2) + 2 * Object(Id%, 21) * xh!(1) * xh!(3) + Object(Id%, 23) * xh!(2) ^ 2 + Object(Id%, 24) * xh!(3) ^ 2
xo!(1) = xo!(1) + 2 * Object(Id%, 26) * xh!(1) + Object(Id%, 29) * xh!(2) + Object(Id%, 31) * xh!(3) + Object(Id%, 32)
xo!(2) = 3 * Object(Id%, 18) * xh!(2) ^ 2 + Object(Id%, 16) * xh!(3) * xh!(1) + 2 * Object(Id%, 22) * xh!(2) * xh!(3) + 2 * Object(Id%, 23) * xh!(2) * xh!(1) + Object(Id%, 25) * xh!(3) ^ 2 + Object(Id%, 20) * xh!(1) ^ 2
xo!(2) = xo!(2) + 2 * Object(Id%, 27) * xh!(2) + Object(Id%, 30) * xh!(3) + KYX * xh!(1) + Object(Id%, 33)
xo!(3) = 3 * Object(Id%, 19) * xh!(3) ^ 2 + Object(Id%, 16) * xh!(1) * xh!(2) + 2 * Object(Id%, 24) * xh!(3) * xh!(1) + 2 * Object(Id%, 25) * xh!(3) * xh!(2) + Object(Id%, 21) * xh!(1) ^ 2 + Object(Id%, 22) * xh!(2) ^ 2
xo!(3) = xo!(3) + 2 * Object(Id%, 28) * xh!(3) + Object(Id%, 31) * xh!(1) + Object(Id%, 30) * xh!(2) + Object(Id%, 34)
Case 4
xo!(1) = 4 * Object(Id%, 1) * xh!(1) * xh!(1) * xh!(1)
xo!(1) = xo!(1) + 2 * Object(Id%, 10) * xh!(1) * xh!(2) * xh!(2)
xo!(1) = xo!(1) + 2 * Object(Id%, 11) * xh!(1) * xh!(3) * xh!(3)
xo!(1) = xo!(1) + 3 * Object(Id%, 4) * xh!(1) * xh!(1) * xh!(2)
xo!(1) = xo!(1) + 3 * Object(Id%, 5) * xh!(1) * xh!(1) * xh!(3)
xo!(1) = xo!(1) + Object(Id%, 7) * xh!(2) * xh!(2) * xh!(2)
xo!(1) = xo!(1) + Object(Id%, 8) * xh!(3) * xh!(3) * xh!(3)
xo!(1) = xo!(1) + 3 * Object(Id%, 17) * xh!(1) * xh!(1)
xo!(1) = xo!(1) + Object(Id%, 16) * xh!(2) * xh!(3)
xo!(1) = xo!(1) + 2 * Object(Id%, 20) * xh!(1) * xh!(2)
xo!(1) = xo!(1) + 2 * Object(Id%, 21) * xh!(1) * xh!(3)
xo!(1) = xo!(1) + Object(Id%, 23) * xh!(2) * xh!(2)
xo!(1) = xo!(1) + Object(Id%, 24) * xh!(3) * xh!(3)
xo!(1) = xo!(1) + 2 * Object(Id%, 26) * xh!(1)
xo!(1) = xo!(1) + Object(Id%, 29) * xh!(2)
xo!(1) = xo!(1) + Object(Id%, 31) * xh!(3)
xo!(1) = xo!(1) + Object(Id%, 32)
xo!(2) = 4 * Object(Id%, 2) * xh!(2) * xh!(2) * xh!(2)
xo!(2) = xo!(2) + 2 * Object(Id%, 12) * xh!(2) * xh!(3) * xh!(3)
xo!(2) = xo!(2) + 2 * Object(Id%, 10) * xh!(2) * xh!(1) * xh!(1)
xo!(2) = xo!(2) + 3 * Object(Id%, 6) * xh!(2) * xh!(2) * xh!(3)
xo!(2) = xo!(2) + 3 * Object(Id%, 7) * xh!(2) * xh!(2) * xh!(1)
xo!(2) = xo!(2) + Object(Id%, 9) * xh!(3) * xh!(3) * xh!(3)
xo!(2) = xo!(2) + Object(Id%, 4) * xh!(1) * xh!(1) * xh!(1)
xo!(2) = xo!(2) + 3 * Object(Id%, 18) * xh!(2) * xh!(2)
xo!(2) = xo!(2) + Object(Id%, 16) * xh!(3) * xh!(1)
xo!(2) = xo!(2) + 2 * Object(Id%, 22) * xh!(2) * xh!(3)
xo!(2) = xo!(2) + 2 * Object(Id%, 23) * xh!(2) * xh!(1)
xo!(2) = xo!(2) + Object(Id%, 25) * xh!(3) * xh!(3)
xo!(2) = xo!(2) + Object(Id%, 20) * xh!(1) * xh!(1)
xo!(2) = xo!(2) + 2 * Object(Id%, 27) * xh!(2)
xo!(2) = xo!(2) + Object(Id%, 30) * xh!(3)
xo!(2) = xo!(2) + KYX * xh!(1)
xo!(2) = xo!(2) + Object(Id%, 33)
xo!(3) = 4 * Object(Id%, 3) * xh!(3) * xh!(3) * xh!(3)
xo!(3) = xo!(3) + 2 * Object(Id%, 11) * xh!(3) * xh!(1) * xh!(1)
xo!(3) = xo!(3) + 2 * Object(Id%, 12) * xh!(3) * xh!(2) * xh!(2)
xo!(3) = xo!(3) + 3 * Object(Id%, 8) * xh!(3) * xh!(3) * xh!(1)
xo!(3) = xo!(3) + 3 * Object(Id%, 9) * xh!(3) * xh!(3) * xh!(2)
xo!(3) = xo!(3) + Object(Id%, 5) * xh!(1) * xh!(1) * xh!(1)
xo!(3) = xo!(3) + Object(Id%, 6) * xh!(2) * xh!(2) * xh!(2)
xo!(3) = xo!(3) + 3 * Object(Id%, 19) * xh!(3) * xh!(3)
xo!(3) = xo!(3) + Object(Id%, 16) * xh!(1) * xh!(2)
xo!(3) = xo!(3) + 2 * Object(Id%, 24) * xh!(3) * xh!(1)
xo!(3) = xo!(3) + 2 * Object(Id%, 25) * xh!(3) * xh!(2)
xo!(3) = xo!(3) + Object(Id%, 21) * xh!(1) * xh!(1)
xo!(3) = xo!(3) + Object(Id%, 22) * xh!(2) * xh!(2)
xo!(3) = xo!(3) + 2 * Object(Id%, 28) * xh!(3)
xo!(3) = xo!(3) + Object(Id%, 31) * xh!(1)
xo!(3) = xo!(3) + Object(Id%, 30) * xh!(2)
xo!(3) = xo!(3) + Object(Id%, 34)
Case Else
End Select
' Normalise the vector
ModNorm = Module(xo!(1), xo!(2), xo!(3))
If ModNorm = 0 Then Exit Sub
For k% = 1 To 3: xo!(k%) = xo!(k%) / ModNorm: Next k%
' Convert to main axis system
For k% = 1 To 3
xn!(k%) = xo!(1) * Matrix(Id%, 1, k%) + xo!(2) * Matrix(Id%, 2, k%) + xo!(3) * Matrix(Id%, 3, k%)
Next k%
' Reverse the vector if necessary
Side = Ray!(4) * xn!(1) + Ray!(5) * xn!(2) + Ray!(6) * xn!(3)
If Side < 0 Then
For k% = 1 To 3: xn!(k%) = -xn!(k%): Next k%
End If
End Sub
Sub Draw.Space
' Main raytracing routine
Dim Ray(7)
Dim xhHit(3), xtHit(3), xnHit(3)
Dim xhReflect(3), xtReflect(3), xnReflect(3)
Dim xhShadow(3), xtShadow(3), xnShadow(3)
' Main loop: one calculation per pixel
For Ny% = 0 To ScrHeight - 1
For Nx% = 0 To ScrWidth - 1
' Probe the objects with the pixel ray
Probe% = 1: AntiAlias = 0
HitAliasCheck:
Ray.Pixel Nx%, Ny%, Ray(): ShapeHit% = 0
Probe.Ray Ray(), ShapeHit%, Dist, xhHit(), xtHit(), xnHit(), FlagHit%
' AntiAlias checking: iterate until two similar hits in case of a change
If shape(ShapeHit%).Degree = 4 Or shape(LastShape%).Degree = 4 Then
If Probe% = 1 And (ShapeHit% <> LastShape% Or FlagHit% <> LastFlag%) Then
Probe% = 2: AntiAlias = 1
Hit1% = ShapeHit%: Flag1% = FlagHit%
GoTo HitAliasCheck:
End If
If Probe% = 2 And (ShapeHit% <> Hit1% Or FlagHit% <> Flag1%) Then
Probe% = 2: AntiAlias = 0
Hit1% = ShapeHit%: Flag1% = FlagHit%
GoTo HitAliasCheck:
End If
End If
LastShape% = ShapeHit%: LastFlag% = FlagHit%
' Calculate the pixel colour in case a shape is hit
If FlagHit% <> 0 Then
' Check if a shadow must be applied
Probe% = 1: AntiAlias = 0
d1 = xtHit(1) - xLight(1)
d2 = xtHit(2) - xLight(2)
d3 = xtHit(3) - xLight(3)
DistToLight = Module(d1, d2, d3)
ShadowAliasCheck:
Ray.Shadow xtHit(), Ray(): ShapeShadow% = ShapeHit%
Probe.Ray Ray(), ShapeShadow%, DistShadow, xhShadow(), xtShadow(), xnShadow(), FlagShadow%
'IF DistShadow > DistToLight THEN FlagShadow% = 0
If DistShadow > 30 Then FlagShadow% = 0
' AntiAlias checking: iterate until two similar hits in case of a change
If shape(ShapeShadow%).Degree = 4 Or shape(LastShadow%).Degree = 4 Then
If Probe% = 1 And FlagShadow% <> LastShadow% Then
Probe% = 2: AntiAlias = 0
Flag1% = FlagShadow%
GoTo ShadowAliasCheck:
End If
If Probe% = 2 And FlagShadow% <> Flag1% Then
Probe% = 2: AntiAlias = 1
Flag1% = FlagShadow%
GoTo ShadowAliasCheck:
End If
End If
LastShadow% = FlagShadow%
If FlagShadow% <> 0 Then
Shadow = cShadow / (1 + .5 * DistShadow) * shape(ShapeHit%).Brilliance
Else
Shadow = shape(ShapeHit%).Brilliance
End If
' Calculate the pixel colour without reflection
HSVtoRGB Texture.Object!(ShapeHit%, FlagHit%, xhHit(), xnHit()), shape(ShapeHit%).SatRef, Shadow * Light.Specular(xtHit(), xnHit())
' Check if a reflection colour must be added
If shape(ShapeHit%).Reflect <> 0 Then
Probe% = 1: AntiAlias = 0
ReflectAliasCheck:
Ray.Reflect xtHit!(), xnHit!(), Ray(): ShapeReflect% = ShapeHit%
Probe.Ray Ray(), ShapeReflect%, DistReflect, xhReflect(), xtReflect(), xnReflect(), FlagReflect%
' AntiAlias checking: iterate until two similar hits in case of a change
If shape(ShapeReflect%).Degree = 4 Or shape(LastReflect%).Degree = 4 Then
If Probe% = 1 And FlagReflect% <> LastReflect% Then
Probe% = 2: AntiAlias = 0
Flag1% = FlagReflect%
GoTo ReflectAliasCheck:
End If
If Probe% = 2 And FlagReflect% <> Flag1% Then
Probe% = 2: AntiAlias = 1
Flag1% = FlagReflect%
GoTo ReflectAliasCheck:
End If
End If
LastReflect% = FlagReflect%
' Merge the two colours in case of reflection
If FlagReflect% <> 0 Then
r1% = Red%: g1% = Green%: B1% = Blue%
HSVtoRGB Texture.Object!(ShapeReflect%, FlagReflect%, xtReflect(), xnReflect()), shape(ShapeReflect%).SatRef, shape(ShapeReflect%).Brilliance * Light.Specular(xtReflect(), xnReflect())
r2% = Red%: g2% = Green%: B2% = Blue%
Reflection = shape(ShapeHit%).Reflect / (1 + .15 * DistReflect)
Red% = r1% + Reflection * r2%
Green% = g1% + Reflection * g2%
Blue% = B1% + Reflection * B2%
If Red% > 255 Then Red% = 255
If Green% > 255 Then Green% = 255
If Blue% > 255 Then Blue% = 255
End If
End If
' Apply Gamma and exposure corrections if wanted
If Gamma <> 1 Then
RGBtoHSV Hue, Sat, Value
'gamma correction
Value = ((Value / 100) ^ (1 / Gamma)) * 100
'exposure control
Value = (1 - Exp(-Value * .01 * Aperture)) * 100
HSVtoRGB Hue, Sat, Value
End If
' Plot the point...
tt& = _RGB(Red%, Green%, Blue%)
PReset (Nx%, Ny%), tt&
End If
Next Nx%
_Display
If Len(InKey$) Then Exit Sub 'agv. to be able to kill the rendering
Next Ny%
End Sub
Sub Equa1 (A1#, a0#, x1#, Err1%, nSol1%)
' First degree equation resolution
If A1# = 0 And a0# = 0 Then Err1% = 1: nSol1% = 0: Exit Sub
If A1# = 0 And a0# <> 0 Then Err1% = 0: nSol1% = 0: Exit Sub
If A1# <> 0 Then Err1% = 0: x1# = -a0# / A1#: nSol1% = 1: Exit Sub
End Sub
Sub Equa2 (A2#, A1#, a0#, x1#, x2#, Err2%, nSol2%)
' Second degree equation resolution - Discriminant method
Err2% = 0
If A2# = 0 And A1# = 0 And a0# = 0 Then Err2% = 1: nSol2% = 0: Exit Sub
If A2# = 0 And A1# = 0 And a0# <> 0 Then nSol2% = 0: Exit Sub
If A2# = 0 Then
Equa1 A1#, a0#, x1#, Err2%, nSol2%
Else
Delta2# = A1# * A1# - 4 * A2# * a0#
If Delta2# >= 0 Then
nSol2% = 2
x1# = -(A1# + Sqr(Delta2#)) * .5 / A2#
x2# = (Sqr(Delta2#) - A1#) * .5 / A2#
Else
nSol2% = 0: Exit Sub
End If
End If
End Sub
Sub Equa3 (a30#, a20#, a10#, a00#, x1#, x2#, x3#, Err3%, nSol3%)
' Third degree equation resolution - Cardan method
Err3% = 0
If a30# = 0 And a20# = 0 And a10# = 0 And a00# = 0 Then
nSol3% = 0
Err3% = 1
Exit Sub
End If
If a30# = 0 And a20# = 0 And a10# = 0 And a00# <> 0 Then
nSol3% = 0
Exit Sub
End If
If a30# = 0 And a20# = 0 Then
Equa1 a10#, a00#, x1#, Err3%, nSol3%
Exit Sub
End If
If a30# = 0 Then
Equa2 a20#, a10#, a00#, x1#, x2#, Err3%, nSol3%
Exit Sub
End If
A3# = 1
A2# = a20# / a30#
A1# = a10# / a30#
a0# = a00# / a30#
p# = A1# - 1 / 3 * (A2#) ^ 2
q# = 2 / 27 * (A2#) ^ 3 + a0# - A2# / 3 * A1#
Delta3# = q# ^ 2 + 4 / 27 * p# ^ 3
If Delta3# > 0 Then
x1# = Sqr3(-q# / 2 + Sqr(Delta3#) / 2) + Sqr3(-q# / 2 - Sqr(Delta3#) / 2) - A2# / 3
nSol3% = 1
Exit Sub
End If
nSol3% = 3
If Delta3# = 0 Then
If p# <> 0 Then
x1# = q# / p# * 3 - A2# / 3
x2# = -Sqr(-p# / 3) - A2# / 3
x3# = x2#
If x1# > x2# Then Swap x1#, x2#
If x1# > x3# Then Swap x1#, x3#
If x2# > x3# Then Swap x2#, x3#
Else
x1# = -A2# / 3: x2# = x1#: x3# = x1#
End If
Exit Sub
End If
If Delta3# < 0 Then
r# = Sqr(-p# ^ 3 / 27)
CosTeta3# = -q# / 2 / r#
Teta3# = Acos(CosTeta3#)
x1# = 2 * Sqr(-p# / 3) * Cos(Teta3# / 3) - A2# / 3
x2# = 2 * Sqr(-p# / 3) * Cos(Teta3# / 3 + 2 * Pi / 3) - A2# / 3
x3# = 2 * Sqr(-p# / 3) * Cos(Teta3# / 3 + 4 * Pi / 3) - A2# / 3
If x1# > x2# Then Swap x1#, x2#
If x1# > x3# Then Swap x1#, x3#
If x2# > x3# Then Swap x2#, x3#
End If
End Sub
Sub Equa4 (a40#, a30#, a20#, a10#, a00#, x1#, x2#, x3#, x4#, Err4%, nSol4%)
' 4th degree polynomial equations - Ferrari method
' If a40#=0, the equation is a cubic
If a40# = 0 Then
Equa3 a30#, a20#, a10#, a00#, x1#, x2#, x3#, Err4%, nSol4%
Exit Sub
End If
' Change the coefficients so that a4#=1
a4# = 1
A3# = a30# / a40#
A2# = a20# / a40#
A1# = a10# / a40#
a0# = a00# / a40#
' The equation to be solved is now
' x^4 + a3*x^3 + a2*x^2 + a1*x + a0 = 0 (1)
' Change variable to remove the cubic term
' X = x + a3#/4
' The original equation becomes:
' X^4 + A*X^2 + B*X + C = 0 (2)
a# = A2# - 3 / 8 * A3# ^ 2
b# = A3# ^ 3 / 8 - A3# * A2# / 2 + A1#
C# = A3# ^ 2 * A2# / 16 - 3 / 256 * A3# ^ 4 - A1# * A3# / 4 + a0#
' If B = 0 then (2) is a bi-quadric equation:
If b# = 0 Then
Equa2 1, a#, C#, x21#, x22#, Err2%, nSol2%
If x21# < 0 And x22# < 0 Then nSol4% = 0: Exit Sub
If x21# >= 0 And x22# >= 0 Then
nSol4% = 4
x1# = Sqr(x21#) - A3# / 4
x2# = -Sqr(x21#) - A3# / 4
x3# = Sqr(x22#) - A3# / 4
x4# = -Sqr(x22#) - A3# / 4
Exit Sub
End If
If x21# >= 0 And x22# < 0 Then
nSol4% = 2
x1# = Sqr(x21#) - A3# / 4
x2# = -Sqr(x21#) - A3# / 4
x3# = 0
x4# = 0
Exit Sub
End If
If x21# < 0 And x22# >= 0 Then
nSol4% = 2
x1# = Sqr(x22#) - A3# / 4
x2# = -Sqr(x22#) - A3# / 4
x3# = 0
x4# = 0
Exit Sub
End If
End If
' If B<>0, we must introduce an auxiliary variable "u".
' Let's calculate (X^2 + u/2)^2:
' (X^2 + u/2)^2 = (u-A)*X^2 - B*X + u^2/4 - C (3)
' We want (3) to be equivalent to (2) for any "u"
' That means the second term of (3) is a double-root second degree polynom
' This condition is obtained if its discriminant is nul
' That leads to the cubic solvant equation:
' u^3 - A * u^2 - 4 * C * u + (4*A*C - B^2) = 0
p# = -a#
q# = -4 * C#
r# = 4 * a# * C# - b# * b#
Equa3 1, p#, q#, r#, u1#, u2#, u3#, Err3%, nSol3%
' This cubic equation has a root U0, with U0 > A
' If such a root does't exist, then B = 0, which means equation (2)
' is a bi-quadric equation.
If u1# > a# Then
u0# = u1#
Else
If u2# > a# Then
u0# = u2#
Else
If u3# > a# Then
u0# = u3#
Else
nSol4% = 0
End If
End If
End If
' Equation (3) can now be re-written:
' (X^2 + u0/2)^2 = (u0 - A) * (X - B/2/(u0-A))^2
' This bi-quadric equation is equivalent to the two following quadrics:
' X^2 + u0/2 = +SQR(u0-A) * X - B/2/SQR(u0-A)
' X^2 + u0/2 = -SQR(u0-A) * X + B/2/SQR(u0-A)
SF# = Sqr(Abs(u0# - a#))
Equa2 1, -SF#, (u0# / 2 + b# / 2 / SF#), xa1#, xa2#, Err2a%, nSol2a%
Equa2 1, SF#, (u0# / 2 - b# / 2 / SF#), xb1#, xb2#, Err2b%, nSol2b%
If nSol2a% = 0 And nSol2b% = 0 Then nSol4% = 0: Exit Sub
If nSol2a% = 2 And nSol2b% = 0 Then
nSol4% = 2
x1# = xa1# - A3# / 4
x2# = xa2# - A3# / 4
x3# = 0
x4# = 0
Exit Sub
End If
If nSol2a% = 0 And nSol2b% = 2 Then
nSol4% = 2
x1# = xb1# - A3# / 4
x2# = xb2# - A3# / 4
x3# = 0
x4# = 0
Exit Sub
End If
If nSol2a% = 2 And nSol2b% = 2 Then
nSol4% = 4
x1# = xa1# - A3# / 4
x2# = xa2# - A3# / 4
x3# = xb1# - A3# / 4
x4# = xb2# - A3# / 4
Exit Sub
End If
End Sub
Sub Init.Cubic (Id%, Type$, HueRef, x0!, y0!, z0!, dRef1, dRef2, Alpha, Beta)
' The power of three... somehow dangerous!
' I'm not sure this routine is 100% safe
Select Case Type$
Case "C0"
shape(Id%).NumType = 300
KXYZ = 0: KX3 = 1: KY3 = 1: KZ3 = 1
KX2Y = 0: KX2Z = 0: KY2Z = 0
KY2X = 0: KZ2X = 0: KZ2Y = 0
KX2 = 0: KY2 = 0: KZ2 = 0
KXY = 0: KXZ = 0: KYZ = 0
kx = 0: ky = 0: KZ = 0
K0 = -dRef1 ^ 3
Case "C1"
shape(Id%).NumType = 301
KXYZ = 0: KX3 = 0: KY3 = 0: KZ3 = 1
KX2Y = 0: KX2Z = 1: KY2Z = 1
KY2X = 0: KZ2X = 0: KZ2Y = 0
KX2 = 0: KY2 = 0: KZ2 = 0
KXY = 0: KXZ = 0: KYZ = 0
kx = 0: ky = 0: KZ = -dRef1 ^ 2
K0 = 0
Case Else: Exit Sub
End Select
For k% = 1 To 15: Object(Id%, k%) = 0: Next k%
Object(Id%, 16) = KXYZ
Object(Id%, 17) = KX3: Object(Id%, 18) = KY3: Object(Id%, 19) = KZ3
Object(Id%, 20) = KX2Y: Object(Id%, 21) = KX2Z: Object(Id%, 22) = KY2Z
Object(Id%, 23) = KY2X: Object(Id%, 24) = KZ2X: Object(Id%, 25) = KZ2Y
Object(Id%, 26) = KX2: Object(Id%, 27) = KY2: Object(Id%, 28) = KZ2
Object(Id%, 29) = KXY: Object(Id%, 30) = KYZ: Object(Id%, 31) = KXZ
Object(Id%, 32) = kx: Object(Id%, 33) = ky: Object(Id%, 34) = KZ
Object(Id%, 35) = K0
shape(Id%).Degree = 3
shape(Id%).ShapeType = Type$
shape(Id%).HueRef = HueRef: shape(Id%).SatRef = 200 / 3
shape(Id%).dRef1 = dRef1: shape(Id%).dRef2 = dRef2
shape(Id%).x0 = x0!: shape(Id%).y0 = y0!: shape(Id%).z0 = z0!
shape(Id%).Brilliance = 1
shape(Id%).Reflect = cReflect
Matrix(Id%, 1, 1) = CosD(Beta) * CosD(Alpha)
Matrix(Id%, 1, 2) = CosD(Beta) * SinD(Alpha)
Matrix(Id%, 1, 3) = SinD(Beta)
Matrix(Id%, 2, 1) = -SinD(Alpha)
Matrix(Id%, 2, 2) = CosD(Alpha)
Matrix(Id%, 2, 3) = 0
Matrix(Id%, 3, 1) = -SinD(Beta) * CosD(Alpha)
Matrix(Id%, 3, 2) = -SinD(Beta) * SinD(Alpha)
Matrix(Id%, 3, 3) = CosD(Beta)
End Sub
Sub Init.Example1
' Geometry
Init.Space 0, 15, 5, .8, 30, 60
Init.Object "Fl", 240, 0, 0, 0, .15, .15, 0, 0
shape(1).Reflect = .1
Init.Object "H1", 200, 1, 1.2, 1, .8, .8, 20, -50
Init.Object "Sp", 120, -1, -2.3, .5, 1.8, 1.6, -10, -20
Init.Object "Q4", 30, 3.5, 0, .5, .4, .2, 30, -20
shape(1).Texture = 17
End Sub
Sub Init.Example10
'nephroid cup
Init.Space 15, 35, 2.5, .8, 55, 30
'Init.Space 0, 90, 2.5, .8, 55, 30
' Table surface
Init.Object "Fl", 300, 0, 0, 0, 2, 2, 0, 0
shape(1).Reflect = .9
shape(1).Texture = 12
' External surface of the cup
Init.Object "Cp", 60, .3, 0, .9, .8, 1.45, 30, 0
shape(2).Reflect = .1
shape(2).Texture = 11
' Internal surface of the cup
Init.Object "Cp", 150, .3, 0, .9, .75, 1.45, 0, 0
shape(3).Reflect = 0
' Close the cup with a torus ring
Init.Object "T0", 240, .3, 0, .9, (.8 + .75) / 2, .025, 0, 0
shape(4).Reflect = 0
' Coffee surface
'x^2+y^2=-(dRef1/dRef2)^2*z^2 + dRef1^2
dRef1 = .75: dRef2 = 1.45: dZ = .1
Radius = Sqr(dRef1 ^ 2 - (dRef1 / dRef2) ^ 2 * dZ ^ 2)
Init.Object "D1", 30, .3, 0, .8, Radius, Radius, 0, 0
shape(5).Reflect = 0
shape(5).SatRef = 50
shape(5).Texture = 32
' Torus for the cup handle
Init.Object "T0", 240, .3, .7, .4, .3, .05, 0, 90
shape(6).Reflect = 0
End Sub
Sub Init.Example2
' Birthday
Init.Space 0, 30, 5, .8, 30, 60
Init.Object "Fl", 240, 0, 0, 0, .15, .15, 0, 0
shape(1).Texture = 17
r! = 2
For k% = 1 To 16
a! = k% / 16 * 2 * Pi
Init.Object "Bl", k% * 24, r! * Cos(a!), r! * Sin(a!), 1.5, .3, .3, 0, 0
Next k%
r! = 2.5
For k% = 1 To 16
a! = k% / 16 * 2 * Pi
Init.Object "Cy", 180 + k% * 24, r! * Cos(a!), r! * Sin(a!), .4, .3, .8, 0, 0
Next k%
End Sub
Sub Init.Example3
' ADN Helix
Init.Space 0, 40, 5, .8, 30, 60
Dim u(3), v(3), W(3)
Init.Object "Fl", 90, 0, 0, 0, .3, .3, 0, 0
shape(1).Texture = 17
r! = 1.6
Alpha = Pi / 4: Beta = Pi / 3
u(1) = Cos(Alpha) * Cos(Beta): v(1) = -Sin(Alpha): W(1) = -Sin(Beta) * Cos(Alpha)
u(2) = Sin(Alpha) * Cos(Beta): v(2) = Cos(Alpha): W(2) = -Sin(Beta) * Sin(Alpha)
u(3) = Sin(Beta): v(3) = 0: W(3) = Cos(Beta)
For k% = 1 To 48
a! = k% / 16 * 2 * Pi
x0r = r! * Cos(a!)
y0r = r! * Sin(a!)
z0r = -4 + k% * .15
x0! = u(1) * x0r + v(1) * y0r + W(1) * z0r
y0! = u(2) * x0r + v(2) * y0r + W(2) * z0r
z0! = u(3) * x0r + v(3) * y0r + W(3) * z0r
z0! = z0! + r! / 3
Init.Object "Bl", k% * 24, x0!, y0!, z0!, .4, .4, 0, 0
Next k%
End Sub
Sub Init.Example4
' Perlin Ring
Init.Space 23, 30, 5, .8, 30, 60
cShadow = .7: cReflect = .2
Init.Object "T0", 120, 0, 0, .8, 2, .4, 0, 0
shape(1).Texture = 16
For k% = 1 To 4
a! = k% / 2 * Pi
Init.Object "Bl", k% * 90, 2 * Cos(a!), 2 * Sin(a!), .8, .8, .8, 0, 0
Next k%
shape(2).Texture = 13
shape(3).Texture = 14
shape(4).Texture = 15
shape(5).Texture = 12
Init.Object "Fl", 0, 0, 0, 0, 1, 1, 0, 0
shape(6).Texture = 13
End Sub
Sub Init.Example5
' Stormy Sea
Init.Space 5, 10, 5, .6, 40, 30
Init.Object "Sk", 260, 0, 0, 20, 0, 0, 0, 0
Init.Object "Fl", 190, 0, 0, 0, 1, 1, 0, 0
Init.Object "HC", 0, 0, 0, .7, .5, .2, -30, -15
shape(2).Texture = 30