forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas3D.cs
More file actions
3710 lines (3161 loc) · 94.8 KB
/
Canvas3D.cs
File metadata and controls
3710 lines (3161 loc) · 94.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using System.Xml;
using SkiaSharp;
using Waher.Script.Abstraction.Elements;
using Waher.Script.Graphs;
namespace Waher.Script.Graphs3D
{
/// <summary>
/// 3D drawing area.
/// </summary>
public class Canvas3D : Graph
{
private readonly SortedDictionary<float, LinkedList<PolyRec>> transparentPolygons = new SortedDictionary<float, LinkedList<PolyRec>>(new BackToFront());
private Guid id = Guid.NewGuid();
private byte[] pixels;
private float[] zBuffer;
private float[] xBuf;
private float[] yBuf;
private float[] zBuf;
private Vector3[] normalBuf;
private SKColor[] colorBuf;
private SKColor backgroundColor;
private Vector3 viewerPosition;
private Matrix4x4 projectionTransformation;
private Matrix4x4 modelTransformation;
private Vector4 last = Vector4.Zero;
private int width;
private int height;
private int overSampling;
private int w;
private int h;
private int wm1;
private int hm1;
private int cx;
private int cy;
private float distance;
/// <summary>
/// 3D drawing area.
///
/// By default, the camera is looking along the z-axis, with no projection, and no scaling.
/// The center of the canvas is located at origo.
/// </summary>
public Canvas3D()
{
}
/// <summary>
/// 3D drawing area.
///
/// By default, the camera is looking along the z-axis, with no projection, and no scaling.
/// The center of the canvas is located at origo.
/// </summary>
/// <param name="Width">Width of area, in pixels.</param>
/// <param name="Height">Height of area, in pixels.</param>
/// <param name="OverSampling">Number of subpixels for each generated pixel.
/// Oversampling provides a means to achieve anti-aliasing in the rendered result.</param>
/// <param name="BackgroundColor">Background color</param>
public Canvas3D(int Width, int Height, int OverSampling, SKColor BackgroundColor)
{
if (Width <= 0)
throw new ArgumentOutOfRangeException("Width must be a positive integer.", nameof(Width));
if (Height <= 0)
throw new ArgumentOutOfRangeException("Height must be a positive integer.", nameof(Height));
if (OverSampling <= 0)
throw new ArgumentOutOfRangeException("Oversampling must be a positive integer.", nameof(OverSampling));
this.width = Width;
this.height = Height;
this.overSampling = OverSampling;
this.w = Width * OverSampling;
this.h = Height * OverSampling;
this.wm1 = this.w - 1;
this.hm1 = this.h - 1;
this.cx = this.w / 2;
this.cy = this.h / 2;
this.backgroundColor = BackgroundColor;
this.ResetTransforms();
int c = this.w * this.h;
this.pixels = new byte[c * 4];
this.zBuffer = new float[c];
this.xBuf = new float[this.w];
this.yBuf = new float[this.w];
this.zBuf = new float[this.w];
this.normalBuf = new Vector3[this.w];
this.colorBuf = new SKColor[this.w];
this.ClearPixels();
}
private void ClearPixels()
{
byte R = this.backgroundColor.Red;
byte G = this.backgroundColor.Green;
byte B = this.backgroundColor.Blue;
byte A = this.backgroundColor.Alpha;
int i, j, c = this.w * this.h;
for (i = j = 0; i < c; i++)
{
this.pixels[j++] = R;
this.pixels[j++] = G;
this.pixels[j++] = B;
this.pixels[j++] = A;
this.zBuffer[i] = float.MaxValue;
}
}
/// <summary>
/// Clears the canvas.
/// </summary>
public void Clear()
{
this.pixels.Initialize();
this.zBuffer.Initialize();
this.xBuf.Initialize();
this.yBuf.Initialize();
this.zBuf.Initialize();
this.normalBuf.Initialize();
this.colorBuf.Initialize();
this.ResetTransforms();
this.ClearPixels();
}
#region Colors
private static uint ToUInt(SKColor Color)
{
uint Result = Color.Alpha;
Result <<= 8;
Result |= Color.Blue;
Result <<= 8;
Result |= Color.Green;
Result <<= 8;
Result |= Color.Red;
return Result;
}
#endregion
#region Bitmaps
/// <summary>
/// Creates a bitmap from the pixels in the canvas.
/// </summary>
/// <returns></returns>
public SKImage GetBitmap()
{
this.PaintTransparentPolygons();
if (this.overSampling == 1)
return this.GetBitmap(this.pixels);
else
{
byte[] Pixels = new byte[this.width * this.height * 4];
int x, y, dx, dy, p0, p, q = 0;
int o2 = this.overSampling * this.overSampling;
int h = o2 >> 1;
uint SumR, SumG, SumB, SumA;
for (y = 0; y < this.height; y++)
{
for (x = 0; x < this.width; x++)
{
SumR = SumG = SumB = SumA = 0;
p0 = ((y * this.w) + x) * this.overSampling * 4;
for (dy = 0; dy < this.overSampling; dy++, p0 += this.w * 4)
{
for (dx = 0, p = p0; dx < this.overSampling; dx++)
{
SumR += this.pixels[p++];
SumG += this.pixels[p++];
SumB += this.pixels[p++];
SumA += this.pixels[p++];
}
}
Pixels[q++] = (byte)((SumR + h) / o2);
Pixels[q++] = (byte)((SumG + h) / o2);
Pixels[q++] = (byte)((SumB + h) / o2);
Pixels[q++] = (byte)((SumA + h) / o2);
}
}
return this.GetBitmap(Pixels);
}
}
private SKImage GetBitmap(byte[] Pixels)
{
using (SKData Data = SKData.CreateCopy(Pixels))
{
SKImageInfo ImageInfo = new SKImageInfo(this.width, this.height, SKColorType.Rgba8888, SKAlphaType.Premul);
return SKImage.FromPixels(ImageInfo, Data, this.width << 2);
}
}
#endregion
#region Graph interface
/// <summary>
/// Creates a bitmap of the graph.
/// </summary>
/// <param name="Settings">Graph settings.</param>
/// <param name="States">State objects that contain graph-specific information about its inner states.
/// These can be used in calls back to the graph object to make actions on the generated graph.</param>
/// <returns>Bitmap</returns>
public override SKImage CreateBitmap(GraphSettings Settings, out object[] States)
{
States = null;
return this.GetBitmap();
}
/// <summary>
/// Gets script corresponding to a point in a generated bitmap representation of the graph.
/// </summary>
/// <param name="X">X-Coordinate.</param>
/// <param name="Y">Y-Coordinate.</param>
/// <param name="States">State objects for the generated bitmap.</param>
/// <returns>Script.</returns>
public override string GetBitmapClickScript(double X, double Y, object[] States)
{
return string.Empty;
}
/// <summary>
/// The recommended bitmap size of the graph, if such is available, or null if not.
/// </summary>
public override Tuple<int, int> RecommendedBitmapSize
{
get { return new Tuple<int, int>(this.width, this.height); }
}
/// <summary>
/// Tries to add an element to the current element, from the left.
/// </summary>
/// <param name="Element">Element to add.</param>
/// <returns>Result, if understood, null otherwise.</returns>
public override ISemiGroupElement AddLeft(ISemiGroupElement Element)
{
return null;
}
/// <summary>
/// Tries to add an element to the current element, from the right.
/// </summary>
/// <param name="Element">Element to add.</param>
/// <returns>Result, if understood, null otherwise.</returns>
public override ISemiGroupElement AddRight(ISemiGroupElement Element)
{
return null;
}
/// <summary>
/// Compares the element to another.
/// </summary>
/// <param name="obj">Other element to compare against.</param>
/// <returns>If elements are equal.</returns>
public override bool Equals(object obj)
{
return obj is Canvas3D Canvas3D && this.id.Equals(Canvas3D.id);
}
/// <summary>
/// Calculates a hash code of the element.
/// </summary>
/// <returns>Hash code.</returns>
public override int GetHashCode()
{
return this.id.GetHashCode();
}
/// <summary>
/// Converts an object to a <see cref="PhongIntensity"/> object.
/// </summary>
/// <param name="Object">Object</param>
/// <returns>Phong intensity object.</returns>
public static PhongIntensity ToPhongIntensity(object Object)
{
if (Object is PhongIntensity PhongIntensity)
return PhongIntensity;
else
{
SKColor Color = ToColor(Object);
return new PhongIntensity(Color.Red, Color.Green, Color.Blue, Color.Alpha);
}
}
#endregion
#region Projection Transformations
/// <summary>
/// Resets any transforms.
/// </summary>
public void ResetTransforms()
{
this.distance = 0;
this.viewerPosition = new Vector3(0, 0, 0);
this.projectionTransformation = Matrix4x4.CreateTranslation(this.cx, this.cy, 0);
this.projectionTransformation = Matrix4x4.CreateScale(-this.overSampling, this.overSampling, 1) * this.projectionTransformation;
this.modelTransformation = Matrix4x4.Identity;
}
/// <summary>
/// Current projection transformation matrix.
/// </summary>
public Matrix4x4 ProjectionTransformation
{
get => this.projectionTransformation;
set => this.projectionTransformation = value;
}
/// <summary>
/// Applies a perspective projection.
/// </summary>
/// <param name="NearPlaneDistance">Distance between near projection plane and camera.</param>
/// <param name="FarPlaneDistance">Distance between far projection plane and camera.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Perspective(float NearPlaneDistance, float FarPlaneDistance)
{
if (NearPlaneDistance <= 0)
throw new ArgumentOutOfRangeException("Invalid camera distance.", nameof(NearPlaneDistance));
if (FarPlaneDistance <= NearPlaneDistance)
throw new ArgumentOutOfRangeException("Invalid camera distance.", nameof(FarPlaneDistance));
Matrix4x4 Prev = this.projectionTransformation;
this.projectionTransformation = Matrix4x4.CreatePerspective(1, 1, NearPlaneDistance, FarPlaneDistance) * this.projectionTransformation;
this.distance = NearPlaneDistance;
this.viewerPosition = new Vector3(0, 0, -this.distance);
return Prev;
}
/// <summary>
/// Viewer position
/// </summary>
public Vector3 ViewerPosition => this.viewerPosition;
/// <summary>
/// Transforms coordinates to screen coordinates.
/// </summary>
/// <param name="Point">Point.</param>
/// <returns>Transformed point.</returns>
public Vector3 Project(Vector4 Point)
{
Vector4 v = Vector4.Transform(Point, this.projectionTransformation);
float d = 1f / v.W;
return new Vector3(v.X * d, v.Y * d, v.Z * d);
}
/// <summary>
/// Transforms a world coordinate to a display coordinate.
/// </summary>
/// <param name="Point">Point.</param>
/// <returns>Transformed point.</returns>
public Vector3 Project(Vector3 Point)
{
return Vector3.Transform(Point, this.projectionTransformation);
}
#endregion
#region Model Transformations
/// <summary>
/// Current model transformation matrix.
/// </summary>
public Matrix4x4 ModelTransformation
{
get => this.modelTransformation;
set => this.modelTransformation = value;
}
/// <summary>
/// Transforms a world coordinate to a display coordinate.
/// </summary>
/// <param name="Point">Point.</param>
/// <returns>Transformed point.</returns>
public Vector4 ModelTransform(Vector4 Point)
{
return Vector4.Transform(Point, this.modelTransformation);
}
/// <summary>
/// Transforms a world coordinate to a display coordinate.
/// </summary>
/// <param name="Point">Point.</param>
/// <returns>Transformed point.</returns>
public Vector3 ModelTransform(Vector3 Point)
{
return Vector3.Transform(Point, this.modelTransformation);
}
/// <summary>
/// Rotates the world around the X-axis.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateX(float Degrees)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationX(Degrees * degToRad) * this.modelTransformation;
return Prev;
}
private const float degToRad = (float)(Math.PI / 180);
/// <summary>
/// Rotates the world around an axis parallel to the X-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateX(float Degrees, object CenterPoint)
{
return RotateX(Degrees, ToVector3(CenterPoint));
}
/// <summary>
/// Rotates the world around an axis parallel to the X-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateX(float Degrees, Vector3 CenterPoint)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationX(Degrees * degToRad, CenterPoint) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Rotates the world around the Y-axis.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateY(float Degrees)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationY(Degrees * degToRad) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Rotates the world around an axis parallel to the Y-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateY(float Degrees, object CenterPoint)
{
return RotateY(Degrees, ToVector3(CenterPoint));
}
/// <summary>
/// Rotates the world around an axis parallel to the Y-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateY(float Degrees, Vector3 CenterPoint)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationY(Degrees * degToRad, CenterPoint) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Rotates the world around the Z-axis.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateZ(float Degrees)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationZ(Degrees * degToRad) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Rotates the world around an axis parallel to the Z-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateZ(float Degrees, object CenterPoint)
{
return RotateZ(Degrees, ToVector3(CenterPoint));
}
/// <summary>
/// Rotates the world around an axis parallel to the Z-axis, going through
/// the center point <paramref name="CenterPoint"/>.
/// </summary>
/// <param name="Degrees">Degrees</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 RotateZ(float Degrees, Vector3 CenterPoint)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateRotationZ(Degrees * degToRad, CenterPoint) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="Scale">Scale</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float Scale)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateScale(Scale) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="Scale">Scale</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float Scale, object CenterPoint)
{
return this.Scale(Scale, ToVector3(CenterPoint));
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="Scale">Scale</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float Scale, Vector3 CenterPoint)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateScale(Scale, CenterPoint) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="ScaleX">Scale along X-axis.</param>
/// <param name="ScaleY">Scale along Y-axis.</param>
/// <param name="ScaleZ">Scale along Z-axis.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float ScaleX, float ScaleY, float ScaleZ)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateScale(ScaleX, ScaleY, ScaleZ) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="ScaleX">Scale along X-axis.</param>
/// <param name="ScaleY">Scale along Y-axis.</param>
/// <param name="ScaleZ">Scale along Z-axis.</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float ScaleX, float ScaleY, float ScaleZ, object CenterPoint)
{
return this.Scale(ScaleX, ScaleY, ScaleZ, ToVector3(CenterPoint));
}
/// <summary>
/// Scales the world
/// </summary>
/// <param name="ScaleX">Scale along X-axis.</param>
/// <param name="ScaleY">Scale along Y-axis.</param>
/// <param name="ScaleZ">Scale along Z-axis.</param>
/// <param name="CenterPoint">Center point.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Scale(float ScaleX, float ScaleY, float ScaleZ, Vector3 CenterPoint)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateScale(ScaleX, ScaleY, ScaleZ, CenterPoint) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Translates the world.
/// </summary>
/// <param name="Delta">Movement vector.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Translate(Vector3 Delta)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateTranslation(Delta) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Translates the world.
/// </summary>
/// <param name="DeltaX">Movement along the X-axis.</param>
/// <param name="DelayY">Movement along the Y-axis.</param>
/// <param name="DeltaZ">Movement along the Z-axis.</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 Translate(float DeltaX, float DelayY, float DeltaZ)
{
Matrix4x4 Prev = this.modelTransformation;
this.modelTransformation = Matrix4x4.CreateTranslation(DeltaX, DelayY, DeltaZ) * this.modelTransformation;
return Prev;
}
/// <summary>
/// Places the observer at the point (<paramref name="PositionX"/>, <paramref name="PositionY"/>, <paramref name="PositionZ"/>), looking at
/// the point (<paramref name="TargetX"/>, <paramref name="TargetY"/>, <paramref name="TargetZ"/>), with upwards pointing in the direction of
/// (<paramref name="UpX"/>, <paramref name="UpY"/>, <paramref name="UpZ"/>) (as a vector, from the position of the observer).
/// </summary>
/// <param name="PositionX">X-Coordinte of position point of the observer.</param>
/// <param name="PositionY">Y-Coordinte of position point of the observer.</param>
/// <param name="PositionZ">Z-Coordinte of position point of the observer.</param>
/// <param name="TargetX">X-Coordinate of point being observed.</param>
/// <param name="TargetY">Y-Coordinate of point being observed.</param>
/// <param name="TargetZ">Z-Coordinate of point being observed.</param>
/// <param name="UpX">X-Coordinate of vector pointing in the direction of up, from the
/// point of the observer (<paramref name="PositionX"/>).</param>
/// <param name="UpY">Y-Coordinate of vector pointing in the direction of up, from the
/// point of the observer (<paramref name="PositionY"/>).</param>
/// <param name="UpZ">Z-Coordinate of vector pointing in the direction of up, from the
/// point of the observer (<paramref name="PositionZ"/>).</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 LookAt(float PositionX, float PositionY, float PositionZ,
float TargetX, float TargetY, float TargetZ, float UpX, float UpY, float UpZ)
{
return this.LookAt(new Vector3(PositionX, PositionY, PositionZ),
new Vector3(TargetX, TargetY, TargetZ), new Vector3(UpX, UpY, UpZ));
}
/// <summary>
/// Places the observer at the point <paramref name="Position"/>, looking at
/// the point <paramref name="Target"/>, with upwards pointing in the direction of
/// <paramref name="Up"/> (as a vector, from the position of the observer).
/// </summary>
/// <param name="Position">Position point of the observer.</param>
/// <param name="Target">Point being observed.</param>
/// <param name="Up">Vector pointing in the direction of up, from the
/// point of the observer (<paramref name="Position"/>).</param>
/// <returns>Previous model transformation matrix.</returns>
public Matrix4x4 LookAt(Vector3 Position, Vector3 Target, Vector3 Up)
{
// Matrix4x4.CreateLookAt is strange. Perform own linear algebra, flipping axes:
Vector3 V = Vector3.Normalize(Target - Position); // Maps to Z, after transform
Vector3 U = Vector3.Normalize(Up - Vector3.Dot(V, Up) * V); // Maps to Y, after transform
Vector3 R = Vector3.Cross(V, U); // Maps to X, after transform
Matrix4x4 Prev = this.modelTransformation;
Matrix4x4 M = new Matrix4x4(
R.X, U.X, V.X, 0,
R.Y, U.Y, V.Y, 0,
R.Z, U.Z, V.Z, 0,
0, 0, 0, 1);
this.modelTransformation = M * this.modelTransformation;
this.Translate(-Position);
return Prev;
}
#endregion
#region Plot
/// <summary>
/// Plots a point on the 3D-canvas.
/// </summary>
/// <param name="Point">Point to plot.</param>
/// <param name="Color">Color.</param>
public void Plot(Vector4 Point, SKColor Color)
{
this.Plot(Point, ToUInt(Color));
}
/// <summary>
/// Plots a point on the 3D-canvas.
/// </summary>
/// <param name="Point">Point to plot.</param>
/// <param name="Color">Color.</param>
public void Plot(Vector4 Point, uint Color)
{
this.last = Point;
Vector4 WorldPoint = this.ModelTransform(Point);
Vector3 ScreenPoint = this.Project(WorldPoint);
if (ScreenPoint.Z >= 0)
this.Plot((int)(ScreenPoint.X + 0.5f), (int)(ScreenPoint.Y + 0.5f), WorldPoint.Z, Color);
}
private void Plot(int x, int y, float z, uint Color)
{
if (x >= 0 && x < this.w && y >= 0 && y < this.h)
{
int p = y * this.w + x;
if (z >= 0 && z < this.zBuffer[p])
{
this.zBuffer[p] = z;
p <<= 2;
byte A = (byte)(Color >> 24);
if (A == 255)
{
this.pixels[p++] = (byte)Color;
Color >>= 8;
this.pixels[p++] = (byte)Color;
Color >>= 8;
this.pixels[p++] = (byte)Color;
Color >>= 8;
this.pixels[p] = (byte)Color;
}
else
{
byte R = (byte)Color;
byte G = (byte)(Color >> 8);
byte B = (byte)(Color >> 16);
byte R2 = this.pixels[p++];
byte G2 = this.pixels[p++];
byte B2 = this.pixels[p++];
byte A2 = this.pixels[p];
byte R3, G3, B3, A3;
if (A2 == 255)
{
R3 = (byte)(((R * A + R2 * (255 - A)) + 128) / 255);
G3 = (byte)(((G * A + G2 * (255 - A)) + 128) / 255);
B3 = (byte)(((B * A + B2 * (255 - A)) + 128) / 255);
A3 = 255;
}
else
{
R2 = (byte)((R2 * A2 + 128) / 255);
G2 = (byte)((G2 * A2 + 128) / 255);
B2 = (byte)((B2 * A2 + 128) / 255);
R3 = (byte)(((R * A + R2 * (255 - A)) + 128) / 255);
G3 = (byte)(((G * A + G2 * (255 - A)) + 128) / 255);
B3 = (byte)(((B * A + B2 * (255 - A)) + 128) / 255);
A3 = (byte)(255 - (((255 - A) * (255 - A2) + 128) / 255));
}
this.pixels[p--] = A3;
this.pixels[p--] = B3;
this.pixels[p--] = G3;
this.pixels[p] = R3;
}
}
}
}
#endregion
#region Lines
private bool ClipLine(ref float x0, ref float y0, ref float z0,
ref float x1, ref float y1, ref float z1)
{
byte Mask0 = 0;
byte Mask1 = 0;
float Delta;
if (x0 < 0)
Mask0 |= 1;
else if (x0 > this.wm1)
Mask0 |= 2;
if (y0 < 0)
Mask0 |= 4;
else if (y0 > this.hm1)
Mask0 |= 8;
if (x1 < 0)
Mask1 |= 1;
else if (x1 > this.wm1)
Mask1 |= 2;
if (y1 < 0)
Mask1 |= 4;
else if (y1 > this.hm1)
Mask1 |= 8;
if (Mask0 == 0 && Mask1 == 0)
return true;
if ((Mask0 & Mask1) != 0)
return false;
// Left edge:
if ((Mask0 & 1) != 0)
{
Delta = x0 / (x1 - x0); // Divisor is non-zero, or masks would have common bit.
y0 -= (y1 - y0) * Delta;
z0 -= (z1 - z0) * Delta;
x0 = 0;
Mask0 &= 254;
if (y0 < 0)
Mask0 |= 4;
else if (y0 > this.hm1)
Mask0 |= 8;
if ((Mask0 & Mask1) != 0)
return false;
}
if ((Mask1 & 1) != 0)
{
Delta = x1 / (x0 - x1); // Divisor is non-zero, or masks would have common bit.
y1 -= (y0 - y1) * Delta;
z1 -= (z0 - z1) * Delta;
x1 = 0;
Mask1 &= 254;
if (y1 < 0)
Mask1 |= 4;
else if (y1 > this.hm1)
Mask1 |= 8;
if ((Mask0 & Mask1) != 0)
return false;
}
// Top edge:
if ((Mask0 & 4) != 0)
{
Delta = y0 / (y1 - y0); // Divisor is non-zero, or masks would have common bit.
x0 -= (x1 - x0) * Delta;
z0 -= (z1 - z0) * Delta;
y0 = 0;
Mask0 &= 251;
if (x0 < 0)
Mask0 |= 1;
else if (x0 > this.wm1)
Mask0 |= 2;
if ((Mask0 & Mask1) != 0)
return false;
}
if ((Mask1 & 4) != 0)
{
Delta = y1 / (y0 - y1); // Divisor is non-zero, or masks would have common bit.
x1 -= (x0 - x1) * Delta;
z1 -= (z0 - z1) * Delta;
y1 = 0;
Mask1 &= 251;
if (x1 < 0)
Mask1 |= 1;
else if (x1 > this.wm1)
Mask1 |= 2;
if ((Mask0 & Mask1) != 0)
return false;
}
// Right edge:
if ((Mask0 & 2) != 0)
{
Delta = (this.wm1 - x0) / (x1 - x0); // Divisor is non-zero, or masks would have common bit.
y0 += (y1 - y0) * Delta;
z0 += (z1 - z0) * Delta;
x0 = this.wm1;
Mask0 &= 253;
if (y0 < 0)
Mask0 |= 4;
else if (y0 > this.hm1)
Mask0 |= 8;
if ((Mask0 & Mask1) != 0)
return false;
}
if ((Mask1 & 2) != 0)
{
Delta = (this.wm1 - x1) / (x0 - x1); // Divisor is non-zero, or masks would have common bit.
y1 += (y0 - y1) * Delta;
z1 += (z0 - z1) * Delta;
x1 = this.wm1;
Mask1 &= 253;
if (y1 < 0)
Mask1 |= 4;
else if (y1 > this.hm1)
Mask1 |= 8;
if ((Mask0 & Mask1) != 0)
return false;
}
// Bottom edge:
if ((Mask0 & 8) != 0)
{
Delta = (this.hm1 - y0) / (y1 - y0); // Divisor is non-zero, or masks would have common bit.
x0 += (x1 - x0) * Delta;
z0 += (z1 - z0) * Delta;
y0 = this.hm1;
Mask0 &= 247;
if (x0 < 0)
Mask0 |= 1;
else if (x0 > this.wm1)
Mask0 |= 2;
if ((Mask0 & Mask1) != 0)
return false;
}
if ((Mask1 & 8) != 0)
{
Delta = (this.hm1 - y1) / (y0 - y1); // Divisor is non-zero, or masks would have common bit.
x1 += (x0 - x1) * Delta;
z1 += (z0 - z1) * Delta;
y1 = this.hm1;
Mask1 &= 247;
if (x1 < 0)
Mask1 |= 1;
else if (x1 > this.wm1)
Mask1 |= 2;
if ((Mask0 & Mask1) != 0)
return false;
}
return ((Mask0 | Mask1) == 0);
}
private bool ClipLine(ref float x0, ref float y0,
ref float rx0, ref float ry0, ref float rz0,
ref float x1, ref float y1,
ref float rx1, ref float ry1, ref float rz1)
{
byte Mask0 = 0;
byte Mask1 = 0;
float Delta;
if (x0 < 0)
Mask0 |= 1;
else if (x0 > this.wm1)
Mask0 |= 2;
if (y0 < 0)
Mask0 |= 4;
else if (y0 > this.hm1)
Mask0 |= 8;
if (x1 < 0)
Mask1 |= 1;
else if (x1 > this.wm1)
Mask1 |= 2;
if (y1 < 0)
Mask1 |= 4;
else if (y1 > this.hm1)
Mask1 |= 8;
if (Mask0 == 0 && Mask1 == 0)
return true;
if ((Mask0 & Mask1) != 0)
return false;
// Left edge:
if ((Mask0 & 1) != 0)
{
Delta = x0 / (x1 - x0); // Divisor is non-zero, or masks would have common bit.
y0 -= (y1 - y0) * Delta;