ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

zmath.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 /*
00024  * This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
00025  * which is licensed under the zlib-license (see LICENSE).
00026  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
00027  */
00028 
00029 #ifndef GRAPHICS_TINYGL_ZMATH_H
00030 #define GRAPHICS_TINYGL_ZMATH_H
00031 
00032 namespace TinyGL {
00033 
00034 #define X _v[0]
00035 #define Y _v[1]
00036 #define Z _v[2]
00037 #define W _v[3]
00038 
00039 // Matrix & Vertex
00040 class Vector3 {
00041 public:
00042     Vector3() { }
00043     Vector3(float x, float y, float z) {
00044         X = x;
00045         Y = y;
00046         Z = z;
00047     }
00048 
00049     void normalize();
00050 
00051     float getLength() const { return sqrt(X * X + Y * Y + Z * Z); }
00052 
00053     bool operator==(const Vector3 &other) const {
00054         return X == other.X && Y == other.Y && Z == other.Z;
00055     }
00056 
00057     bool operator!=(const Vector3 &other) const {
00058         return X != other.X || Y != other.Y || Z != other.Z;
00059     }
00060 
00061     Vector3 operator-() const {
00062         return Vector3(-X, -Y, -Z);
00063     }
00064 
00065     Vector3 operator*(float factor) const {
00066         return Vector3(X * factor, Y * factor, Z * factor);
00067     }
00068 
00069     Vector3 operator+(const Vector3 &other) const {
00070         return Vector3(X + other.X, Y + other.Y, Z + other.Z);
00071     }
00072 
00073     Vector3 operator-(const Vector3 &other) const {
00074         return Vector3(X - other.X, Y - other.Y, Z - other.Z);
00075     }
00076 
00077     Vector3 &operator*=(float factor) {
00078         X *= factor;
00079         Y *= factor;
00080         Z *= factor;
00081         return *this;
00082     }
00083 
00084     Vector3 &operator+=(float value) {
00085         X += value;
00086         Y += value;
00087         Z += value;
00088         return *this;
00089     }
00090 
00091     Vector3 &operator-=(float value) {
00092         X -= value;
00093         Y -= value;
00094         Z -= value;
00095         return *this;
00096     }
00097 
00098     float _v[3];
00099 };
00100 
00101 class Vector4 {
00102 public:
00103     Vector4() { }
00104     Vector4(const Vector3 &vec, float w);
00105 
00106     Vector4(float x, float y, float z, float w) {
00107         X = x;
00108         Y = y;
00109         Z = z;
00110         W = w;
00111     }
00112 
00113     bool operator==(const Vector4 &other) const {
00114         return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
00115     }
00116 
00117     bool operator!=(const Vector4 &other) const {
00118         return X != other.X || Y != other.Y || Z != other.Z || W != other.W;
00119     }
00120 
00121     Vector4 operator-() const {
00122         return Vector4(-X, -Y, -Z, -W);
00123     }
00124 
00125     Vector4 operator*(float factor) const {
00126         return Vector4(X * factor, Y * factor, Z * factor,W * factor);
00127     }
00128 
00129     Vector4 operator+(const Vector4 &other) const {
00130         return Vector4(X + other.X, Y + other.Y, Z + other.Z, W + other.W);
00131     }
00132 
00133     Vector4 operator-(const Vector4 &other) const {
00134         return Vector4(X - other.X, Y - other.Y, Z - other.Z, W - other.W);
00135     }
00136 
00137     Vector4 &operator*=(float factor) {
00138         X *= factor;
00139         Y *= factor;
00140         Z *= factor;
00141         W *= factor;
00142         return *this;
00143     }
00144 
00145     Vector4 &operator+=(float value) {
00146         X += value;
00147         Y += value;
00148         Z += value;
00149         W += value;
00150         return *this;
00151     }
00152 
00153     Vector4 &operator-=(float value) {
00154         X -= value;
00155         Y -= value;
00156         Z -= value;
00157         W -= value;
00158         return *this;
00159     }
00160 
00161     float _v[4];
00162 };
00163 
00164 class Matrix4 {
00165 public:
00166     Matrix4() { }
00167 
00168     bool isIdentity() const;
00169 
00170     inline Matrix4 operator+(const Matrix4 &b) const {
00171         Matrix4 result;
00172         for (int i = 0; i < 4; i++) {
00173             for (int j = 0; j < 4; j++) {
00174                 result._m[i][j] = _m[i][j] + b._m[i][j];
00175             }
00176         }
00177         return result;
00178     }
00179 
00180     inline Matrix4 operator-(const Matrix4 &b) const {
00181         Matrix4 result;
00182         for (int i = 0; i < 4; i++) {
00183             for (int j = 0; j < 4; j++) {
00184                 result._m[i][j] = _m[i][j] - b._m[i][j];
00185             }
00186         }
00187         return result;
00188     }
00189 
00190     inline Matrix4 operator*(const Matrix4 &b) const {
00191         Matrix4 result;
00192         for (int i = 0; i < 4; i++) {
00193             for (int j = 0; j < 4; j++) {
00194                 float s = 0.0;
00195                 for (int k = 0; k < 4; k++)
00196                     s += _m[i][k] * b._m[k][j];
00197                 result._m[i][j] = s;
00198             }
00199         }
00200         return result;
00201     }
00202 
00203     inline Matrix4 &operator*=(const Matrix4 &b) {
00204         Matrix4 a = *this;
00205         float s;
00206         for (int i = 0; i < 4; i++) {
00207             for (int j = 0; j < 4; j++) {
00208                 s = 0.0;
00209                 for (int k = 0; k < 4; k++)
00210                     s += a._m[i][k] * b._m[k][j];
00211                 this->_m[i][j] = s;
00212             }
00213         }
00214         return *this;
00215     }
00216 
00217     void scale(float x, float y, float z);
00218     void translate(float x, float y, float z);
00219     void identity();
00220     void rotation(float t, int);
00221 
00222     void invert();
00223     void transpose();
00224 
00225     Matrix4 transpose() const;
00226     Matrix4 inverseOrtho() const;
00227     Matrix4 inverse() const;
00228 
00229     static Matrix4 frustum(float left, float right, float bottom, float top, float nearp, float farp);
00230 
00231     inline void transform(const Vector3 &vector, Vector3 &out) const {
00232         out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
00233         out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
00234         out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
00235     }
00236 
00237     // Transform the vector as if this were a 3x3 matrix.
00238     inline void transform3x3(const Vector3 &vector, Vector3 &out) const {
00239         out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
00240         out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
00241         out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
00242     }
00243 
00244     // Transform the vector as if this were a 3x3 matrix.
00245     inline void transform3x3(const Vector4 &vector, Vector3 &out) const {
00246         out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
00247         out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
00248         out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
00249     }
00250 
00251     // Transform the vector as if this were a 3x4 matrix.
00252     inline void transform3x4(const Vector4 &vector, Vector4 &out) const {
00253         out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
00254         out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
00255         out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
00256         out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + _m[3][3];
00257     }
00258 
00259     inline void transform(const Vector4 &vector, Vector4 &out) const {
00260         out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + vector.W * _m[0][3];
00261         out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + vector.W * _m[1][3];
00262         out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + vector.W * _m[2][3];
00263         out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3];
00264     }
00265 
00266     float _m[4][4];
00267 };
00268 
00269 } // end of namespace TinyGL
00270 
00271 #endif


Generated on Sat Jul 13 2019 05:01:16 for ResidualVM by doxygen 1.7.1
curved edge   curved edge