glide_test1
diff matrix.h @ 0:f3ddb2bb7024
first 3dfx glide test, initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Mar 2014 06:27:58 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/matrix.h Sun Mar 09 06:27:58 2014 +0200 1.3 @@ -0,0 +1,111 @@ 1.4 +#ifndef MATRIX_H_ 1.5 +#define MATRIX_H_ 1.6 + 1.7 +#define _USE_MATH_DEFINES 1.8 +#include <math.h> 1.9 + 1.10 +#ifndef M_PI 1.11 +#define M_PI 3.14159265359 1.12 +#endif 1.13 + 1.14 +class Matrix4x4 { 1.15 +public: 1.16 + float m[4][4]; 1.17 + 1.18 + inline Matrix4x4() 1.19 + { 1.20 + *this = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 1.21 + } 1.22 + 1.23 + inline Matrix4x4(float m00, float m01, float m02, float m03, 1.24 + float m10, float m11, float m12, float m13, 1.25 + float m20, float m21, float m22, float m23, 1.26 + float m30, float m31, float m32, float m33) 1.27 + { 1.28 + m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; 1.29 + m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; 1.30 + m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; 1.31 + m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; 1.32 + } 1.33 + 1.34 + friend inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b); 1.35 + 1.36 + inline void set_identity() 1.37 + { 1.38 + *this = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); 1.39 + } 1.40 + 1.41 + inline void translate(float x, float y, float z) 1.42 + { 1.43 + *this = *this * Matrix4x4(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); 1.44 + } 1.45 + 1.46 + inline void scale(float x, float y, float z) 1.47 + { 1.48 + *this = *this * Matrix4x4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); 1.49 + } 1.50 + 1.51 + inline void rotate(float angle, float x, float y, float z) 1.52 + { 1.53 + float rad = M_PI * angle / 180.0; 1.54 + float sina = (float)sin(rad); 1.55 + float cosa = (float)cos(rad); 1.56 + float invcosa = 1.0f - cosa; 1.57 + float nxsq = x * x; 1.58 + float nysq = y * y; 1.59 + float nzsq = z * z; 1.60 + 1.61 + Matrix4x4 xform; 1.62 + xform[0][0] = nxsq + (1.0f - nxsq) * cosa; 1.63 + xform[0][1] = x * y * invcosa - z * sina; 1.64 + xform[0][2] = x * z * invcosa + y * sina; 1.65 + xform[1][0] = x * y * invcosa + z * sina; 1.66 + xform[1][1] = nysq + (1.0f - nysq) * cosa; 1.67 + xform[1][2] = y * z * invcosa - x * sina; 1.68 + xform[2][0] = x * z * invcosa - y * sina; 1.69 + xform[2][1] = y * z * invcosa + x * sina; 1.70 + xform[2][2] = nzsq + (1.0f - nzsq) * cosa; 1.71 + *this = *this * xform; 1.72 + } 1.73 + 1.74 + inline void perspective(float vfov_deg, float aspect, float znear, float zfar) 1.75 + { 1.76 + float vfov_rad = M_PI * vfov_deg / 180.0; 1.77 + float f = 1.0f / tan(vfov_rad * 0.5f); 1.78 + float dz = znear - zfar; 1.79 + 1.80 + Matrix4x4 xform; 1.81 + xform[0][0] = f / aspect; 1.82 + xform[1][1] = f; 1.83 + xform[2][2] = (zfar + znear) / dz; 1.84 + xform[3][2] = -1.0f; 1.85 + xform[2][3] = 2.0f * zfar * znear / dz; 1.86 + xform[3][3] = 0.0f; 1.87 + *this = *this * xform; 1.88 + } 1.89 + 1.90 + inline float *operator [](int idx) 1.91 + { 1.92 + return m[idx]; 1.93 + } 1.94 + 1.95 + inline const float *operator[](int idx) const 1.96 + { 1.97 + return m[idx]; 1.98 + } 1.99 +}; 1.100 + 1.101 +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) 1.102 +{ 1.103 + Matrix4x4 res; 1.104 + 1.105 + for(int i=0; i<4; i++) { 1.106 + for(int j=0; j<4; j++) { 1.107 + res.m[i][j] = a.m[i][0] * b.m[0][j] + a.m[i][1] * b.m[1][j] + a.m[i][2] * b.m[2][j] + a.m[i][3] * b.m[3][j]; 1.108 + } 1.109 + } 1.110 + return res; 1.111 +} 1.112 + 1.113 + 1.114 +#endif // MATRIX_H_ 1.115 \ No newline at end of file