Advertisement
dark-s0ul

glmath

Jan 10th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | None | 0 0
  1. #ifndef GLMATH
  2. #define GLMATH
  3.  
  4. #include <cmath>
  5.  
  6. struct vec2 {
  7.     float x, y;
  8.    
  9.     vec2() : x(0), y(0) {}
  10.     vec2(float x, float y) : x(x), y(y) {}
  11.    
  12.     float magnitude() {
  13.         return sqrtf(dot(*this));
  14.     }
  15.    
  16.     vec2 normalize() {
  17.         float l = magnitude();
  18.         return l != 0 ? (*this / l) : vec2();
  19.     }
  20.    
  21.     float dot(const vec2& a) {
  22.         return x * a.x + y * a.y;
  23.     }
  24.    
  25.     const float& operator[] (int index) const {
  26.         return (&x)[index];
  27.     }
  28.    
  29.     float& operator[] (int index) {
  30.         return (&x)[index];
  31.     }
  32.    
  33.     vec2 operator-() const {
  34.         return vec2(-x, -y);
  35.     }
  36.    
  37.     vec2 operator-(const vec2& a) const {
  38.         return vec2(x - a.x, y - a.y);
  39.     }
  40.    
  41.     vec2 operator+(const vec2& a) const {
  42.         return vec2(x + a.x, y + a.y);
  43.     }
  44.  
  45.     vec2 operator*(const float &a) const {
  46.         return vec2(x * a, y * a);
  47.     }
  48.  
  49.     friend vec2 operator*(const float &a, const vec2 &b) {
  50.         return b * a;
  51.     }
  52.  
  53.     vec2 operator/(const float &a) {
  54.         return operator*(1.0 / a);
  55.     }
  56. };
  57.  
  58.  
  59. struct vec3 {
  60.     float x, y, z;
  61.    
  62.     vec3() : x(0), y(0), z(0) {}
  63.     vec3(vec2 v, float z) : x(v.x), y(v.y), z(z) {}
  64.     vec3(float x, float y, float z) : x(x), y(y), z(z) {}
  65.    
  66.     float magnitude() {
  67.         return sqrtf(dot(*this));
  68.     }
  69.    
  70.     vec3 normalize() {
  71.         float l = magnitude();
  72.         return l != 0 ? (*this / l) : vec3();
  73.     }
  74.    
  75.     float dot(const vec3& a) {
  76.         return x * a.x + y * a.y + z * a.z;
  77.     }
  78.    
  79.     vec3 cross(const vec3& a) {
  80.         return vec3(
  81.             y * a.z - z * a.y,
  82.             z * a.x - x * a.z,
  83.             x * a.y - y * a.x
  84.         );
  85.     }
  86.  
  87.     const float& operator[] (int index) const {
  88.         return (&x)[index];
  89.     }
  90.    
  91.     float& operator[] (int index) {
  92.         return (&x)[index];
  93.     }
  94.    
  95.     vec3 operator-() const {
  96.         return vec3(-x, -y, -z);
  97.     }
  98.    
  99.     vec3 operator-(const vec3& a) const {
  100.         return vec3(x - a.x, y - a.y, z - a.z);
  101.     }
  102.    
  103.     vec3 operator+(const vec3& a) const {
  104.         return vec3(x + a.x, y + a.y, z + a.z);
  105.     }
  106.  
  107.     vec3 operator*(const float &a) const {
  108.         return vec3(x * a, y * a, z * a);
  109.     }
  110.  
  111.     friend vec3 operator*(const float &a, const vec3 &b) {
  112.         return b * a;
  113.     }
  114.  
  115.     vec3 operator/(const float &a) {
  116.         return operator*(1.0 / a);
  117.     }
  118. };
  119.  
  120. struct vec4 {
  121.     float x, y, z, w;
  122.    
  123.     vec4() : x(0), y(0), z(0), w(0) {}
  124.     vec4(vec3 v, float w = 0) : x(v.x), y(v.y), z(v.z), w(w) {}
  125.     vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
  126.    
  127.     float magnitude() {
  128.         return sqrtf(dot(*this));
  129.     }
  130.    
  131.     vec4 normalize() {
  132.         float l = magnitude();
  133.         return l ? vec4(x / l, y / l, z / l, w / l) : vec4(0, 0, 0, 0);
  134.     }
  135.    
  136.     float dot(const vec4& a) {
  137.         return x * a.x + y * a.y + z * a.z + w * a.w;
  138.     }
  139.    
  140.     const float& operator[] (int index) const {
  141.         return (&x)[index];
  142.     }
  143.    
  144.     float& operator[] (int index) {
  145.         return (&x)[index];
  146.     }
  147.    
  148.     vec4 operator-() const {
  149.         return vec4(-x, -y, -z, -w);
  150.     }
  151.    
  152.     vec4 operator-(const vec4& a) const {
  153.         return vec4(x - a.x, y - a.y, z - a.z, w - a.w);
  154.     }
  155.    
  156.     vec4 operator+(const vec4& a) const {
  157.         return vec4(x + a.x, y + a.y, z + a.z, w + a.w);
  158.     }
  159.  
  160.     vec4 operator*(const float &a) const {
  161.         return vec4(x * a, y * a, z * a, w * a);
  162.     }
  163.  
  164.     friend vec4 operator*(const float &a, const vec4 &b) {
  165.         return b * a;
  166.     }
  167.  
  168.     vec4 operator/(const float &a) const {
  169.         return operator*(1.0 / a);
  170.     }
  171. };
  172.  
  173. struct mat3 {
  174.     vec3 x, y, z;
  175.    
  176.     mat3() : x(1, 0, 0), y(0, 1, 0), z(0, 0, 1) {}
  177.     mat3(vec3 x, vec3 y, vec3 z) : x(x), y(y), z(z) {}
  178.    
  179.     const vec3& operator[](int index) const {
  180.         return (&x)[index];
  181.     }
  182.    
  183.     vec3& operator[](int index) {
  184.         return (&x)[index];
  185.     }
  186.  
  187.     friend vec3 operator*(const vec3& v, const mat3& m) {
  188.         return vec3(
  189.             v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2],
  190.             v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2],
  191.             v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2]
  192.         );
  193.     }
  194.        
  195.     friend vec3 operator*(const mat3& m, const vec3& v) {
  196.         return vec3(
  197.             m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2],
  198.             m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2],
  199.             m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2]
  200.         );
  201.     }
  202. };
  203.  
  204. struct mat4 {
  205.     vec4 x, y, z, w;
  206.    
  207.     mat4() : x(1, 0, 0, 0), y(0, 1, 0, 0), z(0, 0, 1, 0), w(0, 0, 0, 1) {}
  208.     mat4(const mat3 &m, vec4 w = vec4(0, 0, 0, 1)) : x(m.x, 0), y(m.y, 0), z(m.z, 0), w(w) {}
  209.     mat4(vec4 x, vec4 y, vec4 z, vec4 w) : x(x), y(y), z(z), w(w) {}
  210.    
  211.     const vec4& operator[](int index) const {
  212.         return (&x)[index];
  213.     }
  214.    
  215.     vec4& operator[](int index) {
  216.         return (&x)[index];
  217.     }
  218.  
  219.     static mat4 translate(float x, float y, float z) {
  220.         return {
  221.             {1, 0, 0, 0},
  222.             {0, 1, 0, 0},
  223.             {0, 0, 1, 0},
  224.             {x, y, z, 1}
  225.         };
  226.     }
  227.  
  228.     static mat4 translate(const vec3 &position) {
  229.         return {
  230.             { 1, 0, 0, 0},
  231.             { 0, 1, 0, 0},
  232.             { 0, 0, 1, 0},
  233.             {position, 1}
  234.         };
  235.     }
  236.  
  237.     static mat4 rotate(float y, float p, float r) {
  238.         float cy = cos(y);
  239.         float sy = sin(y);
  240.        
  241.         float cp = cos(p);
  242.         float sp = sin(p);
  243.        
  244.         float cr = cos(r);
  245.         float sr = sin(r);
  246.        
  247.         // return mat4 (
  248.         //  {sr * sy * sp + cr * cp, cr * sy * sp - sr * cp, -cy * sp, 0},
  249.         //  {               sr * cp,                cr * cp,       sy, 0},
  250.         //  {sr * sy * cp - cr * sp, sy * cp * cr + sp * sr, -cy * cp, 0},
  251.         //  {                     0,                      0,        0, 1}
  252.         // );
  253.  
  254.         mat4 rotateX = {
  255.             {1,  0,   0, 0},
  256.             {0, cy, -sy, 0},
  257.             {0, sy,  cy, 0},
  258.             {0,  0,   0, 1}
  259.         };
  260.  
  261.         mat4 rotateY = {
  262.             { cp, 0, sp, 0},
  263.             {  0, 1,  0, 0},
  264.             {-sp, 0, cp, 0},
  265.             {  0, 0,  0, 1}
  266.         };
  267.  
  268.         mat4 rotateZ = {
  269.             {cr, -sr, 0, 0},
  270.             {sr,  cr, 0, 0},
  271.             { 0,   0, 1, 0},
  272.             { 0,   0, 0, 1}
  273.         };
  274.  
  275.         return rotateZ * rotateY * rotateX;
  276.     }
  277.  
  278.     static mat4 scale(float x, float y, float z) {
  279.         return {
  280.             {x, 0, 0, 0},
  281.             {0, y, 0, 0},
  282.             {0, 0, z, 0},
  283.             {0, 0, 0, 1}
  284.         };
  285.     }
  286.  
  287.     static mat4 scale(const vec3 &scale) {
  288.         return mat4::scale(scale.x, scale.y, scale.z);
  289.     }
  290.  
  291.     friend vec4 operator*(const vec4& v, const mat4& m) {
  292.         return vec4(
  293.             v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2] + v[3] * m[0][3],
  294.             v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2] + v[3] * m[1][3],
  295.             v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2] + v[3] * m[2][3],
  296.             v[0] * m[3][0] + v[1] * m[3][1] + v[2] * m[3][2] + v[3] * m[3][3]
  297.         );
  298.     }
  299.    
  300.     friend vec4 operator*(const mat4& m, const vec4& v) {
  301.         return vec4(
  302.             m[0][0] * v[0] + m[1][0] * v[1] + m[2][0] * v[2] + m[3][0] * v[3],
  303.             m[0][1] * v[0] + m[1][1] * v[1] + m[2][1] * v[2] + m[3][1] * v[3],
  304.             m[0][2] * v[0] + m[1][2] * v[1] + m[2][2] * v[2] + m[3][2] * v[3],
  305.             m[0][3] * v[0] + m[1][3] * v[1] + m[2][3] * v[2] + m[3][3] * v[3]
  306.         );
  307.     }
  308.        
  309.     friend mat4 operator*(const mat4& m1, const mat4& m2) {
  310.         vec4 X = m1 * m2[0];
  311.         vec4 Y = m1 * m2[1];
  312.         vec4 Z = m1 * m2[2];
  313.         vec4 W = m1 * m2[3];
  314.         return mat4(X, Y, Z, W);
  315.     }
  316.  
  317.     static mat4 lookat(vec3 position, vec3 target) {
  318.         vec3 d = (target - position).normalize();
  319.        
  320.         return rotate(atan2(d.x, d.y), asin(d.y), 0) * mat4(
  321.             vec4(1, 0, 0, 0),
  322.             vec4(0, 1, 0, 0),
  323.             vec4(0, 0, 1, 0),
  324.             vec4(-position, 1)
  325.         );
  326.     }
  327.    
  328.     static mat4 orthographic(float size, float aspect, float near, float far) {
  329.         float f = 2.0f / size;
  330.         float a = 2.0f * far * near / (near - far);
  331.         float b = (near + far) / (near - far);
  332.        
  333.         return mat4(
  334.             vec4(f/aspect, 0, 0, 0),
  335.             vec4(       0, f, 0, 0),
  336.             vec4(       0, 0, a, b),
  337.             vec4(       0, 0, 0, 1)
  338.         );
  339.     }
  340.  
  341.     static mat4 perspective(float fov, float aspect, float near, float far) {
  342.         float f = 1.0f / tanf(fov * M_PI / 360.0);
  343.        
  344.         float a = (near + far) / (near - far);
  345.         float b = 2.0f * far / (near - far);
  346.        
  347.         return mat4(
  348.             vec4(f/aspect, 0,  0, 0),
  349.             vec4(       0, f,  0, 0),
  350.             vec4(       0, 0,  a, b),
  351.             vec4(       0, 0, -1, 0)
  352.         );
  353.     }
  354. };
  355.  
  356. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement