Hash :
a971e5b4
        
        Author :
  
        
        Date :
2024-02-28T17:13:38
        
      
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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// matrix_utils.cpp: Contains implementations for Mat4 methods.
#include "common/matrix_utils.h"
namespace angle
{
Mat4::Mat4() : Mat4(1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f)
{}
Mat4::Mat4(const Matrix<float> generalMatrix)
{
    unsigned int minCols = std::min((unsigned int)4, generalMatrix.columns());
    unsigned int minRows = std::min((unsigned int)4, generalMatrix.rows());
    for (unsigned int i = 0; i < minCols; i++)
    {
        for (unsigned int j = 0; j < minRows; j++)
        {
            mElements[j * minCols + i] = generalMatrix.at(j, i);
        }
    }
}
Mat4::Mat4(const std::vector<float> &elements)
{
    std::copy(elements.begin(), std::min(elements.end(), elements.begin() + std::size(mElements)),
              mElements.data());
}
Mat4::Mat4(const float *elements)
{
    std::copy(elements, elements + std::size(mElements), mElements.data());
}
Mat4::Mat4(float m00,
           float m01,
           float m02,
           float m03,
           float m10,
           float m11,
           float m12,
           float m13,
           float m20,
           float m21,
           float m22,
           float m23,
           float m30,
           float m31,
           float m32,
           float m33)
{
    mElements[0]  = m00;
    mElements[1]  = m01;
    mElements[2]  = m02;
    mElements[3]  = m03;
    mElements[4]  = m10;
    mElements[5]  = m11;
    mElements[6]  = m12;
    mElements[7]  = m13;
    mElements[8]  = m20;
    mElements[9]  = m21;
    mElements[10] = m22;
    mElements[11] = m23;
    mElements[12] = m30;
    mElements[13] = m31;
    mElements[14] = m32;
    mElements[15] = m33;
}
// 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);
    float ci             = 1.f - c;
    float s              = sin(angle_radians);
    float x = axis_normalized.x();
    float y = axis_normalized.y();
    float z = axis_normalized.z();
    float x2 = x * x;
    float y2 = y * y;
    float z2 = z * z;
    float xy = x * y;
    float yz = y * z;
    float zx = z * x;
    float r00 = c + ci * x2;
    float r01 = ci * xy + s * z;
    float r02 = ci * zx - s * y;
    float r03 = 0.f;
    float r10 = ci * xy - s * z;
    float r11 = c + ci * y2;
    float r12 = ci * yz + s * x;
    float r13 = 0.f;
    float r20 = ci * zx + s * y;
    float r21 = ci * yz - s * x;
    float r22 = c + ci * z2;
    float r23 = 0.f;
    float r30 = 0.f;
    float r31 = 0.f;
    float r32 = 0.f;
    float r33 = 1.f;
    return Mat4(r00, r01, r02, r03, r10, r11, r12, r13, r20, r21, r22, r23, r30, r31, r32, r33);
}
// static
Mat4 Mat4::Translate(const Vector3 &t)
{
    float r00 = 1.f;
    float r01 = 0.f;
    float r02 = 0.f;
    float r03 = 0.f;
    float r10 = 0.f;
    float r11 = 1.f;
    float r12 = 0.f;
    float r13 = 0.f;
    float r20 = 0.f;
    float r21 = 0.f;
    float r22 = 1.f;
    float r23 = 0.f;
    float r30 = t.x();
    float r31 = t.y();
    float r32 = t.z();
    float r33 = 1.f;
    return Mat4(r00, r01, r02, r03, r10, r11, r12, r13, r20, r21, r22, r23, r30, r31, r32, r33);
}
// static
Mat4 Mat4::Scale(const Vector3 &s)
{
    float r00 = s.x();
    float r01 = 0.f;
    float r02 = 0.f;
    float r03 = 0.f;
    float r10 = 0.f;
    float r11 = s.y();
    float r12 = 0.f;
    float r13 = 0.f;
    float r20 = 0.f;
    float r21 = 0.f;
    float r22 = s.z();
    float r23 = 0.f;
    float r30 = 0.f;
    float r31 = 0.f;
    float r32 = 0.f;
    float r33 = 1.f;
    return Mat4(r00, r01, r02, r03, r10, r11, r12, r13, r20, r21, r22, r23, r30, r31, r32, r33);
}
// static
Mat4 Mat4::Frustum(float l, float r, float b, float t, float n, float f)
{
    float nn  = 2.f * n;
    float fpn = f + n;
    float fmn = f - n;
    float tpb = t + b;
    float tmb = t - b;
    float rpl = r + l;
    float rml = r - l;
    float r00 = nn / rml;
    float r01 = 0.f;
    float r02 = 0.f;
    float r03 = 0.f;
    float r10 = 0.f;
    float r11 = nn / tmb;
    float r12 = 0.f;
    float r13 = 0.f;
    float r20 = rpl / rml;
    float r21 = tpb / tmb;
    float r22 = -fpn / fmn;
    float r23 = -1.f;
    float r30 = 0.f;
    float r31 = 0.f;
    float r32 = -nn * f / fmn;
    float r33 = 0.f;
    return Mat4(r00, r01, r02, r03, r10, r11, r12, r13, r20, r21, r22, r23, r30, r31, r32, r33);
}
// static
Mat4 Mat4::Perspective(float fov, float aspectRatio, float n, float f)
{
    const float frustumHeight = tanf(static_cast<float>(fov / 360.0f * 3.14159265358979323)) * n;
    const float frustumWidth  = frustumHeight * aspectRatio;
    return Frustum(-frustumWidth, frustumWidth, -frustumHeight, frustumHeight, n, f);
}
// static
Mat4 Mat4::Ortho(float l, float r, float b, float t, float n, float f)
{
    float fpn = f + n;
    float fmn = f - n;
    float tpb = t + b;
    float tmb = t - b;
    float rpl = r + l;
    float rml = r - l;
    float r00 = 2.f / rml;
    float r01 = 0.f;
    float r02 = 0.f;
    float r03 = 0.f;
    float r10 = 0.f;
    float r11 = 2.f / tmb;
    float r12 = 0.f;
    float r13 = 0.f;
    float r20 = 0.f;
    float r21 = 0.f;
    float r22 = -2.f / fmn;
    float r23 = 0.f;
    float r30 = -rpl / rml;
    float r31 = -tpb / tmb;
    float r32 = -fpn / fmn;
    float r33 = 1.f;
    return Mat4(r00, r01, r02, r03, r10, r11, r12, r13, r20, r21, r22, r23, r30, r31, r32, r33);
}
Mat4 Mat4::product(const Mat4 &m)
{
    const float *a = mElements.data();
    const float *b = m.mElements.data();
    return Mat4(a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3],
                a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3],
                a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3],
                a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3],
                a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7],
                a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7],
                a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7],
                a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7],
                a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11],
                a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11],
                a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11],
                a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11],
                a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15],
                a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15],
                a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15],
                a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15]);
}
Vector4 Mat4::product(const Vector4 &b)
{
    return Vector4(
        mElements[0] * b.x() + mElements[4] * b.y() + mElements[8] * b.z() + mElements[12] * b.w(),
        mElements[1] * b.x() + mElements[5] * b.y() + mElements[9] * b.z() + mElements[13] * b.w(),
        mElements[2] * b.x() + mElements[6] * b.y() + mElements[10] * b.z() + mElements[14] * b.w(),
        mElements[3] * b.x() + mElements[7] * b.y() + mElements[11] * b.z() +
            mElements[15] * b.w());
}
void Mat4::dump()
{
    printf("[ %f %f %f %f ]\n", mElements[0], mElements[4], mElements[8], mElements[12]);
    printf("[ %f %f %f %f ]\n", mElements[1], mElements[5], mElements[9], mElements[13]);
    printf("[ %f %f %f %f ]\n", mElements[2], mElements[6], mElements[10], mElements[14]);
    printf("[ %f %f %f %f ]\n", mElements[3], mElements[7], mElements[11], mElements[15]);
}
}  // namespace angle