/* * * * * * * * * * * * * Author's note * * * * * * * * * * * *\ * _ _ _ _ _ _ _ _ _ _ _ _ * * |_| |_| |_| |_| |_|_ _|_| |_| |_| _|_|_|_|_| * * |_|_ _ _|_| |_| |_| |_|_|_|_|_| |_| |_| |_|_ _ _ * * |_|_|_|_|_| |_| |_| |_| |_| |_| |_| |_| |_|_|_|_ * * |_| |_| |_|_ _ _|_| |_| |_| |_|_ _ _|_| _ _ _ _|_| * * |_| |_| |_|_|_| |_| |_| |_|_|_| |_|_|_|_| * * * * http://www.humus.name * * * * This file is a part of the work done by Humus. You are free to * * use the code in any way you like, modified, unmodified or copied * * into your own work. However, I expect you to respect these points: * * - If you use this file and its contents unmodified, or use a major * * part of this file, please credit the author and leave this note. * * - For use in anything commercial, please request my approval. * * - Share your work and ideas too as much as you can. * * * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // I would like to thank Humus for giving away this code, it // is very generous and makes my life easier. // - Bill Whitacre #ifndef _VECTOR_H #define _VECTOR_H #include "ravenmath.h" #include #include #define POSITIVE_X 0 #define NEGATIVE_X 1 #define POSITIVE_Y 2 #define NEGATIVE_Y 3 #define POSITIVE_Z 4 #define NEGATIVE_Z 5 struct half { unsigned short sh; half(){}; half(const float x); operator float () const; }; /* --------------------------------------------------------------------------------- */ struct Vector2 { float x, y; Vector2(){} Vector2(const float ixy){ x = y = ixy; } Vector2(const float ix, const float iy){ x = ix; y = iy; } //operator const float *() const { return &x; } operator float *() const { return (float *) &x; } // float &operator[](unsigned int index){ return *(((float *) &x) + index); } void operator += (const float s); void operator += (const Vector2 &v); void operator -= (const float s); void operator -= (const Vector2 &v); void operator *= (const float s); void operator *= (const Vector2 &v); void operator /= (const float s); void operator /= (const Vector2 &v); }; Vector2 operator + (const Vector2 &u, const Vector2 &v); Vector2 operator + (const Vector2 &v, const float s); Vector2 operator + (const float s, const Vector2 &v); Vector2 operator - (const Vector2 &u, const Vector2 &v); Vector2 operator - (const Vector2 &v, const float s); Vector2 operator - (const float s, const Vector2 &v); Vector2 operator - (const Vector2 &v); Vector2 operator * (const Vector2 &u, const Vector2 &v); Vector2 operator * (const float s, const Vector2 &v); Vector2 operator * (const Vector2 &v, const float s); Vector2 operator / (const Vector2 &u, const Vector2 &v); Vector2 operator / (const Vector2 &v, const float s); Vector2 operator / (const float s, const Vector2 &v); bool operator == (const Vector2 &u, const Vector2 &v); /* --------------------------------------------------------------------------------- */ struct Vector3 { float x, y, z; Vector3(){} Vector3(const float ixyz){ x = y = z = ixyz; } Vector3(const float ix, const float iy, const float iz){ x = ix; y = iy; z = iz; } Vector3(const Vector2 iv, const float iz){ x = iv.x; y = iv.y; z = iz; } Vector3(const float ix, const Vector2 iv){ x = ix; y = iv.x; z = iv.y; } //operator const float *() const { return &x; } operator float *() const { return (float *) &x; } // float &operator[](unsigned int index){ return *(((float *) &x) + index); } Vector2 xy() const { return Vector2(x, y); } Vector2 yz() const { return Vector2(y, z); } Vector2 xz() const { return Vector2(x, z); } void operator += (const float s); void operator += (const Vector3 &v); void operator -= (const float s); void operator -= (const Vector3 &v); void operator *= (const float s); void operator *= (const Vector3 &v); void operator /= (const float s); void operator /= (const Vector3 &v); }; Vector3 operator + (const Vector3 &u, const Vector3 &v); Vector3 operator + (const Vector3 &v, const float s); Vector3 operator + (const float s, const Vector3 &v); Vector3 operator - (const Vector3 &u, const Vector3 &v); Vector3 operator - (const Vector3 &v, const float s); Vector3 operator - (const float s, const Vector3 &v); Vector3 operator - (const Vector3 &v); Vector3 operator * (const Vector3 &u, const Vector3 &v); Vector3 operator * (const float s, const Vector3 &v); Vector3 operator * (const Vector3 &v, const float s); Vector3 operator / (const Vector3 &u, const Vector3 &v); Vector3 operator / (const Vector3 &v, const float s); Vector3 operator / (const float s, const Vector3 &v); bool operator == (const Vector3 &u, const Vector3 &v); /* --------------------------------------------------------------------------------- */ struct Vector4 { float x, y, z, w; Vector4(){} Vector4(const float ixyzw){ x = y = z = w = ixyzw; } Vector4(const float ix, const float iy, const float iz, const float iw){ x = ix; y = iy; z = iz; w = iw; } Vector4(const Vector2 iv, const float iz, const float iw){ x = iv.x; y = iv.y; z = iz; w = iw; } Vector4(const float ix, const Vector2 iv, const float iw){ x = ix; y = iv.x; z = iv.y; w = iw; } Vector4(const float ix, const float iy, const Vector2 iv){ x = ix; y = iy; z = iv.x; w = iv.y; } Vector4(const Vector2 iv0, const Vector2 iv1){ x = iv0.x; y = iv0.y; z = iv1.x; w = iv1.y; } Vector4(const Vector3 iv, const float iw){ x = iv.x; y = iv.y; z = iv.z; w = iw; } Vector4(const float ix, const Vector3 iv){ x = ix; y = iv.x; z = iv.y; w = iv.z; } //operator const float *() const { return &x; } operator float *() const { return (float *) &x; } // float &operator[](unsigned int index){ return *(((float *) &x) + index); } Vector2 xy() const { return Vector2(x, y); } Vector2 xz() const { return Vector2(x, z); } Vector2 xw() const { return Vector2(x, w); } Vector2 yz() const { return Vector2(y, z); } Vector2 yw() const { return Vector2(y, w); } Vector2 zw() const { return Vector2(z, w); } Vector3 xyz() const { return Vector3(x, y, z); } Vector3 yzw() const { return Vector3(y, z, w); } void operator += (const float s); void operator += (const Vector4 &v); void operator -= (const float s); void operator -= (const Vector4 &v); void operator *= (const float s); void operator *= (const Vector4 &v); void operator /= (const float s); void operator /= (const Vector4 &v); }; Vector4 operator + (const Vector4 &u, const Vector4 &v); Vector4 operator + (const Vector4 &v, const float s); Vector4 operator + (const float s, const Vector4 &v); Vector4 operator - (const Vector4 &u, const Vector4 &v); Vector4 operator - (const Vector4 &v, const float s); Vector4 operator - (const float s, const Vector4 &v); Vector4 operator - (const Vector4 &v); Vector4 operator * (const Vector4 &u, const Vector4 &v); Vector4 operator * (const float s, const Vector4 &v); Vector4 operator * (const Vector4 &v, const float s); Vector4 operator / (const Vector4 &u, const Vector4 &v); Vector4 operator / (const Vector4 &v, const float s); Vector4 operator / (const float s, const Vector4 &v); bool operator == (const Vector4 &u, const Vector4 &v); /* --------------------------------------------------------------------------------- */ float dot(const Vector2 &u, const Vector2 &v); float dot(const Vector3 &u, const Vector3 &v); float dot(const Vector4 &u, const Vector4 &v); float lerp(const float u, const float v, const float x); Vector2 lerp(const Vector2 &u, const Vector2 &v, const float x); Vector3 lerp(const Vector3 &u, const Vector3 &v, const float x); Vector4 lerp(const Vector4 &u, const Vector4 &v, const float x); Vector2 lerp(const Vector2 &u, const Vector2 &v, const Vector2 &x); Vector3 lerp(const Vector3 &u, const Vector3 &v, const Vector3 &x); Vector4 lerp(const Vector4 &u, const Vector4 &v, const Vector4 &x); float cerp(const float u0, const float u1, const float u2, const float u3, float x); Vector2 cerp(const Vector2 &u0, const Vector2 &u1, const Vector2 &u2, const Vector2 &u3, float x); Vector3 cerp(const Vector3 &u0, const Vector3 &u1, const Vector3 &u2, const Vector3 &u3, float x); Vector4 cerp(const Vector4 &u0, const Vector4 &u1, const Vector4 &u2, const Vector4 &u3, float x); float sign(const float v); Vector2 sign(const Vector2 &v); Vector3 sign(const Vector3 &v); Vector4 sign(const Vector4 &v); float clamp(const float v, const float c0, const float c1); Vector2 clamp(const Vector2 &v, const float c0, const float c1); Vector2 clamp(const Vector2 &v, const Vector2 &c0, const Vector2 &c1); Vector3 clamp(const Vector3 &v, const float c0, const float c1); Vector3 clamp(const Vector3 &v, const Vector3 &c0, const Vector3 &c1); Vector4 clamp(const Vector4 &v, const float c0, const float c1); Vector4 clamp(const Vector4 &v, const Vector4 &c0, const Vector4 &c1); //#define clamp(x, lo, hi) max(min(x, hi), lo) #define saturate(x) clamp(x, 0, 1) Vector2 normalize(const Vector2 &v); Vector3 normalize(const Vector3 &v); Vector4 normalize(const Vector4 &v); Vector2 fastNormalize(const Vector2 &v); Vector3 fastNormalize(const Vector3 &v); Vector4 fastNormalize(const Vector4 &v); float length(const Vector2 &v); float length(const Vector3 &v); float length(const Vector4 &v); Vector3 reflect(const Vector3 &v, const Vector3 &normal); float distance(const Vector2 &u, const Vector2 &v); float distance(const Vector3 &u, const Vector3 &v); float distance(const Vector4 &u, const Vector4 &v); float planeDistance(const Vector3 &normal, const float offset, const Vector3 &point); float planeDistance(const Vector4 &plane, const Vector3 &point); float sCurve(const float t); Vector3 cross(const Vector3 &u, const Vector3 &v); float lineProjection(const Vector3 &line0, const Vector3 &line1, const Vector3 &point); unsigned int toRGBA(const Vector4 &u); unsigned int toBGRA(const Vector4 &u); Vector3 rgbeToRGB(unsigned char *rgbe); unsigned int rgbToRGBE8(const Vector3 &rgb); unsigned int rgbToRGB9E5(const Vector3 &rgb); /* --------------------------------------------------------------------------------- */ struct Matrix2x2 { Vector2 rows[2]; Matrix2x2(){} Matrix2x2(const Vector2 &row0, const Vector2 &row1){ rows[0] = row0; rows[1] = row1; } Matrix2x2(const float m00, const float m01, const float m10, const float m11){ rows[0] = Vector2(m00, m01); rows[1] = Vector2(m10, m11); } operator const float *() const { return (const float *) rows; } }; Matrix2x2 operator + (const Matrix2x2 &m, const Matrix2x2 &n); Matrix2x2 operator - (const Matrix2x2 &m, const Matrix2x2 &n); Matrix2x2 operator - (const Matrix2x2 &m); Matrix2x2 operator * (const Matrix2x2 &m, const Matrix2x2 &n); Vector2 operator * (const Matrix2x2 &m, const Vector2 &v); Matrix2x2 operator * (const Matrix2x2 &m, const float x); Matrix2x2 transpose(const Matrix2x2 &m); float det(const Matrix2x2 &m); Matrix2x2 operator ! (const Matrix2x2 &m); /* --------------------------------------------------------------------------------- */ struct Matrix3x3 { Vector3 rows[3]; Matrix3x3(){} Matrix3x3(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2){ rows[0] = row0; rows[1] = row1; rows[2] = row2; } Matrix3x3(const float m00, const float m01, const float m02, const float m10, const float m11, const float m12, const float m20, const float m21, const float m22){ rows[0] = Vector3(m00, m01, m02); rows[1] = Vector3(m10, m11, m12); rows[2] = Vector3(m20, m21, m22); } operator const float *() const { return (const float *) rows; } }; Matrix3x3 operator + (const Matrix3x3 &m, const Matrix3x3 &n); Matrix3x3 operator - (const Matrix3x3 &m, const Matrix3x3 &n); Matrix3x3 operator - (const Matrix3x3 &m); Matrix3x3 operator * (const Matrix3x3 &m, const Matrix3x3 &n); Vector3 operator * (const Matrix3x3 &m, const Vector3 &v); Matrix3x3 operator * (const Matrix3x3 &m, const float x); Matrix3x3 transpose(const Matrix3x3 &m); float det(const Matrix3x3 &m); Matrix3x3 operator ! (const Matrix3x3 &m); /* --------------------------------------------------------------------------------- */ struct Matrix4x4 { Vector4 rows[4]; Matrix4x4(){} Matrix4x4(const Vector4 &row0, const Vector4 &row1, const Vector4 &row2, const Vector4 &row3){ rows[0] = row0; rows[1] = row1; rows[2] = row2; rows[3] = row3; } Matrix4x4(const float m00, const float m01, const float m02, const float m03, const float m10, const float m11, const float m12, const float m13, const float m20, const float m21, const float m22, const float m23, const float m30, const float m31, const float m32, const float m33){ rows[0] = Vector4(m00, m01, m02, m03); rows[1] = Vector4(m10, m11, m12, m13); rows[2] = Vector4(m20, m21, m22, m23); rows[3] = Vector4(m30, m31, m32, m33); } operator const float *() const { return (const float *) rows; } void translate(const Vector3 &v); }; Matrix4x4 operator + (const Matrix4x4 &m, const Matrix4x4 &n); Matrix4x4 operator - (const Matrix4x4 &m, const Matrix4x4 &n); Matrix4x4 operator - (const Matrix4x4 &m); Matrix4x4 operator * (const Matrix4x4 &m, const Matrix4x4 &n); Vector4 operator * (const Matrix4x4 &m, const Vector4 &v); Matrix4x4 operator * (const Matrix4x4 &m, const float x); Matrix4x4 transpose(const Matrix4x4 &m); Matrix4x4 operator ! (const Matrix4x4 &m); /* --------------------------------------------------------------------------------- */ Matrix2x2 rotate(const float angle); Matrix4x4 rotateX(const float angle); Matrix4x4 rotateY(const float angle); Matrix4x4 rotateZ(const float angle); Matrix4x4 rotateXY(const float angleX, const float angleY); Matrix4x4 rotateYX(const float angleX, const float angleY); Matrix4x4 rotateZXY(const float angleX, const float angleY, const float angleZ); Matrix4x4 translate(const Vector3 &v); Matrix4x4 translate(const float x, const float y, const float z); Matrix4x4 scale(const float x, const float y, const float z); Matrix4x4 perspectiveMatrix(const float fov, const float zNear, const float zFar); Matrix4x4 perspectiveMatrixX(const float fov, const int width, const int height, const float zNear, const float zFar); Matrix4x4 perspectiveMatrixY(const float fov, const int width, const int height, const float zNear, const float zFar); Matrix4x4 orthoMatrixX(const float left, const float right, const float top, const float bottom, const float zNear, const float zFar); Matrix4x4 toD3DProjection(const Matrix4x4 &m); Matrix4x4 toGLProjection(const Matrix4x4 &m); Matrix4x4 cubeViewMatrix(const unsigned int side); Matrix4x4 cubeProjectionMatrixGL(const float zNear, const float zFar); Matrix4x4 cubeProjectionMatrixD3D(const float zNear, const float zFar); Matrix2x2 identity2(); Matrix3x3 identity3(); Matrix4x4 identity4(); typedef Vector2 float2; typedef Vector3 float3; typedef Vector4 float4; typedef Matrix2x2 float2x2; typedef Matrix3x3 float3x3; typedef Matrix4x4 float4x4; #endif