aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/star_control/fpose.cpp
blob: 5e98344b696accaf98b98afd0789a86d95d818c2 (plain)
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
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "titanic/star_control/fpose.h"
#include "titanic/star_control/matrix_transform.h"
#include "titanic/star_control/matrix_inv.h"

#include "common/math.h"

namespace Titanic {

// Non-member functions

void fposeProd(const FPose &a, const FPose &m, FPose &C) {
	C._row1._x = a._row1._y * m._row2._x + a._row1._z * m._row3._x + a._row1._x * m._row1._x;
	C._row1._y = a._row1._x * m._row1._y + m._row2._y * a._row1._y + m._row3._y * a._row1._z;
	C._row1._z = a._row1._x * m._row1._z + a._row1._y * m._row2._z + a._row1._z * m._row3._z;
	C._row2._x = m._row1._x * a._row2._x + m._row3._x * a._row2._z + m._row2._x * a._row2._y;
	C._row2._y = m._row3._y * a._row2._z + m._row1._y * a._row2._x + m._row2._y * a._row2._y;
	C._row2._z = a._row2._z * m._row3._z + a._row2._x * m._row1._z + a._row2._y * m._row2._z;
	C._row3._x = m._row1._x * a._row3._x + a._row3._z * m._row3._x + a._row3._y * m._row2._x;
	C._row3._y = a._row3._y * m._row2._y + a._row3._z * m._row3._y + a._row3._x * m._row1._y;
	C._row3._z = a._row3._x * m._row1._z + a._row3._y * m._row2._z + a._row3._z * m._row3._z;
	C._vector._x = a._vector._x * m._row1._x + a._vector._y * m._row2._x + a._vector._z * m._row3._x + m._vector._x;
	C._vector._y = a._vector._x * m._row1._y + a._vector._y * m._row2._y + a._vector._z * m._row3._y + m._vector._y;
	C._vector._z = a._vector._x * m._row1._z + a._vector._y * m._row2._z + a._vector._z * m._row3._z + m._vector._z;
}

// Member functions

FPose::FPose() {
	clear();
}

FPose::FPose(Axis axis, float amount) {
	setRotationMatrix(axis, amount);
}

FPose::FPose(const FPose &src) : FMatrix() {
	copyFrom(src);
}

FPose::FPose(const FPose &s1, const FPose &s2) {
	fposeProd(s1, s2, *this);
}

FPose::FPose(int mode, const FVector &src) {
	switch (mode) {
	case 0:
		_row1._x = 1.0;
		_row2._y = 1.0;
		_row3._z = 1.0;
		_vector = src;
		break;

	case 1:
		_row1._x = src._x;
		_row2._y = src._y;
		_row3._z = src._z;
		break;

	default:
		_row1._x = 1.0;
		_row2._y = 1.0;
		_row3._z = 1.0;
		break;
	}
}

void FPose::identity() {
	FMatrix::identity();
	_vector.clear();
}

// Source: https://en.wikipedia.org/wiki/Rotation_matrix
void FPose::setRotationMatrix(Axis axis, float amount) {
	float sinVal = sin(Common::deg2rad<float>(amount));
	float cosVal = cos(Common::deg2rad<float>(amount));

	switch (axis) {
	case X_AXIS:
		_row1._x = 1.0;
		_row1._y = 0.0;
		_row1._z = 0.0;
		_row2._x = 0.0;
		_row2._y = cosVal;
		_row2._z = sinVal;
		_row3._x = 0.0;
		_row3._y = -sinVal;
		_row3._z = cosVal;
		break;

	case Y_AXIS:
		_row1._x = cosVal;
		_row1._y = 0.0;
		_row1._z = -sinVal;
		_row2._x = 0.0;
		_row2._y = 1.0;
		_row2._z = 0.0;
		_row3._x = sinVal;
		_row3._y = 0.0;
		_row3._z = cosVal;
		break;

	case Z_AXIS:
		_row1._x = cosVal;
		_row1._y = sinVal;
		_row1._z = 0.0;
		_row2._x = -sinVal;
		_row2._y = cosVal;
		_row2._z = 0.0;
		_row3._x = 0.0;
		_row3._y = 0.0;
		_row3._z = 1.0;
		break;

	default:
		break;
	}

	_vector.clear();
}

void FPose::rotVectAxisY(double angleDeg) {
	_row1.rotVectAxisY(angleDeg);
	_row2.rotVectAxisY(angleDeg);
	_row3.rotVectAxisY(angleDeg);
	_vector.rotVectAxisY(angleDeg);
}

void FPose::copyFrom(const FPose &src) {
	_row1 = src._row1;
	_row2 = src._row2;
	_row3 = src._row3;
	_vector = src._vector;
}

void FPose::copyFrom(const FMatrix &src) {
	_row1 = src._row1;
	_row2 = src._row2;
	_row3 = src._row3;
}

FPose FPose::inverseTransform() const {
	FPose matrix_inv;

	matrix_inv._row1._x = _row1._x;
	matrix_inv._row2._x = _row1._y;
	matrix_inv._row3._x = _row1._z;
	matrix_inv._row1._y = _row2._x;
	matrix_inv._row2._y = _row2._y;
	matrix_inv._row3._y = _row2._z;
	matrix_inv._row1._z = _row3._x;
	matrix_inv._row2._z = _row3._y;
	matrix_inv._row3._z = _row3._z;

	float A[16]={_row1._x,_row1._y,_row1._z, 0.0,
				 _row2._x,_row2._y,_row2._z, 0.0,
				 _row3._x,_row3._y,_row3._z, 0.0,
				 _vector._x,_vector._y,_vector._z, 1.0};
	// Inverse matrix
	float B[16]={};

	// B contains inverse of A
	matrix4Inverse<float>(A,B);	
	matrix_inv._vector._x=B[12];
	matrix_inv._vector._y=B[13];
	matrix_inv._vector._z=B[14];

	return matrix_inv;
}

//TODO: Check math and provide source
void FPose::loadTransform(const CMatrixTransform &src) {
	double total = src.fn1();
	double factor = (total <= 0.0) ? 0.0 : 2.0 / total;
	FVector temp1V = src._vector * factor;
	FVector temp2V = temp1V * src._vector;

	double val1 = temp1V._y * src._vector._x;
	double val2 = temp1V._z * src._vector._x;
	double val3 = temp1V._z * src._vector._y;
	double val4 = temp1V._x * src._field0;
	double val5 = temp1V._y * src._field0;
	double val6 = temp1V._z * src._field0;

	_row1._x = 1.0 - (temp2V._z + temp2V._y);
	_row1._y = val1 + val6;
	_row1._z = val2 - val5;
	_row2._x = val1 - val6;
	_row2._y = 1.0 - (temp2V._z + temp2V._x);
	_row2._z = val3 + val4;
	_row3._x = val2 + val5;
	_row3._y = val3 - val4;
	_row3._z = 1.0 - (temp2V._y + temp2V._x);
	_vector._x = 0;
	_vector._y = 0;
	_vector._z = 0;
}

FPose FPose::compose(const FMatrix &m) {
	FPose dm;
	FPose am;
	am._row1 = m._row1;
	am._row2 = m._row2;
	am._row3 = m._row3;

	fposeProd(*this, am, dm);

	return dm;
}

FPose FPose::compose2(const FPose &m) {
	FPose dm;
	dm._row1 = _row1.matProdRowVect(m);
	dm._row2 = _row2.matProdRowVect(m);
	dm._row3 = _row3.matProdRowVect(m);
	dm._vector = _vector.matProdRowVect(m);

	return dm;
}

} // End of namespace Titanic