| 1 |
// Generated from mat.rs.tera template. Edit the template, not the generated file. |
| 2 |
|
| 3 |
use crate::{f32::math, swizzles::*, DMat4, EulerRot, Mat3, Mat3A, Quat, Vec3, Vec3A, Vec4}; |
| 4 |
#[cfg(not(target_arch = "spirv"))] |
| 5 |
use core::fmt; |
| 6 |
use core::iter::{Product, Sum}; |
| 7 |
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
| 8 |
|
| 9 |
/// Creates a 4x4 matrix from four column vectors. |
| 10 |
#[inline(always)] |
| 11 |
pub const fn mat4(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Mat4 { |
| 12 |
Mat4::from_cols(x_axis, y_axis, z_axis, w_axis) |
| 13 |
} |
| 14 |
|
| 15 |
/// A 4x4 column major matrix. |
| 16 |
/// |
| 17 |
/// This 4x4 matrix type features convenience methods for creating and using affine transforms and |
| 18 |
/// perspective projections. If you are primarily dealing with 3D affine transformations |
| 19 |
/// considering using [`Affine3A`](crate::Affine3A) which is faster than a 4x4 matrix |
| 20 |
/// for some affine operations. |
| 21 |
/// |
| 22 |
/// Affine transformations including 3D translation, rotation and scale can be created |
| 23 |
/// using methods such as [`Self::from_translation()`], [`Self::from_quat()`], |
| 24 |
/// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`]. |
| 25 |
/// |
| 26 |
/// Orthographic projections can be created using the methods [`Self::orthographic_lh()`] for |
| 27 |
/// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed |
| 28 |
/// systems. The resulting matrix is also an affine transformation. |
| 29 |
/// |
| 30 |
/// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods |
| 31 |
/// are provided for performing affine transformations on 3D vectors and points. These |
| 32 |
/// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0` |
| 33 |
/// for vectors respectively. These methods assume that `Self` contains a valid affine |
| 34 |
/// transform. |
| 35 |
/// |
| 36 |
/// Perspective projections can be created using methods such as |
| 37 |
/// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and |
| 38 |
/// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and |
| 39 |
/// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and |
| 40 |
/// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems. |
| 41 |
/// |
| 42 |
/// The resulting perspective project can be use to transform 3D vectors as points with |
| 43 |
/// perspective correction using the [`Self::project_point3()`] convenience method. |
| 44 |
#[derive(Clone, Copy)] |
| 45 |
#[cfg_attr( |
| 46 |
any( |
| 47 |
not(any(feature = "scalar-math", target_arch = "spirv")), |
| 48 |
feature = "cuda" |
| 49 |
), |
| 50 |
repr(align(16)) |
| 51 |
)] |
| 52 |
#[repr(C)] |
| 53 |
pub struct Mat4 { |
| 54 |
pub x_axis: Vec4, |
| 55 |
pub y_axis: Vec4, |
| 56 |
pub z_axis: Vec4, |
| 57 |
pub w_axis: Vec4, |
| 58 |
} |
| 59 |
|
| 60 |
impl Mat4 { |
| 61 |
/// A 4x4 matrix with all elements set to `0.0`. |
| 62 |
pub const ZERO: Self = Self::from_cols(Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO); |
| 63 |
|
| 64 |
/// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`. |
| 65 |
pub const IDENTITY: Self = Self::from_cols(Vec4::X, Vec4::Y, Vec4::Z, Vec4::W); |
| 66 |
|
| 67 |
/// All NAN:s. |
| 68 |
pub const NAN: Self = Self::from_cols(Vec4::NAN, Vec4::NAN, Vec4::NAN, Vec4::NAN); |
| 69 |
|
| 70 |
#[allow(clippy::too_many_arguments)] |
| 71 |
#[inline(always)] |
| 72 |
const fn new( |
| 73 |
m00: f32, |
| 74 |
m01: f32, |
| 75 |
m02: f32, |
| 76 |
m03: f32, |
| 77 |
m10: f32, |
| 78 |
m11: f32, |
| 79 |
m12: f32, |
| 80 |
m13: f32, |
| 81 |
m20: f32, |
| 82 |
m21: f32, |
| 83 |
m22: f32, |
| 84 |
m23: f32, |
| 85 |
m30: f32, |
| 86 |
m31: f32, |
| 87 |
m32: f32, |
| 88 |
m33: f32, |
| 89 |
) -> Self { |
| 90 |
Self { |
| 91 |
x_axis: Vec4::new(m00, m01, m02, m03), |
| 92 |
y_axis: Vec4::new(m10, m11, m12, m13), |
| 93 |
z_axis: Vec4::new(m20, m21, m22, m23), |
| 94 |
w_axis: Vec4::new(m30, m31, m32, m33), |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/// Creates a 4x4 matrix from four column vectors. |
| 99 |
#[inline(always)] |
| 100 |
pub const fn from_cols(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Self { |
| 101 |
Self { |
| 102 |
x_axis, |
| 103 |
y_axis, |
| 104 |
z_axis, |
| 105 |
w_axis, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/// Creates a 4x4 matrix from a `[f32; 16]` array stored in column major order. |
| 110 |
/// If your data is stored in row major you will need to `transpose` the returned |
| 111 |
/// matrix. |
| 112 |
#[inline] |
| 113 |
pub const fn from_cols_array(m: &[f32; 16]) -> Self { |
| 114 |
Self::new( |
| 115 |
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], |
| 116 |
m[14], m[15], |
| 117 |
) |
| 118 |
} |
| 119 |
|
| 120 |
/// Creates a `[f32; 16]` array storing data in column major order. |
| 121 |
/// If you require data in row major order `transpose` the matrix first. |
| 122 |
#[inline] |
| 123 |
pub const fn to_cols_array(&self) -> [f32; 16] { |
| 124 |
[ |
| 125 |
self.x_axis.x, |
| 126 |
self.x_axis.y, |
| 127 |
self.x_axis.z, |
| 128 |
self.x_axis.w, |
| 129 |
self.y_axis.x, |
| 130 |
self.y_axis.y, |
| 131 |
self.y_axis.z, |
| 132 |
self.y_axis.w, |
| 133 |
self.z_axis.x, |
| 134 |
self.z_axis.y, |
| 135 |
self.z_axis.z, |
| 136 |
self.z_axis.w, |
| 137 |
self.w_axis.x, |
| 138 |
self.w_axis.y, |
| 139 |
self.w_axis.z, |
| 140 |
self.w_axis.w, |
| 141 |
] |
| 142 |
} |
| 143 |
|
| 144 |
/// Creates a 4x4 matrix from a `[[f32; 4]; 4]` 4D array stored in column major order. |
| 145 |
/// If your data is in row major order you will need to `transpose` the returned |
| 146 |
/// matrix. |
| 147 |
#[inline] |
| 148 |
pub const fn from_cols_array_2d(m: &[[f32; 4]; 4]) -> Self { |
| 149 |
Self::from_cols( |
| 150 |
Vec4::from_array(m[0]), |
| 151 |
Vec4::from_array(m[1]), |
| 152 |
Vec4::from_array(m[2]), |
| 153 |
Vec4::from_array(m[3]), |
| 154 |
) |
| 155 |
} |
| 156 |
|
| 157 |
/// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order. |
| 158 |
/// If you require data in row major order `transpose` the matrix first. |
| 159 |
#[inline] |
| 160 |
pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { |
| 161 |
[ |
| 162 |
self.x_axis.to_array(), |
| 163 |
self.y_axis.to_array(), |
| 164 |
self.z_axis.to_array(), |
| 165 |
self.w_axis.to_array(), |
| 166 |
] |
| 167 |
} |
| 168 |
|
| 169 |
/// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0. |
| 170 |
#[doc(alias = "scale")] |
| 171 |
#[inline] |
| 172 |
pub const fn from_diagonal(diagonal: Vec4) -> Self { |
| 173 |
Self::new( |
| 174 |
diagonal.x, 0.0, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, 0.0, diagonal.z, 0.0, 0.0, |
| 175 |
0.0, 0.0, diagonal.w, |
| 176 |
) |
| 177 |
} |
| 178 |
|
| 179 |
#[inline] |
| 180 |
fn quat_to_axes(rotation: Quat) -> (Vec4, Vec4, Vec4) { |
| 181 |
glam_assert!(rotation.is_normalized()); |
| 182 |
|
| 183 |
let (x, y, z, w) = rotation.into(); |
| 184 |
let x2 = x + x; |
| 185 |
let y2 = y + y; |
| 186 |
let z2 = z + z; |
| 187 |
let xx = x * x2; |
| 188 |
let xy = x * y2; |
| 189 |
let xz = x * z2; |
| 190 |
let yy = y * y2; |
| 191 |
let yz = y * z2; |
| 192 |
let zz = z * z2; |
| 193 |
let wx = w * x2; |
| 194 |
let wy = w * y2; |
| 195 |
let wz = w * z2; |
| 196 |
|
| 197 |
let x_axis = Vec4::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0); |
| 198 |
let y_axis = Vec4::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0); |
| 199 |
let z_axis = Vec4::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0); |
| 200 |
(x_axis, y_axis, z_axis) |
| 201 |
} |
| 202 |
|
| 203 |
/// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and |
| 204 |
/// `translation`. |
| 205 |
/// |
| 206 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 207 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 208 |
/// |
| 209 |
/// # Panics |
| 210 |
/// |
| 211 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 212 |
#[inline] |
| 213 |
pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self { |
| 214 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 215 |
Self::from_cols( |
| 216 |
x_axis.mul(scale.x), |
| 217 |
y_axis.mul(scale.y), |
| 218 |
z_axis.mul(scale.z), |
| 219 |
Vec4::from((translation, 1.0)), |
| 220 |
) |
| 221 |
} |
| 222 |
|
| 223 |
/// Creates an affine transformation matrix from the given 3D `translation`. |
| 224 |
/// |
| 225 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 226 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 227 |
/// |
| 228 |
/// # Panics |
| 229 |
/// |
| 230 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 231 |
#[inline] |
| 232 |
pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self { |
| 233 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 234 |
Self::from_cols(x_axis, y_axis, z_axis, Vec4::from((translation, 1.0))) |
| 235 |
} |
| 236 |
|
| 237 |
/// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is |
| 238 |
/// expected to be a 3D affine transformation matrix otherwise the output will be invalid. |
| 239 |
/// |
| 240 |
/// # Panics |
| 241 |
/// |
| 242 |
/// Will panic if the determinant of `self` is zero or if the resulting scale vector |
| 243 |
/// contains any zero elements when `glam_assert` is enabled. |
| 244 |
#[inline] |
| 245 |
pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) { |
| 246 |
let det = self.determinant(); |
| 247 |
glam_assert!(det != 0.0); |
| 248 |
|
| 249 |
let scale = Vec3::new( |
| 250 |
self.x_axis.length() * math::signum(det), |
| 251 |
self.y_axis.length(), |
| 252 |
self.z_axis.length(), |
| 253 |
); |
| 254 |
|
| 255 |
glam_assert!(scale.cmpne(Vec3::ZERO).all()); |
| 256 |
|
| 257 |
let inv_scale = scale.recip(); |
| 258 |
|
| 259 |
let rotation = Quat::from_rotation_axes( |
| 260 |
self.x_axis.mul(inv_scale.x).xyz(), |
| 261 |
self.y_axis.mul(inv_scale.y).xyz(), |
| 262 |
self.z_axis.mul(inv_scale.z).xyz(), |
| 263 |
); |
| 264 |
|
| 265 |
let translation = self.w_axis.xyz(); |
| 266 |
|
| 267 |
(scale, rotation, translation) |
| 268 |
} |
| 269 |
|
| 270 |
/// Creates an affine transformation matrix from the given `rotation` quaternion. |
| 271 |
/// |
| 272 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 273 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 274 |
/// |
| 275 |
/// # Panics |
| 276 |
/// |
| 277 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 278 |
#[inline] |
| 279 |
pub fn from_quat(rotation: Quat) -> Self { |
| 280 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 281 |
Self::from_cols(x_axis, y_axis, z_axis, Vec4::W) |
| 282 |
} |
| 283 |
|
| 284 |
/// Creates an affine transformation matrix from the given 3x3 linear transformation |
| 285 |
/// matrix. |
| 286 |
/// |
| 287 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 288 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 289 |
#[inline] |
| 290 |
pub fn from_mat3(m: Mat3) -> Self { |
| 291 |
Self::from_cols( |
| 292 |
Vec4::from((m.x_axis, 0.0)), |
| 293 |
Vec4::from((m.y_axis, 0.0)), |
| 294 |
Vec4::from((m.z_axis, 0.0)), |
| 295 |
Vec4::W, |
| 296 |
) |
| 297 |
} |
| 298 |
|
| 299 |
/// Creates an affine transformation matrix from the given 3x3 linear transformation |
| 300 |
/// matrix. |
| 301 |
/// |
| 302 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 303 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 304 |
#[inline] |
| 305 |
pub fn from_mat3a(m: Mat3A) -> Self { |
| 306 |
Self::from_cols( |
| 307 |
Vec4::from((m.x_axis, 0.0)), |
| 308 |
Vec4::from((m.y_axis, 0.0)), |
| 309 |
Vec4::from((m.z_axis, 0.0)), |
| 310 |
Vec4::W, |
| 311 |
) |
| 312 |
} |
| 313 |
|
| 314 |
/// Creates an affine transformation matrix from the given 3D `translation`. |
| 315 |
/// |
| 316 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 317 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 318 |
#[inline] |
| 319 |
pub fn from_translation(translation: Vec3) -> Self { |
| 320 |
Self::from_cols( |
| 321 |
Vec4::X, |
| 322 |
Vec4::Y, |
| 323 |
Vec4::Z, |
| 324 |
Vec4::new(translation.x, translation.y, translation.z, 1.0), |
| 325 |
) |
| 326 |
} |
| 327 |
|
| 328 |
/// Creates an affine transformation matrix containing a 3D rotation around a normalized |
| 329 |
/// rotation `axis` of `angle` (in radians). |
| 330 |
/// |
| 331 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 332 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 333 |
/// |
| 334 |
/// # Panics |
| 335 |
/// |
| 336 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 337 |
#[inline] |
| 338 |
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self { |
| 339 |
glam_assert!(axis.is_normalized()); |
| 340 |
|
| 341 |
let (sin, cos) = math::sin_cos(angle); |
| 342 |
let axis_sin = axis.mul(sin); |
| 343 |
let axis_sq = axis.mul(axis); |
| 344 |
let omc = 1.0 - cos; |
| 345 |
let xyomc = axis.x * axis.y * omc; |
| 346 |
let xzomc = axis.x * axis.z * omc; |
| 347 |
let yzomc = axis.y * axis.z * omc; |
| 348 |
Self::from_cols( |
| 349 |
Vec4::new( |
| 350 |
axis_sq.x * omc + cos, |
| 351 |
xyomc + axis_sin.z, |
| 352 |
xzomc - axis_sin.y, |
| 353 |
0.0, |
| 354 |
), |
| 355 |
Vec4::new( |
| 356 |
xyomc - axis_sin.z, |
| 357 |
axis_sq.y * omc + cos, |
| 358 |
yzomc + axis_sin.x, |
| 359 |
0.0, |
| 360 |
), |
| 361 |
Vec4::new( |
| 362 |
xzomc + axis_sin.y, |
| 363 |
yzomc - axis_sin.x, |
| 364 |
axis_sq.z * omc + cos, |
| 365 |
0.0, |
| 366 |
), |
| 367 |
Vec4::W, |
| 368 |
) |
| 369 |
} |
| 370 |
|
| 371 |
#[inline] |
| 372 |
/// Creates a affine transformation matrix containing a rotation from the given euler |
| 373 |
/// rotation sequence and angles (in radians). |
| 374 |
/// |
| 375 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 376 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 377 |
pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self { |
| 378 |
let quat = Quat::from_euler(order, a, b, c); |
| 379 |
Self::from_quat(quat) |
| 380 |
} |
| 381 |
|
| 382 |
/// Creates an affine transformation matrix containing a 3D rotation around the x axis of |
| 383 |
/// `angle` (in radians). |
| 384 |
/// |
| 385 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 386 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 387 |
#[inline] |
| 388 |
pub fn from_rotation_x(angle: f32) -> Self { |
| 389 |
let (sina, cosa) = math::sin_cos(angle); |
| 390 |
Self::from_cols( |
| 391 |
Vec4::X, |
| 392 |
Vec4::new(0.0, cosa, sina, 0.0), |
| 393 |
Vec4::new(0.0, -sina, cosa, 0.0), |
| 394 |
Vec4::W, |
| 395 |
) |
| 396 |
} |
| 397 |
|
| 398 |
/// Creates an affine transformation matrix containing a 3D rotation around the y axis of |
| 399 |
/// `angle` (in radians). |
| 400 |
/// |
| 401 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 402 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 403 |
#[inline] |
| 404 |
pub fn from_rotation_y(angle: f32) -> Self { |
| 405 |
let (sina, cosa) = math::sin_cos(angle); |
| 406 |
Self::from_cols( |
| 407 |
Vec4::new(cosa, 0.0, -sina, 0.0), |
| 408 |
Vec4::Y, |
| 409 |
Vec4::new(sina, 0.0, cosa, 0.0), |
| 410 |
Vec4::W, |
| 411 |
) |
| 412 |
} |
| 413 |
|
| 414 |
/// Creates an affine transformation matrix containing a 3D rotation around the z axis of |
| 415 |
/// `angle` (in radians). |
| 416 |
/// |
| 417 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 418 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 419 |
#[inline] |
| 420 |
pub fn from_rotation_z(angle: f32) -> Self { |
| 421 |
let (sina, cosa) = math::sin_cos(angle); |
| 422 |
Self::from_cols( |
| 423 |
Vec4::new(cosa, sina, 0.0, 0.0), |
| 424 |
Vec4::new(-sina, cosa, 0.0, 0.0), |
| 425 |
Vec4::Z, |
| 426 |
Vec4::W, |
| 427 |
) |
| 428 |
} |
| 429 |
|
| 430 |
/// Creates an affine transformation matrix containing the given 3D non-uniform `scale`. |
| 431 |
/// |
| 432 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 433 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 434 |
/// |
| 435 |
/// # Panics |
| 436 |
/// |
| 437 |
/// Will panic if all elements of `scale` are zero when `glam_assert` is enabled. |
| 438 |
#[inline] |
| 439 |
pub fn from_scale(scale: Vec3) -> Self { |
| 440 |
// Do not panic as long as any component is non-zero |
| 441 |
glam_assert!(scale.cmpne(Vec3::ZERO).any()); |
| 442 |
|
| 443 |
Self::from_cols( |
| 444 |
Vec4::new(scale.x, 0.0, 0.0, 0.0), |
| 445 |
Vec4::new(0.0, scale.y, 0.0, 0.0), |
| 446 |
Vec4::new(0.0, 0.0, scale.z, 0.0), |
| 447 |
Vec4::W, |
| 448 |
) |
| 449 |
} |
| 450 |
|
| 451 |
/// Creates a 4x4 matrix from the first 16 values in `slice`. |
| 452 |
/// |
| 453 |
/// # Panics |
| 454 |
/// |
| 455 |
/// Panics if `slice` is less than 16 elements long. |
| 456 |
#[inline] |
| 457 |
pub const fn from_cols_slice(slice: &[f32]) -> Self { |
| 458 |
Self::new( |
| 459 |
slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7], |
| 460 |
slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], slice[15], |
| 461 |
) |
| 462 |
} |
| 463 |
|
| 464 |
/// Writes the columns of `self` to the first 16 elements in `slice`. |
| 465 |
/// |
| 466 |
/// # Panics |
| 467 |
/// |
| 468 |
/// Panics if `slice` is less than 16 elements long. |
| 469 |
#[inline] |
| 470 |
pub fn write_cols_to_slice(self, slice: &mut [f32]) { |
| 471 |
slice[0] = self.x_axis.x; |
| 472 |
slice[1] = self.x_axis.y; |
| 473 |
slice[2] = self.x_axis.z; |
| 474 |
slice[3] = self.x_axis.w; |
| 475 |
slice[4] = self.y_axis.x; |
| 476 |
slice[5] = self.y_axis.y; |
| 477 |
slice[6] = self.y_axis.z; |
| 478 |
slice[7] = self.y_axis.w; |
| 479 |
slice[8] = self.z_axis.x; |
| 480 |
slice[9] = self.z_axis.y; |
| 481 |
slice[10] = self.z_axis.z; |
| 482 |
slice[11] = self.z_axis.w; |
| 483 |
slice[12] = self.w_axis.x; |
| 484 |
slice[13] = self.w_axis.y; |
| 485 |
slice[14] = self.w_axis.z; |
| 486 |
slice[15] = self.w_axis.w; |
| 487 |
} |
| 488 |
|
| 489 |
/// Returns the matrix column for the given `index`. |
| 490 |
/// |
| 491 |
/// # Panics |
| 492 |
/// |
| 493 |
/// Panics if `index` is greater than 3. |
| 494 |
#[inline] |
| 495 |
pub fn col(&self, index: usize) -> Vec4 { |
| 496 |
match index { |
| 497 |
0 => self.x_axis, |
| 498 |
1 => self.y_axis, |
| 499 |
2 => self.z_axis, |
| 500 |
3 => self.w_axis, |
| 501 |
_ => panic!("index out of bounds"), |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/// Returns a mutable reference to the matrix column for the given `index`. |
| 506 |
/// |
| 507 |
/// # Panics |
| 508 |
/// |
| 509 |
/// Panics if `index` is greater than 3. |
| 510 |
#[inline] |
| 511 |
pub fn col_mut(&mut self, index: usize) -> &mut Vec4 { |
| 512 |
match index { |
| 513 |
0 => &mut self.x_axis, |
| 514 |
1 => &mut self.y_axis, |
| 515 |
2 => &mut self.z_axis, |
| 516 |
3 => &mut self.w_axis, |
| 517 |
_ => panic!("index out of bounds"), |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/// Returns the matrix row for the given `index`. |
| 522 |
/// |
| 523 |
/// # Panics |
| 524 |
/// |
| 525 |
/// Panics if `index` is greater than 3. |
| 526 |
#[inline] |
| 527 |
pub fn row(&self, index: usize) -> Vec4 { |
| 528 |
match index { |
| 529 |
0 => Vec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x), |
| 530 |
1 => Vec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y), |
| 531 |
2 => Vec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z), |
| 532 |
3 => Vec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w), |
| 533 |
_ => panic!("index out of bounds"), |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/// Returns `true` if, and only if, all elements are finite. |
| 538 |
/// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 539 |
#[inline] |
| 540 |
pub fn is_finite(&self) -> bool { |
| 541 |
self.x_axis.is_finite() |
| 542 |
&& self.y_axis.is_finite() |
| 543 |
&& self.z_axis.is_finite() |
| 544 |
&& self.w_axis.is_finite() |
| 545 |
} |
| 546 |
|
| 547 |
/// Returns `true` if any elements are `NaN`. |
| 548 |
#[inline] |
| 549 |
pub fn is_nan(&self) -> bool { |
| 550 |
self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() || self.w_axis.is_nan() |
| 551 |
} |
| 552 |
|
| 553 |
/// Returns the transpose of `self`. |
| 554 |
#[must_use] |
| 555 |
#[inline] |
| 556 |
pub fn transpose(&self) -> Self { |
| 557 |
Self { |
| 558 |
x_axis: Vec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x), |
| 559 |
y_axis: Vec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y), |
| 560 |
z_axis: Vec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z), |
| 561 |
w_axis: Vec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w), |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/// Returns the determinant of `self`. |
| 566 |
pub fn determinant(&self) -> f32 { |
| 567 |
let (m00, m01, m02, m03) = self.x_axis.into(); |
| 568 |
let (m10, m11, m12, m13) = self.y_axis.into(); |
| 569 |
let (m20, m21, m22, m23) = self.z_axis.into(); |
| 570 |
let (m30, m31, m32, m33) = self.w_axis.into(); |
| 571 |
|
| 572 |
let a2323 = m22 * m33 - m23 * m32; |
| 573 |
let a1323 = m21 * m33 - m23 * m31; |
| 574 |
let a1223 = m21 * m32 - m22 * m31; |
| 575 |
let a0323 = m20 * m33 - m23 * m30; |
| 576 |
let a0223 = m20 * m32 - m22 * m30; |
| 577 |
let a0123 = m20 * m31 - m21 * m30; |
| 578 |
|
| 579 |
m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223) |
| 580 |
- m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223) |
| 581 |
+ m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123) |
| 582 |
- m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123) |
| 583 |
} |
| 584 |
|
| 585 |
/// Returns the inverse of `self`. |
| 586 |
/// |
| 587 |
/// If the matrix is not invertible the returned matrix will be invalid. |
| 588 |
/// |
| 589 |
/// # Panics |
| 590 |
/// |
| 591 |
/// Will panic if the determinant of `self` is zero when `glam_assert` is enabled. |
| 592 |
#[must_use] |
| 593 |
pub fn inverse(&self) -> Self { |
| 594 |
let (m00, m01, m02, m03) = self.x_axis.into(); |
| 595 |
let (m10, m11, m12, m13) = self.y_axis.into(); |
| 596 |
let (m20, m21, m22, m23) = self.z_axis.into(); |
| 597 |
let (m30, m31, m32, m33) = self.w_axis.into(); |
| 598 |
|
| 599 |
let coef00 = m22 * m33 - m32 * m23; |
| 600 |
let coef02 = m12 * m33 - m32 * m13; |
| 601 |
let coef03 = m12 * m23 - m22 * m13; |
| 602 |
|
| 603 |
let coef04 = m21 * m33 - m31 * m23; |
| 604 |
let coef06 = m11 * m33 - m31 * m13; |
| 605 |
let coef07 = m11 * m23 - m21 * m13; |
| 606 |
|
| 607 |
let coef08 = m21 * m32 - m31 * m22; |
| 608 |
let coef10 = m11 * m32 - m31 * m12; |
| 609 |
let coef11 = m11 * m22 - m21 * m12; |
| 610 |
|
| 611 |
let coef12 = m20 * m33 - m30 * m23; |
| 612 |
let coef14 = m10 * m33 - m30 * m13; |
| 613 |
let coef15 = m10 * m23 - m20 * m13; |
| 614 |
|
| 615 |
let coef16 = m20 * m32 - m30 * m22; |
| 616 |
let coef18 = m10 * m32 - m30 * m12; |
| 617 |
let coef19 = m10 * m22 - m20 * m12; |
| 618 |
|
| 619 |
let coef20 = m20 * m31 - m30 * m21; |
| 620 |
let coef22 = m10 * m31 - m30 * m11; |
| 621 |
let coef23 = m10 * m21 - m20 * m11; |
| 622 |
|
| 623 |
let fac0 = Vec4::new(coef00, coef00, coef02, coef03); |
| 624 |
let fac1 = Vec4::new(coef04, coef04, coef06, coef07); |
| 625 |
let fac2 = Vec4::new(coef08, coef08, coef10, coef11); |
| 626 |
let fac3 = Vec4::new(coef12, coef12, coef14, coef15); |
| 627 |
let fac4 = Vec4::new(coef16, coef16, coef18, coef19); |
| 628 |
let fac5 = Vec4::new(coef20, coef20, coef22, coef23); |
| 629 |
|
| 630 |
let vec0 = Vec4::new(m10, m00, m00, m00); |
| 631 |
let vec1 = Vec4::new(m11, m01, m01, m01); |
| 632 |
let vec2 = Vec4::new(m12, m02, m02, m02); |
| 633 |
let vec3 = Vec4::new(m13, m03, m03, m03); |
| 634 |
|
| 635 |
let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2)); |
| 636 |
let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4)); |
| 637 |
let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5)); |
| 638 |
let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5)); |
| 639 |
|
| 640 |
let sign_a = Vec4::new(1.0, -1.0, 1.0, -1.0); |
| 641 |
let sign_b = Vec4::new(-1.0, 1.0, -1.0, 1.0); |
| 642 |
|
| 643 |
let inverse = Self::from_cols( |
| 644 |
inv0.mul(sign_a), |
| 645 |
inv1.mul(sign_b), |
| 646 |
inv2.mul(sign_a), |
| 647 |
inv3.mul(sign_b), |
| 648 |
); |
| 649 |
|
| 650 |
let col0 = Vec4::new( |
| 651 |
inverse.x_axis.x, |
| 652 |
inverse.y_axis.x, |
| 653 |
inverse.z_axis.x, |
| 654 |
inverse.w_axis.x, |
| 655 |
); |
| 656 |
|
| 657 |
let dot0 = self.x_axis.mul(col0); |
| 658 |
let dot1 = dot0.x + dot0.y + dot0.z + dot0.w; |
| 659 |
|
| 660 |
glam_assert!(dot1 != 0.0); |
| 661 |
|
| 662 |
let rcp_det = dot1.recip(); |
| 663 |
inverse.mul(rcp_det) |
| 664 |
} |
| 665 |
|
| 666 |
/// Creates a left-handed view matrix using a camera position, an up direction, and a facing |
| 667 |
/// direction. |
| 668 |
/// |
| 669 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 670 |
#[inline] |
| 671 |
pub fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self { |
| 672 |
Self::look_to_rh(eye, -dir, up) |
| 673 |
} |
| 674 |
|
| 675 |
/// Creates a right-handed view matrix using a camera position, an up direction, and a facing |
| 676 |
/// direction. |
| 677 |
/// |
| 678 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 679 |
#[inline] |
| 680 |
pub fn look_to_rh(eye: Vec3, dir: Vec3, up: Vec3) -> Self { |
| 681 |
let f = dir.normalize(); |
| 682 |
let s = f.cross(up).normalize(); |
| 683 |
let u = s.cross(f); |
| 684 |
|
| 685 |
Self::from_cols( |
| 686 |
Vec4::new(s.x, u.x, -f.x, 0.0), |
| 687 |
Vec4::new(s.y, u.y, -f.y, 0.0), |
| 688 |
Vec4::new(s.z, u.z, -f.z, 0.0), |
| 689 |
Vec4::new(-eye.dot(s), -eye.dot(u), eye.dot(f), 1.0), |
| 690 |
) |
| 691 |
} |
| 692 |
|
| 693 |
/// Creates a left-handed view matrix using a camera position, an up direction, and a focal |
| 694 |
/// point. |
| 695 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 696 |
/// |
| 697 |
/// # Panics |
| 698 |
/// |
| 699 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 700 |
#[inline] |
| 701 |
pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self { |
| 702 |
glam_assert!(up.is_normalized()); |
| 703 |
Self::look_to_lh(eye, center.sub(eye), up) |
| 704 |
} |
| 705 |
|
| 706 |
/// Creates a right-handed view matrix using a camera position, an up direction, and a focal |
| 707 |
/// point. |
| 708 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 709 |
/// |
| 710 |
/// # Panics |
| 711 |
/// |
| 712 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 713 |
#[inline] |
| 714 |
pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self { |
| 715 |
glam_assert!(up.is_normalized()); |
| 716 |
Self::look_to_rh(eye, center.sub(eye), up) |
| 717 |
} |
| 718 |
|
| 719 |
/// Creates a right-handed perspective projection matrix with [-1,1] depth range. |
| 720 |
/// This is the same as the OpenGL `gluPerspective` function. |
| 721 |
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml> |
| 722 |
#[inline] |
| 723 |
pub fn perspective_rh_gl( |
| 724 |
fov_y_radians: f32, |
| 725 |
aspect_ratio: f32, |
| 726 |
z_near: f32, |
| 727 |
z_far: f32, |
| 728 |
) -> Self { |
| 729 |
let inv_length = 1.0 / (z_near - z_far); |
| 730 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 731 |
let a = f / aspect_ratio; |
| 732 |
let b = (z_near + z_far) * inv_length; |
| 733 |
let c = (2.0 * z_near * z_far) * inv_length; |
| 734 |
Self::from_cols( |
| 735 |
Vec4::new(a, 0.0, 0.0, 0.0), |
| 736 |
Vec4::new(0.0, f, 0.0, 0.0), |
| 737 |
Vec4::new(0.0, 0.0, b, -1.0), |
| 738 |
Vec4::new(0.0, 0.0, c, 0.0), |
| 739 |
) |
| 740 |
} |
| 741 |
|
| 742 |
/// Creates a left-handed perspective projection matrix with `[0,1]` depth range. |
| 743 |
/// |
| 744 |
/// # Panics |
| 745 |
/// |
| 746 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 747 |
/// enabled. |
| 748 |
#[inline] |
| 749 |
pub fn perspective_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self { |
| 750 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 751 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 752 |
let h = cos_fov / sin_fov; |
| 753 |
let w = h / aspect_ratio; |
| 754 |
let r = z_far / (z_far - z_near); |
| 755 |
Self::from_cols( |
| 756 |
Vec4::new(w, 0.0, 0.0, 0.0), |
| 757 |
Vec4::new(0.0, h, 0.0, 0.0), |
| 758 |
Vec4::new(0.0, 0.0, r, 1.0), |
| 759 |
Vec4::new(0.0, 0.0, -r * z_near, 0.0), |
| 760 |
) |
| 761 |
} |
| 762 |
|
| 763 |
/// Creates a right-handed perspective projection matrix with `[0,1]` depth range. |
| 764 |
/// |
| 765 |
/// # Panics |
| 766 |
/// |
| 767 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 768 |
/// enabled. |
| 769 |
#[inline] |
| 770 |
pub fn perspective_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Self { |
| 771 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 772 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 773 |
let h = cos_fov / sin_fov; |
| 774 |
let w = h / aspect_ratio; |
| 775 |
let r = z_far / (z_near - z_far); |
| 776 |
Self::from_cols( |
| 777 |
Vec4::new(w, 0.0, 0.0, 0.0), |
| 778 |
Vec4::new(0.0, h, 0.0, 0.0), |
| 779 |
Vec4::new(0.0, 0.0, r, -1.0), |
| 780 |
Vec4::new(0.0, 0.0, r * z_near, 0.0), |
| 781 |
) |
| 782 |
} |
| 783 |
|
| 784 |
/// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range. |
| 785 |
/// |
| 786 |
/// # Panics |
| 787 |
/// |
| 788 |
/// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 789 |
#[inline] |
| 790 |
pub fn perspective_infinite_lh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self { |
| 791 |
glam_assert!(z_near > 0.0); |
| 792 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 793 |
let h = cos_fov / sin_fov; |
| 794 |
let w = h / aspect_ratio; |
| 795 |
Self::from_cols( |
| 796 |
Vec4::new(w, 0.0, 0.0, 0.0), |
| 797 |
Vec4::new(0.0, h, 0.0, 0.0), |
| 798 |
Vec4::new(0.0, 0.0, 1.0, 1.0), |
| 799 |
Vec4::new(0.0, 0.0, -z_near, 0.0), |
| 800 |
) |
| 801 |
} |
| 802 |
|
| 803 |
/// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range. |
| 804 |
/// |
| 805 |
/// # Panics |
| 806 |
/// |
| 807 |
/// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 808 |
#[inline] |
| 809 |
pub fn perspective_infinite_reverse_lh( |
| 810 |
fov_y_radians: f32, |
| 811 |
aspect_ratio: f32, |
| 812 |
z_near: f32, |
| 813 |
) -> Self { |
| 814 |
glam_assert!(z_near > 0.0); |
| 815 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 816 |
let h = cos_fov / sin_fov; |
| 817 |
let w = h / aspect_ratio; |
| 818 |
Self::from_cols( |
| 819 |
Vec4::new(w, 0.0, 0.0, 0.0), |
| 820 |
Vec4::new(0.0, h, 0.0, 0.0), |
| 821 |
Vec4::new(0.0, 0.0, 0.0, 1.0), |
| 822 |
Vec4::new(0.0, 0.0, z_near, 0.0), |
| 823 |
) |
| 824 |
} |
| 825 |
|
| 826 |
/// Creates an infinite right-handed perspective projection matrix with |
| 827 |
/// `[0,1]` depth range. |
| 828 |
#[inline] |
| 829 |
pub fn perspective_infinite_rh(fov_y_radians: f32, aspect_ratio: f32, z_near: f32) -> Self { |
| 830 |
glam_assert!(z_near > 0.0); |
| 831 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 832 |
Self::from_cols( |
| 833 |
Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 834 |
Vec4::new(0.0, f, 0.0, 0.0), |
| 835 |
Vec4::new(0.0, 0.0, -1.0, -1.0), |
| 836 |
Vec4::new(0.0, 0.0, -z_near, 0.0), |
| 837 |
) |
| 838 |
} |
| 839 |
|
| 840 |
/// Creates an infinite reverse right-handed perspective projection matrix |
| 841 |
/// with `[0,1]` depth range. |
| 842 |
#[inline] |
| 843 |
pub fn perspective_infinite_reverse_rh( |
| 844 |
fov_y_radians: f32, |
| 845 |
aspect_ratio: f32, |
| 846 |
z_near: f32, |
| 847 |
) -> Self { |
| 848 |
glam_assert!(z_near > 0.0); |
| 849 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 850 |
Self::from_cols( |
| 851 |
Vec4::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 852 |
Vec4::new(0.0, f, 0.0, 0.0), |
| 853 |
Vec4::new(0.0, 0.0, 0.0, -1.0), |
| 854 |
Vec4::new(0.0, 0.0, z_near, 0.0), |
| 855 |
) |
| 856 |
} |
| 857 |
|
| 858 |
/// Creates a right-handed orthographic projection matrix with `[-1,1]` depth |
| 859 |
/// range. This is the same as the OpenGL `glOrtho` function in OpenGL. |
| 860 |
/// See |
| 861 |
/// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml> |
| 862 |
#[inline] |
| 863 |
pub fn orthographic_rh_gl( |
| 864 |
left: f32, |
| 865 |
right: f32, |
| 866 |
bottom: f32, |
| 867 |
top: f32, |
| 868 |
near: f32, |
| 869 |
far: f32, |
| 870 |
) -> Self { |
| 871 |
let a = 2.0 / (right - left); |
| 872 |
let b = 2.0 / (top - bottom); |
| 873 |
let c = -2.0 / (far - near); |
| 874 |
let tx = -(right + left) / (right - left); |
| 875 |
let ty = -(top + bottom) / (top - bottom); |
| 876 |
let tz = -(far + near) / (far - near); |
| 877 |
|
| 878 |
Self::from_cols( |
| 879 |
Vec4::new(a, 0.0, 0.0, 0.0), |
| 880 |
Vec4::new(0.0, b, 0.0, 0.0), |
| 881 |
Vec4::new(0.0, 0.0, c, 0.0), |
| 882 |
Vec4::new(tx, ty, tz, 1.0), |
| 883 |
) |
| 884 |
} |
| 885 |
|
| 886 |
/// Creates a left-handed orthographic projection matrix with `[0,1]` depth range. |
| 887 |
#[inline] |
| 888 |
pub fn orthographic_lh( |
| 889 |
left: f32, |
| 890 |
right: f32, |
| 891 |
bottom: f32, |
| 892 |
top: f32, |
| 893 |
near: f32, |
| 894 |
far: f32, |
| 895 |
) -> Self { |
| 896 |
let rcp_width = 1.0 / (right - left); |
| 897 |
let rcp_height = 1.0 / (top - bottom); |
| 898 |
let r = 1.0 / (far - near); |
| 899 |
Self::from_cols( |
| 900 |
Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 901 |
Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 902 |
Vec4::new(0.0, 0.0, r, 0.0), |
| 903 |
Vec4::new( |
| 904 |
-(left + right) * rcp_width, |
| 905 |
-(top + bottom) * rcp_height, |
| 906 |
-r * near, |
| 907 |
1.0, |
| 908 |
), |
| 909 |
) |
| 910 |
} |
| 911 |
|
| 912 |
/// Creates a right-handed orthographic projection matrix with `[0,1]` depth range. |
| 913 |
#[inline] |
| 914 |
pub fn orthographic_rh( |
| 915 |
left: f32, |
| 916 |
right: f32, |
| 917 |
bottom: f32, |
| 918 |
top: f32, |
| 919 |
near: f32, |
| 920 |
far: f32, |
| 921 |
) -> Self { |
| 922 |
let rcp_width = 1.0 / (right - left); |
| 923 |
let rcp_height = 1.0 / (top - bottom); |
| 924 |
let r = 1.0 / (near - far); |
| 925 |
Self::from_cols( |
| 926 |
Vec4::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 927 |
Vec4::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 928 |
Vec4::new(0.0, 0.0, r, 0.0), |
| 929 |
Vec4::new( |
| 930 |
-(left + right) * rcp_width, |
| 931 |
-(top + bottom) * rcp_height, |
| 932 |
r * near, |
| 933 |
1.0, |
| 934 |
), |
| 935 |
) |
| 936 |
} |
| 937 |
|
| 938 |
/// Transforms the given 3D vector as a point, applying perspective correction. |
| 939 |
/// |
| 940 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`. |
| 941 |
/// The perspective divide is performed meaning the resulting 3D vector is divided by `w`. |
| 942 |
/// |
| 943 |
/// This method assumes that `self` contains a projective transform. |
| 944 |
#[inline] |
| 945 |
pub fn project_point3(&self, rhs: Vec3) -> Vec3 { |
| 946 |
let mut res = self.x_axis.mul(rhs.x); |
| 947 |
res = self.y_axis.mul(rhs.y).add(res); |
| 948 |
res = self.z_axis.mul(rhs.z).add(res); |
| 949 |
res = self.w_axis.add(res); |
| 950 |
res = res.mul(res.wwww().recip()); |
| 951 |
res.xyz() |
| 952 |
} |
| 953 |
|
| 954 |
/// Transforms the given 3D vector as a point. |
| 955 |
/// |
| 956 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 957 |
/// `1.0`. |
| 958 |
/// |
| 959 |
/// This method assumes that `self` contains a valid affine transform. It does not perform |
| 960 |
/// a persective divide, if `self` contains a perspective transform, or if you are unsure, |
| 961 |
/// the [`Self::project_point3()`] method should be used instead. |
| 962 |
/// |
| 963 |
/// # Panics |
| 964 |
/// |
| 965 |
/// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 966 |
#[inline] |
| 967 |
pub fn transform_point3(&self, rhs: Vec3) -> Vec3 { |
| 968 |
glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6)); |
| 969 |
let mut res = self.x_axis.mul(rhs.x); |
| 970 |
res = self.y_axis.mul(rhs.y).add(res); |
| 971 |
res = self.z_axis.mul(rhs.z).add(res); |
| 972 |
res = self.w_axis.add(res); |
| 973 |
res.xyz() |
| 974 |
} |
| 975 |
|
| 976 |
/// Transforms the give 3D vector as a direction. |
| 977 |
/// |
| 978 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 979 |
/// `0.0`. |
| 980 |
/// |
| 981 |
/// This method assumes that `self` contains a valid affine transform. |
| 982 |
/// |
| 983 |
/// # Panics |
| 984 |
/// |
| 985 |
/// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 986 |
#[inline] |
| 987 |
pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 { |
| 988 |
glam_assert!(self.row(3).abs_diff_eq(Vec4::W, 1e-6)); |
| 989 |
let mut res = self.x_axis.mul(rhs.x); |
| 990 |
res = self.y_axis.mul(rhs.y).add(res); |
| 991 |
res = self.z_axis.mul(rhs.z).add(res); |
| 992 |
res.xyz() |
| 993 |
} |
| 994 |
|
| 995 |
/// Transforms the given [`Vec3A`] as 3D point. |
| 996 |
/// |
| 997 |
/// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `1.0`. |
| 998 |
#[inline] |
| 999 |
pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A { |
| 1000 |
self.transform_point3(rhs.into()).into() |
| 1001 |
} |
| 1002 |
|
| 1003 |
/// Transforms the give [`Vec3A`] as 3D vector. |
| 1004 |
/// |
| 1005 |
/// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `0.0`. |
| 1006 |
#[inline] |
| 1007 |
pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A { |
| 1008 |
self.transform_vector3(rhs.into()).into() |
| 1009 |
} |
| 1010 |
|
| 1011 |
/// Transforms a 4D vector. |
| 1012 |
#[inline] |
| 1013 |
pub fn mul_vec4(&self, rhs: Vec4) -> Vec4 { |
| 1014 |
let mut res = self.x_axis.mul(rhs.x); |
| 1015 |
res = res.add(self.y_axis.mul(rhs.y)); |
| 1016 |
res = res.add(self.z_axis.mul(rhs.z)); |
| 1017 |
res = res.add(self.w_axis.mul(rhs.w)); |
| 1018 |
res |
| 1019 |
} |
| 1020 |
|
| 1021 |
/// Multiplies two 4x4 matrices. |
| 1022 |
#[inline] |
| 1023 |
pub fn mul_mat4(&self, rhs: &Self) -> Self { |
| 1024 |
Self::from_cols( |
| 1025 |
self.mul(rhs.x_axis), |
| 1026 |
self.mul(rhs.y_axis), |
| 1027 |
self.mul(rhs.z_axis), |
| 1028 |
self.mul(rhs.w_axis), |
| 1029 |
) |
| 1030 |
} |
| 1031 |
|
| 1032 |
/// Adds two 4x4 matrices. |
| 1033 |
#[inline] |
| 1034 |
pub fn add_mat4(&self, rhs: &Self) -> Self { |
| 1035 |
Self::from_cols( |
| 1036 |
self.x_axis.add(rhs.x_axis), |
| 1037 |
self.y_axis.add(rhs.y_axis), |
| 1038 |
self.z_axis.add(rhs.z_axis), |
| 1039 |
self.w_axis.add(rhs.w_axis), |
| 1040 |
) |
| 1041 |
} |
| 1042 |
|
| 1043 |
/// Subtracts two 4x4 matrices. |
| 1044 |
#[inline] |
| 1045 |
pub fn sub_mat4(&self, rhs: &Self) -> Self { |
| 1046 |
Self::from_cols( |
| 1047 |
self.x_axis.sub(rhs.x_axis), |
| 1048 |
self.y_axis.sub(rhs.y_axis), |
| 1049 |
self.z_axis.sub(rhs.z_axis), |
| 1050 |
self.w_axis.sub(rhs.w_axis), |
| 1051 |
) |
| 1052 |
} |
| 1053 |
|
| 1054 |
/// Multiplies a 4x4 matrix by a scalar. |
| 1055 |
#[inline] |
| 1056 |
pub fn mul_scalar(&self, rhs: f32) -> Self { |
| 1057 |
Self::from_cols( |
| 1058 |
self.x_axis.mul(rhs), |
| 1059 |
self.y_axis.mul(rhs), |
| 1060 |
self.z_axis.mul(rhs), |
| 1061 |
self.w_axis.mul(rhs), |
| 1062 |
) |
| 1063 |
} |
| 1064 |
|
| 1065 |
/// Returns true if the absolute difference of all elements between `self` and `rhs` |
| 1066 |
/// is less than or equal to `max_abs_diff`. |
| 1067 |
/// |
| 1068 |
/// This can be used to compare if two matrices contain similar elements. It works best |
| 1069 |
/// when comparing with a known value. The `max_abs_diff` that should be used used |
| 1070 |
/// depends on the values being compared against. |
| 1071 |
/// |
| 1072 |
/// For more see |
| 1073 |
/// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 1074 |
#[inline] |
| 1075 |
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool { |
| 1076 |
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff) |
| 1077 |
&& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff) |
| 1078 |
&& self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff) |
| 1079 |
&& self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff) |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[inline] |
| 1083 |
pub fn as_dmat4(&self) -> DMat4 { |
| 1084 |
DMat4::from_cols( |
| 1085 |
self.x_axis.as_dvec4(), |
| 1086 |
self.y_axis.as_dvec4(), |
| 1087 |
self.z_axis.as_dvec4(), |
| 1088 |
self.w_axis.as_dvec4(), |
| 1089 |
) |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
impl Default for Mat4 { |
| 1094 |
#[inline] |
| 1095 |
fn default() -> Self { |
| 1096 |
Self::IDENTITY |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
impl Add<Mat4> for Mat4 { |
| 1101 |
type Output = Self; |
| 1102 |
#[inline] |
| 1103 |
fn add(self, rhs: Self) -> Self::Output { |
| 1104 |
self.add_mat4(&rhs) |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
impl AddAssign<Mat4> for Mat4 { |
| 1109 |
#[inline] |
| 1110 |
fn add_assign(&mut self, rhs: Self) { |
| 1111 |
*self = self.add_mat4(&rhs); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
impl Sub<Mat4> for Mat4 { |
| 1116 |
type Output = Self; |
| 1117 |
#[inline] |
| 1118 |
fn sub(self, rhs: Self) -> Self::Output { |
| 1119 |
self.sub_mat4(&rhs) |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
impl SubAssign<Mat4> for Mat4 { |
| 1124 |
#[inline] |
| 1125 |
fn sub_assign(&mut self, rhs: Self) { |
| 1126 |
*self = self.sub_mat4(&rhs); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
impl Neg for Mat4 { |
| 1131 |
type Output = Self; |
| 1132 |
#[inline] |
| 1133 |
fn neg(self) -> Self::Output { |
| 1134 |
Self::from_cols( |
| 1135 |
self.x_axis.neg(), |
| 1136 |
self.y_axis.neg(), |
| 1137 |
self.z_axis.neg(), |
| 1138 |
self.w_axis.neg(), |
| 1139 |
) |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
impl Mul<Mat4> for Mat4 { |
| 1144 |
type Output = Self; |
| 1145 |
#[inline] |
| 1146 |
fn mul(self, rhs: Self) -> Self::Output { |
| 1147 |
self.mul_mat4(&rhs) |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
impl MulAssign<Mat4> for Mat4 { |
| 1152 |
#[inline] |
| 1153 |
fn mul_assign(&mut self, rhs: Self) { |
| 1154 |
*self = self.mul_mat4(&rhs); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
impl Mul<Vec4> for Mat4 { |
| 1159 |
type Output = Vec4; |
| 1160 |
#[inline] |
| 1161 |
fn mul(self, rhs: Vec4) -> Self::Output { |
| 1162 |
self.mul_vec4(rhs) |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
impl Mul<Mat4> for f32 { |
| 1167 |
type Output = Mat4; |
| 1168 |
#[inline] |
| 1169 |
fn mul(self, rhs: Mat4) -> Self::Output { |
| 1170 |
rhs.mul_scalar(self) |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
impl Mul<f32> for Mat4 { |
| 1175 |
type Output = Self; |
| 1176 |
#[inline] |
| 1177 |
fn mul(self, rhs: f32) -> Self::Output { |
| 1178 |
self.mul_scalar(rhs) |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
impl MulAssign<f32> for Mat4 { |
| 1183 |
#[inline] |
| 1184 |
fn mul_assign(&mut self, rhs: f32) { |
| 1185 |
*self = self.mul_scalar(rhs); |
| 1186 |
} |
| 1187 |
} |
| 1188 |
|
| 1189 |
impl Sum<Self> for Mat4 { |
| 1190 |
fn sum<I>(iter: I) -> Self |
| 1191 |
where |
| 1192 |
I: Iterator<Item = Self>, |
| 1193 |
{ |
| 1194 |
iter.fold(Self::ZERO, Self::add) |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
impl<'a> Sum<&'a Self> for Mat4 { |
| 1199 |
fn sum<I>(iter: I) -> Self |
| 1200 |
where |
| 1201 |
I: Iterator<Item = &'a Self>, |
| 1202 |
{ |
| 1203 |
iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
impl Product for Mat4 { |
| 1208 |
fn product<I>(iter: I) -> Self |
| 1209 |
where |
| 1210 |
I: Iterator<Item = Self>, |
| 1211 |
{ |
| 1212 |
iter.fold(Self::IDENTITY, Self::mul) |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
impl<'a> Product<&'a Self> for Mat4 { |
| 1217 |
fn product<I>(iter: I) -> Self |
| 1218 |
where |
| 1219 |
I: Iterator<Item = &'a Self>, |
| 1220 |
{ |
| 1221 |
iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b)) |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
impl PartialEq for Mat4 { |
| 1226 |
#[inline] |
| 1227 |
fn eq(&self, rhs: &Self) -> bool { |
| 1228 |
self.x_axis.eq(&rhs.x_axis) |
| 1229 |
&& self.y_axis.eq(&rhs.y_axis) |
| 1230 |
&& self.z_axis.eq(&rhs.z_axis) |
| 1231 |
&& self.w_axis.eq(&rhs.w_axis) |
| 1232 |
} |
| 1233 |
} |
| 1234 |
|
| 1235 |
#[cfg(not(target_arch = "spirv"))] |
| 1236 |
impl AsRef<[f32; 16]> for Mat4 { |
| 1237 |
#[inline] |
| 1238 |
fn as_ref(&self) -> &[f32; 16] { |
| 1239 |
unsafe { &*(self as *const Self as *const [f32; 16]) } |
| 1240 |
} |
| 1241 |
} |
| 1242 |
|
| 1243 |
#[cfg(not(target_arch = "spirv"))] |
| 1244 |
impl AsMut<[f32; 16]> for Mat4 { |
| 1245 |
#[inline] |
| 1246 |
fn as_mut(&mut self) -> &mut [f32; 16] { |
| 1247 |
unsafe { &mut *(self as *mut Self as *mut [f32; 16]) } |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
#[cfg(not(target_arch = "spirv"))] |
| 1252 |
impl fmt::Debug for Mat4 { |
| 1253 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1254 |
fmt.debug_struct(stringify!(Mat4)) |
| 1255 |
.field("x_axis", &self.x_axis) |
| 1256 |
.field("y_axis", &self.y_axis) |
| 1257 |
.field("z_axis", &self.z_axis) |
| 1258 |
.field("w_axis", &self.w_axis) |
| 1259 |
.finish() |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
#[cfg(not(target_arch = "spirv"))] |
| 1264 |
impl fmt::Display for Mat4 { |
| 1265 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1266 |
write!( |
| 1267 |
f, |
| 1268 |
"[{}, {}, {}, {}]", |
| 1269 |
self.x_axis, self.y_axis, self.z_axis, self.w_axis |
| 1270 |
) |
| 1271 |
} |
| 1272 |
} |