Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Add Matrix::LookAt #37361

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,52 @@ TEST(GeometryTest, MatrixIsAligned) {
}
}

TEST(GeometryTest, MatrixLookAt) {
{
auto m = Matrix::MakeLookAt(Vector3(0, 0, -1), Vector3(0, 0, 1),
Vector3(0, 1, 0));
auto expected = Matrix{
1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, 1, 0, //
0, 0, 1, 1, //
};
ASSERT_MATRIX_NEAR(m, expected);
}

// Sideways tilt.
{
auto m = Matrix::MakeLookAt(Vector3(0, 0, -1), Vector3(0, 0, 1),
Vector3(1, 1, 0).Normalize());

// clang-format off
auto expected = Matrix{
k1OverSqrt2, k1OverSqrt2, 0, 0,
-k1OverSqrt2, k1OverSqrt2, 0, 0,
0, 0, 1, 0,
0, 0, 1, 1,
};
// clang-format on
ASSERT_MATRIX_NEAR(m, expected);
}

// Half way between +x and -y, yaw 90
{
auto m =
Matrix::MakeLookAt(Vector3(), Vector3(10, -10, 0), Vector3(0, 0, -1));

// clang-format off
auto expected = Matrix{
-k1OverSqrt2, 0, k1OverSqrt2, 0,
-k1OverSqrt2, 0, -k1OverSqrt2, 0,
0, -1, 0, 0,
0, 0, 0, 1,
};
// clang-format on
ASSERT_MATRIX_NEAR(m, expected);
}
}

TEST(GeometryTest, QuaternionLerp) {
auto q1 = Quaternion{{0.0, 0.0, 1.0}, 0.0};
auto q2 = Quaternion{{0.0, 0.0, 1.0}, kPiOver4};
Expand Down
17 changes: 17 additions & 0 deletions impeller/geometry/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,23 @@ struct Matrix {
return MakePerspective(fov_y, static_cast<Scalar>(size.width) / size.height,
z_near, z_far);
}

static constexpr Matrix MakeLookAt(Vector3 position,
Vector3 target,
Vector3 up) {
Vector3 forward = (target - position).Normalize();
Vector3 right = up.Cross(forward);
up = forward.Cross(right);

// clang-format off
return {
right.x, up.x, forward.x, 0.0f,
right.y, up.y, forward.y, 0.0f,
right.z, up.z, forward.z, 0.0f,
-right.Dot(position), -up.Dot(position), -forward.Dot(position), 1.0f
};
// clang-format on
}
};

static_assert(sizeof(struct Matrix) == sizeof(Scalar) * 16,
Expand Down