rayzor
diff src/vmathmat.h @ 1:a826bf0fb169
fixed line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Apr 2014 09:05:26 +0300 |
parents | 2a5340a6eee4 |
children | d94a69933a71 |
line diff
1.1 --- a/src/vmathmat.h Sat Apr 05 08:46:27 2014 +0300 1.2 +++ b/src/vmathmat.h Sat Apr 05 09:05:26 2014 +0300 1.3 @@ -1,116 +1,116 @@ 1.4 -#ifndef VMATH_MATRIX_H_ 1.5 -#define VMATH_MATRIX_H_ 1.6 - 1.7 -#include <math.h> 1.8 - 1.9 -#ifndef M_PI 1.10 -#define M_PI 3.141592653 1.11 -#endif 1.12 - 1.13 -class Vector3; 1.14 - 1.15 -class Matrix4x4 { 1.16 -public: 1.17 - float m[4][4]; 1.18 - 1.19 - Matrix4x4() 1.20 - { 1.21 - set_identity(); 1.22 - } 1.23 - 1.24 - Matrix4x4(float m00, float m01, float m02, float m03, 1.25 - float m10, float m11, float m12, float m13, 1.26 - float m20, float m21, float m22, float m23, 1.27 - float m30, float m31, float m32, float m33) 1.28 - { 1.29 - m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; 1.30 - m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; 1.31 - m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; 1.32 - m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; 1.33 - } 1.34 - 1.35 - inline void set_identity(); 1.36 - inline void translate(float x, float y, float z); 1.37 - inline void rotate(float angle, float x, float y, float z); 1.38 - inline void scale(float x, float y, float z); 1.39 - inline void perspective(float vfov, float aspect, float znear, float zfar); 1.40 - inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up); 1.41 - 1.42 - float *operator [](int idx) { return m[idx]; } 1.43 - const float *operator [](int idx) const { return m[idx]; } 1.44 -}; 1.45 - 1.46 -inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) 1.47 -{ 1.48 - Matrix4x4 res; 1.49 - for(int i=0; i<4; i++) { 1.50 - for(int j=0; j<4; j++) { 1.51 - res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + 1.52 - a[i][2] * b[2][j] + a[i][3] * b[3][j]; 1.53 - } 1.54 - } 1.55 - return res; 1.56 -} 1.57 - 1.58 -inline void Matrix4x4::set_identity() 1.59 -{ 1.60 - m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; 1.61 - m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0; 1.62 - m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0; 1.63 -} 1.64 - 1.65 -inline void Matrix4x4::translate(float x, float y, float z) 1.66 -{ 1.67 - Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); 1.68 - *this = *this * m; 1.69 -} 1.70 - 1.71 -inline void Matrix4x4::rotate(float angle, float x, float y, float z) 1.72 -{ 1.73 - float sina = (float)sin(angle); 1.74 - float cosa = (float)cos(angle); 1.75 - float rcosa = 1.0f - cosa; 1.76 - float nxsq = x * x; 1.77 - float nysq = y * y; 1.78 - float nzsq = z * z; 1.79 - 1.80 - Matrix4x4 m; 1.81 - m[0][0] = nxsq + (1.0f - nxsq) * cosa; 1.82 - m[0][1] = x * y * rcosa - z * sina; 1.83 - m[0][2] = x * z * rcosa + y * sina; 1.84 - 1.85 - m[1][0] = x * y * rcosa + z * sina; 1.86 - m[1][1] = nysq + (1.0f - nysq) * cosa; 1.87 - m[1][2] = y * z * rcosa - x * sina; 1.88 - 1.89 - m[2][0] = x * z * rcosa - y * sina; 1.90 - m[2][1] = y * z * rcosa + x * sina; 1.91 - m[2][2] = nzsq + (1.0f - nzsq) * cosa; 1.92 - 1.93 - *this = *this * m; 1.94 -} 1.95 - 1.96 -inline void Matrix4x4::scale(float x, float y, float z) 1.97 -{ 1.98 - Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); 1.99 - *this = *this * m; 1.100 -} 1.101 - 1.102 -inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar) 1.103 -{ 1.104 - float f = 1.0f / tan(vfov * 0.5f); 1.105 - float dz = znear - zfar; 1.106 - 1.107 - Matrix4x4 m; 1.108 - m[0][0] = f / aspect; 1.109 - m[1][1] = f; 1.110 - m[2][2] = (zfar + znear) / dz; 1.111 - m[3][2] = -1.0f; 1.112 - m[2][3] = 2.0f * zfar * znear / dz; 1.113 - m[3][3] = 0.0f; 1.114 - 1.115 - *this = *this * m; 1.116 -} 1.117 - 1.118 - 1.119 -#endif // VMATH_MATRIX_H_ 1.120 +#ifndef VMATH_MATRIX_H_ 1.121 +#define VMATH_MATRIX_H_ 1.122 + 1.123 +#include <math.h> 1.124 + 1.125 +#ifndef M_PI 1.126 +#define M_PI 3.141592653 1.127 +#endif 1.128 + 1.129 +class Vector3; 1.130 + 1.131 +class Matrix4x4 { 1.132 +public: 1.133 + float m[4][4]; 1.134 + 1.135 + Matrix4x4() 1.136 + { 1.137 + set_identity(); 1.138 + } 1.139 + 1.140 + Matrix4x4(float m00, float m01, float m02, float m03, 1.141 + float m10, float m11, float m12, float m13, 1.142 + float m20, float m21, float m22, float m23, 1.143 + float m30, float m31, float m32, float m33) 1.144 + { 1.145 + m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; 1.146 + m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; 1.147 + m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; 1.148 + m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; 1.149 + } 1.150 + 1.151 + inline void set_identity(); 1.152 + inline void translate(float x, float y, float z); 1.153 + inline void rotate(float angle, float x, float y, float z); 1.154 + inline void scale(float x, float y, float z); 1.155 + inline void perspective(float vfov, float aspect, float znear, float zfar); 1.156 + inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up); 1.157 + 1.158 + float *operator [](int idx) { return m[idx]; } 1.159 + const float *operator [](int idx) const { return m[idx]; } 1.160 +}; 1.161 + 1.162 +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) 1.163 +{ 1.164 + Matrix4x4 res; 1.165 + for(int i=0; i<4; i++) { 1.166 + for(int j=0; j<4; j++) { 1.167 + res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + 1.168 + a[i][2] * b[2][j] + a[i][3] * b[3][j]; 1.169 + } 1.170 + } 1.171 + return res; 1.172 +} 1.173 + 1.174 +inline void Matrix4x4::set_identity() 1.175 +{ 1.176 + m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; 1.177 + m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0; 1.178 + m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0; 1.179 +} 1.180 + 1.181 +inline void Matrix4x4::translate(float x, float y, float z) 1.182 +{ 1.183 + Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); 1.184 + *this = *this * m; 1.185 +} 1.186 + 1.187 +inline void Matrix4x4::rotate(float angle, float x, float y, float z) 1.188 +{ 1.189 + float sina = (float)sin(angle); 1.190 + float cosa = (float)cos(angle); 1.191 + float rcosa = 1.0f - cosa; 1.192 + float nxsq = x * x; 1.193 + float nysq = y * y; 1.194 + float nzsq = z * z; 1.195 + 1.196 + Matrix4x4 m; 1.197 + m[0][0] = nxsq + (1.0f - nxsq) * cosa; 1.198 + m[0][1] = x * y * rcosa - z * sina; 1.199 + m[0][2] = x * z * rcosa + y * sina; 1.200 + 1.201 + m[1][0] = x * y * rcosa + z * sina; 1.202 + m[1][1] = nysq + (1.0f - nysq) * cosa; 1.203 + m[1][2] = y * z * rcosa - x * sina; 1.204 + 1.205 + m[2][0] = x * z * rcosa - y * sina; 1.206 + m[2][1] = y * z * rcosa + x * sina; 1.207 + m[2][2] = nzsq + (1.0f - nzsq) * cosa; 1.208 + 1.209 + *this = *this * m; 1.210 +} 1.211 + 1.212 +inline void Matrix4x4::scale(float x, float y, float z) 1.213 +{ 1.214 + Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); 1.215 + *this = *this * m; 1.216 +} 1.217 + 1.218 +inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar) 1.219 +{ 1.220 + float f = 1.0f / tan(vfov * 0.5f); 1.221 + float dz = znear - zfar; 1.222 + 1.223 + Matrix4x4 m; 1.224 + m[0][0] = f / aspect; 1.225 + m[1][1] = f; 1.226 + m[2][2] = (zfar + znear) / dz; 1.227 + m[3][2] = -1.0f; 1.228 + m[2][3] = 2.0f * zfar * znear / dz; 1.229 + m[3][3] = 0.0f; 1.230 + 1.231 + *this = *this * m; 1.232 +} 1.233 + 1.234 + 1.235 +#endif // VMATH_MATRIX_H_