Add more utilities for using Matrix3x3

This commit is contained in:
Wojciech Figat
2022-04-04 17:09:47 +02:00
parent 52bb5803b3
commit 3ded5326a2
8 changed files with 95 additions and 72 deletions

View File

@@ -1,6 +1,7 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include "Matrix.h"
#include "Matrix3x3.h"
#include "Vector2.h"
#include "Quaternion.h"
#include "Transform.h"
@@ -15,6 +16,17 @@ const Matrix Matrix::Identity(
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
Matrix::Matrix(const Matrix3x3& matrix)
{
Platform::MemoryCopy(&M11, &matrix.M11, sizeof(Vector3));
Platform::MemoryCopy(&M21, &matrix.M21, sizeof(Vector3));
Platform::MemoryCopy(&M31, &matrix.M31, sizeof(Vector3));
M14 = 0.0f;
M24 = 0.0f;
M34 = 0.0f;
M44 = 1.0f;
}
String Matrix::ToString() const
{
return String::Format(TEXT("{}"), *this);
@@ -284,8 +296,6 @@ void Matrix::LookAt(const Vector3& eye, const Vector3& target, const Vector3& up
xaxis.Normalize();
Vector3::Cross(zaxis, xaxis, yaxis);
result = Identity;
result.M11 = xaxis.X;
result.M21 = xaxis.Y;
result.M31 = xaxis.Z;
@@ -298,9 +308,14 @@ void Matrix::LookAt(const Vector3& eye, const Vector3& target, const Vector3& up
result.M23 = zaxis.Y;
result.M33 = zaxis.Z;
result.M14 = 0.0f;
result.M24 = 0.0f;
result.M34 = 0.0f;
result.M41 = -Vector3::Dot(xaxis, eye);
result.M42 = -Vector3::Dot(yaxis, eye);
result.M43 = -Vector3::Dot(zaxis, eye);
result.M44 = 1.0f;
}
void Matrix::OrthoOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, Matrix& result)
@@ -587,33 +602,7 @@ void Matrix::Transformation2D(Vector2& scalingCenter, float scalingRotation, con
Matrix Matrix::CreateWorld(const Vector3& position, const Vector3& forward, const Vector3& up)
{
Matrix result;
Vector3 vector3, vector31, vector32;
Vector3::Normalize(forward, vector3);
vector3.Negate();
Vector3::Normalize(Vector3::Cross(up, vector3), vector31);
Vector3::Cross(vector3, vector31, vector32);
result.M11 = vector31.X;
result.M12 = vector31.Y;
result.M13 = vector31.Z;
result.M14 = 0.0f;
result.M21 = vector32.X;
result.M22 = vector32.Y;
result.M23 = vector32.Z;
result.M24 = 0.0f;
result.M31 = vector3.X;
result.M32 = vector3.Y;
result.M33 = vector3.Z;
result.M34 = 0.0f;
result.M41 = position.X;
result.M42 = position.Y;
result.M43 = position.Z;
result.M44 = 1.0f;
CreateWorld(position, forward, up, result);
return result;
}
@@ -649,41 +638,9 @@ void Matrix::CreateWorld(const Vector3& position, const Vector3& forward, const
Matrix Matrix::CreateFromAxisAngle(const Vector3& axis, float angle)
{
Matrix matrix;
const float x = axis.X;
const float y = axis.Y;
const float z = axis.Z;
const float single = Math::Sin(angle);
const float single1 = Math::Cos(angle);
const float single2 = x * x;
const float single3 = y * y;
const float single4 = z * z;
const float single5 = x * y;
const float single6 = x * z;
const float single7 = y * z;
matrix.M11 = single2 + single1 * (1.0f - single2);
matrix.M12 = single5 - single1 * single5 + single * z;
matrix.M13 = single6 - single1 * single6 - single * y;
matrix.M14 = 0.0f;
matrix.M21 = single5 - single1 * single5 - single * z;
matrix.M22 = single3 + single1 * (1.0f - single3);
matrix.M23 = single7 - single1 * single7 + single * x;
matrix.M24 = 0.0f;
matrix.M31 = single6 - single1 * single6 + single * y;
matrix.M32 = single7 - single1 * single7 - single * x;
matrix.M33 = single4 + single1 * (1.0f - single4);
matrix.M34 = 0.0f;
matrix.M41 = 0.0f;
matrix.M42 = 0.0f;
matrix.M43 = 0.0f;
matrix.M44 = 1.0f;
return matrix;
Matrix result;
CreateFromAxisAngle(axis, angle, result);
return result;
}
void Matrix::CreateFromAxisAngle(const Vector3& axis, float angle, Matrix& result)