diff options
author | Mohan Maiya <m.maiya@samsung.com> | 2024-02-28 17:13:38 -0600 |
---|---|---|
committer | Angle LUCI CQ <angle-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-02-29 04:12:52 +0000 |
commit | a971e5b42e1e8e61ccb0d8e4b3f2245aaa4f2d4d (patch) | |
tree | 3f1606cc9ed57bab66f0c8e97ab17a8971217271 | |
parent | 434a5b01707deca484b50a2a16c55f466b3d4a2b (diff) | |
download | angle-a971e5b42e1e8e61ccb0d8e4b3f2245aaa4f2d4d.tar.gz |
Account for zero vector axes in Mat4::Rotate(...)
When the axis passed in to the Rotate function is (0, 0, 0), normalizing
that vector will result in NaN values. Prevent this by returning an
identity matrix and early out instead.
Bug: angleproject:2306
Tests: MatrixBuiltinsTest.RotateAxisZero
Change-Id: I65fd0b9944885daf56a4a35201d424e7f0aa9ba6
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5333834
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
-rw-r--r-- | CONTRIBUTORS | 1 | ||||
-rw-r--r-- | src/common/matrix_utils.cpp | 5 | ||||
-rw-r--r-- | src/common/vector_utils.h | 2 | ||||
-rw-r--r-- | src/tests/gl_tests/gles1/MatrixBuiltinsTest.cpp | 26 |
4 files changed, 34 insertions, 0 deletions
diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 217e3b8cd9..4194befe83 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -176,6 +176,7 @@ Samsung Electronics, Inc. Hyunchang Kim Hyunseok Ko Jaedon Lee + Jaewoo Kim Jeff Vigil Jonah Taylor Kevin Jung diff --git a/src/common/matrix_utils.cpp b/src/common/matrix_utils.cpp index eaecb93224..3283be21f8 100644 --- a/src/common/matrix_utils.cpp +++ b/src/common/matrix_utils.cpp @@ -76,6 +76,11 @@ Mat4::Mat4(float m00, // static Mat4 Mat4::Rotate(float angle, const Vector3 &axis) { + if (axis.length() == 0.0f) + { + return Mat4(); + } + auto axis_normalized = axis.normalized(); float angle_radians = angle * (3.14159265358979323f / 180.0f); float c = cos(angle_radians); diff --git a/src/common/vector_utils.h b/src/common/vector_utils.h index 88c7492e72..053fae0239 100644 --- a/src/common/vector_utils.h +++ b/src/common/vector_utils.h @@ -12,6 +12,7 @@ #include <cstddef> #include <ostream> #include <type_traits> +#include "common/debug.h" namespace angle { @@ -490,6 +491,7 @@ Vector<Dimension, Type> VectorBase<Dimension, Type>::normalized() const { static_assert(std::is_floating_point<Type>::value, "VectorN::normalized is only defined for floating point vectors"); + ASSERT(length() != Type()); return *this / length(); } diff --git a/src/tests/gl_tests/gles1/MatrixBuiltinsTest.cpp b/src/tests/gl_tests/gles1/MatrixBuiltinsTest.cpp index 8757c3bd8b..b1ecbdfe39 100644 --- a/src/tests/gl_tests/gles1/MatrixBuiltinsTest.cpp +++ b/src/tests/gl_tests/gles1/MatrixBuiltinsTest.cpp @@ -51,6 +51,32 @@ TEST_P(MatrixBuiltinsTest, Rotate) EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix)); } +// Test that rotation of a (0, 0, 0) Vector doesn't result in NaN values. +TEST_P(MatrixBuiltinsTest, RotateAxisZero) +{ + constexpr float angle = 90.0f; + constexpr float x = 0.0f; + constexpr float y = 0.0f; + constexpr float z = 0.0f; + + angle::Mat4 testMatrix = angle::Mat4::Rotate(angle, angle::Vector3(x, y, z)); + + glRotatef(angle, x, y, z); + EXPECT_GL_NO_ERROR(); + + angle::Mat4 outputMatrix; + glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data()); + EXPECT_GL_NO_ERROR(); + + const float *inputArray = testMatrix.data(); + const float *outputArray = outputMatrix.data(); + for (int index = 0; index < 16; index++) + { + EXPECT_FALSE(isnan(inputArray[index])); + EXPECT_FALSE(isnan(outputArray[index])); + } +} + // Test translation and check the matrix for closeness to a translation matrix. TEST_P(MatrixBuiltinsTest, Translate) { |