Add Create from quaternion method from avc81, OpenTK Issue 2439

This commit is contained in:
Andy Korth 2013-01-03 10:19:06 -06:00
Родитель 84e97adf45
Коммит 0c7c1e8bd9
2 изменённых файлов: 152 добавлений и 0 удалений

Просмотреть файл

@ -667,7 +667,82 @@ namespace OpenTK
CreatePerspectiveOffCenter(left, right, bottom, top, zNear, zFar, out result);
return result;
}
#region CreateFromQuaternion
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <param name="m">Matrix result.</param>
public static void CreateFromQuaternion(ref Quaternion q,ref Matrix4 m)
{
m = Matrix4.Identity;
float X = q.X;
float Y = q.Y;
float Z = q.Z;
float W = q.W;
float xx = X * X;
float xy = X * Y;
float xz = X * Z;
float xw = X * W;
float yy = Y * Y;
float yz = Y * Z;
float yw = Y * W;
float zz = Z * Z;
float zw = Z * W;
m.M11 = 1 - 2 * (yy + zz);
m.M21 = 2 * (xy - zw);
m.M31 = 2 * (xz + yw);
m.M12 = 2 * (xy + zw);
m.M22 = 1 - 2 * (xx + zz);
m.M32 = 2 * (yz - xw);
m.M13 = 2 * (xz - yw);
m.M23 = 2 * (yz + xw);
m.M33 = 1 - 2 * (xx + yy);
}
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <returns>A matrix instance.</returns>
public static Matrix4 CreateFromQuaternion(ref Quaternion q)
{
Matrix4 result = Matrix4.Identity;
float X = q.X;
float Y = q.Y;
float Z = q.Z;
float W = q.W;
float xx = X * X;
float xy = X * Y;
float xz = X * Z;
float xw = X * W;
float yy = Y * Y;
float yz = Y * Z;
float yw = Y * W;
float zz = Z * Z;
float zw = Z * W;
result.M11 = 1 - 2 * (yy + zz);
result.M21 = 2 * (xy - zw);
result.M31 = 2 * (xz + yw);
result.M12 = 2 * (xy + zw);
result.M22 = 1 - 2 * (xx + zz);
result.M32 = 2 * (yz - xw);
result.M13 = 2 * (xz - yw);
result.M23 = 2 * (yz + xw);
result.M33 = 1 - 2 * (xx + yy);
return result;
}
#endregion
#endregion
#region Obsolete Functions

Просмотреть файл

@ -670,6 +670,83 @@ namespace OpenTK
#endregion
#region CreateFromQuaternion
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <param name="m">Matrix result.</param>
public static void CreateFromQuaternion(ref Quaternion q,ref Matrix4 m)
{
m = Matrix4.Identity;
float X = q.X;
float Y = q.Y;
float Z = q.Z;
float W = q.W;
float xx = X * X;
float xy = X * Y;
float xz = X * Z;
float xw = X * W;
float yy = Y * Y;
float yz = Y * Z;
float yw = Y * W;
float zz = Z * Z;
float zw = Z * W;
m.M11 = 1 - 2 * (yy + zz);
m.M21 = 2 * (xy - zw);
m.M31 = 2 * (xz + yw);
m.M12 = 2 * (xy + zw);
m.M22 = 1 - 2 * (xx + zz);
m.M32 = 2 * (yz - xw);
m.M13 = 2 * (xz - yw);
m.M23 = 2 * (yz + xw);
m.M33 = 1 - 2 * (xx + yy);
}
/// <summary>
/// Build a rotation matrix from the specified quaternion.
/// </summary>
/// <param name="q">Quaternion to translate.</param>
/// <returns>A matrix instance.</returns>
public static Matrix4 CreateFromQuaternion(ref Quaternion q)
{
Matrix4 result = Matrix4.Identity;
float X = q.X;
float Y = q.Y;
float Z = q.Z;
float W = q.W;
float xx = X * X;
float xy = X * Y;
float xz = X * Z;
float xw = X * W;
float yy = Y * Y;
float yz = Y * Z;
float yw = Y * W;
float zz = Z * Z;
float zw = Z * W;
result.M11 = 1 - 2 * (yy + zz);
result.M21 = 2 * (xy - zw);
result.M31 = 2 * (xz + yw);
result.M12 = 2 * (xy + zw);
result.M22 = 1 - 2 * (xx + zz);
result.M32 = 2 * (yz - xw);
result.M13 = 2 * (xz - yw);
result.M23 = 2 * (yz + xw);
result.M33 = 1 - 2 * (xx + yy);
return result;
}
#endregion
#region Obsolete Functions
#region Translation Functions