CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Math/lin/matrix4x4.h
Views: 1401
#ifndef _MATH_LIN_MATRIX4X4_H1#define _MATH_LIN_MATRIX4X4_H23#include "Common/Math/lin/vec3.h"45namespace Lin {67class Quaternion;89class Matrix4x4 {10public:11union {12struct {13float xx, xy, xz, xw;14float yx, yy, yz, yw;15float zx, zy, zz, zw;16float wx, wy, wz, ww;17};18float m[16];19};2021const Vec3 right() const {return Vec3(xx, xy, xz);}22const Vec3 up() const {return Vec3(yx, yy, yz);}23const Vec3 front() const {return Vec3(zx, zy, zz);}24const Vec3 move() const {return Vec3(wx, wy, wz);}2526const float &operator[](int i) const {27return *(((const float *)this) + i);28}29float &operator[](int i) {30return *(((float *)this) + i);31}32Matrix4x4 operator * (const Matrix4x4 &other) const ;33void operator *= (const Matrix4x4 &other) {34*this = *this * other;35}36const float *getReadPtr() const {37return (const float *)this;38}39void empty() {40memset(this, 0, 16 * sizeof(float));41}42static Matrix4x4 identity() {43Matrix4x4 id;44id.setIdentity();45return id;46}47void setIdentity() {48empty();49xx = yy = zz = ww = 1.0f;50}51void setTranslation(const Vec3 &trans) {52setIdentity();53wx = trans.x;54wy = trans.y;55wz = trans.z;56}5758Matrix4x4 transpose() const;5960// Exact angles to avoid any artifacts.61void setRotationZ90() {62empty();63float c = 0.0f;64float s = 1.0f;65xx = c; xy = s;66yx = -s; yy = c;67zz = 1.0f;68ww = 1.0f;69}70void setRotationZ180() {71empty();72float c = -1.0f;73float s = 0.0f;74xx = c; xy = s;75yx = -s; yy = c;76zz = 1.0f;77ww = 1.0f;78}79void setRotationZ270() {80empty();81float c = 0.0f;82float s = -1.0f;83xx = c; xy = s;84yx = -s; yy = c;85zz = 1.0f;86ww = 1.0f;87}8889void setOrtho(float left, float right, float bottom, float top, float near, float far);90void setOrthoD3D(float left, float right, float bottom, float top, float near, float far);91void setOrthoVulkan(float left, float right, float top, float bottom, float near, float far);9293void setViewFrame(const Vec3 &pos, const Vec3 &right, const Vec3 &forward, const Vec3 &up);94void toText(char *buffer, int len) const;95void print() const;9697void translateAndScale(const Vec3 &trans, const Vec3 &scale) {98xx = xx * scale.x + xw * trans.x;99xy = xy * scale.y + xw * trans.y;100xz = xz * scale.z + xw * trans.z;101102yx = yx * scale.x + yw * trans.x;103yy = yy * scale.y + yw * trans.y;104yz = yz * scale.z + yw * trans.z;105106zx = zx * scale.x + zw * trans.x;107zy = zy * scale.y + zw * trans.y;108zz = zz * scale.z + zw * trans.z;109110wx = wx * scale.x + ww * trans.x;111wy = wy * scale.y + ww * trans.y;112wz = wz * scale.z + ww * trans.z;113}114};115116} // namespace Lin117118#endif // _MATH_LIN_MATRIX4X4_H119120121122