File: zmath.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (270 lines) | stat: -rw-r--r-- 7,027 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This file is based on, or a modified version of code from TinyGL (C) 1997-2022 Fabrice Bellard,
 * which is licensed under the MIT license (see LICENSE).
 * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
 */

#ifndef GRAPHICS_TINYGL_ZMATH_H
#define GRAPHICS_TINYGL_ZMATH_H

namespace TinyGL {

#define X _v[0]
#define Y _v[1]
#define Z _v[2]
#define W _v[3]

// Matrix & Vertex
class Vector3 {
public:
	Vector3() { }
	Vector3(float x, float y, float z) {
		X = x;
		Y = y;
		Z = z;
	}

	void normalize();

	float getLength() const { return sqrt(X * X + Y * Y + Z * Z); }

	bool operator==(const Vector3 &other) const {
		return X == other.X && Y == other.Y && Z == other.Z;
	}

	bool operator!=(const Vector3 &other) const {
		return X != other.X || Y != other.Y || Z != other.Z;
	}

	Vector3 operator-() const {
		return Vector3(-X, -Y, -Z);
	}

	Vector3 operator*(float factor) const {
		return Vector3(X * factor, Y * factor, Z * factor);
	}

	Vector3 operator+(const Vector3 &other) const {
		return Vector3(X + other.X, Y + other.Y, Z + other.Z);
	}

	Vector3 operator-(const Vector3 &other) const {
		return Vector3(X - other.X, Y - other.Y, Z - other.Z);
	}

	Vector3 &operator*=(float factor) {
		X *= factor;
		Y *= factor;
		Z *= factor;
		return *this;
	}

	Vector3 &operator+=(float value) {
		X += value;
		Y += value;
		Z += value;
		return *this;
	}

	Vector3 &operator-=(float value) {
		X -= value;
		Y -= value;
		Z -= value;
		return *this;
	}

	float _v[3];
};

class Vector4 {
public:
	Vector4() { }
	Vector4(const Vector3 &vec, float w);

	Vector4(float x, float y, float z, float w) {
		X = x;
		Y = y;
		Z = z;
		W = w;
	}

	bool operator==(const Vector4 &other) const {
		return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
	}

	bool operator!=(const Vector4 &other) const {
		return X != other.X || Y != other.Y || Z != other.Z || W != other.W;
	}

	Vector4 operator-() const {
		return Vector4(-X, -Y, -Z, -W);
	}

	Vector4 operator*(float factor) const {
		return Vector4(X * factor, Y * factor, Z * factor,W * factor);
	}

	Vector4 operator+(const Vector4 &other) const {
		return Vector4(X + other.X, Y + other.Y, Z + other.Z, W + other.W);
	}

	Vector4 operator-(const Vector4 &other) const {
		return Vector4(X - other.X, Y - other.Y, Z - other.Z, W - other.W);
	}

	Vector4 &operator*=(float factor) {
		X *= factor;
		Y *= factor;
		Z *= factor;
		W *= factor;
		return *this;
	}

	Vector4 &operator+=(float value) {
		X += value;
		Y += value;
		Z += value;
		W += value;
		return *this;
	}

	Vector4 &operator-=(float value) {
		X -= value;
		Y -= value;
		Z -= value;
		W -= value;
		return *this;
	}

	float _v[4];
};

class Matrix4 {
public:
	Matrix4() { }

	bool isIdentity() const;

	inline Matrix4 operator+(const Matrix4 &b) const {
		Matrix4 result;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				result._m[i][j] = _m[i][j] + b._m[i][j];
			}
		}
		return result;
	}

	inline Matrix4 operator-(const Matrix4 &b) const {
		Matrix4 result;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				result._m[i][j] = _m[i][j] - b._m[i][j];
			}
		}
		return result;
	}

	inline Matrix4 operator*(const Matrix4 &b) const {
		Matrix4 result;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				float s = 0.0;
				for (int k = 0; k < 4; k++)
					s += _m[i][k] * b._m[k][j];
				result._m[i][j] = s;
			}
		}
		return result;
	}

	inline Matrix4 &operator*=(const Matrix4 &b) {
		Matrix4 a = *this;
		float s;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				s = 0.0;
				for (int k = 0; k < 4; k++)
					s += a._m[i][k] * b._m[k][j];
				_m[i][j] = s;
			}
		}
		return *this;
	}

	void scale(float x, float y, float z);
	void translate(float x, float y, float z);
	void identity();
	void rotation(float t, int);

	void invert();
	void transpose();

	Matrix4 transpose() const;
	Matrix4 inverseOrtho() const;
	Matrix4 inverse() const;

	static Matrix4 frustum(float left, float right, float bottom, float top, float nearp, float farp);

	inline void transform(const Vector3 &vector, Vector3 &out) const {
		out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
		out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
		out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
	}

	// Transform the vector as if this were a 3x3 matrix.
	inline void transform3x3(const Vector3 &vector, Vector3 &out) const {
		out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
		out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
		out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
	}

	// Transform the vector as if this were a 3x3 matrix.
	inline void transform3x3(const Vector4 &vector, Vector3 &out) const {
		out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
		out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
		out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2];
	}

	// Transform the vector as if this were a 3x4 matrix.
	inline void transform3x4(const Vector4 &vector, Vector4 &out) const {
		out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
		out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
		out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
		out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + _m[3][3];
	}

	inline void transform(const Vector4 &vector, Vector4 &out) const {
		out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + vector.W * _m[0][3];
		out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + vector.W * _m[1][3];
		out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + vector.W * _m[2][3];
		out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3];
	}

	float _m[4][4];
};

} // end of namespace TinyGL

#endif