25 vec2(
float _x,
float _y) :
x(_x),
y(_y) {}
29 return vec2(
x + other.
x,
y + other.
y);
33 return vec2(
x - other.
x,
y - other.
y);
37 return vec2(
x * scalar,
y * scalar);
41 float invScalar = 1.0f / scalar;
42 return vec2(
x * invScalar,
y * invScalar);
47 return x * other.
x +
y * other.
y;
52 return std::sqrt(
x *
x +
y *
y);
71 vec3(
float _x,
float _y,
float _z) :
x(_x),
y(_y),
z(_z) {}
72 vec3(
float value) :
x(value),
y(value),
z(value) {}
81 return vec3(
x + other.
x,
y + other.
y,
z + other.
z);
85 return vec3(
x - other.
x,
y - other.
y,
z - other.
z);
89 return vec3(
x * scalar,
y * scalar,
z * scalar);
93 float invScalar = 1.0f / scalar;
94 return vec3(
x * invScalar,
y * invScalar,
z * invScalar);
107 return vec3(v.
x * scalar, v.
y * scalar, v.
z * scalar);
133 throw std::out_of_range(
"Index out of range in vec3 subscript operator");
144 throw std::out_of_range(
"Index out of range in vec3 subscript operator");
149 return x * other.
x +
y * other.
y +
z * other.
z;
154 return vec3(
y * other.
z -
z * other.
y,
z * other.
x -
x * other.
z,
x * other.
y -
y * other.
x);
159 return std::sqrt(
x *
x +
y *
y +
z *
z);
178 for (
int i = 0; i < 4; ++i)
179 for (
int j = 0; j < 4; ++j)
180 elements[i][j] = (i == j) ? 1.0f : 0.0f;
185 for (
int i = 0; i < 4; ++i)
186 for (
int j = 0; j < 4; ++j)
197 if (
this != &other) {
215 for (
int i = 0; i < 4; ++i)
216 for (
int j = 0; j < 4; ++j)
217 result(i, j) =
elements[i][j] - other(i, j);
224 for (
int i = 0; i < 4; ++i)
225 for (
int j = 0; j < 4; ++j)
226 for (
int k = 0; k < 4; ++k)
227 result(i, j) +=
elements[i][k] * other(k, j);
242 for (
int i = 0; i < 4; ++i) {
243 for (
int j = 0; j < 4; ++j) {
244 os << m(i, j) <<
' ';
254 for (
int i = 0; i < 3; ++i)
255 for (
int j = 0; j < 3; ++j)
267 quat(
float _x,
float _y,
float _z,
float _w) :
x(_x),
y(_y),
z(_z),
w(_w) {}
272 w * other.
x +
x * other.
w +
y * other.
z -
z * other.
y,
273 w * other.
y +
y * other.
w +
z * other.
x -
x * other.
z,
274 w * other.
z +
z * other.
w +
x * other.
y -
y * other.
x,
275 w * other.
w -
x * other.
x -
y * other.
y -
z * other.
z
281 float len = std::sqrt(
x *
x +
y *
y +
z *
z +
w *
w);
283 return quat(
x / len,
y / len,
z / len,
w / len);
291 const float xy =
x *
y;
292 const float xz =
x *
z;
293 const float xw =
x *
w;
295 const float yy =
y *
y;
296 const float yz =
y *
z;
297 const float yw =
y *
w;
299 const float zz =
z *
z;
300 const float zw =
z *
w;
302 const float ww =
w *
w;
306 result[0][0] = 1 - 2 * (zz + ww);
307 result[0][1] = 2 * (yz - xw);
308 result[0][2] = 2 * (yw + xz);
310 result[1][0] = 2 * (yz + xw);
311 result[1][1] = 1 - 2 * (yy + ww);
312 result[1][2] = 2 * (zw - xy);
314 result[2][0] = 2 * (yw - xz);
315 result[2][1] = 2 * (zw + xy);
316 result[2][2] = 1 - 2 * (yy + zz);
325 return vec3(std::min(a.
x, b.
x), std::min(a.
y, b.
y), std::min(a.
z, b.
z));
329 return vec3(std::max(a.
x, b.
x), std::max(a.
y, b.
y), std::max(a.
z, b.
z));
333 return sqrt(v.
x * v.
x + v.
y * v.
y + v.
z * v.
z);
342 static float lerp(
float a,
float b,
float t) {
343 return a + t * (b - a);
347 return a + t * (b - a);
358 static float clamp(
float x,
float minVal,
float maxVal) {
361 }
else if (x > maxVal) {
369 mat4 result = matrix;
371 for (
int i = 0; i < 3; ++i) {
372 result(3, i) += translation[i];
379 float c = cos(angle);
380 float s = sin(angle);
389 rotationMatrix(0, 0) = t * x * x + c;
390 rotationMatrix(0, 1) = t * x * y + s * z;
391 rotationMatrix(0, 2) = t * x * z - s * y;
393 rotationMatrix(1, 0) = t * x * y - s * z;
394 rotationMatrix(1, 1) = t * y * y + c;
395 rotationMatrix(1, 2) = t * y * z + s * x;
397 rotationMatrix(2, 0) = t * x * z + s * y;
398 rotationMatrix(2, 1) = t * y * z - s * x;
399 rotationMatrix(2, 2) = t * z * z + c;
401 return rotationMatrix;
422 Result[3][0] = -s.
dot(eye);
423 Result[3][1] = -u.
dot(eye);
424 Result[3][2] = f.
dot(eye);
430 float tanHalfFov = tan(fov * 0.5f);
434 projMatrix(0, 0) = 1.0f / (aspectRatio * tanHalfFov);
435 projMatrix(1, 1) = 1.0f / tanHalfFov;
436 projMatrix(2, 2) = -(far + near) / (far - near);
437 projMatrix(2, 3) = -1.0f;
438 projMatrix(3, 2) = -(2.0f * far * near) / (far - near);
440#ifdef MATH_FORCE_DEPTH_ZERO_TO_ONE
442 projMatrix(2, 2) = -2.0f / (far - near);
443 projMatrix(2, 3) = -(far + near) / (far - near);
450 return vec3(a.
y * b.
z - a.
z * b.
y, a.
z * b.
x - a.
x * b.
z, a.
x * b.
y - a.
y * b.
x);
454 float halfAngle = angle * 0.5f;
455 float sinHalf = std::sin(halfAngle);
456 return quat(std::cos(halfAngle), axis.
x * sinHalf, axis.
y * sinHalf, axis.
z * sinHalf).
normalize();
mat4 & operator=(const mat4 &other)
friend std::ostream & operator<<(std::ostream &os, const mat4 &m)
float * operator[](int index)
vec3 operator*(const vec3 &v) const
float & operator()(int row, int col)
mat4 operator-(const mat4 &other) const
const float * operator[](int index) const
const float & operator()(int row, int col) const
mat4 operator*(const mat4 &other) const
static mat4 rotate(float angle, const vec3 &axis)
static float length(vec3 v)
static mat4 translate(const mat4 &matrix, const vec3 &translation)
static vec3 max(const vec3 &a, const vec3 &b)
static vec3 normalize(const vec3 &v)
static float degrees(float radians)
static mat4 lookAt(const vec3 &eye, const vec3 ¢er, const vec3 &up)
static float lerp(float a, float b, float t)
static float radians(float degrees)
static quat angleAxis(float angle, const vec3 &axis)
static vec3 min(const vec3 &a, const vec3 &b)
static mat4 toMat4(const quat &q)
static vec3 lerp(const vec3 &a, const vec3 &b, float t)
static mat4 perspective(float fov, float aspectRatio, float near, float far)
static float clamp(float x, float minVal, float maxVal)
static vec3 cross(const vec3 &a, const vec3 &b)
quat(float _x, float _y, float _z, float _w)
quat operator*(const quat &other) const
vec2 operator+(const vec2 &other) const
vec2 operator*(float scalar) const
vec2 operator-(const vec2 &other) const
vec2 operator/(float scalar) const
float dot(const vec2 &other) const
vec3 & operator/=(float scalar)
vec3 operator+(const vec3 &other) const
vec3(float _x, float _y, float _z)
vec3 operator/(float scalar) const
float dot(const vec3 &other) const
vec3 cross(const vec3 &other) const
float & operator[](int index)
vec3 & operator+=(const vec3 &other)
vec3 operator*(float scalar) const
vec3 & operator-=(const vec3 &other)
const float & operator[](int index) const
friend vec3 operator*(float scalar, const vec3 &v)
vec3 operator-(const vec3 &other) const