-
Notifications
You must be signed in to change notification settings - Fork 7
/
draw.cpp
1764 lines (1544 loc) · 58.5 KB
/
draw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
/*
** Daedalus (Version 3.5) File: draw.cpp
** By Walter D. Pullen, [email protected], http://www.astrolog.org/labyrnth.htm
**
** IMPORTANT NOTICE: Daedalus and all Maze generation and general
** graphics routines used in this program are Copyright (C) 1998-2024 by
** Walter D. Pullen. Permission is granted to freely use, modify, and
** distribute these routines provided these credits and notices remain
** unmodified with any altered or distributed versions of the program.
** The user does have all rights to Mazes and other graphic output
** they make in Daedalus, like a novel created in a word processor.
**
** More formally: This program is free software; you can redistribute it
** and/or modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of the
** License, or (at your option) any later version. This program is
** distributed in the hope that it will be useful and inspiring, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details, a copy of which is in the
** LICENSE.HTM included with Daedalus, and at http://www.gnu.org
**
** This file contains advanced non-perspective graphics routines, unrelated to
** Mazes.
**
** Created: 6/15/1990.
** Last code change: 10/30/2024.
*/
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "draw.h"
DS ds = {
// Draw settings
10, 10, 10, 10, 10, 0, 0, 0, fFalse,
0, 0, 0, 0, 10, 10, fTrue, fTrue,
// Obscure draw settings
fTrue, fFalse, fFalse, fTrue, fFalse, fFalse, 1, 1, 200.0, 20.0, 20.0, 20.0,
kvBlack, kvBlue, kvYellow, kvGray, kvGreen, kvRed, {100.0, 75.0, 50.0},
// Inside settings
fFalse, 500, 0,
// Macro accessible only settings
10990099, -1, fFalse, fFalse, 0, 0, 0, 1000, fTrue, 0, fFalse,
// Internal settings
fFalse, xStart, 0.0, 0, NULL};
/*
******************************************************************************
** Render Overview
******************************************************************************
*/
// Draw some or all of the faces of a solid orthographic block, with one face
// oriented toward you. Used by various FillOverview functions.
void FillCube(CMap &b, int x, int y, int xs, int ys, int zs, int dir,
KV kv0, KV kv1, real rx, real ry, real rz, flag fX, flag fY, flag fZ)
{
int i;
KV kvX, kvY, kvZ;
// Draw the outline of the cube with one color.
if (fX) {
b.LineY(x + ys*dir, y - ys, y - ys + zs, kv0);
b.LineZ(x + dir, y + zs - 1, y - ys + zs + 1, dir, kv0);
}
if (fY) {
b.LineX(x - (xs - 1)*dir, x - dir, y + zs, kv0);
b.LineY(x - xs*dir, y, y + zs, kv0);
}
if (fZ) {
b.LineX(x - (xs - ys)*dir, x + (ys - 1)*dir, y - ys, kv0);
b.LineZ(x - (xs - 1)*dir, y - 1, y - ys + 1, dir, kv0);
}
if (fX && fY)
b.LineY(x, y, y + zs, kv0);
if (fY && fZ)
b.LineX(x - (xs - 1)*dir, x - dir, y, kv0);
if (fX && fZ)
b.LineZ(x + dir, y - 1, y - ys + 1, dir, kv0);
// Fill in the three visible faces of the cube with another color. For color
// bitmaps, each of the three faces gets shaded with a passed in value.
if (!b.FColor()) {
kvX = kv1; kvY = !ds.fShading; kvZ = !ds.fSkyShade;
} else {
kvX = KvShade(kv1, rx); kvY = KvShade(kv1, ry); kvZ = KvShade(kv1, rz);
}
if (fY && xs > 1 && zs > 1)
b.Block(x - (xs - 1)*dir, y + 1, x - dir, y + zs - 1, kvY);
for (i = 1; i < ys; i++) {
if (fZ && xs > 1)
b.LineX(x - (xs - 1 - i)*dir, x + (i - 1)*dir, y - i, kvZ);
if (fX && zs > 1)
b.LineY(x + i*dir, y - i + 1, y + zs - 1 - i, kvX);
}
}
// Draw some or all of the faces of a solid angled orthographic block, with
// one edge oriented toward you. Used by various FillOverview2 functions.
void FillCube2(CMon &b, int x, int y, int xs, int ys, int zs,
flag fX, flag fY, flag fZ)
{
int xs2 = xs*ys, i, j;
// Draw the outline of the cube.
if (fX) {
b.LineY(x + xs2 + 1, y - ys - 1, y - ys - 1 + zs, fOff);
b.LineZ(x + 1, y + zs - 1, y + zs - ys, xs, fOff);
}
if (fY) {
b.LineY(x - xs2 - 1, y - ys - 1, y - ys - 1 + zs, fOff);
b.LineZ(x - 1, y + zs - 1, y + zs - ys, -xs, fOff);
}
if (fZ) {
b.LineZ(x + xs2, y - ys - 2, y - ys*2 - 1, -xs, fOff);
b.LineZ(x - xs2, y - ys - 2, y - ys*2 - 1, xs, fOff);
b.Set0(x, y - ys*2 - 2);
}
if (fX && fY)
b.LineY(x, y, y + zs, fOff);
if (fY && fZ)
b.LineZ(x - 1, y - 1, y - ys, -xs, fOff);
if (fX && fZ)
b.LineZ(x + 1, y - 1, y - ys, xs, fOff);
// Fill in the three visible faces of the cube.
if (fZ) {
b.LineX(x - xs2, x + xs2, y - ys - 1, !ds.fSkyShade);
for (i = 1; i <= ys; i++) {
j = (ys - i)*xs;
b.LineX(x - j, x + j, y - ys - 1 - i, !ds.fSkyShade);
b.LineX(x - j, x + j, y - ys - 1 + i, !ds.fSkyShade);
}
}
if ((fX || fY) && zs > 2)
for (i = 0; i < ys; i++) {
if (fX)
b.Block(x + i*xs + 1, y - i, x + i*xs + xs, y - i + zs - 2, fOn);
if (fY)
b.Block(x - i*xs - 1, y - i, x - i*xs - xs, y - i + zs - 2,
!ds.fShading);
}
}
// Draw an orthographic overview of a monochrome bitmap within the bounds of a
// color bitmap. The color of each block comes from a second color bitmap.
void FillOverview(CMap &c, CONST CMon &b, CCol *c2)
{
int x, y, horloc, verloc, dir;
real rx = 0.0, ry = 0.0, rz = 0.0;
KV kv, kvT;
flag fColor = c.FColor();
if (ds.depsiz < 1)
return;
if (fColor && ds.fShading) {
rx = ds.vLight.m_x / 100.0;
ry = ds.vLight.m_y / 100.0;
rz = ds.vLight.m_z / 100.0;
}
dir = ds.fRight*2 - 1;
for (y = 0; y < b.m_y; y++) {
verloc = (y + 1)*ds.versiz;
for (x = ds.fRight ? 0 : b.m_x - 1; x >= 0 && x < b.m_x; x += dir) {
if (!b.Get(x, y))
continue;
if (ds.fRight)
horloc = (x + 1)*ds.horsiz + (b.m_y - y - 1)*ds.versiz;
else
horloc = x*ds.horsiz + (y + 1)*ds.versiz;
// Draw a colored block for each set pixel.
kv = !fColor ? fOn : c2->Get(x, y);
FillCube(c, horloc, verloc, ds.horsiz, ds.versiz, ds.depsiz - 1, dir,
!fColor ? fOff : ds.kvTrim, kv, rx, ry, rz,
!b.Get(x + dir, y), !b.Get(x, y + 1), fTrue);
// Draw lines removing seams between this and earlier drawn blocks, in a
// a color half way between the color used for the two faces.
if (ds.fMerge) {
if (b.Get(x - dir, y)) {
if (fColor)
kvT = KvBlend(kv, c2->Get(x - dir, y));
if (ds.versiz > 1)
c.LineZ(horloc - (ds.horsiz - 1)*dir, verloc - 1, verloc -
ds.versiz + 1, dir, !fColor ? !ds.fSkyShade : KvShade(kvT, rz));
if (ds.depsiz > 2)
c.LineY(horloc - ds.horsiz*dir, verloc + 1, verloc +
ds.depsiz - 2, !fColor ? !ds.fShading : KvShade(kvT, ry));
if (b.Get(x, y - 1) && b.Get(x - dir, y - 1))
c.Set(horloc - (ds.horsiz - ds.versiz)*dir, verloc - ds.versiz,
!fColor ? !ds.fSkyShade : KvShade(KvBlend(kvT, KvBlend(c2->
Get(x, y - 1), c2->Get(x - dir, y - 1))), rz));
}
if (b.Get(x, y - 1)) {
if (fColor)
kvT = KvBlend(kv, c2->Get(x, y - 1));
if (ds.horsiz > 1)
c.LineX(horloc - (ds.horsiz - ds.versiz - 1)*dir,
horloc + (ds.versiz - 1)*dir, verloc - ds.versiz,
!fColor ? !ds.fSkyShade : KvShade(kvT, rz));
if (!b.Get(x + dir, y - 1) && ds.depsiz > 2)
c.LineY(horloc + ds.versiz*dir, verloc - ds.versiz + 1,
verloc - ds.versiz + ds.depsiz - 2,
!fColor ? fOn : KvShade(kvT, rx));
}
}
}
UpdateDisplay();
}
}
// Replace bitmap with an orthographic overview of a monochrome bitmap. The
// color of each block comes from the contents of an optional color bitmap.
// Implements the Render Bitmap Overview command for 2D bitmaps.
flag DrawOverview(CMap &c, CONST CMon &b)
{
CMon bCopy;
CCol cCopy;
flag fColor = c.FColor();
if (fColor ? !cCopy.FBitmapCopy(c) : !bCopy.FBitmapCopy(c))
return fFalse;
if (!c.FBitmapSizeSet(b.m_x*ds.horsiz + b.m_y*ds.versiz + 1,
b.m_y*ds.versiz + ds.depsiz))
return fFalse;
c.BitmapSet(!fColor ? !ds.fGroundShade : ds.kvGroundLo);
UpdateDisplay();
FillOverview(c, !fColor ? bCopy : b, !fColor ? NULL : &cCopy);
return fTrue;
}
// Draw an orthographic overview of a 3D bitmap within the bounds of another
// bitmap. The color of each block comes from an optional second color bitmap.
void FillOverviewCube(CMap &c, CONST CMon3 &t, CONST CCol3 *c2,
int lensiz, int widsiz, int depsiz, flag fView, flag fMerge)
{
int x, y, z, horloc, verloc, dir;
real rx = 0.0, ry = 0.0, rz = 0.0;
KV kv, kvT;
flag fColor = c.FColor(), fRainbow = fColor &&
(t.m_x3 != c2->m_x3 || t.m_y3 != c2->m_y3 || t.m_z3 != c2->m_z3), f1, f2;
if (fColor && ds.fShading) {
rx = ds.vLight.m_x / 100.0;
ry = ds.vLight.m_y / 100.0;
rz = ds.vLight.m_z / 100.0;
}
dir = fView*2 - 1;
for (y = 0; y < t.m_y3; y++) {
for (z = t.m_z3-1; z >= 0; z--) {
verloc = z*depsiz + (y + 1)*widsiz;
for (x = fView ? 0 : t.m_x3 - 1; x >= 0 && x < t.m_x3; x += dir) {
if (!t.Get3(x, y, z))
continue;
if (fView)
horloc = (x + 1)*lensiz + (t.m_y3 - 1 - y)*widsiz;
else
horloc = x*lensiz + (y + 1)*widsiz;
// Draw a colored block for each set pixel.
kv = !fColor ? fOn : c2->Get3R(x, y, z, fRainbow);
FillCube(c, horloc, verloc, lensiz, widsiz, depsiz, dir,
!fColor ? fOff : ds.kvTrim, kv, rx, ry, rz,
!t.Get3(x + dir, y, z), !t.Get3(x, y + 1, z), !t.Get3(x, y, z - 1));
// Draw lines removing seams between this and earlier drawn blocks,
// in a color half way between the color used for the two faces.
if (fMerge) {
if (t.Get3(x - dir, y, z)) {
if (fColor)
kvT = KvBlend(kv, c2->Get3R(x - dir, y, z, fRainbow));
if (depsiz > 1)
c.LineY(horloc - lensiz*dir, verloc + 1, verloc + depsiz - 1,
!fColor ? !ds.fShading : KvShade(kvT, ry));
if (widsiz > 1)
c.LineZ(horloc - (lensiz - 1)*dir, verloc - 1, verloc - widsiz +
1, dir, !fColor ? !ds.fSkyShade : KvShade(kvT, rz));
f1 = t.Get3(x, y, z + 1) && t.Get3(x - dir, y, z + 1);
if (f1)
c.Set(horloc - lensiz*dir, verloc + depsiz,
!fColor ? !ds.fShading : KvShade(KvBlend(kvT,
KvBlend(c2->Get3R(x, y, z + 1, fRainbow),
c2->Get3R(x - dir, y, z + 1, fRainbow))), ry));
f1 = t.Get3(x, y - 1, z) && t.Get3(x - dir, y - 1, z);
f2 = !(t.Get3(x, y - 1, z - 1) || t.Get3(x - dir, y - 1, z - 1));
if (f1 && f2)
c.Set(horloc - (lensiz - widsiz)*dir, verloc - widsiz,
!fColor ? !ds.fSkyShade : KvShade(KvBlend(kvT,
KvBlend(c2->Get3R(x, y - 1, z, fRainbow),
c2->Get3R(x - dir, y - 1, z, fRainbow))), rz));
}
if (t.Get3(x, y, z + 1)) {
if (fColor)
kvT = KvBlend(kv, c2->Get3R(x, y, z + 1, fRainbow));
if (lensiz > 1)
c.LineX(horloc - (lensiz - 1)*dir, horloc - dir,
verloc + depsiz, !fColor ? !ds.fShading : KvShade(kvT, ry));
if (!t.Get3(x + dir, y, z + 1) && widsiz > 1)
c.LineZ(horloc + dir, verloc + depsiz - 1, verloc - widsiz +
depsiz + 1, dir, !fColor ? fOn : KvShade(kvT, rx));
f1 = t.Get3(x, y - 1, z) && t.Get3(x, y - 1, z + 1);
f2 = !(t.Get3(x + dir, y - 1, z) || t.Get3(x + dir, y, z + 1));
if (f1 && f2 && !t.Get3(x + dir, y - 1, z + 1))
c.Set(horloc + widsiz*dir, verloc - widsiz + depsiz,
!fColor ? fOn : KvShade(KvBlend(kvT,
KvBlend(c2->Get3R(x, y - 1, z, fRainbow),
c2->Get3R(x, y - 1, z + 1, fRainbow))), rx));
}
if (t.Get3(x, y - 1, z)) {
if (fColor)
kvT = KvBlend(kv, c2->Get3R(x, y - 1, z, fRainbow));
if (!t.Get3(x + dir, y - 1, z) && depsiz > 1)
c.LineY(horloc + widsiz*dir,
verloc - widsiz + 1, verloc - widsiz + depsiz - 1,
!fColor ? fOn : KvShade(kvT, rx));
if (!t.Get3(x, y - 1, z - 1) && lensiz > 1)
c.LineX(horloc - (lensiz - widsiz - 1)*dir,
horloc + (widsiz - 1)*dir, verloc - widsiz,
!fColor ? !ds.fSkyShade : KvShade(kvT, rz));
}
}
}
}
UpdateDisplay();
}
}
// Replace bitmap with an orthographic overview of a 3D monochrome bitmap. The
// color of each block comes from the contents of an optional color bitmap.
// Implements the Render Bitmap Overview command for 3D bitmaps.
flag DrawOverviewCube(CMap &c, CONST CMon3 &t)
{
CMon3 tCopy;
CCol3 cCopy;
flag fColor = c.FColor();
if (fColor ? !cCopy.FBitmapCopy(c) : !tCopy.FBitmapCopy(c))
return fFalse;
if (!c.FBitmapSizeSet(t.m_x3*ds.horsiz + t.m_y3*ds.versiz + 1,
t.m_z3*ds.depsiz + t.m_y3*ds.versiz + 1))
return fFalse;
c.BitmapSet(!fColor ? !ds.fGroundShade : ds.kvGroundLo);
UpdateDisplay();
FillOverviewCube(c, !fColor ? tCopy : t, !fColor ? NULL : &cCopy,
ds.horsiz, ds.versiz, ds.depsiz, ds.fRight, ds.fMerge);
return fTrue;
}
// Draw an angled orthographic overview of one bitmap within the bounds of
// another.
void FillOverview2(CMon &b2, CONST CMon &b1)
{
int xs = ds.horsiz*ds.versiz, x, y, horloc, verloc;
if (ds.depsiz < 1)
return;
for (y = 0; y < b1.m_y; y++) {
for (x = 0; x < b1.m_x; x++) if (b1.Get(x, y)) {
horloc = (x + (b1.m_y-y)) * (xs + 1);
verloc = (x + y + 2)*(ds.versiz + 1);
// Draw a block for each set pixel.
FillCube2(b2, horloc, verloc, ds.horsiz, ds.versiz, ds.depsiz - 1,
!b1.Get(x + 1, y), !b1.Get(x, y + 1), fTrue);
// Draw lines removing seams between this and earlier drawn blocks.
if (ds.fMerge) {
if (b1.Get(x - 1, y)) {
b2.LineZ(horloc - xs, verloc - ds.versiz - 2,
verloc - ds.versiz*2 - 1, ds.horsiz, !ds.fSkyShade);
if (ds.depsiz > 2)
b2.LineY(horloc - xs - 1, verloc - ds.versiz,
verloc - ds.versiz + ds.depsiz - 3, !ds.fShading);
if (b1.Get(x, y - 1) && b1.Get(x - 1, y - 1))
b2.Set(horloc, verloc - ds.versiz*2 - 2, !ds.fSkyShade);
}
if (b1.Get(x, y - 1)) {
b2.LineZ(horloc + xs, verloc - ds.versiz - 2,
verloc - ds.versiz*2 - 1, -ds.horsiz, !ds.fSkyShade);
if (!b1.Get(x + 1, y - 1) && ds.depsiz > 2)
b2.LineY(horloc + xs + 1, verloc - ds.versiz,
verloc - ds.versiz + ds.depsiz - 3, fOn);
}
}
}
UpdateDisplay();
}
}
// Replace a bitmap with an angled orthographic overview of itself. Implements
// the Overview2 operation for monochrome 2D bitmaps.
flag DrawOverview2(CMon &b)
{
CMon b1;
if (!b1.FBitmapCopy(b))
return fFalse;
if (!b.FBitmapSizeSet((b1.m_x + b1.m_y)*(ds.horsiz*ds.versiz + 1) + 1,
(b1.m_x + b1.m_y)*(ds.versiz + 1) + ds.depsiz))
return fFalse;
b.BitmapSet(!ds.fGroundShade);
UpdateDisplay();
FillOverview2(b, b1);
return fTrue;
}
// Draw an angled orthographic overview of a 3D bitmap within the bounds of
// another bitmap.
void FillOverviewCube2(CMon &b, CONST CMon3 &t,
int lensiz, int widsiz, int depsiz, flag fMerge)
{
int xs = ds.horsiz*ds.versiz, x, y, z, horloc, verloc;
flag f1, f2;
for (y = 0; y < t.m_y3; y++) {
for (z = t.m_z3-1; z >= 0; z--)
for (x = 0; x < t.m_x3; x++)
if (t.Get3(x, y, z)) {
horloc = (x + (t.m_y3-y)) * (xs + 1);
verloc = (x + y + 2)*(ds.versiz + 1) + z*ds.depsiz;
// Draw a block for each set pixel.
FillCube2(b, horloc, verloc, lensiz, widsiz, depsiz,
!t.Get3(x + 1, y, z), !t.Get3(x, y + 1, z), !t.Get3(x, y, z - 1));
// Draw lines removing seams between this and earlier drawn blocks.
if (fMerge) {
if (t.Get3(x - 1, y, z)) {
if (depsiz > 1)
b.LineY(horloc - xs - 1, verloc - widsiz,
verloc - widsiz + depsiz - 2, !ds.fShading);
if (widsiz > 0)
b.LineZ(horloc - xs, verloc - widsiz - 2,
verloc - widsiz*2 - 1, lensiz, !ds.fSkyShade);
f1 = t.Get3(x, y, z + 1) && t.Get3(x - 1, y, z + 1);
if (f1)
b.Set(horloc - xs - 1, verloc - widsiz + depsiz - 1,
!ds.fShading);
f1 = t.Get3(x, y - 1, z) && t.Get3(x - 1, y - 1, z);
f2 = !(t.Get3(x, y - 1, z - 1) || t.Get3(x - 1, y - 1, z - 1));
if (f1 && f2)
b.Set(horloc, verloc - widsiz*2 - 2, !ds.fSkyShade);
}
if (t.Get3(x, y, z + 1)) {
if (lensiz > 0)
b.LineZ(horloc - 1, verloc + depsiz - 1,
verloc + depsiz - widsiz, -lensiz, !ds.fShading);
if (!t.Get3(x + 1, y, z + 1) && widsiz > 0)
b.LineZ(horloc + 1, verloc + depsiz - 1,
verloc + depsiz - widsiz, lensiz, fOn);
f1 = t.Get3(x, y - 1, z) && t.Get3(x, y - 1, z + 1);
f2 = !(t.Get3(x + 1, y - 1, z) || t.Get3(x + 1, y, z + 1));
if (f1 && f2 && !t.Get3(x + 1, y - 1, z + 1))
b.Set1(horloc + xs + 1, verloc - widsiz + depsiz - 1);
}
if (t.Get3(x, y - 1, z)) {
if (!t.Get3(x + 1, y - 1, z) && depsiz > 1)
b.LineY(horloc + xs + 1, verloc - widsiz,
verloc - widsiz + depsiz - 2, fOn);
if (!t.Get3(x, y - 1, z - 1) && lensiz > 0)
b.LineZ(horloc + xs, verloc - widsiz - 2,
verloc - widsiz*2 - 1, -lensiz, !ds.fSkyShade);
}
}
}
UpdateDisplay();
}
}
// Replace a 3D bitmap with an angled orthographic overview of itself.
// Implements the Overview2 operation for monochrome 3D bitmaps.
flag DrawOverviewCube2(CMon3 &b)
{
CMon3 t;
if (!t.FBitmapCopy(b))
return fFalse;
if (!b.FBitmapSizeSet((t.m_x3 + t.m_y3)*(ds.horsiz*ds.versiz + 1) + 1,
(t.m_x3 + t.m_y3)*(ds.versiz + 1) + t.m_z3*ds.depsiz + 1))
return fFalse;
b.BitmapSet(!ds.fGroundShade);
UpdateDisplay();
FillOverviewCube2(b, t, ds.horsiz, ds.versiz, ds.depsiz, ds.fMerge);
return fTrue;
}
#define ZAltitude(kv) (NMin(NMultDiv(kv, zscale, 1000), zmax) - 1)
// Draw an orthographic overview of a color bitmap, where the color value of
// each pixel indicates the height of that block, within the bounds of another
// bitmap. The color for each block comes from an optional third bitmap.
void FillOverviewAltitude(CMap &c, CONST CMap &c1, CONST CMap *c2,
int zscale, int zmax)
{
int x, y, z, z1, z2, horloc, verloc, dir;
real rx = 0.0, ry = 0.0, rz = 0.0;
KV kv, kvT;
flag fColor = c.FColor();
if (fColor && ds.fShading) {
rx = ds.vLight.m_x / 100.0;
ry = ds.vLight.m_y / 100.0;
rz = ds.vLight.m_z / 100.0;
}
dir = ds.fRight*2 - 1;
for (y = 0; y < c1.m_y; y++) {
verloc = (y + 1)*ds.versiz + zmax - 1;
for (x = ds.fRight ? 0 : c1.m_x - 1; x >= 0 && x < c1.m_x; x += dir) {
kvT = c1.Get(x, y);
z = ZAltitude(kvT);
if (z < 0)
continue;
if (ds.fRight)
horloc = (x + 1)*ds.horsiz + (c1.m_y - y - 1)*ds.versiz;
else
horloc = x*ds.horsiz + (y + 1)*ds.versiz;
// Draw a colored block of appropriate height for each non-zero pixel.
kv = !fColor ? fOn : c2->Get(x, y);
FillCube(c, horloc, verloc - z, ds.horsiz, ds.versiz, z, dir,
ds.kvTrim, kv, rx, ry, rz,
c1.Get(x + dir, y) < kvT, c1.Get(x, y + 1) < kvT, fTrue);
// Draw lines removing seams between this and earlier drawn blocks, in a
// color half way between the color used for the two faces.
if (ds.fMerge) {
z1 = ZAltitude(c1.Get(x - dir, y));
if (z1 >= 0) {
if (fColor)
kvT = KvBlend(kv, c2->Get(x - dir, y));
if (z == z1 && ds.versiz > 1)
c.LineZ(horloc - (ds.horsiz - 1)*dir, verloc - z - 1,
verloc - z - ds.versiz + 1, dir,
!fColor ? !ds.fSkyShade : KvShade(kvT, rz));
z2 = Min(z1, z);
if (z2 > 1)
c.LineY(horloc - ds.horsiz*dir, verloc - z2 + 1, verloc - 1,
!fColor ? !ds.fShading : KvShade(kvT, ry));
if (z == z1 && z == ZAltitude(c1.Get(x, y - 1)) &&
z == ZAltitude(c1.Get(x - dir, y - 1)))
c.Set(horloc - (ds.horsiz - ds.versiz)*dir, verloc - z -
ds.versiz, !fColor ? !ds.fSkyShade : KvShade(KvBlend(kvT,
KvBlend(c2->Get(x, y - 1), c2->Get(x - dir, y - 1))), rz));
}
z1 = ZAltitude(c1.Get(x, y - 1));
if (z1 >= 0) {
if (fColor)
kvT = KvBlend(kv, c2->Get(x, y - 1));
if (z == z1 && ds.horsiz > 1)
c.LineX(horloc - (ds.horsiz - ds.versiz - 1)*dir,
horloc + (ds.versiz - 1)*dir, verloc - z - ds.versiz,
!fColor ? !ds.fSkyShade : KvShade(kvT, rz));
z1 = Min(z1, z);
if (z1 > 1) {
z2 = ZAltitude(c1.Get(x + dir, y - 1)); z2 = Max(z2, 0);
if (z1 > z2 + 1)
c.LineY(horloc + ds.versiz*dir, verloc - ds.versiz - z1 + 1,
verloc - ds.versiz - z2 - 1,
!fColor ? fOn : KvShade(kvT, rx));
}
}
}
}
UpdateDisplay();
}
}
// Draw an orthographic overview of a color bitmap, where the color value of
// each pixel indicates the height of that block, in a different bitmap. The
// color to use for each block comes from an optional second bitmap.
// Implements the Altitude operation for monochrome and color bitmaps.
flag DrawOverviewAltitude(CMap &c, CONST CCol &c2, int zscale, int zmax)
{
CCol c1;
CONST CCol *pc;
long l;
flag fColor = c.FColor();
pc = !fColor ? &c2 : &c1;
if (fColor && !c1.FBitmapCopy(c))
return fFalse;
if (zscale >= 0)
zmax = NMultDiv(zmax, zscale, 1000);
else {
l = pc->ColmapFind(0);
zscale = (zmax * 1000 + 499) / (l == 0 ? 1 : l);
}
if (!c.FBitmapSizeSet(pc->m_x*ds.horsiz + pc->m_y*ds.versiz + 1,
pc->m_y*ds.versiz + zmax))
return fFalse;
c.BitmapSet(!fColor ? !ds.fGroundShade : ds.kvGroundLo);
UpdateDisplay();
if (!fColor)
FillOverviewAltitude(c, c2, NULL, zscale, zmax);
else
FillOverviewAltitude(c, c1, &c2, zscale, zmax);
return fTrue;
}
/*
******************************************************************************
** Render Pyramid
******************************************************************************
*/
// Return whether a point between four blocks forms a visible corner that
// should have an edge line drawn at that point.
flag FCorner(CONST CMon &b, int x, int y)
{
flag f0, f1, f2, f3;
int count;
// A single block forms a convex corner, three blocks form a concave corner,
// and two opposite blocks form a checkerboard corner.
f0 = b.Get(x, y);
f1 = b.Get(x - 1, y);
f2 = b.Get(x, y - 1);
f3 = b.Get(x - 1, y - 1);
count = f0 + f1 + f2 + f3;
return FOdd(count) || (count == 2 && f0 == f3);
}
// Draw an edge line from an exposed block corner to the vanishing point. Stop
// drawing if the line becomes no longer visible behind other blocks.
void PLine(CMap &b2, CONST CMon &b1, int x1, int y1,
int x2, int y2, int xv, int yv)
{
int x = x1, y = y1, dx = x2 - x1, dy = y2 - y1, xInc, yInc, xInc2, yInc2,
d, dInc, z, zMax, xT, yT;
flag fColor = b2.FColor(), fStarted = fFalse, fProbation = fFalse,
f0, f1, f2, f3;
// If blocks are transparent, just draw the whole line.
if (!ds.fMerge) {
b2.Line(x1, y1, x2, y2, !fColor ? fOff : ds.kvTrim);
return;
}
// If vanishing point is covered by a block, don't draw edges of that block.
xT = x1 / ds.horsiz; yT = y1 / ds.versiz;
if (b1.Get(xT - (xT > xv), yT - (yT > yv)))
return;
// Determine slope.
if (NAbs(dx) >= NAbs(dy)) {
xInc = NSgn(dx); yInc = 0;
xInc2 = 0; yInc2 = NSgn(dy);
zMax = NAbs(dx); dInc = NAbs(dy);
d = zMax - (!FOdd(dx) && x1 > x2);
} else {
xInc = 0; yInc = NSgn(dy);
xInc2 = NSgn(dx); yInc2 = 0;
zMax = NAbs(dy); dInc = NAbs(dx);
d = zMax - (!FOdd(dy) && y1 > y2);
}
d >>= 1;
// Loop over long axis, adjusting short axis for slope as needed.
for (z = 0; z <= zMax; z++) {
f0 = b1.Get(x / ds.horsiz, y / ds.versiz);
f1 = b1.Get((x-1) / ds.horsiz, y / ds.versiz);
f2 = b1.Get(x / ds.horsiz, (y-1) / ds.versiz);
f3 = b1.Get((x-1) / ds.horsiz, (y-1) / ds.versiz);
// If entirely within a block, definitely stop drawing.
if (f0 && f1 && f2 && f3)
break;
// If adjacent to a block, that's ok unless it happens twice in a row.
if (f0 || f1 || f2 || f3) {
if (fStarted) {
if (fProbation)
break;
fProbation = fTrue;
}
} else {
fStarted = fTrue;
fProbation = fFalse;
}
// Not within a block, so draw next pixel of line.
if (!fColor)
b2.Set0(x, y);
else
b2.Set(x, y, ds.kvTrim);
x += xInc; y += yInc; d += dInc;
if (d >= zMax) {
x += xInc2; y += yInc2; d -= zMax;
}
}
}
// Draw a single block in a single point perspective graphic.
void FillBlock(CMap &b2, CONST CMon &b1, int x, int y, int xv, int yv)
{
int x1, y1, x2, y2;
flag fColor = b2.FColor();
if (!b1.Get(x, y) || !ds.fMerge)
return;
// Draw the front face, and the faces that go to the vanishing point.
if (!fColor) {
if ((x < xv && !b1.Get(x+1, y)) || (x > xv && !b1.Get(x-1, y)) ||
(y < yv && !b1.Get(x, y+1)) || (y > yv && !b1.Get(x, y-1))) {
x1 = (x + (y == yv && x < xv))*ds.horsiz;
y1 = (y + ((y < yv && x <= xv) || (y > yv && x > xv)))*ds.versiz;
x2 = (x + (y != yv || x < xv))*ds.horsiz;
y2 = (y + ((y >= yv || x >= xv) && (y <= yv || x < xv)))*ds.versiz;
b2.FTriangle(ds.horv, ds.verv, x1, y1, x2, y2, fOn, 0);
}
b2.Block(x*ds.horsiz, y*ds.versiz, (x+1)*ds.horsiz, (y+1)*ds.versiz,
!ds.fShading);
} else {
x1 = x*ds.horsiz; y1 = y*ds.versiz;
x2 = x1 + ds.horsiz; y2 = y1 + ds.versiz;
if (x < xv && !b1.Get(x+1, y))
b2.FTriangle(ds.horv, ds.verv, x2, y1, x2, y2,
ds.fShading ? ds.kvGroundLo : ds.kvSkyHi, 0);
if (x > xv && !b1.Get(x-1, y))
b2.FTriangle(ds.horv, ds.verv, x1, y1, x1, y2,
ds.fShading ? ds.kvGroundHi : ds.kvSkyHi, 0);
if (y < yv && !b1.Get(x, y+1))
b2.FTriangle(ds.horv, ds.verv, x1, y2, x2, y2,
ds.fShading ? ds.kvSkyLo : ds.kvSkyHi, 0);
if (y > yv && !b1.Get(x, y-1))
b2.FTriangle(ds.horv, ds.verv, x1, y1, x2, y1,
ds.kvSkyHi, 0);
b2.Block(x*ds.horsiz, y*ds.versiz, (x+1)*ds.horsiz, (y+1)*ds.versiz,
ds.kvObject);
}
}
// Draw a line of blocks in a single point perspective graphic, in the given
// order. It's assumed the vanishing point doesn't intersect the line.
void FillLine(CMap &b2, CONST CMon &b1, int x1, int y1, int x2, int y2,
int xv, int yv)
{
int xi, yi, x, y;
// Draw blocks in a line from (x1, y1) to (x2, y2).
Assert(x1 == x2 || y1 == y2);
if (x1 == x2) {
yi = y1 < y2 ? 1 : -1; xi = 0;
} else {
xi = x1 < x2 ? 1 : -1; yi = 0;
}
x = x1; y = y1;
loop {
FillBlock(b2, b1, x, y, xv, yv);
if (x == x2 && y == y2)
break;
x += xi; y += yi;
}
}
// Draw a grid of blocks in a single point perspective graphic, in the given
// order from one corner to the opposite. It's assumed the vanishing point
// doesn't intersect the grid. Lines of blocks are drawn touching the diagonal
// leading up to the end corner. For example, a 5x4 grid from lower left to
// upper right has blocks drawn in the following order:
//
// 16 17 18 19 20
// 9 10 11 12 15
// 4 5 6 8 14
// 1 2 3 7 13
void FillSection(CMap &b2, CONST CMon &b1, int x1, int y1, int x2, int y2,
int xv, int yv)
{
int xi, yi, xd, yd, xo, yo, iMax, i;
// Draw blocks in a grid from (x1, y1) to (x2, y2).
if (x2 < 0 || y2 < 0)
return;
xi = x2 > x1 ? 1 : -1; yi = y2 > y1 ? 1 : -1;
xd = NAbs(x2 - x1) + 1; yd = NAbs(y2 - y1) + 1;
if (xd > yd) {
xo = xd - yd; yo = 0;
} else {
yo = yd - xd; xo = 0;
}
// Draw the first line touching the start corner.
FillLine(b2, b1, x1, y1, x1 + xo*xi, y1 + yo*yi, xv, yv);
// Draw pairs of lines growing out from the first line like layers of an
// onion, until the final layer forming the edges touching the end corner.
iMax = xd < yd ? xd : yd;
for (i = 1; i < iMax; i++) {
FillLine(b2, b1,
x1 + (xo+i)*xi, y1, x1 + (xo+i)*xi, y1 + (yo+i-1)*yi, xv, yv);
FillLine(b2, b1,
x1, y1 + (yo+i)*yi, x1 + (xo+i)*xi, y1 + (yo+i)*yi, xv, yv);
}
}
// Draw a single point perspective graphic of one bitmap in another. Each
// pixel becomes a rectangle with lines leading to the vanishing point.
void FillPyramid(CMap &b2, CONST CMon &b1)
{
int xv, yv, x, y;
flag fColor = b2.FColor();
xv = ds.horv / ds.horsiz;
yv = ds.verv / ds.versiz;
if (xv > b1.m_x)
xv = b1.m_x;
if (yv > b1.m_y)
yv = b1.m_y;
// Draw the blocks in four quadrants, divided by the location of the
// vanishing point.
FillSection(b2, b1, 0, 0, xv-1, yv-1, xv, yv);
FillSection(b2, b1, b1.m_x-1, 0, xv, yv-1, xv, yv);
FillSection(b2, b1, 0, b1.m_y-1, xv-1, yv, xv, yv);
FillSection(b2, b1, b1.m_x-1, b1.m_y-1, xv, yv, xv, yv);
// Draw the front edges of the blocks, where not adjacent to other blocks.
for (y = 0; y <= b1.m_y; y++)
for (x = 0; x <= b1.m_x; x++) {
if (b1.Get(x, y) != b1.Get(x, y - 1))
b2.LineX(x*ds.horsiz, (x+1)*ds.horsiz, y*ds.versiz,
!fColor ? fOff : ds.kvTrim);
if (b1.Get(x, y) != b1.Get(x - 1, y))
b2.LineY(x*ds.horsiz, y*ds.versiz, (y+1)*ds.versiz,
!fColor ? fOff : ds.kvTrim);
// Draw visible edges of the block that go to the vanishing point. Don't
// draw lines if the block is solid and the line would be behind it.
if (FCorner(b1, x, y))
PLine(b2, b1, x*ds.horsiz, y*ds.versiz, ds.horv, ds.verv, xv, yv);
}
}
// Replace a bitmap with a single point perspective graphic of it. Implements
// the Render Pyramid command.
flag DrawPyramid(CMon &b, CCol *c)
{
CMon bCopy;
int x, y;
flag fColor = (c != NULL);
x = b.m_x*ds.horsiz + 1; y = b.m_y*ds.versiz + 1;
if (FErrorRange("X cell size", ds.horsiz, 2, (xBitmap-1) / b.m_x))
return fFalse;
if (FErrorRange("Y cell size", ds.versiz, 2, (yBitmap-1) / b.m_y))
return fFalse;
if (FErrorRange("X vanishing point", ds.horv, 0, x-1))
return fFalse;
if (FErrorRange("Y vanishing point", ds.verv, 0, y-1))
return fFalse;
if (!fColor) {
if (!bCopy.FBitmapCopy(b))
return fFalse;
if (b.FBitmapSizeSet(x, y)) {
b.BitmapSet(!ds.fGroundShade);
UpdateDisplay();
FillPyramid(b, bCopy);
}
} else {
if (c->FBitmapSizeSet(x, y)) {
c->BitmapSet(kvWhite);
UpdateDisplay();
FillPyramid(*c, b);
}
}
return fTrue;
}
/*
******************************************************************************
** File Reading Routines
******************************************************************************
*/
// Load a Daedalus wireframe file, into a new list of lines.
long ReadWirelist(COOR **pcoor, FILE *file)
{
char szLine[cchSzDef];
COOR *coor;
long count = 0, ichFile, i, j;
real x1, y1, z1, x2, y2, z2;
KV kv = ds.kvTrim;
char ch, ch2;
ch = getbyte(); ch2 = getbyte();
if (ch != 'D' || ch2 != 'W') {
PrintSz_W("This file does not look like a Daedalus wireframe.\n");
return -1;
}
// Figure out how many coordinate pairs are in this wireframe file.
ch = getbyte();
if (ch == '#')
fscanf(file, "%ld", &count);
else {
ichFile = ftell(file);
loop {
while (!feof(file) && (ch = getc(file)) < ' ')
;
if (feof(file))
break;
if (!FDigitCh(ch) && ch != '-') {
for (szLine[0] = ch, j = 1; j < cchSzDef-1 && !feof(file) &&
(ch = getbyte()) >= ' '; j++)
;
continue;
} else
ungetc(ch, file);
ReadCoordinates(file, &x1, &y1, &z1, &x2, &y2, &z2);
count++;
}
fseek(file, ichFile, SEEK_SET);
}
// Allocate a line list of the appropriate size.
coor = RgAllocate(count, COOR);
if (coor == NULL)
return -1;
if (*pcoor != NULL)
DeallocateP(*pcoor);
*pcoor = coor;
// Load the appropriate number of actual lines from the file.
for (i = 0; i < count; i++) {
while (!feof(file) && (ch = getc(file)) < ' ')
;
if (feof(file))
break;
if (!FDigitCh(ch) && ch != '-') {
for (szLine[0] = ch, j = 1; j < cchSzDef-1 && !feof(file) &&
(ch = getbyte()) >= ' '; j++)
szLine[j] = ch;
szLine[j] = chNull;
kv = ParseColor(szLine, fFalse);
i--;
continue;
} else
ungetc(ch, file);
ReadCoordinates(file, &x1, &y1, &z1, &x2, &y2, &z2);
coor[i].x1 = x1; coor[i].y1 = y1; coor[i].z1 = z1;
coor[i].x2 = x2; coor[i].y2 = y2; coor[i].z2 = z2;
coor[i].kv = kv;
}
return count;
}