-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreeDee.java
1332 lines (1129 loc) · 37.9 KB
/
ThreeDee.java
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
import java.util.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Array;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.stream.Collectors;
//#region Primitive types
class Vector2i
{
public int X, Y;
public Vector2i(int x, int y)
{
X = x;
Y = y;
}
public Vector2i clipRangeZeroToEnd(Vector2i end)
{
return clipRange(new Vector2i(0, 0), end);
}
public Vector2i clipRange(Vector2i start, Vector2i end)
{
int x = X < start.X ? start.X : (X >= end.X ? end.X - 1 : X);
int y = Y < start.Y ? start.Y : (Y >= end.Y ? end.Y - 1 : Y);
return new Vector2i(x, y);
}
}
class Vector2f
{
public float X, Y;
public Vector2f(float x, float y)
{
X = x;
Y = y;
}
@Override
public String toString()
{
return "[" + X + ", " + Y + "]";
}
}
class Vector3i
{
public int X, Y, Z;
public Vector3i(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
public Vector3i multiply(float scalar)
{
return new Vector3i((int) (X * scalar), (int) (Y * scalar), (int) (Z * scalar));
}
}
class Vector3f
{
public float X, Y, Z;
public Vector3f(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
@Override
public String toString()
{
return "[" + X + ", " + Y + ", " + Z + "]";
}
public Vector3f zProject()
{
float z = (float) Math.log(Z + 1);
return new Vector3f(X / z, Y / z, Z);
}
public Vector4f toVector4f(float w)
{
return new Vector4f(X, Y, Z, w);
}
public Vector2f toVector2f()
{
return new Vector2f(X, Y);
}
public Vector3i toVector3i()
{
return new Vector3i((int) X, (int) Y, (int) Z);
}
public Vector2i toVector2i()
{
return new Vector2i((int) X, (int) Y);
}
}
class Vector4f
{
public float X, Y, Z, W;
public Vector4f(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
@Override
public String toString()
{
return "[" + X + ", " + Y + ", " + Z + ", " + W + "]";
}
public static Vector4f fromEulerAngles(float roll, float pitch, float yaw) // roll (x), pitch (y), yaw (z)
{
float cr = (float) Math.cos(roll * 0.5);
float sr = (float) Math.sin(roll * 0.5);
float cp = (float) Math.cos(pitch * 0.5);
float sp = (float) Math.sin(pitch * 0.5);
float cy = (float) Math.cos(yaw * 0.5);
float sy = (float) Math.sin(yaw * 0.5);
return new Vector4f(sr * cp * cy - cr * sp * sy,
cr * sp * cy + sr * cp * sy,
cr * cp * sy - sr * sp * cy,
cr * cp * cy + sr * sp * sy);
}
/*public Vector4f zProject()
{
float z = (float) Math.log(Z + 1);
return new Vector4f(X / z, Y / z, Z, W / z);
}*/
public Vector3f xyz()
{
return toVector3f(false);
}
public Vector3f toVector3f(boolean normalize)
{
return normalize ? new Vector3f(X / W, Y / W, Z / W) : new Vector3f(X, Y, Z);
}
}
class Matrix4x4f
{
public float A11, A12, A13, A14,
A21, A22, A23, A24,
A31, A32, A33, A34,
A41, A42, A43, A44;
public static final Matrix4x4f IDENTITY = new Matrix4x4f(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
public static final Matrix4x4f EMPTY = new Matrix4x4f(
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0);
public Matrix4x4f(float a11, float a12, float a13, float a14,
float a21, float a22, float a23, float a24,
float a31, float a32, float a33, float a34,
float a41, float a42, float a43, float a44)
{
A11 = a11;
A12 = a12;
A13 = a13;
A14 = a14;
A21 = a21;
A22 = a22;
A23 = a23;
A24 = a24;
A31 = a31;
A32 = a32;
A33 = a33;
A34 = a34;
A41 = a41;
A42 = a42;
A43 = a43;
A44 = a44;
}
public static Matrix4x4f scale(Vector3f input)
{
return new Matrix4x4f(
input.X, 0, 0, 0,
0, input.Y, 0, 0,
0, 0, input.Z, 0,
0, 0, 0, 1);
}
public static Matrix4x4f rotation(Vector4f q)
{
Matrix4x4f ret = Matrix4x4f.IDENTITY;
float sqx = q.X * q.X;
float sqy = q.Y * q.Y;
float sqz = q.Z * q.Z;
float sqw = q.W * q.W;
float xy = q.X * q.Y;
float xz = q.X * q.Z;
float xw = q.X * q.W;
float yz = q.Y * q.Z;
float yw = q.Y * q.W;
float zw = q.Z * q.W;
float s2 = 2f / (sqx + sqy + sqz + sqw);
ret.A11 = 1f - (s2 * (sqy + sqz));
ret.A22 = 1f - (s2 * (sqx + sqz));
ret.A33 = 1f - (s2 * (sqx + sqy));
ret.A12 = s2 * (xy + zw);
ret.A21 = s2 * (xy - zw);
ret.A31 = s2 * (xz + yw);
ret.A13 = s2 * (xz - yw);
ret.A32 = s2 * (yz - xw);
ret.A23 = s2 * (yz + xw);
return ret;
}
public static Matrix4x4f projection(float fov, float nearPlane, float farPlane)
{
float s = (float) (1 / Math.tan((fov / 2) + (Math.PI / 180)));
float z1 = -(farPlane / (farPlane - nearPlane));
float z2 = -((farPlane * nearPlane) / (farPlane - nearPlane));
return new Matrix4x4f(s, 0.0f, 0.0f, 0.0f,
0.0f, s, 0.0f, 0.0f,
0.0f, 0.0f, z1, -1.0f,
0.0f, 0.0f, z2, 0.0f);
}
public static Matrix4x4f translation(Vector3f input)
{
return new Matrix4x4f(
1, 0, 0, input.X,
0, 1, 0, input.Y,
0, 0, 1, input.Z,
0, 0, 0, 1);
}
public Vector4f multiply(Vector4f right)
{
return multiply(right, false);
}
public Vector4f multiply(Vector4f right, boolean normalize)
{
var x = (A11 * right.X) + (A12 * right.Y) + (A13 * right.Z) + (A14 * right.W);
var y = (A21 * right.X) + (A22 * right.Y) + (A23 * right.Z) + (A24 * right.W);
var z = (A31 * right.X) + (A32 * right.Y) + (A33 * right.Z) + (A34 * right.W);
var w = (A41 * right.X) + (A42 * right.Y) + (A43 * right.Z) + (A44 * right.W);
if (normalize)
{
x /= w;
y /= w;
z /= w;
w = 1;
}
return new Vector4f(x, y, z, w);
}
public Matrix4x4f multiply(Matrix4x4f right)
{
// A1x*Bx1 -> A11*B11 + A12*B21 ...
float r11 = (A11 * right.A11) + (A12 * right.A21) + (A13 * right.A31) + (A14 * right.A41);
// A1x*Bx2 -> A11*B12 + A12*B22 ...
float r12 = (A11 * right.A12) + (A12 * right.A22) + (A13 * right.A32) + (A14 * right.A42);
// A1x*Bx3 -> A11*B13 + A12*B23 ...
float r13 = (A11 * right.A13) + (A12 * right.A23) + (A13 * right.A33) + (A14 * right.A43);
// A1x*Bx4 -> A11*B14 + A12*B24 ...
float r14 = (A11 * right.A14) + (A12 * right.A24) + (A13 * right.A34) + (A14 * right.A44);
// A2x*Bx1 -> A21*B12 + A22*B22 ...
float r21 = (A21 * right.A11) + (A22 * right.A21) + (A23 * right.A31) + (A24 * right.A41);
float r22 = (A21 * right.A12) + (A22 * right.A22) + (A23 * right.A32) + (A24 * right.A42);
float r23 = (A21 * right.A13) + (A22 * right.A23) + (A23 * right.A33) + (A24 * right.A43);
float r24 = (A21 * right.A14) + (A22 * right.A24) + (A23 * right.A34) + (A24 * right.A44);
// A3x*Bx1 -> A31*B12 + A32*B22 ...
float r31 = (A31 * right.A11) + (A32 * right.A21) + (A33 * right.A31) + (A34 * right.A41);
float r32 = (A31 * right.A12) + (A32 * right.A22) + (A33 * right.A32) + (A34 * right.A42);
float r33 = (A31 * right.A13) + (A32 * right.A23) + (A33 * right.A33) + (A34 * right.A43);
float r34 = (A31 * right.A14) + (A32 * right.A24) + (A33 * right.A34) + (A34 * right.A44);
// A4x*Bx1 -> A41*B12 + A42*B22 ...
float r41 = (A41 * right.A11) + (A42 * right.A21) + (A43 * right.A31) + (A44 * right.A41);
float r42 = (A41 * right.A12) + (A42 * right.A22) + (A43 * right.A32) + (A44 * right.A42);
float r43 = (A41 * right.A13) + (A42 * right.A23) + (A43 * right.A33) + (A44 * right.A43);
float r44 = (A41 * right.A14) + (A42 * right.A24) + (A43 * right.A34) + (A44 * right.A44);
return new Matrix4x4f(r11, r12, r13, r14, r21, r22, r23, r24, r31, r32, r33, r34, r41, r42, r43, r44);
}
}
//#endregion
//#region Drawing types
class Texture
{
int width, height;
Vector3i[][] pixels;
public Texture(Vector3i[] rawRgbData, int width)
{
this.width = width;
height = rawRgbData.length / this.width;
pixels = new Vector3i[this.width][height];
for (int i = 0; i < rawRgbData.length; i++)
{
int x = i % width, y = i / width;
pixels[x][y] = rawRgbData[i];
}
}
public Texture(String path) throws IOException
{
BufferedImage buffer;
if (path.contains(":")) // It's a URL.
{
URL url = new URL(path);
InputStream iStr = url.openStream();
buffer = ImageIO.read(iStr);
}
else // TODO: Is it necessary to make this distinction?
{
buffer = ImageIO.read(new File(path));
}
width = buffer.getWidth();
height = buffer.getHeight();
// Get all pixels and pre-process them in a Vector3i array.
pixels = new Vector3i[width][height];
for (int i = 0; i < width * height; i++)
{
int x = i % width, y = i / width;
int p = buffer.getRGB(x % width, y % height);
//int a = (p >> 24) & 0xff;
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
pixels[x][y] = new Vector3i(r, g, b);
}
}
public Vector3i getNdcPixel(Vector2f xy)
{
return getPixel((int) (xy.X * width), (int) (xy.Y * height));
}
public Vector3i getPixel(int x, int y)
{
return pixels[x % width][y % height];
}
}
class Framebuffer<T>
{
T[] buffer;
Vector2i bufferSize;
T defaultValue;
float[] zBuffer;
float farPlane = 100.0f;
public Framebuffer(int width, int height, T defaultVal)
{
@SuppressWarnings("unchecked")
final T[] a = (T[]) Array.newInstance(defaultVal.getClass(), width * height);
buffer = a;
zBuffer = new float[width * height];
bufferSize = new Vector2i(width, height);
defaultValue = defaultVal;
clear();
}
public void clear()
{
fill(defaultValue);
}
public void fill(T value)
{
for (int i = 0; i < buffer.length; i++)
{
buffer[i] = value;
zBuffer[i] = farPlane;
}
}
public T get(int index)
{
return (index > 0 && index < buffer.length) ? buffer[index] : defaultValue;
}
public void safeSet(int index, T value)
{
if (index > 0 && index < buffer.length)
{
buffer[index] = value;
}
}
public void safeSet(Vector2i coord, T value)
{
if (coord.X >= 0 && coord.X < bufferSize.X &&
coord.Y >= 0 && coord.Y < bufferSize.Y)
{
buffer[coord.X + (coord.Y * bufferSize.X)] = value;
}
}
public void safeSet(Vector3f coord, T value)
{
if (coord.X >= 0 && coord.X < bufferSize.X &&
coord.Y >= 0 && coord.Y < bufferSize.Y &&
coord.Z > 0.1f && coord.Z < farPlane) // TODO: This should be handled by the shader.
{
int address = (int) coord.X + ((int) coord.Y * bufferSize.X);
if (coord.Z < zBuffer[address]) // Fragment is nearer the last fragment.
{
buffer[address] = value;
zBuffer[address] = coord.Z;
}
}
}
public void safeNdcPixelSet(Vector3f ndc, T value)
{
safeNdcPixelSet(ndc.X, ndc.Y, ndc.Z, value);
}
public void safeNdcPixelSet(float x, float y, float z, T value)
{
Vector3f fbc = ThreeDee.ndcToFbSize(x, y, z, bufferSize.X, bufferSize.Y);
if (fbc.Z > 0.1f && fbc.Z < farPlane) // TODO: Take near plane into account too.
{
safeSet((int) fbc.X + ((int) fbc.Y * bufferSize.X), value);
}
}
}
class BasicShaders
{
static Vector3f[] line(Vector3f a, Vector3f b)
{
float xLen = Math.abs(Math.abs(a.X) + Math.abs(b.X));
float yLen = Math.abs(Math.abs(a.Y) + Math.abs(b.Y));
//float zLen = Math.abs(Math.abs(a.Z) + Math.abs(b.Z)); // TODO: Take Z into account.
int steps = (int) Math.min(Math.max(xLen, yLen), 1024);
List<Vector3f> list = new ArrayList<Vector3f>();
for (int i = 0; i < steps; i++)
{
float interpFactor = i / (float) steps;
float z = a.Z * (1.0f - interpFactor) + b.Z * interpFactor;
if (z < 0.0f)
{
continue;
}
float x = a.X * (1.0f - interpFactor) + b.X * interpFactor;
float y = a.Y * (1.0f - interpFactor) + b.Y * interpFactor;
list.add(new Vector3f(x, y, z));
}
Vector3f[] ret = new Vector3f[list.size()];
list.toArray(ret);
return ret;
}
static float edgeFunction(Vector3f a, Vector3f b, Vector3f c)
{
return edgeFunction(a.toVector2f(), b.toVector2f(), c.toVector2f());
}
static float edgeFunction(Vector2f a, Vector2f b, Vector2f c)
{
return ((c.X - a.X) * (b.Y - a.Y) - (c.Y - a.Y) * (b.X - a.X));
}
// TODO: Should this be a framebuffer method?
// TODO: Use `XYZUV` as inputs.
// TODO: This should discard fragments outside of the bounds of the Z buffer.
static XYZUV[] face(Vector3f a, Vector3f b, Vector3f c, Vector2f aUv, Vector2f bUv, Vector2f cUv, Vector2i framebufferSize)
{
float startX = Math.min(Math.min(a.X, b.X), Math.min(c.X, framebufferSize.X));
float endX = Math.max(Math.max(a.X, b.X), Math.max(c.X, 0));
float startY = Math.min(Math.min(a.Y, b.Y), Math.min(c.Y, framebufferSize.Y));
float endY = Math.max(Math.max(a.Y, b.Y), Math.max(c.Y, 0));
float startZ = Math.min(Math.min(a.Z, b.Z), Math.min(c.Z, Float.MAX_VALUE));
float endZ = Math.max(Math.max(a.Z, b.Z), Math.max(c.Z, 0));
// Check if:
// 1. The triangle is behind the camera or too far away.
// 2. The triangle is outside of the bounds of the framebuffer.
// If one or both of them is true, do not render the triangle; return an empty fragment array.
if ((endZ < 0.01f || startZ > 100.0f) ||
((endX < 0 || startX > framebufferSize.X) && (endY < 0 || startY > framebufferSize.Y)))
{
return new XYZUV[] {};
}
List<XYZUV> list = new ArrayList<XYZUV>();
float area = edgeFunction(a, b, c);
for (int y = (int) Math.max(0, startY); y < Math.min(framebufferSize.Y, endY); y++)
{
for (int x = (int) Math.max(0, startX); x < Math.min(framebufferSize.X, endX); x++)
{
Vector3f framebufferPoint = new Vector3f(x, y, 0);
float w0 = edgeFunction(a, b, framebufferPoint);
float w1 = edgeFunction(b, c, framebufferPoint);
float w2 = edgeFunction(c, a, framebufferPoint);
// If `framebufferPoint` is inside the triangle (between `a`, `b` and `c`).
if (w0 >= 0 && w1 >= 0 && w2 >= 0)
{
w0 /= area;
w1 /= area;
w2 /= area;
float z = w0 * c.Z + w1 * a.Z + w2 * b.Z;
float u = w0 * cUv.X + w1 * aUv.X + w2 * bUv.X;
float v = w0 * cUv.Y + w1 * aUv.Y + w2 * bUv.Y;
list.add(new XYZUV(new Vector3f(x, y, z), new Vector2f(u, v)));
}
}
}
XYZUV[] ret = new XYZUV[list.size()];
list.toArray(ret);
return ret;
}
}
//#endregion
class XYZUV
{
public Vector3f XYZ;
public Vector2f UV;
public XYZUV(Vector3f xyz, Vector2f uv)
{
XYZ = xyz;
UV = uv;
}
}
enum ConsoleRenderMode
{
// Single block character modes:
B0W1, // Black=0, white=1
B1W0, // Black=1, white=0
// Grayscale block characters modes:
GrayscaleChars, // This uses the shaded block Unicode characters without ANSI color escape codes.
// Single block character with ANSI/VT100 color codes:
RGB4, // This uses the standard and widely-supported 4-bit ANSI color escape codes (16 colors).
RGB8, // This uses the paletted 216-color version of the last one (216 colors).
RGB24, // This uses the 24-bit color version of the last one (16,777,216 colors).
}
class WindowsInterop
{
// Source: https://learn.microsoft.com/es-es/windows/console/setconsolemode
static final String CSHARP_OVER_POWERSHELL_INTEROP_KERNEL32 = "$sigKernel32 = @'\n" +
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
"public static extern IntPtr GetStdHandle(int nStdHandle);\n" +
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
"public static extern uint GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);\n" +
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
"public static extern uint SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);\n" +
"public const int STD_INPUT_HANDLE = -10;\n" +
"'@\n";
static final String POWERSHELL_GET_STDIN_HANDLE = CSHARP_OVER_POWERSHELL_INTEROP_KERNEL32 +
"$Kernel32Api = Add-Type -MemberDefinition $sigKernel32 -Name Kernel32Api -Namespace ThreeDee -PassThru\n" +
"$handle = $Kernel32Api::GetStdHandle($Kernel32Api::STD_INPUT_HANDLE)\n" +
"if ($handle -eq -1) {\n" +
" throw \"Cannot get standard input handle.\"\n" +
"}\n";
public static final int KERNEL32_ENABLE_PROCESSED_INPUT = 0x0001;
public static final int KERNEL32_ENABLE_LINE_INPUT = 0x0002;
public static final int KERNEL32_ENABLE_ECHO_INPUT = 0x0004;
public static String getStdinModeChangePowershellScript(int flag, boolean enable)
{
return POWERSHELL_GET_STDIN_HANDLE +
"$mode = 0\n" +
"$ret = $Kernel32Api::GetConsoleMode($handle, [ref]$mode)\n" +
"if ($ret -eq 0) {\n" +
" throw \"Cannot get standard input mode.\"\n" +
"}\n" +
// "Echo \"Console mode is $mode\"\n" +
"$newmode = $mode " +
(
enable ?
"-bor " + flag :
"-band (-bnot " + flag + ")"
) + "\n" +
// "Echo \"New console mode is $newmode\"\n" +
"$ret = $Kernel32Api::SetConsoleMode($handle, $newmode)\n" +
"if ($ret -eq 0) {\n" +
" throw \"Cannot set standard input mode.\"\n" +
"}\n" +
"$ret = $Kernel32Api::GetConsoleMode($handle, [ref]$mode)\n";
// "Echo \"Console mode is now $mode\"\n";
}
public static String runPowershellScript(String script) throws Exception, IOException, InterruptedException
{
ProcessBuilder powershellProcessBuilder = null;
if (script.contains("\n"))
{
String wrappedScript = "& {\n" + script + "\n}";
Base64.Encoder b64enc = Base64.getEncoder();
String encodedScript = b64enc.encodeToString(wrappedScript.getBytes(StandardCharsets.UTF_16LE));
powershellProcessBuilder = new ProcessBuilder("powershell", "-NoP", "-NonInteractive", "-EncodedCommand", encodedScript);
}
else
{
powershellProcessBuilder = new ProcessBuilder("powershell", "-NoP", "-command", script);
}
// AFAIK, this is required to modify the current `conhost.exe` instance.
powershellProcessBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
Process powershellProcess = powershellProcessBuilder.start();
int powershellStatus = powershellProcess.waitFor();
if (powershellStatus != 0)
{
throw new Exception("Powershell script failed with status code " + powershellStatus + ".");
}
BufferedReader br = new BufferedReader(new InputStreamReader(powershellProcess.getInputStream()));
String ret = String.join("\n", br.lines().collect(Collectors.toList()));
br.close();
return ret;
}
}
/**
* A basic 3D renderer for ANSI X3.64-compliant terminals/consoles.
*
* @author Unai Domínguez
*/
class ThreeDee
{
static String textureFile = null;
static boolean showLines = false;
static boolean showVertices = false;
static ConsoleRenderMode consoleRenderMode = ConsoleRenderMode.RGB24;
static Framebuffer<Vector3i> framebuffer;
// Runtime registers
static boolean MainLoop = true;
static float CameraX = 0.0f, CameraY = 0.0f, CameraZ = -1.5f;
static float MovementFactor = 0.5f;
static Vector3f[] vertices = new Vector3f[]
{
// Z+
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f),
// Z-
new Vector3f(-0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
// Y+
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, -0.5f),
// Y-
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
// X+
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
// X-
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, -0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
};
static Vector2f[] textureCoordinates = new Vector2f[]
{
// Z+
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
// Z-
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
// Y+
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
// Y-
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
// X+
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
// X-
new Vector2f(0.0f, 1.0f),
new Vector2f(1.0f, 1.0f),
new Vector2f(1.0f, 0.0f),
new Vector2f(0.0f, 0.0f),
};
static int[] indices = new int[]
{
0, 3, 1,
1, 3, 2,
4, 5, 7,
5, 6, 7,
8, 11, 9, // Y+ 0, 3, 1,
9, 11, 10, // 1, 3, 2,
12, 13, 15, // Y- 0, 1, 3,
13, 14, 15, // 1, 2, 3,
16, 19, 17, // X+ 0, 3, 1,
17, 19, 18, // 1, 3, 2,
20, 21, 23, // X- 0, 1, 3,
21, 22, 23 // 1, 2, 3,
};
static Texture texture;
static Texture defaultTexture = new Texture(new Vector3i[]
{
new Vector3i(240, 0, 16),
new Vector3i(0, 216, 32),
new Vector3i(26, 0, 240),
new Vector3i(255, 255, 0),
}, 2);
public static Vector3f ndcToFbSize(Vector3f input, int fbw, int fbh)
{
return ndcToFbSize(input.X, input.Y, input.Z, fbw, fbh);
}
public static Vector3f ndcToFbSize(float x, float y, float z, int fbw, int fbh)
{
float retX = ((x + 1.0f) / 2) * fbw;
float retY = ((y + 1.0f) / 2) * fbh;
return new Vector3f(retX, retY, z);
}
public static void setConsoleGrayscale256Color(float grayscale)
{
if (Float.isInfinite(grayscale))
{
System.out.print("\u001b[31m");
}
else if (grayscale < 0.0f)
{
System.out.print("\u001b[30m");
}
else if (grayscale >= 0.9f)
{
System.out.print("\u001b[32m");
}
else
{
System.out.print("\u001b[38;5;" + (int) (232 + (grayscale * 16.0f)) + "m");
}
}
public static String getAnsiCodeForFgRgb24(Vector3i rgb)
{
return getAnsiCodeForFgRgb24(rgb.X, rgb.Y, rgb.Z);
}
public static String getAnsiCodeForFgRgb24(int r, int g, int b)
{
return "\u001b[38;2;" + Math.min(r, 255) + ";" + Math.min(g, 255) + ";" + Math.min(b, 255) + "m";
}
public static void setConsoleCursorPosition(int x, int y)
{
System.out.print("\u001b[" + (y + 1) + ";" + (x + 1) + "H");
}
public static void processUserInput() throws IOException
{
// This polls all pending characters from `stdin` each frame.
if (IsWindows ? true : System.in.available() > 0)
{
char input = (char) System.in.read();
switch (input)
{
case 'q':
MainLoop = false;
break;
case 'w':
CameraZ += MovementFactor;
break;
case 'W':
CameraZ += MovementFactor * 5;
break;
case 's':
CameraZ -= MovementFactor;
break;
case 'S':
CameraZ -= MovementFactor * 5;
break;
case 'a':
CameraX -= MovementFactor;
break;
case 'd':
CameraX += MovementFactor;
break;
case 'z':
CameraY -= MovementFactor;
break;
case 'x':
CameraY += MovementFactor;
break;
}
}
}
public static boolean IsWindows = System.getProperty("os.name").startsWith("Windows");
public static void main(String[] args) throws InterruptedException, IOException, Exception
{
// Read the arguments from the command line.
for (int i = 0; i < args.length; i++)
{
String[] argkv = args[i].split("=", 2);
switch (argkv[0])
{
case "-T":
case "--texture":
textureFile = argkv[1];
break;
case "--show-lines":
showLines = true;
break;
case "--show-vertices":
showVertices = true;
break;
case "--color-mode":
consoleRenderMode = Enum.valueOf(ConsoleRenderMode.class, argkv[1]);
break;
}
}
// Enter non-canonical (raw) mode.
if (IsWindows)
{
// Non-canonical mode is a console mode that requires Win32 API calls. Pure Java doesn't support native calls like this. But C# does. And with the help of Powershell, C# code can actually be executed inline.
// These flags are the ones that need to be disabled.
int consoleModeFlags = WindowsInterop.KERNEL32_ENABLE_LINE_INPUT |
WindowsInterop.KERNEL32_ENABLE_PROCESSED_INPUT |
WindowsInterop.KERNEL32_ENABLE_ECHO_INPUT;
// Run Powershell with a predefined script that can change the terminal to non-canonical mode.
WindowsInterop.runPowershellScript(WindowsInterop.getStdinModeChangePowershellScript(consoleModeFlags, false));
}
else
{
String[] nonCanonicalModeCmdUnix = { "/bin/sh", "-c", "stty raw < /dev/tty" };
Runtime.getRuntime().exec(nonCanonicalModeCmdUnix).waitFor();
}
// Get the terminal buffer size.
Vector2i terminalSize = new Vector2i(80, 25);
if (IsWindows)
{
// This is the worst way of retrieving the data, but that's just how Windows works.
// There's no way of retrieving the terminal size without calling native Windows API's, which would require adding dependencies like JNI to this project, which is exactly what this project tries to avoid.
// The terminal size will be retrieved by Powershell instead. This time, no native API calling is required.
String[] sizeString = WindowsInterop.runPowershellScript("$Host.Ui.RawUi.WindowSize.ToString()").split(",");
int width = Integer.parseInt(sizeString[0]);
int height = Integer.parseInt(sizeString[1]);
terminalSize = new Vector2i(width, height);
}
else
{
// Use ANSI escape codes to retrieve the terminal buffer size.
System.out.print(
"\u001b[s" + // Save the cursor position in an internal register.
"\u001b[1024;1024H" + // Move the cursor to a ridiculously high number of columns and rows.
"\u001b[6n" + // Print the current *actual* position of the cursor to `stdin`.
"\u001b[u" // Restore the cursor position that was saved earlier from that internal register.
);
// It's not guaranteed that the data we want will be immediately available since the terminal seems to return the data asynchronously.
// The program basically needs to wait for the data to be available on `stdin`.
while (System.in.available() < 6)
{}
// Get the ANSI escape code from `stdin`, containing the total number of rows and columns (in that order).
String terminalSizeRawAnsi = "";
while (System.in.available() > 0)
{
char input = (char) System.in.read();
terminalSizeRawAnsi += input;
if (input == 'R') // 'R' marks the end of this specific ANSI escape code.
{
System.err.println(terminalSizeRawAnsi);
break;
}
}
// Parse the ANSI escape code.
String[] terminalSizeRawTup = terminalSizeRawAnsi.substring(2, terminalSizeRawAnsi.length() - 1).split("\\;");
// Create a `Vector2i` that contains the number of columns and rows (in that order).
terminalSize = new Vector2i(Integer.parseInt(terminalSizeRawTup[1]), Integer.parseInt(terminalSizeRawTup[0]));
}
System.out.printf("Resolution is %s×%s%n", terminalSize.X, terminalSize.Y);