ScummVM API documentation
zmath.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This file is based on, or a modified version of code from TinyGL (C) 1997-2022 Fabrice Bellard,
24  * which is licensed under the MIT license (see LICENSE).
25  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
26  */
27 
28 #ifndef GRAPHICS_TINYGL_ZMATH_H
29 #define GRAPHICS_TINYGL_ZMATH_H
30 
31 namespace TinyGL {
32 
33 #define X _v[0]
34 #define Y _v[1]
35 #define Z _v[2]
36 #define W _v[3]
37 
38 // Matrix & Vertex
39 class Vector3 {
40 public:
41  Vector3() { }
42  Vector3(float x, float y, float z) {
43  X = x;
44  Y = y;
45  Z = z;
46  }
47 
48  void normalize();
49 
50  float getLength() const { return sqrt(X * X + Y * Y + Z * Z); }
51 
52  bool operator==(const Vector3 &other) const {
53  return X == other.X && Y == other.Y && Z == other.Z;
54  }
55 
56  bool operator!=(const Vector3 &other) const {
57  return X != other.X || Y != other.Y || Z != other.Z;
58  }
59 
60  Vector3 operator-() const {
61  return Vector3(-X, -Y, -Z);
62  }
63 
64  Vector3 operator*(float factor) const {
65  return Vector3(X * factor, Y * factor, Z * factor);
66  }
67 
68  Vector3 operator+(const Vector3 &other) const {
69  return Vector3(X + other.X, Y + other.Y, Z + other.Z);
70  }
71 
72  Vector3 operator-(const Vector3 &other) const {
73  return Vector3(X - other.X, Y - other.Y, Z - other.Z);
74  }
75 
76  Vector3 &operator*=(float factor) {
77  X *= factor;
78  Y *= factor;
79  Z *= factor;
80  return *this;
81  }
82 
83  Vector3 &operator+=(float value) {
84  X += value;
85  Y += value;
86  Z += value;
87  return *this;
88  }
89 
90  Vector3 &operator-=(float value) {
91  X -= value;
92  Y -= value;
93  Z -= value;
94  return *this;
95  }
96 
97  float _v[3];
98 };
99 
100 class Vector4 {
101 public:
102  Vector4() { }
103  Vector4(const Vector3 &vec, float w);
104 
105  Vector4(float x, float y, float z, float w) {
106  X = x;
107  Y = y;
108  Z = z;
109  W = w;
110  }
111 
112  bool operator==(const Vector4 &other) const {
113  return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
114  }
115 
116  bool operator!=(const Vector4 &other) const {
117  return X != other.X || Y != other.Y || Z != other.Z || W != other.W;
118  }
119 
120  Vector4 operator-() const {
121  return Vector4(-X, -Y, -Z, -W);
122  }
123 
124  Vector4 operator*(float factor) const {
125  return Vector4(X * factor, Y * factor, Z * factor,W * factor);
126  }
127 
128  Vector4 operator+(const Vector4 &other) const {
129  return Vector4(X + other.X, Y + other.Y, Z + other.Z, W + other.W);
130  }
131 
132  Vector4 operator-(const Vector4 &other) const {
133  return Vector4(X - other.X, Y - other.Y, Z - other.Z, W - other.W);
134  }
135 
136  Vector4 &operator*=(float factor) {
137  X *= factor;
138  Y *= factor;
139  Z *= factor;
140  W *= factor;
141  return *this;
142  }
143 
144  Vector4 &operator+=(float value) {
145  X += value;
146  Y += value;
147  Z += value;
148  W += value;
149  return *this;
150  }
151 
152  Vector4 &operator-=(float value) {
153  X -= value;
154  Y -= value;
155  Z -= value;
156  W -= value;
157  return *this;
158  }
159 
160  float _v[4];
161 };
162 
163 class Matrix4 {
164 public:
165  Matrix4() { }
166 
167  bool isIdentity() const;
168 
169  inline Matrix4 operator+(const Matrix4 &b) const {
170  Matrix4 result;
171  for (int i = 0; i < 4; i++) {
172  for (int j = 0; j < 4; j++) {
173  result._m[i][j] = _m[i][j] + b._m[i][j];
174  }
175  }
176  return result;
177  }
178 
179  inline Matrix4 operator-(const Matrix4 &b) const {
180  Matrix4 result;
181  for (int i = 0; i < 4; i++) {
182  for (int j = 0; j < 4; j++) {
183  result._m[i][j] = _m[i][j] - b._m[i][j];
184  }
185  }
186  return result;
187  }
188 
189  inline Matrix4 operator*(const Matrix4 &b) const {
190  Matrix4 result;
191  for (int i = 0; i < 4; i++) {
192  for (int j = 0; j < 4; j++) {
193  float s = 0.0;
194  for (int k = 0; k < 4; k++)
195  s += _m[i][k] * b._m[k][j];
196  result._m[i][j] = s;
197  }
198  }
199  return result;
200  }
201 
202  inline Matrix4 &operator*=(const Matrix4 &b) {
203  Matrix4 a = *this;
204  float s;
205  for (int i = 0; i < 4; i++) {
206  for (int j = 0; j < 4; j++) {
207  s = 0.0;
208  for (int k = 0; k < 4; k++)
209  s += a._m[i][k] * b._m[k][j];
210  _m[i][j] = s;
211  }
212  }
213  return *this;
214  }
215 
216  void scale(float x, float y, float z);
217  void translate(float x, float y, float z);
218  void identity();
219  void rotation(float t, int);
220 
221  void invert();
222  void transpose();
223 
224  Matrix4 transpose() const;
225  Matrix4 inverseOrtho() const;
226  Matrix4 inverse() const;
227 
228  static Matrix4 frustum(float left, float right, float bottom, float top, float nearp, float farp);
229 
230  inline void transform(const Vector3 &vector, Vector3 &out) const {
231  out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
232  out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
233  out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
234  }
235 
236  // Transform the vector as if this were a 3x3 matrix.
237  inline void transform3x3(const Vector3 &vector, Vector3 &out) const {
238  out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
239  out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
240  out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
241  }
242 
243  // Transform the vector as if this were a 3x3 matrix.
244  inline void transform3x3(const Vector4 &vector, Vector3 &out) const {
245  out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
246  out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
247  out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
248  }
249 
250  // Transform the vector as if this were a 3x4 matrix.
251  inline void transform3x4(const Vector4 &vector, Vector4 &out) const {
252  out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
253  out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
254  out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
255  out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + _m[3][3];
256  }
257 
258  inline void transform(const Vector4 &vector, Vector4 &out) const {
259  out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + vector.W * _m[0][3];
260  out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + vector.W * _m[1][3];
261  out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + vector.W * _m[2][3];
262  out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3];
263  }
264 
265  float _m[4][4];
266 };
267 
268 } // end of namespace TinyGL
269 
270 #endif
Definition: zmath.h:100
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: colormasks.h:27
Definition: zmath.h:163
Definition: zmath.h:39