forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cs
2474 lines (2312 loc) · 113 KB
/
Matrix.cs
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
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Represents the right-handed 4x4 floating point matrix, which can store translation, scale and rotation information.
/// </summary>
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Matrix : IEquatable<Matrix>
{
#region Public Constructors
/// <summary>
/// Constructs a matrix.
/// </summary>
/// <param name="m11">A first row and first column value.</param>
/// <param name="m12">A first row and second column value.</param>
/// <param name="m13">A first row and third column value.</param>
/// <param name="m14">A first row and fourth column value.</param>
/// <param name="m21">A second row and first column value.</param>
/// <param name="m22">A second row and second column value.</param>
/// <param name="m23">A second row and third column value.</param>
/// <param name="m24">A second row and fourth column value.</param>
/// <param name="m31">A third row and first column value.</param>
/// <param name="m32">A third row and second column value.</param>
/// <param name="m33">A third row and third column value.</param>
/// <param name="m34">A third row and fourth column value.</param>
/// <param name="m41">A fourth row and first column value.</param>
/// <param name="m42">A fourth row and second column value.</param>
/// <param name="m43">A fourth row and third column value.</param>
/// <param name="m44">A fourth row and fourth column value.</param>
public Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31,
float m32, float m33, float m34, float m41, float m42, float m43, float m44)
{
this.M11 = m11;
this.M12 = m12;
this.M13 = m13;
this.M14 = m14;
this.M21 = m21;
this.M22 = m22;
this.M23 = m23;
this.M24 = m24;
this.M31 = m31;
this.M32 = m32;
this.M33 = m33;
this.M34 = m34;
this.M41 = m41;
this.M42 = m42;
this.M43 = m43;
this.M44 = m44;
}
/// <summary>
/// Constructs a matrix.
/// </summary>
/// <param name="row1">A first row of the created matrix.</param>
/// <param name="row2">A second row of the created matrix.</param>
/// <param name="row3">A third row of the created matrix.</param>
/// <param name="row4">A fourth row of the created matrix.</param>
public Matrix(Vector4 row1, Vector4 row2, Vector4 row3, Vector4 row4)
{
this.M11 = row1.X;
this.M12 = row1.Y;
this.M13 = row1.Z;
this.M14 = row1.W;
this.M21 = row2.X;
this.M22 = row2.Y;
this.M23 = row2.Z;
this.M24 = row2.W;
this.M31 = row3.X;
this.M32 = row3.Y;
this.M33 = row3.Z;
this.M34 = row3.W;
this.M41 = row4.X;
this.M42 = row4.Y;
this.M43 = row4.Z;
this.M44 = row4.W;
}
#endregion
#region Public Fields
/// <summary>
/// A first row and first column value.
/// </summary>
[DataMember]
public float M11;
/// <summary>
/// A first row and second column value.
/// </summary>
[DataMember]
public float M12;
/// <summary>
/// A first row and third column value.
/// </summary>
[DataMember]
public float M13;
/// <summary>
/// A first row and fourth column value.
/// </summary>
[DataMember]
public float M14;
/// <summary>
/// A second row and first column value.
/// </summary>
[DataMember]
public float M21;
/// <summary>
/// A second row and second column value.
/// </summary>
[DataMember]
public float M22;
/// <summary>
/// A second row and third column value.
/// </summary>
[DataMember]
public float M23;
/// <summary>
/// A second row and fourth column value.
/// </summary>
[DataMember]
public float M24;
/// <summary>
/// A third row and first column value.
/// </summary>
[DataMember]
public float M31;
/// <summary>
/// A third row and second column value.
/// </summary>
[DataMember]
public float M32;
/// <summary>
/// A third row and third column value.
/// </summary>
[DataMember]
public float M33;
/// <summary>
/// A third row and fourth column value.
/// </summary>
[DataMember]
public float M34;
/// <summary>
/// A fourth row and first column value.
/// </summary>
[DataMember]
public float M41;
/// <summary>
/// A fourth row and second column value.
/// </summary>
[DataMember]
public float M42;
/// <summary>
/// A fourth row and third column value.
/// </summary>
[DataMember]
public float M43;
/// <summary>
/// A fourth row and fourth column value.
/// </summary>
[DataMember]
public float M44;
#endregion
#region Indexers
public float this[int index]
{
get
{
switch (index)
{
case 0: return M11;
case 1: return M12;
case 2: return M13;
case 3: return M14;
case 4: return M21;
case 5: return M22;
case 6: return M23;
case 7: return M24;
case 8: return M31;
case 9: return M32;
case 10: return M33;
case 11: return M34;
case 12: return M41;
case 13: return M42;
case 14: return M43;
case 15: return M44;
}
throw new ArgumentOutOfRangeException();
}
set
{
switch (index)
{
case 0: M11 = value; break;
case 1: M12 = value; break;
case 2: M13 = value; break;
case 3: M14 = value; break;
case 4: M21 = value; break;
case 5: M22 = value; break;
case 6: M23 = value; break;
case 7: M24 = value; break;
case 8: M31 = value; break;
case 9: M32 = value; break;
case 10: M33 = value; break;
case 11: M34 = value; break;
case 12: M41 = value; break;
case 13: M42 = value; break;
case 14: M43 = value; break;
case 15: M44 = value; break;
default: throw new ArgumentOutOfRangeException();
}
}
}
public float this[int row, int column]
{
get
{
return this[(row * 4) + column];
}
set
{
this[(row * 4) + column] = value;
}
}
#endregion
#region Private Members
private static Matrix identity = new Matrix(1f, 0f, 0f, 0f,
0f, 1f, 0f, 0f,
0f, 0f, 1f, 0f,
0f, 0f, 0f, 1f);
#endregion
#region Public Properties
/// <summary>
/// The backward vector formed from the third row M31, M32, M33 elements.
/// </summary>
public Vector3 Backward
{
get
{
return new Vector3(this.M31, this.M32, this.M33);
}
set
{
this.M31 = value.X;
this.M32 = value.Y;
this.M33 = value.Z;
}
}
/// <summary>
/// The down vector formed from the second row -M21, -M22, -M23 elements.
/// </summary>
public Vector3 Down
{
get
{
return new Vector3(-this.M21, -this.M22, -this.M23);
}
set
{
this.M21 = -value.X;
this.M22 = -value.Y;
this.M23 = -value.Z;
}
}
/// <summary>
/// The forward vector formed from the third row -M31, -M32, -M33 elements.
/// </summary>
public Vector3 Forward
{
get
{
return new Vector3(-this.M31, -this.M32, -this.M33);
}
set
{
this.M31 = -value.X;
this.M32 = -value.Y;
this.M33 = -value.Z;
}
}
/// <summary>
/// Returns the identity matrix.
/// </summary>
public static Matrix Identity
{
get { return identity; }
}
/// <summary>
/// The left vector formed from the first row -M11, -M12, -M13 elements.
/// </summary>
public Vector3 Left
{
get
{
return new Vector3(-this.M11, -this.M12, -this.M13);
}
set
{
this.M11 = -value.X;
this.M12 = -value.Y;
this.M13 = -value.Z;
}
}
/// <summary>
/// The right vector formed from the first row M11, M12, M13 elements.
/// </summary>
public Vector3 Right
{
get
{
return new Vector3(this.M11, this.M12, this.M13);
}
set
{
this.M11 = value.X;
this.M12 = value.Y;
this.M13 = value.Z;
}
}
/// <summary>
/// Position stored in this matrix.
/// </summary>
public Vector3 Translation
{
get
{
return new Vector3(this.M41, this.M42, this.M43);
}
set
{
this.M41 = value.X;
this.M42 = value.Y;
this.M43 = value.Z;
}
}
/// <summary>
/// The upper vector formed from the second row M21, M22, M23 elements.
/// </summary>
public Vector3 Up
{
get
{
return new Vector3(this.M21, this.M22, this.M23);
}
set
{
this.M21 = value.X;
this.M22 = value.Y;
this.M23 = value.Z;
}
}
#endregion
#region Public Methods
/// <summary>
/// Creates a new <see cref="Matrix"/> which contains sum of two matrixes.
/// </summary>
/// <param name="matrix1">The first matrix to add.</param>
/// <param name="matrix2">The second matrix to add.</param>
/// <returns>The result of the matrix addition.</returns>
public static Matrix Add(Matrix matrix1, Matrix matrix2)
{
matrix1.M11 += matrix2.M11;
matrix1.M12 += matrix2.M12;
matrix1.M13 += matrix2.M13;
matrix1.M14 += matrix2.M14;
matrix1.M21 += matrix2.M21;
matrix1.M22 += matrix2.M22;
matrix1.M23 += matrix2.M23;
matrix1.M24 += matrix2.M24;
matrix1.M31 += matrix2.M31;
matrix1.M32 += matrix2.M32;
matrix1.M33 += matrix2.M33;
matrix1.M34 += matrix2.M34;
matrix1.M41 += matrix2.M41;
matrix1.M42 += matrix2.M42;
matrix1.M43 += matrix2.M43;
matrix1.M44 += matrix2.M44;
return matrix1;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> which contains sum of two matrixes.
/// </summary>
/// <param name="matrix1">The first matrix to add.</param>
/// <param name="matrix2">The second matrix to add.</param>
/// <param name="result">The result of the matrix addition as an output parameter.</param>
public static void Add(ref Matrix matrix1, ref Matrix matrix2, out Matrix result)
{
result.M11 = matrix1.M11 + matrix2.M11;
result.M12 = matrix1.M12 + matrix2.M12;
result.M13 = matrix1.M13 + matrix2.M13;
result.M14 = matrix1.M14 + matrix2.M14;
result.M21 = matrix1.M21 + matrix2.M21;
result.M22 = matrix1.M22 + matrix2.M22;
result.M23 = matrix1.M23 + matrix2.M23;
result.M24 = matrix1.M24 + matrix2.M24;
result.M31 = matrix1.M31 + matrix2.M31;
result.M32 = matrix1.M32 + matrix2.M32;
result.M33 = matrix1.M33 + matrix2.M33;
result.M34 = matrix1.M34 + matrix2.M34;
result.M41 = matrix1.M41 + matrix2.M41;
result.M42 = matrix1.M42 + matrix2.M42;
result.M43 = matrix1.M43 + matrix2.M43;
result.M44 = matrix1.M44 + matrix2.M44;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> for spherical billboarding that rotates around specified object position.
/// </summary>
/// <param name="objectPosition">Position of billboard object. It will rotate around that vector.</param>
/// <param name="cameraPosition">The camera position.</param>
/// <param name="cameraUpVector">The camera up vector.</param>
/// <param name="cameraForwardVector">Optional camera forward vector.</param>
/// <returns>The <see cref="Matrix"/> for spherical billboarding.</returns>
public static Matrix CreateBillboard(Vector3 objectPosition, Vector3 cameraPosition,
Vector3 cameraUpVector, Nullable<Vector3> cameraForwardVector)
{
Matrix result;
// Delegate to the other overload of the function to do the work
CreateBillboard(ref objectPosition, ref cameraPosition, ref cameraUpVector, cameraForwardVector, out result);
return result;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> for spherical billboarding that rotates around specified object position.
/// </summary>
/// <param name="objectPosition">Position of billboard object. It will rotate around that vector.</param>
/// <param name="cameraPosition">The camera position.</param>
/// <param name="cameraUpVector">The camera up vector.</param>
/// <param name="cameraForwardVector">Optional camera forward vector.</param>
/// <param name="result">The <see cref="Matrix"/> for spherical billboarding as an output parameter.</param>
public static void CreateBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
ref Vector3 cameraUpVector, Vector3? cameraForwardVector, out Matrix result)
{
Vector3 vector;
Vector3 vector2;
Vector3 vector3;
vector.X = objectPosition.X - cameraPosition.X;
vector.Y = objectPosition.Y - cameraPosition.Y;
vector.Z = objectPosition.Z - cameraPosition.Z;
float num = vector.LengthSquared();
if (num < 0.0001f)
{
vector = cameraForwardVector.HasValue ? -cameraForwardVector.Value : Vector3.Forward;
}
else
{
Vector3.Multiply(ref vector, (float)(1f / ((float)Math.Sqrt((double)num))), out vector);
}
Vector3.Cross(ref cameraUpVector, ref vector, out vector3);
vector3.Normalize();
Vector3.Cross(ref vector, ref vector3, out vector2);
result.M11 = vector3.X;
result.M12 = vector3.Y;
result.M13 = vector3.Z;
result.M14 = 0;
result.M21 = vector2.X;
result.M22 = vector2.Y;
result.M23 = vector2.Z;
result.M24 = 0;
result.M31 = vector.X;
result.M32 = vector.Y;
result.M33 = vector.Z;
result.M34 = 0;
result.M41 = objectPosition.X;
result.M42 = objectPosition.Y;
result.M43 = objectPosition.Z;
result.M44 = 1;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> for cylindrical billboarding that rotates around specified axis.
/// </summary>
/// <param name="objectPosition">Object position the billboard will rotate around.</param>
/// <param name="cameraPosition">Camera position.</param>
/// <param name="rotateAxis">Axis of billboard for rotation.</param>
/// <param name="cameraForwardVector">Optional camera forward vector.</param>
/// <param name="objectForwardVector">Optional object forward vector.</param>
/// <returns>The <see cref="Matrix"/> for cylindrical billboarding.</returns>
public static Matrix CreateConstrainedBillboard(Vector3 objectPosition, Vector3 cameraPosition,
Vector3 rotateAxis, Nullable<Vector3> cameraForwardVector, Nullable<Vector3> objectForwardVector)
{
Matrix result;
CreateConstrainedBillboard(ref objectPosition, ref cameraPosition, ref rotateAxis,
cameraForwardVector, objectForwardVector, out result);
return result;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> for cylindrical billboarding that rotates around specified axis.
/// </summary>
/// <param name="objectPosition">Object position the billboard will rotate around.</param>
/// <param name="cameraPosition">Camera position.</param>
/// <param name="rotateAxis">Axis of billboard for rotation.</param>
/// <param name="cameraForwardVector">Optional camera forward vector.</param>
/// <param name="objectForwardVector">Optional object forward vector.</param>
/// <param name="result">The <see cref="Matrix"/> for cylindrical billboarding as an output parameter.</param>
public static void CreateConstrainedBillboard(ref Vector3 objectPosition, ref Vector3 cameraPosition,
ref Vector3 rotateAxis, Vector3? cameraForwardVector, Vector3? objectForwardVector, out Matrix result)
{
float num;
Vector3 vector;
Vector3 vector2;
Vector3 vector3;
vector2.X = objectPosition.X - cameraPosition.X;
vector2.Y = objectPosition.Y - cameraPosition.Y;
vector2.Z = objectPosition.Z - cameraPosition.Z;
float num2 = vector2.LengthSquared();
if (num2 < 0.0001f)
{
vector2 = cameraForwardVector.HasValue ? -cameraForwardVector.Value : Vector3.Forward;
}
else
{
Vector3.Multiply(ref vector2, (float) (1f / ((float) Math.Sqrt((double) num2))), out vector2);
}
Vector3 vector4 = rotateAxis;
Vector3.Dot(ref rotateAxis, ref vector2, out num);
if (Math.Abs(num) > 0.9982547f)
{
if (objectForwardVector.HasValue)
{
vector = objectForwardVector.Value;
Vector3.Dot(ref rotateAxis, ref vector, out num);
if (Math.Abs(num) > 0.9982547f)
{
num = ((rotateAxis.X * Vector3.Forward.X) + (rotateAxis.Y * Vector3.Forward.Y)) + (rotateAxis.Z * Vector3.Forward.Z);
vector = (Math.Abs(num) > 0.9982547f) ? Vector3.Right : Vector3.Forward;
}
}
else
{
num = ((rotateAxis.X * Vector3.Forward.X) + (rotateAxis.Y * Vector3.Forward.Y)) + (rotateAxis.Z * Vector3.Forward.Z);
vector = (Math.Abs(num) > 0.9982547f) ? Vector3.Right : Vector3.Forward;
}
Vector3.Cross(ref rotateAxis, ref vector, out vector3);
vector3.Normalize();
Vector3.Cross(ref vector3, ref rotateAxis, out vector);
vector.Normalize();
}
else
{
Vector3.Cross(ref rotateAxis, ref vector2, out vector3);
vector3.Normalize();
Vector3.Cross(ref vector3, ref vector4, out vector);
vector.Normalize();
}
result.M11 = vector3.X;
result.M12 = vector3.Y;
result.M13 = vector3.Z;
result.M14 = 0;
result.M21 = vector4.X;
result.M22 = vector4.Y;
result.M23 = vector4.Z;
result.M24 = 0;
result.M31 = vector.X;
result.M32 = vector.Y;
result.M33 = vector.Z;
result.M34 = 0;
result.M41 = objectPosition.X;
result.M42 = objectPosition.Y;
result.M43 = objectPosition.Z;
result.M44 = 1;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> which contains the rotation moment around specified axis.
/// </summary>
/// <param name="axis">The axis of rotation.</param>
/// <param name="angle">The angle of rotation in radians.</param>
/// <returns>The rotation <see cref="Matrix"/>.</returns>
public static Matrix CreateFromAxisAngle(Vector3 axis, float angle)
{
Matrix result;
CreateFromAxisAngle(ref axis, angle, out result);
return result;
}
/// <summary>
/// Creates a new <see cref="Matrix"/> which contains the rotation moment around specified axis.
/// </summary>
/// <param name="axis">The axis of rotation.</param>
/// <param name="angle">The angle of rotation in radians.</param>
/// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
public static void CreateFromAxisAngle(ref Vector3 axis, float angle, out Matrix result)
{
float x = axis.X;
float y = axis.Y;
float z = axis.Z;
float num2 = (float) Math.Sin((double) angle);
float num = (float) Math.Cos((double) angle);
float num11 = x * x;
float num10 = y * y;
float num9 = z * z;
float num8 = x * y;
float num7 = x * z;
float num6 = y * z;
result.M11 = num11 + (num * (1f - num11));
result.M12 = (num8 - (num * num8)) + (num2 * z);
result.M13 = (num7 - (num * num7)) - (num2 * y);
result.M14 = 0;
result.M21 = (num8 - (num * num8)) - (num2 * z);
result.M22 = num10 + (num * (1f - num10));
result.M23 = (num6 - (num * num6)) + (num2 * x);
result.M24 = 0;
result.M31 = (num7 - (num * num7)) + (num2 * y);
result.M32 = (num6 - (num * num6)) - (num2 * x);
result.M33 = num9 + (num * (1f - num9));
result.M34 = 0;
result.M41 = 0;
result.M42 = 0;
result.M43 = 0;
result.M44 = 1;
}
/// <summary>
/// Creates a new rotation <see cref="Matrix"/> from a <see cref="Quaternion"/>.
/// </summary>
/// <param name="quaternion"><see cref="Quaternion"/> of rotation moment.</param>
/// <returns>The rotation <see cref="Matrix"/>.</returns>
public static Matrix CreateFromQuaternion(Quaternion quaternion)
{
Matrix result;
CreateFromQuaternion(ref quaternion, out result);
return result;
}
/// <summary>
/// Creates a new rotation <see cref="Matrix"/> from a <see cref="Quaternion"/>.
/// </summary>
/// <param name="quaternion"><see cref="Quaternion"/> of rotation moment.</param>
/// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
public static void CreateFromQuaternion(ref Quaternion quaternion, out Matrix result)
{
float num9 = quaternion.X * quaternion.X;
float num8 = quaternion.Y * quaternion.Y;
float num7 = quaternion.Z * quaternion.Z;
float num6 = quaternion.X * quaternion.Y;
float num5 = quaternion.Z * quaternion.W;
float num4 = quaternion.Z * quaternion.X;
float num3 = quaternion.Y * quaternion.W;
float num2 = quaternion.Y * quaternion.Z;
float num = quaternion.X * quaternion.W;
result.M11 = 1f - (2f * (num8 + num7));
result.M12 = 2f * (num6 + num5);
result.M13 = 2f * (num4 - num3);
result.M14 = 0f;
result.M21 = 2f * (num6 - num5);
result.M22 = 1f - (2f * (num7 + num9));
result.M23 = 2f * (num2 + num);
result.M24 = 0f;
result.M31 = 2f * (num4 + num3);
result.M32 = 2f * (num2 - num);
result.M33 = 1f - (2f * (num8 + num9));
result.M34 = 0f;
result.M41 = 0f;
result.M42 = 0f;
result.M43 = 0f;
result.M44 = 1f;
}
/// <summary>
/// Creates a new rotation <see cref="Matrix"/> from the specified yaw, pitch and roll values.
/// </summary>
/// <param name="yaw">The yaw rotation value in radians.</param>
/// <param name="pitch">The pitch rotation value in radians.</param>
/// <param name="roll">The roll rotation value in radians.</param>
/// <returns>The rotation <see cref="Matrix"/>.</returns>
/// <remarks>For more information about yaw, pitch and roll visit http://en.wikipedia.org/wiki/Euler_angles.
/// </remarks>
public static Matrix CreateFromYawPitchRoll(float yaw, float pitch, float roll)
{
Matrix matrix;
CreateFromYawPitchRoll(yaw, pitch, roll, out matrix);
return matrix;
}
/// <summary>
/// Creates a new rotation <see cref="Matrix"/> from the specified yaw, pitch and roll values.
/// </summary>
/// <param name="yaw">The yaw rotation value in radians.</param>
/// <param name="pitch">The pitch rotation value in radians.</param>
/// <param name="roll">The roll rotation value in radians.</param>
/// <param name="result">The rotation <see cref="Matrix"/> as an output parameter.</param>
/// <remarks>For more information about yaw, pitch and roll visit http://en.wikipedia.org/wiki/Euler_angles.
/// </remarks>
public static void CreateFromYawPitchRoll(float yaw, float pitch, float roll, out Matrix result)
{
Quaternion quaternion;
Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll, out quaternion);
CreateFromQuaternion(ref quaternion, out result);
}
/// <summary>
/// Creates a new viewing <see cref="Matrix"/>.
/// </summary>
/// <param name="cameraPosition">Position of the camera.</param>
/// <param name="cameraTarget">Lookup vector of the camera.</param>
/// <param name="cameraUpVector">The direction of the upper edge of the camera.</param>
/// <returns>The viewing <see cref="Matrix"/>.</returns>
public static Matrix CreateLookAt(Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector)
{
Matrix matrix;
CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector, out matrix);
return matrix;
}
/// <summary>
/// Creates a new viewing <see cref="Matrix"/>.
/// </summary>
/// <param name="cameraPosition">Position of the camera.</param>
/// <param name="cameraTarget">Lookup vector of the camera.</param>
/// <param name="cameraUpVector">The direction of the upper edge of the camera.</param>
/// <param name="result">The viewing <see cref="Matrix"/> as an output parameter.</param>
public static void CreateLookAt(ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix result)
{
var vector = Vector3.Normalize(cameraPosition - cameraTarget);
var vector2 = Vector3.Normalize(Vector3.Cross(cameraUpVector, vector));
var vector3 = Vector3.Cross(vector, vector2);
result.M11 = vector2.X;
result.M12 = vector3.X;
result.M13 = vector.X;
result.M14 = 0f;
result.M21 = vector2.Y;
result.M22 = vector3.Y;
result.M23 = vector.Y;
result.M24 = 0f;
result.M31 = vector2.Z;
result.M32 = vector3.Z;
result.M33 = vector.Z;
result.M34 = 0f;
result.M41 = -Vector3.Dot(vector2, cameraPosition);
result.M42 = -Vector3.Dot(vector3, cameraPosition);
result.M43 = -Vector3.Dot(vector, cameraPosition);
result.M44 = 1f;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for orthographic view.
/// </summary>
/// <param name="width">Width of the viewing volume.</param>
/// <param name="height">Height of the viewing volume.</param>
/// <param name="zNearPlane">Depth of the near plane.</param>
/// <param name="zFarPlane">Depth of the far plane.</param>
/// <returns>The new projection <see cref="Matrix"/> for orthographic view.</returns>
public static Matrix CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane)
{
Matrix matrix;
CreateOrthographic(width, height, zNearPlane, zFarPlane, out matrix);
return matrix;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for orthographic view.
/// </summary>
/// <param name="width">Width of the viewing volume.</param>
/// <param name="height">Height of the viewing volume.</param>
/// <param name="zNearPlane">Depth of the near plane.</param>
/// <param name="zFarPlane">Depth of the far plane.</param>
/// <param name="result">The new projection <see cref="Matrix"/> for orthographic view as an output parameter.</param>
public static void CreateOrthographic(float width, float height, float zNearPlane, float zFarPlane, out Matrix result)
{
result.M11 = 2f / width;
result.M12 = result.M13 = result.M14 = 0f;
result.M22 = 2f / height;
result.M21 = result.M23 = result.M24 = 0f;
result.M33 = 1f / (zNearPlane - zFarPlane);
result.M31 = result.M32 = result.M34 = 0f;
result.M41 = result.M42 = 0f;
result.M43 = zNearPlane / (zNearPlane - zFarPlane);
result.M44 = 1f;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
/// </summary>
/// <param name="left">Lower x-value at the near plane.</param>
/// <param name="right">Upper x-value at the near plane.</param>
/// <param name="bottom">Lower y-coordinate at the near plane.</param>
/// <param name="top">Upper y-value at the near plane.</param>
/// <param name="zNearPlane">Depth of the near plane.</param>
/// <param name="zFarPlane">Depth of the far plane.</param>
/// <returns>The new projection <see cref="Matrix"/> for customized orthographic view.</returns>
public static Matrix CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane)
{
Matrix matrix;
CreateOrthographicOffCenter(left, right, bottom, top, zNearPlane, zFarPlane, out matrix);
return matrix;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
/// </summary>
/// <param name="viewingVolume">The viewing volume.</param>
/// <param name="zNearPlane">Depth of the near plane.</param>
/// <param name="zFarPlane">Depth of the far plane.</param>
/// <returns>The new projection <see cref="Matrix"/> for customized orthographic view.</returns>
public static Matrix CreateOrthographicOffCenter(Rectangle viewingVolume, float zNearPlane, float zFarPlane)
{
Matrix matrix;
CreateOrthographicOffCenter(viewingVolume.Left, viewingVolume.Right, viewingVolume.Bottom, viewingVolume.Top, zNearPlane, zFarPlane, out matrix);
return matrix;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for customized orthographic view.
/// </summary>
/// <param name="left">Lower x-value at the near plane.</param>
/// <param name="right">Upper x-value at the near plane.</param>
/// <param name="bottom">Lower y-coordinate at the near plane.</param>
/// <param name="top">Upper y-value at the near plane.</param>
/// <param name="zNearPlane">Depth of the near plane.</param>
/// <param name="zFarPlane">Depth of the far plane.</param>
/// <param name="result">The new projection <see cref="Matrix"/> for customized orthographic view as an output parameter.</param>
public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, out Matrix result)
{
result.M11 = (float)(2.0 / ((double)right - (double)left));
result.M12 = 0.0f;
result.M13 = 0.0f;
result.M14 = 0.0f;
result.M21 = 0.0f;
result.M22 = (float)(2.0 / ((double)top - (double)bottom));
result.M23 = 0.0f;
result.M24 = 0.0f;
result.M31 = 0.0f;
result.M32 = 0.0f;
result.M33 = (float)(1.0 / ((double)zNearPlane - (double)zFarPlane));
result.M34 = 0.0f;
result.M41 = (float)(((double)left + (double)right) / ((double)left - (double)right));
result.M42 = (float)(((double)top + (double)bottom) / ((double)bottom - (double)top));
result.M43 = (float)((double)zNearPlane / ((double)zNearPlane - (double)zFarPlane));
result.M44 = 1.0f;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for perspective view.
/// </summary>
/// <param name="width">Width of the viewing volume.</param>
/// <param name="height">Height of the viewing volume.</param>
/// <param name="nearPlaneDistance">Distance to the near plane.</param>
/// <param name="farPlaneDistance">Distance to the far plane.</param>
/// <returns>The new projection <see cref="Matrix"/> for perspective view.</returns>
public static Matrix CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance)
{
Matrix matrix;
CreatePerspective(width, height, nearPlaneDistance, farPlaneDistance, out matrix);
return matrix;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for perspective view.
/// </summary>
/// <param name="width">Width of the viewing volume.</param>
/// <param name="height">Height of the viewing volume.</param>
/// <param name="nearPlaneDistance">Distance to the near plane.</param>
/// <param name="farPlaneDistance">Distance to the far plane.</param>
/// <param name="result">The new projection <see cref="Matrix"/> for perspective view as an output parameter.</param>
public static void CreatePerspective(float width, float height, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
{
if (nearPlaneDistance <= 0f)
{
throw new ArgumentException("nearPlaneDistance <= 0");
}
if (farPlaneDistance <= 0f)
{
throw new ArgumentException("farPlaneDistance <= 0");
}
if (nearPlaneDistance >= farPlaneDistance)
{
throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
}
result.M11 = (2f * nearPlaneDistance) / width;
result.M12 = result.M13 = result.M14 = 0f;
result.M22 = (2f * nearPlaneDistance) / height;
result.M21 = result.M23 = result.M24 = 0f;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M31 = result.M32 = 0f;
result.M34 = -1f;
result.M41 = result.M42 = result.M44 = 0f;
result.M43 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for perspective view with field of view.
/// </summary>
/// <param name="fieldOfView">Field of view in the y direction in radians.</param>
/// <param name="aspectRatio">Width divided by height of the viewing volume.</param>
/// <param name="nearPlaneDistance">Distance to the near plane.</param>
/// <param name="farPlaneDistance">Distance to the far plane.</param>
/// <returns>The new projection <see cref="Matrix"/> for perspective view with FOV.</returns>
public static Matrix CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance)
{
Matrix result;
CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance, out result);
return result;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for perspective view with field of view.
/// </summary>
/// <param name="fieldOfView">Field of view in the y direction in radians.</param>
/// <param name="aspectRatio">Width divided by height of the viewing volume.</param>
/// <param name="nearPlaneDistance">Distance of the near plane.</param>
/// <param name="farPlaneDistance">Distance of the far plane.</param>
/// <param name="result">The new projection <see cref="Matrix"/> for perspective view with FOV as an output parameter.</param>
public static void CreatePerspectiveFieldOfView(float fieldOfView, float aspectRatio, float nearPlaneDistance, float farPlaneDistance, out Matrix result)
{
if ((fieldOfView <= 0f) || (fieldOfView >= 3.141593f))
{
throw new ArgumentException("fieldOfView <= 0 or >= PI");
}
if (nearPlaneDistance <= 0f)
{
throw new ArgumentException("nearPlaneDistance <= 0");
}
if (farPlaneDistance <= 0f)
{
throw new ArgumentException("farPlaneDistance <= 0");
}
if (nearPlaneDistance >= farPlaneDistance)
{
throw new ArgumentException("nearPlaneDistance >= farPlaneDistance");
}
float num = 1f / ((float) Math.Tan((double) (fieldOfView * 0.5f)));
float num9 = num / aspectRatio;
result.M11 = num9;
result.M12 = result.M13 = result.M14 = 0;
result.M22 = num;
result.M21 = result.M23 = result.M24 = 0;
result.M31 = result.M32 = 0f;
result.M33 = farPlaneDistance / (nearPlaneDistance - farPlaneDistance);
result.M34 = -1;
result.M41 = result.M42 = result.M44 = 0;
result.M43 = (nearPlaneDistance * farPlaneDistance) / (nearPlaneDistance - farPlaneDistance);
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for customized perspective view.
/// </summary>
/// <param name="left">Lower x-value at the near plane.</param>
/// <param name="right">Upper x-value at the near plane.</param>
/// <param name="bottom">Lower y-coordinate at the near plane.</param>
/// <param name="top">Upper y-value at the near plane.</param>
/// <param name="nearPlaneDistance">Distance to the near plane.</param>
/// <param name="farPlaneDistance">Distance to the far plane.</param>
/// <returns>The new <see cref="Matrix"/> for customized perspective view.</returns>
public static Matrix CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float nearPlaneDistance, float farPlaneDistance)
{
Matrix result;
CreatePerspectiveOffCenter(left, right, bottom, top, nearPlaneDistance, farPlaneDistance, out result);
return result;
}
/// <summary>
/// Creates a new projection <see cref="Matrix"/> for customized perspective view.