// Module vmath.h tiré des exemples du "Red Book" : // https://github.com/openglredbook/examples/blob/master/include/vmath.h // // BUGFIX 09/01/2025 - Edouard.Thiel@univ-amu.fr // fonction ortho() : erreur de signe corrigée #ifndef __VMATH_H__ #define __VMATH_H__ #define _USE_MATH_DEFINES 1 // Include constants defined in math.h #include namespace vmath { template class matNM; template class vecN; template class Tquaternion; template inline T degrees(T angleInRadians) { return angleInRadians * static_cast(180.0/M_PI); } template inline T radians(T angleInDegrees) { return angleInDegrees * static_cast(M_PI/180.0); } template struct random { operator T () { static unsigned int seed = 0x13371337; unsigned int res; unsigned int tmp; seed *= 16807; tmp = seed ^ (seed >> 4) ^ (seed << 15); res = (tmp >> 9) | 0x3F800000; return static_cast(res); } }; template<> struct random { operator float() { static unsigned int seed = 0x13371337; float res; unsigned int tmp; seed *= 16807; tmp = seed ^ (seed >> 4) ^ (seed << 15); *((unsigned int *) &res) = (tmp >> 9) | 0x3F800000; return (res - 1.0f); } }; template<> struct random { operator unsigned int() { static unsigned int seed = 0x13371337; unsigned int res; unsigned int tmp; seed *= 16807; tmp = seed ^ (seed >> 4) ^ (seed << 15); res = (tmp >> 9) | 0x3F800000; return res; } }; template class vecN { public: typedef class vecN my_type; typedef T element_type; // Default constructor does nothing, just like built-in types inline vecN() { // Uninitialized variable } // Copy constructor inline vecN(const vecN& that) { assign(that); } // Construction from scalar inline vecN(T s) { int n; for (n = 0; n < len; n++) { data[n] = s; } } // Assignment operator inline vecN& operator=(const vecN& that) { assign(that); return *this; } inline vecN& operator=(const T& that) { int n; for (n = 0; n < len; n++) data[n] = that; return *this; } inline vecN operator+(const vecN& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] + that.data[n]; return result; } inline vecN& operator+=(const vecN& that) { return (*this = *this + that); } inline vecN operator-() const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = -data[n]; return result; } inline vecN operator-(const vecN& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] - that.data[n]; return result; } inline vecN& operator-=(const vecN& that) { return (*this = *this - that); } inline vecN operator*(const vecN& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] * that.data[n]; return result; } inline vecN& operator*=(const vecN& that) { return (*this = *this * that); } inline vecN operator*(const T& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] * that; return result; } inline vecN& operator*=(const T& that) { assign(*this * that); return *this; } inline vecN operator/(const vecN& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] / that.data[n]; return result; } inline vecN& operator/=(const vecN& that) { assign(*this / that); return *this; } inline vecN operator/(const T& that) const { my_type result; int n; for (n = 0; n < len; n++) result.data[n] = data[n] / that; return result; } inline vecN& operator/=(const T& that) { assign(*this / that); return *this; } inline T& operator[](int n) { return data[n]; } inline const T& operator[](int n) const { return data[n]; } inline static int size(void) { return len; } inline operator const T* () const { return &data[0]; } static inline vecN random() { vecN result; int i; for (i = 0; i < len; i++) { result[i] = vmath::random(); } return result; } protected: T data[len]; inline void assign(const vecN& that) { int n; for (n = 0; n < len; n++) data[n] = that.data[n]; } }; template class Tvec2 : public vecN { public: typedef vecN base; // Uninitialized variable inline Tvec2() {} // Copy constructor inline Tvec2(const base& v) : base(v) {} // vec2(x, y); inline Tvec2(T x, T y) { base::data[0] = x; base::data[1] = y; } }; template class Tvec3 : public vecN { public: typedef vecN base; // Uninitialized variable inline Tvec3() {} // Copy constructor inline Tvec3(const base& v) : base(v) {} // vec3(x, y, z); inline Tvec3(T x, T y, T z) { base::data[0] = x; base::data[1] = y; base::data[2] = z; } // vec3(v, z); inline Tvec3(const Tvec2& v, T z) { base::data[0] = v[0]; base::data[1] = v[1]; base::data[2] = z; } // vec3(x, v) inline Tvec3(T x, const Tvec2& v) { base::data[0] = x; base::data[1] = v[0]; base::data[2] = v[1]; } }; template class Tvec4 : public vecN { public: typedef vecN base; // Uninitialized variable inline Tvec4() {} // Copy constructor inline Tvec4(const base& v) : base(v) {} // vec4(x, y, z, w); inline Tvec4(T x, T y, T z, T w) { base::data[0] = x; base::data[1] = y; base::data[2] = z; base::data[3] = w; } // vec4(v, z, w); inline Tvec4(const Tvec2& v, T z, T w) { base::data[0] = v[0]; base::data[1] = v[1]; base::data[2] = z; base::data[3] = w; } // vec4(x, v, w); inline Tvec4(T x, const Tvec2& v, T w) { base::data[0] = x; base::data[1] = v[0]; base::data[2] = v[1]; base::data[3] = w; } // vec4(x, y, v); inline Tvec4(T x, T y, const Tvec2& v) { base::data[0] = x; base::data[1] = y; base::data[2] = v[0]; base::data[3] = v[1]; } // vec4(v1, v2); inline Tvec4(const Tvec2& u, const Tvec2& v) { base::data[0] = u[0]; base::data[1] = u[1]; base::data[2] = v[0]; base::data[3] = v[1]; } // vec4(v, w); inline Tvec4(const Tvec3& v, T w) { base::data[0] = v[0]; base::data[1] = v[1]; base::data[2] = v[2]; base::data[3] = w; } // vec4(x, v); inline Tvec4(T x, const Tvec3& v) { base::data[0] = x; base::data[1] = v[0]; base::data[2] = v[1]; base::data[3] = v[2]; } }; // These types don't exist in GLSL and don't have full implementations // (constructors and such). This is enough to get some template functions // to compile correctly. typedef vecN vec1; typedef vecN ivec1; typedef vecN uvec1; typedef vecN dvec1; typedef Tvec2 vec2; typedef Tvec2 ivec2; typedef Tvec2 uvec2; typedef Tvec2 dvec2; typedef Tvec3 vec3; typedef Tvec3 ivec3; typedef Tvec3 uvec3; typedef Tvec3 dvec3; typedef Tvec4 vec4; typedef Tvec4 ivec4; typedef Tvec4 uvec4; typedef Tvec4 dvec4; template static inline const vecN operator * (T x, const vecN& v) { return v * x; } template static inline const Tvec2 operator / (T x, const Tvec2& v) { return Tvec2(x / v[0], x / v[1]); } template static inline const Tvec3 operator / (T x, const Tvec3& v) { return Tvec3(x / v[0], x / v[1], x / v[2]); } template static inline const Tvec4 operator / (T x, const Tvec4& v) { return Tvec4(x / v[0], x / v[1], x / v[2], x / v[3]); } template static inline T dot(const vecN& a, const vecN& b) { int n; T total = T(0); for (n = 0; n < len; n++) { total += a[n] * b[n]; } return total; } template static inline vecN cross(const vecN& a, const vecN& b) { return Tvec3(a[1] * b[2] - b[1] * a[2], a[2] * b[0] - b[2] * a[0], a[0] * b[1] - b[0] * a[1]); } template static inline T length(const vecN& v) { T result(0); for (int i = 0; i < v.size(); ++i) { result += v[i] * v[i]; } return (T)sqrt(result); } template static inline vecN normalize(const vecN& v) { return v / length(v); } template static inline T distance(const vecN& a, const vecN& b) { return length(b - a); } template static inline T angle(const vecN& a, const vecN& b) { return arccos(dot(a, b)); } template class Tquaternion { public: inline Tquaternion() { } inline Tquaternion(const Tquaternion& q) : r(q.r), v(q.v) { } inline Tquaternion(T _r) : r(_r), v(T(0)) { } inline Tquaternion(T _r, const Tvec3& _v) : r(_r), v(_v) { } inline Tquaternion(const Tvec4& _v) : r(_v[0]), v(_v[1], _v[2], _v[3]) { } inline Tquaternion(T _x, T _y, T _z, T _w) : r(_x), v(_y, _z, _w) { } inline T& operator[](int n) { return a[n]; } inline const T& operator[](int n) const { return a[n]; } inline Tquaternion operator+(const Tquaternion& q) const { return quaternion(r + q.r, v + q.v); } inline Tquaternion& operator+=(const Tquaternion& q) { r += q.r; v += q.v; return *this; } inline Tquaternion operator-(const Tquaternion& q) const { return quaternion(r - q.r, v - q.v); } inline Tquaternion& operator-=(const Tquaternion& q) { r -= q.r; v -= q.v; return *this; } inline Tquaternion operator-() const { return Tquaternion(-r, -v); } inline Tquaternion operator*(const T s) const { return Tquaternion(a[0] * s, a[1] * s, a[2] * s, a[3] * s); } inline Tquaternion& operator*=(const T s) { r *= s; v *= s; return *this; } inline Tquaternion operator*(const Tquaternion& q) const { const T x1 = a[0]; const T y1 = a[1]; const T z1 = a[2]; const T w1 = a[3]; const T x2 = q.a[0]; const T y2 = q.a[1]; const T z2 = q.a[2]; const T w2 = q.a[3]; return Tquaternion(w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2, w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2, w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2, w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2); } inline Tquaternion operator/(const T s) const { return Tquaternion(a[0] / s, a[1] / s, a[2] / s, a[3] / s); } inline Tquaternion& operator/=(const T s) { r /= s; v /= s; return *this; } inline operator Tvec4&() { return *(Tvec4*)&a[0]; } inline operator const Tvec4&() const { return *(const Tvec4*)&a[0]; } inline bool operator==(const Tquaternion& q) const { return (r == q.r) && (v == q.v); } inline bool operator!=(const Tquaternion& q) const { return (r != q.r) || (v != q.v); } inline matNM asMatrix() const { matNM m; const T xx = x * x; const T yy = y * y; const T zz = z * z; const T ww = w * w; const T xy = x * y; const T xz = x * z; const T xw = x * w; const T yz = y * z; const T yw = y * w; const T zw = z * w; m[0][0] = T(1) - T(2) * (yy + zz); m[0][1] = T(2) * (xy - zw); m[0][2] = T(2) * (xz + yw); m[0][3] = T(0); m[1][0] = T(2) * (xy + zw); m[1][1] = T(1) - T(2) * (xx + zz); m[1][2] = T(2) * (yz - xw); m[1][3] = T(0); m[2][0] = T(2) * (xz - yw); m[2][1] = T(2) * (yz + xw); m[2][2] = T(1) - T(2) * (xx + yy); m[2][3] = T(0); m[3][0] = T(0); m[3][1] = T(0); m[3][2] = T(0); m[3][3] = T(1); return m; } /* inline T length() const { return vmath::length( Tvec4(r, v) ); } */ private: union { struct { T r; Tvec3 v; }; struct { T x; T y; T z; T w; }; T a[4]; }; }; typedef Tquaternion quaternion; typedef Tquaternion iquaternion; typedef Tquaternion uquaternion; typedef Tquaternion dquaternion; template static inline Tquaternion operator*(T a, const Tquaternion& b) { return b * a; } template static inline Tquaternion operator/(T a, const Tquaternion& b) { return Tquaternion(a / b[0], a / b[1], a / b[2], a / b[3]); } template static inline Tquaternion normalize(const Tquaternion& q) { return q / length(vecN(q)); } template class matNM { public: typedef class matNM my_type; typedef class vecN vector_type; // Default constructor does nothing, just like built-in types inline matNM() { // Uninitialized variable } // Copy constructor inline matNM(const matNM& that) { assign(that); } // Construction from element type // explicit to prevent assignment from T explicit inline matNM(T f) { for (int n = 0; n < w; n++) { data[n] = f; } } // Construction from vector inline matNM(const vector_type& v) { for (int n = 0; n < w; n++) { data[n] = v; } } // Assignment operator inline matNM& operator=(const my_type& that) { assign(that); return *this; } inline matNM operator+(const my_type& that) const { my_type result; int n; for (n = 0; n < w; n++) result.data[n] = data[n] + that.data[n]; return result; } inline my_type& operator+=(const my_type& that) { return (*this = *this + that); } inline my_type operator-(const my_type& that) const { my_type result; int n; for (n = 0; n < w; n++) result.data[n] = data[n] - that.data[n]; return result; } inline my_type& operator-=(const my_type& that) { return (*this = *this - that); } inline my_type operator*(const T& that) const { my_type result; int n; for (n = 0; n < w; n++) result.data[n] = data[n] * that; return result; } inline my_type& operator*=(const T& that) { int n; for (n = 0; n < w; n++) data[n] = data[n] * that; return *this; } // Matrix multiply. // TODO: This only works for square matrices. Need more template skill to make a non-square version. inline my_type operator*(const my_type& that) const { my_type result(0); for (int j = 0; j < w; j++) { for (int i = 0; i < h; i++) { T sum(0); for (int n = 0; n < w; n++) { sum += data[n][i] * that[j][n]; } result[j][i] = sum; } } return result; } inline my_type& operator*=(const my_type& that) { return (*this = *this * that); } inline vector_type& operator[](int n) { return data[n]; } inline const vector_type& operator[](int n) const { return data[n]; } inline operator T*() { return &data[0][0]; } inline operator const T*() const { return &data[0][0]; } inline matNM transpose(void) const { matNM result; int x, y; for (y = 0; y < w; y++) { for (x = 0; x < h; x++) { result[x][y] = data[y][x]; } } return result; } static inline my_type identity() { my_type result(0); for (int i = 0; i < w; i++) { result[i][i] = 1; } return result; } static inline int width(void) { return w; } static inline int height(void) { return h; } protected: // Column primary data (essentially, array of vectors) vecN data[w]; // Assignment function - called from assignment operator and copy constructor. inline void assign(const matNM& that) { int n; for (n = 0; n < w; n++) data[n] = that.data[n]; } }; /* template class TmatN : public matNM { public: typedef matNM base; typedef TmatN my_type; inline TmatN() {} inline TmatN(const my_type& that) : base(that) {} inline TmatN(float f) : base(f) {} inline TmatN(const vecN& v) : base(v) {} inline my_type transpose(void) { my_type result; int x, y; for (y = 0; y < h; y++) { for (x = 0; x < h; x++) { result[x][y] = data[y][x]; } } return result; } }; */ template class Tmat4 : public matNM { public: typedef matNM base; typedef Tmat4 my_type; inline Tmat4() {} inline Tmat4(const my_type& that) : base(that) {} inline Tmat4(const base& that) : base(that) {} inline Tmat4(const vecN& v) : base(v) {} inline Tmat4(const vecN& v0, const vecN& v1, const vecN& v2, const vecN& v3) { base::data[0] = v0; base::data[1] = v1; base::data[2] = v2; base::data[3] = v3; } }; typedef Tmat4 mat4; typedef Tmat4 imat4; typedef Tmat4 umat4; typedef Tmat4 dmat4; template class Tmat3 : public matNM { public: typedef matNM base; typedef Tmat3 my_type; inline Tmat3() {} inline Tmat3(const my_type& that) : base(that) {} inline Tmat3(const base& that) : base(that) {} inline Tmat3(const vecN& v) : base(v) {} inline Tmat3(const vecN& v0, const vecN& v1, const vecN& v2) { base::data[0] = v0; base::data[1] = v1; base::data[2] = v2; } }; typedef Tmat3 mat3; typedef Tmat3 imat3; typedef Tmat3umat3; typedef Tmat3 dmat3; template class Tmat2 : public matNM { public: typedef matNM base; typedef Tmat2 my_type; inline Tmat2() {} inline Tmat2(const my_type& that) : base(that) {} inline Tmat2(const base& that) : base(that) {} inline Tmat2(const vecN& v) : base(v) {} inline Tmat2(const vecN& v0, const vecN& v1) { base::data[0] = v0; base::data[1] = v1; } }; typedef Tmat2 mat2; static inline mat4 frustum(float left, float right, float bottom, float top, float n, float f) { mat4 result(mat4::identity()); if ((right == left) || (top == bottom) || (n == f) || (n < 0.0) || (f < 0.0)) return result; result[0][0] = (2.0f * n) / (right - left); result[1][1] = (2.0f * n) / (top - bottom); result[2][0] = (right + left) / (right - left); result[2][1] = (top + bottom) / (top - bottom); result[2][2] = -(f + n) / (f - n); result[2][3]= -1.0f; result[3][2] = -(2.0f * f * n) / (f - n); result[3][3] = 0.0f; return result; } static inline mat4 perspective(float fovy, float aspect, float n, float f) { float q = 1.0f / tan(radians(0.5f * fovy)); float A = q / aspect; float B = (n + f) / (n - f); float C = (2.0f * n * f) / (n - f); mat4 result; result[0] = vec4(A, 0.0f, 0.0f, 0.0f); result[1] = vec4(0.0f, q, 0.0f, 0.0f); result[2] = vec4(0.0f, 0.0f, B, -1.0f); result[3] = vec4(0.0f, 0.0f, C, 0.0f); return result; } static inline mat4 ortho(float left, float right, float bottom, float top, float n, float f) { return mat4( vec4(2.0f / (right - left), 0.0f, 0.0f, 0.0f), vec4(0.0f, 2.0f / (top - bottom), 0.0f, 0.0f), vec4(0.0f, 0.0f, 2.0f / (n - f), 0.0f), vec4( (left + right) / (left - right), (bottom + top) / (bottom - top), // (n + f) / (f - n), 1.0f) (n + f) / (n - f), 1.0f) // BUGFIX: erreur de signe ); } template static inline Tmat4 translate(T x, T y, T z) { return Tmat4(Tvec4(1.0f, 0.0f, 0.0f, 0.0f), Tvec4(0.0f, 1.0f, 0.0f, 0.0f), Tvec4(0.0f, 0.0f, 1.0f, 0.0f), Tvec4(x, y, z, 1.0f)); } template static inline Tmat4 translate(const vecN& v) { return translate(v[0], v[1], v[2]); } template static inline Tmat4 lookat(const vecN& eye, const vecN& center, const vecN& up) { const Tvec3 f = normalize(center - eye); const Tvec3 upN = normalize(up); const Tvec3 s = cross(f, upN); const Tvec3 u = cross(s, f); const Tmat4 M = Tmat4(Tvec4(s[0], u[0], -f[0], T(0)), Tvec4(s[1], u[1], -f[1], T(0)), Tvec4(s[2], u[2], -f[2], T(0)), Tvec4(T(0), T(0), T(0), T(1))); return M * translate(-eye); } template static inline Tmat4 scale(T x, T y, T z) { return Tmat4(Tvec4(x, 0.0f, 0.0f, 0.0f), Tvec4(0.0f, y, 0.0f, 0.0f), Tvec4(0.0f, 0.0f, z, 0.0f), Tvec4(0.0f, 0.0f, 0.0f, 1.0f)); } template static inline Tmat4 scale(const Tvec3& v) { return scale(v[0], v[1], v[2]); } template static inline Tmat4 scale(T x) { return Tmat4(Tvec4(x, 0.0f, 0.0f, 0.0f), Tvec4(0.0f, x, 0.0f, 0.0f), Tvec4(0.0f, 0.0f, x, 0.0f), Tvec4(0.0f, 0.0f, 0.0f, 1.0f)); } template static inline Tmat4 rotate(T angle, T x, T y, T z) { Tmat4 result; const T x2 = x * x; const T y2 = y * y; const T z2 = z * z; float rads = float(angle) * 0.0174532925f; const float c = cosf(rads); const float s = sinf(rads); const float omc = 1.0f - c; result[0] = Tvec4(T(x2 * omc + c), T(y * x * omc + z * s), T(x * z * omc - y * s), T(0)); result[1] = Tvec4(T(x * y * omc - z * s), T(y2 * omc + c), T(y * z * omc + x * s), T(0)); result[2] = Tvec4(T(x * z * omc + y * s), T(y * z * omc - x * s), T(z2 * omc + c), T(0)); result[3] = Tvec4(T(0), T(0), T(0), T(1)); return result; } template static inline Tmat4 rotate(T angle, const vecN& v) { return rotate(angle, v[0], v[1], v[2]); } template static inline Tmat4 rotate(T angle_x, T angle_y, T angle_z) { return rotate(angle_z, 0.0f, 0.0f, 1.0f) * rotate(angle_y, 0.0f, 1.0f, 0.0f) * rotate(angle_x, 1.0f, 0.0f, 0.0f); } #ifdef min #undef min #endif template static inline T min(T a, T b) { return a < b ? a : b; } #ifdef max #undef max #endif template static inline T max(T a, T b) { return a >= b ? a : b; } template static inline vecN min(const vecN& x, const vecN& y) { vecN t; int n; for (n = 0; n < N; n++) { t[n] = min(x[n], y[n]); } return t; } template static inline vecN max(const vecN& x, const vecN& y) { vecN t; int n; for (n = 0; n < N; n++) { t[n] = max(x[n], y[n]); } return t; } template static inline vecN clamp(const vecN& x, const vecN& minVal, const vecN& maxVal) { return min(max(x, minVal), maxVal); } template static inline vecN smoothstep(const vecN& edge0, const vecN& edge1, const vecN& x) { vecN t; t = clamp((x - edge0) / (edge1 - edge0), vecN(T(0)), vecN(T(1))); return t * t * (vecN(T(3)) - vecN(T(2)) * t); } template static inline vecN reflect(const vecN& I, const vecN& N) { return I - 2 * dot(N, I) * N; } template static inline vecN refract(const vecN& I, const vecN& N, T eta) { T d = dot(N, I); T k = T(1) - eta * eta * (T(1) - d * d); if (k < 0.0) { return vecN(0); } else { return eta * I - (eta * d + sqrt(k)) * N; } } template static inline matNM matrixCompMult(const matNM& x, const matNM& y) { matNM result; int i, j; for (j = 0; j < M; ++j) { for (i = 0; i < N; ++i) { result[i][j] = x[i][j] * y[i][j]; } } return result; } template static inline vecN operator*(const vecN& vec, const matNM& mat) { int n, m; vecN result(T(0)); for (m = 0; m < M; m++) { for (n = 0; n < N; n++) { result[n] += vec[m] * mat[n][m]; } } return result; } template static inline vecN operator/(const T s, const vecN& v) { int n; vecN result; for (n = 0; n < N; n++) { result[n] = s / v[n]; } return result; } /* template static inline void quaternionToMatrix(const Tquaternion& q, matNM& m) { m[0][0] = q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]; m[0][1] = T(2) * (q[1] * q[2] + q[0] * q[3]); m[0][2] = T(2) * (q[1] * q[3] - q[0] * q[2]); m[0][3] = 0.0f; m[1][0] = T(2) * (q[1] * q[2] - q[0] * q[3]); m[1][1] = q[0] * q[0] - q[1] * q[1] + q[2] * q[2] - q[3] * q[3]; m[1][2] = T(2) * (q[2] * q[3] + q[0] * q[1]); m[1][3] = 0.0f; m[2][0] = T(2) * (q[1] * q[3] + q[0] * q[2]); m[2][1] = T(2) * (q[2] * q[3] - q[0] * q[1]); m[2][2] = q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]; m[2][3] = 0.0f; m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f; } */ template static inline void quaternionToMatrix(const Tquaternion& q, matNM& m) { m = q.asMatrix(); } template static inline T mix(const T& A, const T& B, typename T::element_type t) { return B + t * (B - A); } template static inline T mix(const T& A, const T& B, const T& t) { return B + t * (B - A); } }; #endif /* __VMATH_H__ */