forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dim3Paint.Mod
1624 lines (1486 loc) · 49.1 KB
/
Dim3Paint.Mod
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
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE Dim3Paint; (** portable *) (* David Ulrich Nov 95 - März 96 *)
(** Dim3Paint covers the basic drawing functions of the module Dim3Engine. It includes color management and
polygon drawing with and without the dynamic screen data structure **)
IMPORT
Display, Display3, Pictures, Texts, Oberon, Files, SYSTEM, Math, Dim3Base;
CONST
NoPattern = 0; Light = 1; Middle = 2; Dark = 3;
PersLimit = 2; (* maximal limit for no perspective correction *)
Ln2 = 0.693147; (* ln(2) *)
MinTexture = 2; MaxTexture = 200; (* minimal and maximal texture size for mipmaps *)
cheat = 0.9999;
TYPE
TextureMap* = POINTER TO TextureMapDesc; (** pointer to background bitmap for shaded texture **)
DSEntry* = POINTER TO DSEntryDesc; (** Pointer to a dynamic screen entry **)
TSEntry* = POINTER TO TSEntryDesc; (** Pointer to the scanline entry for a transparent texture **)
Color = POINTER TO ColorDesc;
TextureMapDesc = RECORD END;
DSEntryDesc = RECORD (* dynamic screen entry *)
min,max: INTEGER;
active: BOOLEAN;
next: DSEntry;
END;
TSEntryDesc = RECORD (* Scanline entry for transparent textures *)
texture: TextureMap;
X, Y, W: INTEGER;
u, v, uStep, vStep: REAL;
next: TSEntry;
END;
ColorDesc = RECORD (* data structure of dithering table *)
pattern: LONGINT; deltaR, deltaG, deltaB: INTEGER;
END;
Point = RECORD (* help structure for points *)
x, y, u, v, w: REAL;
END;
VAR
T: Texts.Writer;
DSPool: DSEntry;
TSPool: TSEntry;
ColorIndex: ARRAY 10, 10, 6 OF INTEGER;
DitherTab: ARRAY 4, 4, 4 OF Color;
PatternTable: ARRAY 4 OF LONGINT;
ColorTab: ARRAY 3,256 OF INTEGER;
PDots: ARRAY 4,4 OF BOOLEAN;
seed: LONGINT;
GrayIndex: ARRAY 5 OF INTEGER;
(* Random procedure for dithering *)
PROCEDURE Random (): REAL;
CONST a = 16807; m = 2147483647; q = m DIV a; r = m MOD a; (*invm = 0.025 / m;*)
BEGIN
seed := a*(seed MOD q) - r*(seed DIV q);
IF seed <= 0 THEN seed := seed + m END;
RETURN (seed * (0.025 / m) - 0.0125);
END Random;
(*--- Color Handling ---*)
(** initialize standard rembrandt colormap, initialize color and dither tables **)
PROCEDURE InitColors*;
VAR F: Files.File; R: Files.Rider;
i,h,k, pattern, iSgn, hSgn, kSgn, sum, R1, G, B: INTEGER;
r, g, b: CHAR;
PROCEDURE GetNearestColor(r,g,b : INTEGER):INTEGER;
VAR col, i: INTEGER; d, t, min: LONGINT;
BEGIN
IF (r = g) & (r = b) THEN (* use only grayscale colors *)
min := MAX(LONGINT); col := Dim3Base.White;
i := 0;
WHILE i # 256 DO
IF (ColorTab[0][i] = ColorTab[1][i]) & (ColorTab[0][i] = ColorTab[2][i]) THEN
d := (r - ColorTab[0][i]);
IF ABS(d) < min THEN min := ABS(d); col := i END;
END;
INC(i)
END;
RETURN col
ELSE
min := MAX(LONGINT);
i := 0;
WHILE i # 256 DO
t := (r - ColorTab[0][i]);
IF r > ColorTab[0][i] THEN t := t*2 END; d := t * t;
t := (g - ColorTab[1][i]);
IF g > ColorTab[1][i] THEN t := t*2 END; INC(d, t * t);
t := (b - ColorTab[2][i]);
IF b > ColorTab[2][i] THEN t := t*2 END; INC(d, t * t);
IF ABS(d) < min THEN min := ABS(d); col := i END;
INC(i)
END;
RETURN col
END;
END GetNearestColor;
BEGIN
(* works only with 256 colors *)
IF Display.Depth(Display.ColLeft) # 8 THEN
Texts.WriteString(T, "3D-Engine: current implementation works on 256 color systems");
Texts.WriteLn(T); Texts.Append(Oberon.Log, T.buf);
HALT(99);
END;
FOR i := 0 TO 3 DO
IF i = 0 THEN iSgn := 0 ELSE iSgn := 1 END;
FOR h := 0 TO 3 DO
IF h = 0 THEN hSgn := 0 ELSE hSgn := 1 END;
FOR k := 0 TO 3 DO
IF k = 0 THEN kSgn := 0 ELSE kSgn := 1 END;
NEW(DitherTab[i][h][k]);
DitherTab[i][h][k].deltaR := iSgn;
DitherTab[i][h][k].deltaG := hSgn;
DitherTab[i][h][k].deltaB := kSgn;
sum := 2 * iSgn + 2 * hSgn + kSgn;
IF sum # 0 THEN pattern := (2 * i + 2 * h + k) DIV sum ELSE pattern := 0 END;
DitherTab[i][h][k].pattern := PatternTable[pattern];
END;
END;
END;
F:= Files.Old("Rembrandt0.Pal");
Files.Set(R, F, 0);
FOR i:= 0 TO 255 DO
Files.Read(R, r);
Files.Read(R, g);
Files.Read(R, b);
ColorTab[0][i] := ORD(r);
ColorTab[1][i] := ORD(g);
ColorTab[2][i] := ORD(b);
Display.SetColor(i, ORD(r), ORD(g), ORD(b));
END;
Dim3Base.CheckColorTab(ColorTab[0], ColorTab[1], ColorTab[2]);
FOR i := 0 TO 8 DO
FOR h := 0 TO 8 DO
FOR k := 0 TO 4 DO
R1 := SHORT(ENTIER(i*31.99)); G := SHORT(ENTIER(h*31.99)); B := SHORT(ENTIER(k*63.99));
ColorIndex[i][h][k] := GetNearestColor(R1, G, B);
END;
END;
END;
FOR i := 0 TO 8 DO
FOR h := 0 TO 4 DO
ColorIndex[9][i][h] := ColorIndex[8][i][h];
ColorIndex[i][9][h] := ColorIndex[i][8][h];
END;
END;
FOR i := 0 TO 8 DO
FOR h := 0 TO 8 DO
ColorIndex[i][h][5] := ColorIndex[i][h][4];
END;
END;
FOR i := 0 TO 4 DO
B := SHORT(ENTIER(i*63.99));
GrayIndex[i] := GetNearestColor(B, B, B);
END;
END InitColors;
(** get color index for colormap that matches best **)
PROCEDURE GetColor* (col: ARRAY OF REAL; light: REAL; grayscale: BOOLEAN):INTEGER;
VAR R, G, B: LONGINT;
BEGIN
IF light <= 0.0 THEN
RETURN Dim3Base.Black
END;
IF grayscale THEN
R := ENTIER(light * col[0] * 4.99);
IF R > 4 THEN R := 4 END;
RETURN GrayIndex[R]
ELSE
R := ENTIER(light * col[0] * 8.99);
IF R > 8 THEN R := 8 END;
G := ENTIER(light * col[1] * 8.99);
IF G > 8 THEN G := 8 END;
B := ENTIER(light * col[2] * 4.99);
IF B > 4 THEN B := 4 END;
RETURN ColorIndex[R][G][B]
END;
END GetColor;
(* get color indexes for colormap and dithering that matches best *)
PROCEDURE GetColorDither (col: ARRAY OF REAL; light: REAL; grayscale: BOOLEAN; VAR baseCol, patCol: INTEGER;
VAR pat: LONGINT; rand: REAL);
VAR R, G, B, R1, G1, B1, RMod, GMod, BMod: LONGINT; color: Color;
BEGIN
light := light + rand;
IF light <= 0.0 THEN
baseCol := Dim3Base.Black;
pat := NoPattern;
RETURN;
END;
IF grayscale THEN
B := ENTIER(light * 16.99 * col[2]); IF B > 16 THEN B := 16 END;
B1 := ASH(B, -2);
BMod := (B - ASH(B1, 2));
baseCol := GrayIndex[B1];
pat := BMod;
IF B1 < 4 THEN patCol := GrayIndex[B1 + 1] ELSE patCol := baseCol END
ELSE
R := ENTIER(light * 32.99 * col[0]); IF R > 32 THEN R := 32 END;
R1 := ASH(R, -2);
RMod := (R - ASH(R1, 2));
G := ENTIER(light * 32.99 * col[1]); IF G > 32 THEN G := 32 END;
G1 := ASH(G, -2);
GMod := (G - ASH(G1, 2));
B := ENTIER(light * 16.99 * col[2]); IF B > 16 THEN B := 16 END;
B1 := ASH(B, -2);
BMod := (B - ASH(B1, 2));
color := DitherTab[RMod][GMod][BMod];
baseCol := ColorIndex[R1][G1][B1];
patCol := ColorIndex[R1 + color.deltaR][G1 + color.deltaG][B1 + color.deltaB];
pat := color.pattern;
END;
END GetColorDither;
(* get color indexes for colormap and dithering that matches best, for specular reflection*)
PROCEDURE GetColorSpecular (col: ARRAY OF REAL; light, spec: REAL; grayscale: BOOLEAN; VAR baseCol, patCol: INTEGER;
VAR pat: LONGINT; rand: REAL);
VAR R, G, B, R1, G1, B1, RMod, GMod, BMod: LONGINT; color: Color;
BEGIN
light := light + rand; spec := spec + 0.001;
IF light <= 0.0 THEN
baseCol := Dim3Base.Black;
pat := NoPattern;
RETURN;
END;
IF grayscale THEN
B := ENTIER(light * 16.99 * (col[2] + spec)); IF B > 16 THEN B := 16 END;
B1 := ASH(B, -2);
BMod := (B - ASH(B1, 2));
baseCol := GrayIndex[B1];
pat := BMod;
IF B1 < 4 THEN patCol := GrayIndex[B1 + 1] ELSE patCol := baseCol END
ELSE
R := ENTIER(light * 32.99 * (col[0] + spec)); IF R > 32 THEN R := 32 END;
R1 := ASH(R, -2);
RMod := (R - ASH(R1, 2));
G := ENTIER(light * 32.99 * (col[1] + spec)); IF G > 32 THEN G := 32 END;
G1 := ASH(G, -2);
GMod := (G - ASH(G1, 2));
B := ENTIER(light * 16.99 * (col[2] + spec)); IF B > 16 THEN B := 16 END;
B1 := ASH(B, -2);
BMod := (B - ASH(B1, 2));
color := DitherTab[RMod][GMod][BMod];
baseCol := ColorIndex[R1][G1][B1];
patCol := ColorIndex[R1 + color.deltaR][G1 + color.deltaG][B1 + color.deltaB];
pat := color.pattern
END
END GetColorSpecular;
(*--- TS and DS entry mangement ---*)
(* freeing of a DSEntry to pool *)
PROCEDURE FreeDSEntry(entry: DSEntry);
BEGIN
entry.next := DSPool;
DSPool := entry
END FreeDSEntry;
(** freeing of a DSEntryArray to pool **)
PROCEDURE FreeDSList* (VAR ds: ARRAY OF DSEntry; n: INTEGER);
VAR
i: INTEGER;
entry: DSEntry;
BEGIN
FOR i := 0 TO n DO
IF ds[i] # NIL THEN
entry := ds[i];
WHILE entry.next # NIL DO
entry := entry.next;
END;
entry.next := DSPool;
DSPool := ds[i];
ds[i] := NIL
END;
END;
END FreeDSList;
(* allocating of a DSEntry from pool *)
PROCEDURE AllocDSEntry(VAR entry: DSEntry);
BEGIN
IF DSPool = NIL THEN
NEW(entry)
ELSE
entry := DSPool;
DSPool := entry.next;
END;
entry.next := NIL;
entry.active := TRUE;
END AllocDSEntry;
(* allocating of a TSEntry from pool *)
PROCEDURE AllocTSEntry(VAR entry: TSEntry);
BEGIN
IF TSPool = NIL THEN
NEW(entry)
ELSE
entry := TSPool;
TSPool := entry.next;
END;
END AllocTSEntry;
(** initialisation of the dynamic Screen datastructure, max: screen width, n: screen height **)
PROCEDURE InitDynScreen*(VAR ds: ARRAY OF DSEntry; max,n: INTEGER);
VAR
i: INTEGER;
BEGIN
FOR i := 0 TO n DO
AllocDSEntry(ds[i]);
ds[i].min := 0;
ds[i].max := max;
END;
END InitDynScreen;
(*--- Rendering ---*)
(** Fill not painted rest of the screen with color col, free dynamic screen datastructure **)
PROCEDURE DrawRest* (P: Pictures.Picture; ds: ARRAY OF DSEntry; col: INTEGER);
VAR
i,max: INTEGER;
entry, old: DSEntry;
BEGIN
Dim3Base.SetPicture(P);
max := P.height;
FOR i := 0 TO max DO
entry := ds[i];
WHILE entry # NIL DO
Dim3Base.ReplConst(col, entry.min, i, entry.max - entry.min + 1);
old := entry;
entry := entry.next;
FreeDSEntry(old);
END;
END;
END DrawRest;
(* Draw pattern using ReplConst and Dot *)
PROCEDURE DrawPattern(baseCol, patCol: INTEGER; pattern: LONGINT;
X, Y, W: INTEGER);
VAR oddY, oddX: BOOLEAN; index: INTEGER; pictAdr: LONGINT;
BEGIN
oddY := ODD(Y); oddX := ODD(X);
IF ((pattern = Light) & oddY) OR (pattern = NoPattern) THEN
Dim3Base.ReplConst(baseCol, X, Y, W);
RETURN;
ELSIF (pattern = Dark) & (~oddY) THEN
Dim3Base.ReplConst(patCol, X, Y, W);
RETURN;
ELSIF oddY THEN
IF oddX THEN index := 1 ELSE index := 0 END;
ELSE
IF oddX THEN index := 0 ELSE index := 1 END;
END;
Dim3Base.ReplConst(baseCol, X, Y, W);
pictAdr := Dim3Base.GetAddress(X, Y);
WHILE index < W DO
SYSTEM.PUT(pictAdr + index, CHR(patCol));
INC(index,2)
END;
END DrawPattern;
(* split dynamic screen entry into two entries *)
PROCEDURE SplitSegment(i,j: INTEGER; k: DSEntry);
VAR
newEntry: DSEntry;
BEGIN
AllocDSEntry(newEntry);
newEntry.min := j + 1;
newEntry.max := k.max;
k.max := i - 1;
newEntry.next := k.next;
k.next := newEntry;
END SplitSegment;
(* draw a gouraud shaded polygon scanline, with dithering and specular reflection *)
PROCEDURE DrawSpecularSegment(sel: BOOLEAN; col: ARRAY OF REAL; planeCol: INTEGER; grayscale: BOOLEAN; entry: DSEntry;
xL, xR: REAL; Y, botL, botR, topL, topR: INTEGER; y, i, s: ARRAY OF REAL);
VAR XL, XR, W: INTEGER; t1, t3, iL, iR, dInten, dSpec, sL, sR: REAL;
PROCEDURE Display(X, W: INTEGER; iLeft, sLeft: REAL);
VAR x, color, oddY, oddX, patCol: INTEGER; inten, spec: REAL; pat, pictAdr: LONGINT; rand: REAL;
BEGIN
IF W > 2 THEN rand := Random() ELSE rand := 0 END;
IF ODD(Y) THEN oddY := 0 ELSE oddY := 2 END;
oddX := 0;
inten := iLeft; spec := sLeft;
pictAdr := Dim3Base.GetAddress(X, Y);
WHILE W >= 15 DO
FOR x := 1 TO 15 DO
GetColorSpecular(col, inten, spec, grayscale, color, patCol, pat, rand);
inten := inten + dInten; spec := spec +dSpec;
IF PDots[pat][oddY + oddX] THEN
SYSTEM.PUT(pictAdr, CHR(patCol))
ELSE
SYSTEM.PUT(pictAdr, CHR(color))
END;
INC(pictAdr);
oddX := (oddX + 1) MOD 2
END;
rand := Random(); DEC(W, 15);
END;
WHILE W > 0 DO
GetColorSpecular(col, inten, spec, grayscale, color, patCol, pat, rand);
inten := inten + dInten; spec := spec + dSpec;
IF PDots[pat][oddY + oddX] THEN
SYSTEM.PUT(pictAdr, CHR(patCol))
ELSE
SYSTEM.PUT(pictAdr, CHR(color))
END;
INC(pictAdr);
oddX := (oddX + 1) MOD 2;
DEC(W);
END;
END Display;
BEGIN
IF xL <= xR THEN
XL := SHORT(ENTIER(xL)) + 1;
XR := SHORT(ENTIER(xR));
W := XR - XL + 1;
t1 := (y[topL] - Y) / (y[topL] - y[botL]);
t3 := i[botL] - i[topL]; iL := i[topL] + t1 * t3;
t3 := s[botL] - s[topL]; sL := s[topL] + t1 * t3;
t1 := (y[topR] - Y) / (y[topR] - y[botR]);
t3 := i[botR] - i[topR]; iR := i[topR] + t1 * t3;
t3 := s[botR] - s[topR]; sR := s[topR] + t1 * t3;
ELSE
XL := SHORT(ENTIER(xR)) + 1;
XR := SHORT(ENTIER(xL));
W := XR - XL + 1;
t1 := (y[topL] - Y) / (y[topL] - y[botL]);
t3 := i[botL] - i[topL]; iR := i[topL] + t1 * t3;
t3 := s[botL] - s[topL]; sR := s[topL] + t1 * t3;
t1 := (y[topR] - Y) / (y[topR] - y[botR]);
t3 := i[botR] - i[topR]; iL := i[topR] + t1 * t3;
t3 := s[botR] - s[topR]; sL := s[topR] + t1 * t3;
END;
dInten := (iR - iL) / (W - cheat);
dSpec := (sR - sL) / (W - cheat);
(* Loop for merging *)
LOOP
IF W <= 0 THEN EXIT END;
IF ~entry.active THEN
entry := entry.next
ELSIF XR < entry.min THEN (* case 1 *)
EXIT;
ELSIF XL > entry.max THEN (* case 8 *)
entry := entry.next;
ELSE
IF XR < entry.max THEN
IF XL <= entry.min THEN (* case 2 *)
Display(entry.min, XR - entry.min + 1, iL + (entry.min - XL) * dInten, sL + (entry.min - XL) * dSpec);
entry.min := XR + 1;
EXIT;
ELSE (* case 3 *)
Display( XL, W, iL, sL);
SplitSegment(XL, XR, entry);
EXIT;
END;
ELSIF XR > entry.max THEN
IF XL <= entry.min THEN (* case 6 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten, sL + (entry.min - XL) * dSpec);
entry.active := FALSE;
entry := entry.next;
ELSE (* case 7 *)
Display(XL, entry.max - XL + 1, iL, sL);
entry.max := XL - 1;
entry := entry.next;
END;
ELSE
IF XL <= entry.min THEN (* case 4 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten, sL + (entry.min - XL) * dSpec);
entry.active := FALSE;
EXIT;
ELSE (* case 5 *)
Display(XL, W, iL, sL);
entry.max := XL - 1;
EXIT;
END;
END;
END;
END;
END DrawSpecularSegment;
(* draw a gouraud shaded polygon scanline, with dithering *)
PROCEDURE DrawDitherSegment(sel: BOOLEAN; col: ARRAY OF REAL; planeCol: INTEGER; grayscale: BOOLEAN; entry: DSEntry;
xL, xR: REAL; Y, botL, botR, topL, topR: INTEGER; y, i, s: ARRAY OF REAL);
VAR XL, XR, W: INTEGER; t1, t2, t3, iL, iR, dInten: REAL;
PROCEDURE Display(X, W: INTEGER; iLeft: REAL);
VAR x, color, oddY, oddX, patCol: INTEGER; inten: REAL; pat, pictAdr: LONGINT; rand: REAL;
BEGIN
IF W > 2 THEN rand := Random() ELSE rand := 0 END;
IF ODD(Y) THEN oddY := 0 ELSE oddY := 2 END;
oddX := 0;
inten := iLeft;
pictAdr := Dim3Base.GetAddress(X, Y);
WHILE W >= 15 DO
FOR x := 1 TO 15 DO
GetColorDither(col, inten, grayscale, color, patCol, pat, rand);
inten := inten + dInten;
IF PDots[pat][oddY + oddX] THEN
SYSTEM.PUT(pictAdr, CHR(patCol))
ELSE
SYSTEM.PUT(pictAdr, CHR(color))
END;
INC(pictAdr);
oddX := (oddX + 1) MOD 2
END;
rand := Random(); DEC(W, 15);
END;
WHILE W > 0 DO
GetColorDither(col, inten, grayscale, color, patCol, pat, rand);
inten := inten + dInten;
IF PDots[pat][oddY + oddX] THEN
SYSTEM.PUT(pictAdr, CHR(patCol))
ELSE
SYSTEM.PUT(pictAdr, CHR(color))
END;
INC(pictAdr);
oddX := (oddX + 1) MOD 2;
DEC(W);
END;
END Display;
BEGIN
IF xL <= xR THEN
XL := SHORT(ENTIER(xL)) + 1;
XR := SHORT(ENTIER(xR));
W := XR - XL + 1;
t1 := y[topL] - Y; t2 := y[topL] - y[botL]; t3 := i[botL] - i[topL];
iL := i[topL] + t1 / t2 * t3;
t1 := y[topR] - Y; t2 := y[topR] - y[botR]; t3 := i[botR] - i[topR];
iR := i[topR] + t1 / t2 * t3;
dInten := (iR - iL) / (W - cheat);
ELSE
XL := SHORT(ENTIER(xR)) + 1;
XR := SHORT(ENTIER(xL));
W := XR - XL + 1;
t1 := y[topL] - Y; t2 := y[topL] - y[botL]; t3 := i[botL] - i[topL];
iR := i[topL] + t1 / t2 * t3;
t1 := y[topR] - Y; t2 := y[topR] - y[botR]; t3 := i[botR] - i[topR];
iL := i[topR] + t1 / t2 * t3;
dInten := (iR - iL) / (W - cheat);
END;
(* Loop for merging *)
LOOP
IF W <= 0 THEN EXIT END;
IF ~entry.active THEN
entry := entry.next
ELSIF XR < entry.min THEN (* case 1 *)
EXIT;
ELSIF XL > entry.max THEN (* case 8 *)
entry := entry.next;
ELSE
IF XR < entry.max THEN
IF XL <= entry.min THEN (* case 2 *)
Display(entry.min, XR - entry.min + 1,iL + (entry.min - XL) * dInten);
entry.min := XR + 1;
EXIT;
ELSE (* case 3 *)
Display( XL, W, iL);
SplitSegment(XL, XR, entry);
EXIT;
END;
ELSIF XR > entry.max THEN
IF XL <= entry.min THEN (* case 6 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten);
entry.active := FALSE;
entry := entry.next;
ELSE (* case 7 *)
Display(XL, entry.max - XL + 1, iL);
entry.max := XL - 1;
entry := entry.next;
END;
ELSE
IF XL <= entry.min THEN (* case 4 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten);
entry.active := FALSE;
EXIT;
ELSE (* case 5 *)
Display(XL, W, iL);
entry.max := XL - 1;
EXIT;
END;
END;
END;
END;
END DrawDitherSegment;
(* draw a gouraud shaded polygon scanline, no dithering *)
PROCEDURE DrawGouraudSegment(sel: BOOLEAN; col: ARRAY OF REAL; planeCol: INTEGER; grayscale: BOOLEAN; entry: DSEntry;
xL, xR: REAL; Y, botL, botR, topL, topR: INTEGER; y, i, s: ARRAY OF REAL);
VAR XL, XR, W: INTEGER; t1, t2, t3, iL, iR, dInten: REAL;
PROCEDURE Display(X, W: INTEGER; iLeft: REAL);
VAR x, color: INTEGER; inten, dInt: REAL; pictAdr: LONGINT;
BEGIN
DEC(W);
inten := iLeft;
dInt := dInten;
pictAdr := Dim3Base.GetAddress(X, Y);
FOR x := 0 TO W DO
color := GetColor(col, inten, grayscale);
inten := inten + dInt;
SYSTEM.PUT(pictAdr, CHR(color));
INC(pictAdr);
END;
END Display;
BEGIN
IF xL <= xR THEN
XL := SHORT(ENTIER(xL)) + 1;
XR := SHORT(ENTIER(xR));
W := XR - XL + 1;
t1 := y[topL] - Y; t2 := y[topL] - y[botL]; t3 := i[botL] - i[topL];
iL := i[topL] + t1 / t2 * t3;
t1 := y[topR] - Y; t2 := y[topR] - y[botR]; t3 := i[botR] - i[topR];
iR := i[topR] + t1 / t2 * t3;
dInten := (iR - iL) / (W - cheat);
ELSE
XL := SHORT(ENTIER(xR)) + 1;
XR := SHORT(ENTIER(xL));
W := XR - XL + 1;
t1 := y[topL] - Y; t2 := y[topL] - y[botL]; t3 := i[botL] - i[topL];
iR := i[topL] + t1 / t2 * t3;
t1 := y[topR] - Y; t2 := y[topR] - y[botR]; t3 := i[botR] - i[topR];
iL := i[topR] + t1 / t2 * t3;
dInten := (iR - iL) / (W - cheat);
END;
(* Loop for merging *)
LOOP
IF W <= 0 THEN EXIT END;
IF ~entry.active THEN
entry := entry.next
ELSIF XR < entry.min THEN (* case 1 *)
EXIT;
ELSIF XL > entry.max THEN (* case 8 *)
entry := entry.next;
ELSE
IF XR < entry.max THEN
IF XL <= entry.min THEN (* case 2 *)
Display(entry.min, XR - entry.min + 1,iL + (entry.min - XL) * dInten);
entry.min := XR + 1;
EXIT;
ELSE (* case 3 *)
Display( XL, W, iL);
SplitSegment(XL, XR, entry);
EXIT;
END;
ELSIF XR > entry.max THEN
IF XL <= entry.min THEN (* case 6 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten);
entry.active := FALSE;
entry := entry.next;
ELSE (* case 7 *)
Display(XL, entry.max - XL + 1, iL);
entry.max := XL - 1;
entry := entry.next;
END;
ELSE
IF XL <= entry.min THEN (* case 4 *)
Display(entry.min, entry.max - entry.min + 1, iL + (entry.min - XL) * dInten);
entry.active := FALSE;
EXIT;
ELSE (* case 5 *)
Display(XL, W, iL);
entry.max := XL - 1;
EXIT;
END;
END;
END;
END;
END DrawGouraudSegment;
(* draw a constant shaded polygon scanline *)
PROCEDURE DrawUniSegment(sel: BOOLEAN; col: ARRAY OF REAL; planeCol: INTEGER; grayscale: BOOLEAN;
entry: DSEntry; xL, xR: REAL; Y, botL, botR, topL, topR: INTEGER; y, i, s: ARRAY OF REAL);
VAR XL, XR, W: INTEGER;
PROCEDURE Display(X, W: INTEGER);
BEGIN
IF sel THEN
DrawPattern(planeCol, Display3.BG, Middle, X, Y, W)
ELSE
Dim3Base.ReplConst(planeCol, X, Y, W)
END;
END Display;
BEGIN
IF xL <= xR THEN
XL := SHORT(ENTIER(xL)) + 1;
XR := SHORT(ENTIER(xR));
W := XR - XL + 1;
ELSE
XL := SHORT(ENTIER(xR)) + 1;
XR := SHORT(ENTIER(xL));
W := XR - XL + 1;
END;
(* Loop for merging *)
LOOP
IF W <= 0 THEN EXIT END;
IF ~entry.active THEN
entry := entry.next
ELSIF XR < entry.min THEN (* case 1 *)
EXIT;
ELSIF XL > entry.max THEN (* case 8 *)
entry := entry.next;
ELSE
IF XR < entry.max THEN
IF XL <= entry.min THEN (* case 2 *)
Display(entry.min, XR - entry.min + 1);
entry.min := XR + 1;
EXIT;
ELSE (* case 3 *)
Display(XL, W);
SplitSegment(XL, XR, entry);
EXIT;
END;
ELSIF XR > entry.max THEN
IF XL <= entry.min THEN (* case 6 *)
Display(entry.min, entry.max - entry.min + 1);
entry.active := FALSE;
entry := entry.next;
ELSE (* case 7 *)
Display(XL, entry.max - XL + 1);
entry.max := XL - 1;
entry := entry.next;
END;
ELSE
IF XL <= entry.min THEN (* case 4 *)
Display(entry.min, entry.max - entry.min + 1);
entry.active := FALSE;
EXIT;
ELSE (* case 5 *)
Display(XL, W);
entry.max := XL - 1;
EXIT;
END;
END;
END;
END;
END DrawUniSegment;
(** Draw polygon with the dynamic screen method as following:
uni = TRUE : draw a constant shaded polygon;
uni = FALSE & dither = FALSE & specular = FALSE : draw a gouraud shaded polygon without dithering and without specular reflection;
uni = FALSE & dither = FALSE & specular = TRUE : not allowed;
uni = FALSE & dither = TRUE & specular = FALSE : draw a gouraud shaded polygon with 2*2 dithering and without specular reflection;
uni = FALSE & dither = TRUE & specular = TRUE : draw a gouraud shaded polygon with 2*2 dithering and with specular reflection; **)
PROCEDURE DrawShadePoly* (P: Pictures.Picture; VAR ds: ARRAY OF DSEntry; col: ARRAY OF REAL; planeCol: INTEGER;
grayscale, uni, sel, dither, specular: BOOLEAN; VAR x, y, i, s: ARRAY OF REAL; n: INTEGER);
VAR
xL, xR, mL, mR, dxL, dxR, dyL, dyR, yt, pw, ph: REAL;
sy, topY, stopL, stopR: LONGINT;
topL, topR, botL, botR, t, Y: INTEGER;
drawScanline: PROCEDURE(sel: BOOLEAN; col: ARRAY OF REAL; planeCol: INTEGER; grayscale: BOOLEAN;
entry: DSEntry; xL, xR: REAL; Y, botL, botR, topL, topR: INTEGER; y, i, s: ARRAY OF REAL);
BEGIN
seed := 1235; DEC(n);
Dim3Base.SetPicture(P);
IF uni OR sel THEN
drawScanline := DrawUniSegment
ELSIF ~dither THEN (* draw with dithering or not *)
drawScanline := DrawGouraudSegment
ELSIF ~specular THEN
drawScanline := DrawDitherSegment
ELSE
drawScanline := DrawSpecularSegment;
END;
(* find bottommost point and highest y value *)
botL := 0; topL := 0;
pw := ASH(P.width, -1); ph := ASH(P.height, -1);
x[0] := pw * (cheat + x[0]);
y[0] := ph * (cheat + y[0]);
FOR t := 1 TO n DO
x[t] := pw * (cheat +x[t]);
yt := ph * (cheat + y[t]);
y[t] := yt;
IF yt < y[botL] THEN botL := t END;
IF yt > y[topL] THEN topL := t END
END;
sy := ENTIER(y[botL]) + 1; (* initialize current scanline *)
IF sy > ENTIER(y[topL]) THEN (* polygon lies between scanlines *)
RETURN
END;
topY := ENTIER(y[topL]); (* last scanline to be considered *)
botR := botL;
IF botL = 0 THEN topL := n ELSE topL := botL - 1 END;
IF botR = n THEN topR := 0 ELSE topR := botR + 1 END;
(* set up parameters for first two edges *)
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL]);
WHILE stopL < sy DO
botL := topL;
IF topL = 0 THEN topL := n ELSE DEC(topL) END;
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL])
END;
mL := dxL / dyL;
xL := x[botL] + (sy - y[botL]) * mL;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR]);
WHILE stopR < sy DO
botR := topR;
IF topR = n THEN topR := 0 ELSE INC(topR) END;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR])
END;
mR := dxR / dyR;
xR := x[botR] + (sy - y[botR]) * mR;
LOOP
(* draw current scanline *)
Y := SHORT(sy);
drawScanline(sel, col, planeCol, grayscale, ds[sy], xL, xR, Y, botL, botR, topL, topR, y, i, s);
(* exit from loop if topmost scanline has just been drawn *)
IF sy = topY THEN EXIT END;
(* update left edge *)
IF sy = stopL THEN
REPEAT
botL := topL;
IF topL = 0 THEN topL := n ELSE DEC(topL) END;
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL])
UNTIL stopL > sy;
mL := dxL / dyL;
xL := x[botL] + (sy+1 - y[botL]) * mL;
ELSE
xL := xL + mL
END;
(* update right edge *)
IF sy = stopR THEN
REPEAT
botR := topR;
IF topR = n THEN topR := 0 ELSE INC(topR) END;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR])
UNTIL stopR > sy;
mR := dxR / dyR;
xR := x[botR] + (sy+1 - y[botR]) * mR;
ELSE
xR := xR + mR
END;
INC(sy)
END (* End outer loop *)
END DrawShadePoly;
(** Draw constant shaded polygon without the dynamic screen method **)
PROCEDURE DrawPolygon* (P: Pictures.Picture; col: INTEGER; sel: BOOLEAN; VAR x, y: ARRAY OF REAL; n: INTEGER);
VAR
xL, xR, mL, mR, dxL, dxR, dyL, dyR, yt, pw, ph: REAL;
sy, topY, stopL, stopR: LONGINT;
topL, topR, botL, botR, t, X, W: INTEGER;
BEGIN
Dim3Base.SetPicture(P); DEC(n);
(* find bottommost point and highest y value *)
botL := 0; topL := 0;
pw := ASH(P.width, -1); ph := ASH(P.height, -1);
x[0] := pw * (cheat + x[0]);
y[0] := ph * (cheat + y[0]);
FOR t := 1 TO n DO
x[t] := pw * (cheat +x[t]);
yt := ph * (cheat + y[t]);
y[t] := yt;
IF yt < y[botL] THEN botL := t END;
IF yt > y[topL] THEN topL := t END
END;
sy := ENTIER(y[botL]) + 1; (* initialize current scanline *)
IF sy > ENTIER(y[topL]) THEN (* polygon lies between scanlines *)
RETURN
END;
topY := ENTIER(y[topL]); (* last scanline to be considered *)
botR := botL;
IF botL = 0 THEN topL := n ELSE topL := botL - 1 END;
IF botR = n THEN topR := 0 ELSE topR := botR + 1 END;
(* set up parameters for first two edges *)
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL]);
WHILE stopL < sy DO
botL := topL;
IF topL = 0 THEN topL := n ELSE DEC(topL) END;
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL])
END;
mL := dxL / dyL;
xL := x[botL] + (sy - y[botL]) * mL;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR]);
WHILE stopR < sy DO
botR := topR;
IF topR = n THEN topR := 0 ELSE INC(topR) END;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR])
END;
mR := dxR / dyR;
xR := x[botR] + (sy - y[botR]) * mR;
LOOP
(* draw current scanline *)
IF xL <= xR THEN
X := SHORT(ENTIER(xL));
W := SHORT(ENTIER(xR)) - X
ELSE
X := SHORT(ENTIER(xR));
W := SHORT(ENTIER(xL)) - X
END;
IF sel THEN
DrawPattern(col, Display3.BG, Middle, X + 1, SHORT(sy), W)
ELSE
Dim3Base.ReplConst(col, X + 1, SHORT(sy), W);
END;
(* exit from loop if topmost scanline has just been drawn *)
IF sy = topY THEN EXIT END;
(* update left edge *)
IF sy = stopL THEN
REPEAT
botL := topL;
IF topL = 0 THEN topL := n ELSE DEC(topL) END;
dxL := x[topL] - x[botL]; dyL := y[topL] - y[botL];
stopL := ENTIER(y[topL])
UNTIL stopL > sy;
mL := dxL / dyL;
xL := x[botL] + (sy+1 - y[botL]) * mL
ELSE
xL := xL + mL
END;
(* update right edge *)
IF sy = stopR THEN
REPEAT
botR := topR;
IF topR = n THEN topR := 0 ELSE INC(topR) END;
dxR := x[topR] - x[botR]; dyR := y[topR] - y[botR];
stopR := ENTIER(y[topR])
UNTIL stopR > sy;
mR := dxR / dyR;
xR := x[botR] + (sy+1 - y[botR]) * mR
ELSE
xR := xR + mR
END;
INC(sy)
END
END DrawPolygon;