00001 #ifndef _GLMATH_H_
00002 #define _GLMATH_H_
00003
00004 #ifndef M_PI
00005 #define M_PI 3.14159265358979323846
00006 #endif
00007
00008 #ifndef F_PI
00009 #define F_PI 3.14159265358979323846f
00010 #endif
00011
00012 typedef GLfloat ang_t;
00013
00014 class ang3_t {
00015 public:
00016 ang_t pitch;
00017 ang_t yaw;
00018 ang_t roll;
00019
00020 ang3_t () {
00021 pitch = 0; yaw = 0; roll = 0;
00022 };
00023
00024 ang3_t (ang_t a, ang_t b, ang_t c) {
00025 pitch = a; yaw = b; roll = c;
00026 };
00027
00028 ang_t &operator [] (const long i) {
00029 return *((&pitch) + i);
00030 };
00031 };
00032
00033 typedef GLfloat vec_t;
00034
00035 class vec3_t {
00036 public:
00037 vec_t x, y, z;
00038
00039 vec3_t () {
00040 x = 0; y = 0; z = 0;
00041 };
00042
00043 vec3_t (vec_t a, vec_t b, vec_t c) {
00044 x = a; y = b; z = c;
00045 };
00046
00047 vec_t &operator [] (const long i) {
00048 return *((&x) + i);
00049 };
00050
00051 const bool operator ==(const vec3_t &v) const {
00052 return (v.x == x && v.y == y && v.z == z);
00053 };
00054
00055 const bool operator !=(const vec3_t &v) const {
00056 return !(v == *this);
00057 };
00058
00059 const vec3_t operator -() const {
00060 return vec3_t (-x, -y, -z);
00061 };
00062
00063 const vec3_t &operator =(const vec3_t &v) {
00064 x = v.x;
00065 y = v.y;
00066 z = v.z;
00067 return *this;
00068 };
00069
00070 const vec3_t &operator +=(const vec3_t &v) {
00071 x += v.x;
00072 y += v.y;
00073 z += v.z;
00074 return *this;
00075 };
00076
00077 const vec3_t &operator -=(const vec3_t &v) {
00078 x -= v.x;
00079 y -= v.y;
00080 z -= v.z;
00081 return *this;
00082 };
00083
00084 const vec3_t &operator *=(const vec_t &s) {
00085 x *= s;
00086 y *= s;
00087 z *= s;
00088 return *this;
00089 };
00090
00091 const vec3_t &operator /=(const vec_t &s) {
00092 const vec_t r = 1 / s;
00093
00094 x *= r;
00095 y *= r;
00096 z *= r;
00097 return *this;
00098 };
00099
00100 const vec3_t operator +(const vec3_t &v) const {
00101 return vec3_t (x + v.x, y + v.y, z + v.z);
00102 };
00103
00104 const vec3_t operator -(const vec3_t &v) const {
00105 return vec3_t (x - v.x, y - v.y, z - v.z);
00106 };
00107
00108 const vec3_t operator *(const vec_t &s) const {
00109 return vec3_t (x * s, y * s, z * s);
00110 };
00111
00112 friend inline const vec3_t operator *(const vec_t &s, const vec3_t &v) {
00113 return v * s;
00114 };
00115
00116 const vec3_t operator /(vec_t s) const {
00117 s = 1 / s;
00118 return vec3_t (s * x, s * y, s * z);
00119 };
00120
00121 const vec3_t cross(const vec3_t &v) const {
00122 return vec3_t (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
00123 };
00124
00125 const vec_t dot(const vec3_t &v) const {
00126 return x * v.x + y * v.y + z * v.z;
00127 };
00128
00129 const vec_t length() const {
00130 return (vec_t)sqrt((double)this->dot(*this));
00131 };
00132
00133 const vec3_t unit() const {
00134 return (*this) / length();
00135 };
00136
00137 void normalize() {
00138 (*this) /= length();
00139 };
00140
00141 const bool nearlyEquals(const vec3_t &v, const vec_t e) const {
00142 return fabs(x - v.x) < e && fabs(y - v.y) < e && fabs(z - v.z) < e;
00143 };
00144 };
00145
00146 const vec3_t vec3_origin(0, 0, 0);
00147
00148 const vec3_t ProjectPointOnPlane(vec3_t &p, vec3_t &normal);
00149 const vec3_t RotatePointAroundVector(vec3_t &dir, vec3_t &point, float degrees);
00150
00151 void AngleVectors (ang3_t angles, vec3_t *forward, vec3_t *right, vec3_t *up);
00152
00153 void infinitePerspective(GLdouble fovy, GLdouble aspect, GLdouble znear);
00154
00155 #endif // _GLMATH_H_