/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Matrix44.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/Matrix44.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 83 - (show annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (9 years, 11 months ago) by william
File size: 9833 byte(s)
bulk commit --
** Enable Support for Skeletons & Bones

** A bug has found its way into the code
 -get an unhandled EHAccessViolation
 - I think the problem is in the Textures Class (or one of the sub-classes)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace gr2lib.core.coretypes.implementation
8 {
9 [StructLayout(LayoutKind.Sequential)]
10 public struct Matrix44
11 {
12 public Vector4 right;
13 public Vector4 up;
14 public Vector4 at;
15 public Vector4 pos;
16 public static readonly Matrix44 Invalid = new Matrix44(Vector4.Invalid, Vector4.Invalid, Vector4.Invalid, Vector4.Invalid);
17 public static readonly Matrix44 Identity = new Matrix44(new Vector4(1f, 0f, 0f, 0f), new Vector4(0f, 1f, 0f, 0f), new Vector4(0f, 0f, 1f, 0f), new Vector4(0f, 0f, 0f, 1f));
18
19
20 public Matrix44(Vector4 r, Vector4 u, Vector4 a, Vector4 p)
21 {
22 this.right = r;
23 this.up = u;
24 this.at = a;
25 this.pos = p;
26 }
27
28 internal Matrix44(native.Matrix44 m)
29 {
30 this.right = new Vector4(m.Right.X, m.Right.Y, m.Right.Z, m.Right.W);
31 this.up = new Vector4(m.Up.X, m.Up.Y, m.Up.Z, m.Up.W);
32 this.at = new Vector4(m.Back.X, m.Back.Y, m.Back.Z, m.Back.W);
33 this.pos = new Vector4(m.Pos.X, m.Pos.Y, m.Pos.Z, m.Pos.W);
34 }
35
36 public Matrix44(Matrix33 m)
37 {
38 this.right = new Vector4(m.right.x, m.right.y, m.right.z, 0f);
39 this.up = new Vector4(m.up.x, m.up.y, m.up.z, 0f);
40 this.at = new Vector4(m.at.x, m.at.y, m.at.z, 0f);
41 this.pos = new Vector4(0f, 0f, 0f, 1f);
42 }
43
44 public void SetEulerRotations(float rx, float ry, float rz)
45 {
46 float sX = (float)Math.Sin((double)rx);
47 float cX = (float)Math.Cos((double)rx);
48 float sY = (float)Math.Sin((double)ry);
49 float cY = (float)Math.Cos((double)ry);
50 float sZ = (float)Math.Sin((double)rz);
51 float cZ = (float)Math.Cos((double)rz);
52 this.right.Set((cY * cZ), -(cY * sZ * cX) + (sY * sX), (cY * sZ * sX) + (sY * cX), 0f);
53 this.up.Set(sZ, (cZ * cX), -(cZ * sX), 0f);
54 this.at.Set(-(sY * cZ), (sY * sZ * cX) + (cY * sX), -(sY * sZ * sX) + (cY * cX), 0f);
55 }
56
57 public void SetIdentity()
58 {
59 this.right.Set(1f, 0f, 0f, 0f);
60 this.up.Set(0f, 1f, 0f, 0f);
61 this.at.Set(0f, 0f, 1f, 0f);
62 this.pos.Set(0f, 0f, 0f, 1f);
63 }
64
65 public void SetPerspective(float w, float h, float zn, float zf)
66 {
67 this.right.Set((2f * zn) / w, 0f, 0f, 0f);
68 this.up.Set(0f, (2f * zn) / h, 0f, 0f);
69 this.at.Set(0f, 0f, zf / (zn - zf), -1f);
70 this.pos.Set(0f, 0f, (zn * zf) / (zn - zf), 0f);
71 }
72
73 public void SetRotation(Quaternion q)
74 {
75 float len = q.Magnitude;
76 q = q / len;
77 this.right.Set(1 - 2 * (q.v.y * q.v.y + q.v.z * q.v.z), 2 * (q.v.x * q.v.y - q.v.z * q.n), 2 * (q.v.x * q.v.z + q.v.y * q.n), 0f);
78 this.up.Set(2 * (q.v.x * q.v.y + q.v.z * q.n), 1 - 2 * (q.v.x * q.v.x + q.v.z * q.v.z), 2 * (q.v.y * q.v.z - q.v.x * q.n), 0f);
79 this.at.Set(2 * (q.v.x * q.v.z - q.v.y * q.n), 2 * (q.v.y * q.v.z + q.v.x * q.n), 1 - 2 * (q.v.x * q.v.x + q.v.y * q.v.y), 0f);
80 this.right *= len;
81 this.up *= len;
82 this.at *= len;
83 }
84
85 public void SetRotation(Vector3 axis, float angle)
86 {
87 float len = axis.Length();
88 axis.Normalize();
89 float cos = (float)Math.Cos(angle);
90 float sin = (float)Math.Sin(angle);
91 this.right.Set(axis.x * axis.x + (1 - axis.x * axis.x) * cos, axis.x * axis.y * (1 - cos) - axis.z * sin, axis.x * axis.z * (1 - cos) + axis.y * sin, 0f);
92 this.up.Set(axis.x * axis.y * (1 - cos) + axis.z * sin, axis.y * axis.y + (1 - axis.y * axis.y) * cos, axis.y * axis.z * (1 - cos) - axis.x * sin, 0f);
93 this.at.Set(axis.x * axis.z * (1 - cos) - axis.y * sin, axis.y * axis.z * (1 - cos) + axis.x * sin, axis.z * axis.z + (1 - axis.z * axis.z) * cos, 0f);
94 this.right *= len;
95 this.up *= len;
96 this.at *= len;
97 }
98
99 public void SetRotation(Vector4 raxis)
100 {
101 SetRotation(raxis.V3, raxis.w);
102 }
103
104 public void SetRotationX(float rads)
105 {
106 float sin = (float)Math.Sin((double)rads);
107 float cos = (float)Math.Cos((double)rads);
108 this.right.Set(1f, 0f, 0f, 0f);
109 this.up.Set(0f, cos, -sin, 0f);
110 this.at.Set(0f, sin, cos, 0f);
111 }
112
113 public void SetRotationY(float rads)
114 {
115 float sin = (float)Math.Sin((double)rads);
116 float cos = (float)Math.Cos((double)rads);
117 this.right.Set(cos, 0f, sin, 0f);
118 this.up.Set(0f, 1f, 0f, 0f);
119 this.at.Set(-sin, 0f, cos, 0f);
120 }
121
122 public void SetRotationZ(float rads)
123 {
124 float sin = (float)Math.Sin((double)rads);
125 float cos = (float)Math.Cos((double)rads);
126 this.right.Set(cos, -sin, 0f, 0f);
127 this.up.Set(sin, cos, 0f, 0f);
128 this.at.Set(0f, 0f, 1f, 0f);
129 }
130
131 public double Magnitude()
132 {
133 return Math.Sqrt(this.right.LengthSqr() + this.up.LengthSqr() + this.at.LengthSqr() + this.pos.LengthSqr());
134 }
135
136 public override bool Equals(object obj)
137 {
138 if ((obj == null) || (obj.GetType() != base.GetType()))
139 {
140 return false;
141 }
142 Matrix44 matrix = (Matrix44)obj;
143 return (this.right.Equals(matrix.right) && this.up.Equals(matrix.up) && this.at.Equals(matrix.at) && this.pos.Equals(matrix.pos));
144 }
145
146 public override int GetHashCode()
147 {
148 return (this.right.GetHashCode() ^ this.up.GetHashCode() ^ this.at.GetHashCode() ^ this.pos.GetHashCode());
149 }
150
151 public override string ToString()
152 {
153 StringBuilder builder = new StringBuilder();
154 builder.Append("r:[");
155 builder.Append(this.right.ToString());
156 builder.Append("] u:[");
157 builder.Append(this.up.ToString());
158 builder.Append("] a:[");
159 builder.Append(this.at.ToString());
160 builder.Append("] p:[");
161 builder.Append(this.pos.ToString());
162 builder.Append("]");
163 return builder.ToString();
164 }
165
166 public Quaternion ToQuaternion()
167 {
168 double mag = this.Magnitude();
169 double m00 = (double)this.right.x / mag;
170 double m11 = (double)this.up.y / mag;
171 double m22 = (double)this.at.z / mag;
172 double qx = Math.Sqrt(0.25 * (1.0 - m00 - m11 + m22)) * mag;
173 double qy = Math.Sqrt(0.25 * (1.0 - m00 + m11 - m22)) * mag;
174 double qz = Math.Sqrt(0.25 * (1.0 + m00 - m11 - m22)) * mag;
175 double[] qw = new double[6];
176 double m01 = (double)this.right.y / mag;
177 qw[0] = (0.5 * m01 - qx * qy) / qz * mag;
178 double m02 = (double)this.right.z / mag;
179 qw[1] = (qx * qz - 0.5 * m02) / qy * mag;
180 double m10 = (double)this.up.x / mag;
181 qw[2] = (qx * qy - 0.5 * m10) / qz * mag;
182 double m12 = (double)this.up.z / mag;
183 qw[3] = (0.5 * m12 - qy * qz) / qx * mag;
184 double m20 = (double)this.at.x / mag;
185 qw[4] = (0.5 * m20 - qx * qz) / qy * mag;
186 double m21 = (double)this.at.y / mag;
187 qw[5] = (qy * qz - 0.5 * m21) / qx * mag;
188 return new Quaternion((float)qx, (float)qy, (float)qz, (float)qw[1]);
189 }
190
191 public Matrix33 ToMatrix33()
192 {
193 return new Matrix33(this.right.V3, this.up.V3, this.at.V3);
194 }
195
196 public void DebugDrawMatrixTimed(float time)
197 {
198 }
199
200 public Vector3 TransformVector(Vector3 vec)
201 {
202 return (Vector3)(((this.right.V3 * vec.x) + (this.up.V3 * vec.y)) + (this.at.V3 * vec.z));
203 }
204
205 public Vector3 InverseTransformPoint(Vector3 vec)
206 {
207 return this.InverseTransformVector(vec - this.pos.V3);
208 }
209
210 public Vector3 InverseTransformVector(Vector3 vec)
211 {
212 return new Vector3((float)(this.right.V3 * vec), (float)(this.up.V3 * vec), (float)(this.at.V3 * vec));
213 }
214
215 public static Matrix44 CopyMatrix(Matrix44 obj)
216 {
217 Matrix44 copy = new Matrix44();
218 copy.pos = new Vector4(obj.pos.x, obj.pos.y, obj.pos.z, obj.pos.w);
219 copy.right = new Vector4(obj.right.x, obj.right.y, obj.right.z, obj.right.w);
220 copy.up = new Vector4(obj.up.x, obj.up.y, obj.up.z, obj.up.w);
221 copy.at = new Vector4(obj.at.x, obj.at.y, obj.at.z, obj.at.w);
222 return copy;
223 }
224
225 public static Matrix44 operator *(Matrix44 a, Matrix44 b)
226 {
227 Matrix44 matrix = Matrix44.CopyMatrix(Identity);
228 matrix.right.V3 = (Vector3)(((b.right.V3 * a.right.V3.x) + (b.up.V3 * a.right.V3.y)) + (b.at.V3 * a.right.V3.z));
229 matrix.up.V3 = (Vector3)(((b.right.V3 * a.up.V3.x) + (b.up.V3 * a.up.V3.y)) + (b.at.V3 * a.up.V3.z));
230 matrix.at.V3 = (Vector3)(((b.right.V3 * a.at.V3.x) + (b.up.V3 * a.at.V3.y)) + (b.at.V3 * a.at.V3.z));
231 matrix.pos.V3 = ((Vector3)(((b.right.V3 * a.pos.V3.x) + (b.up.V3 * a.pos.V3.y)) + (b.at.V3 * a.pos.V3.z))) + b.pos.V3;
232 return matrix;
233 }
234 }
235 }

  ViewVC Help
Powered by ViewVC 1.1.22