aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/matrix.c
blob: b2b8815ae0a8c09c8a65896a0c04fbb33e07f290 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <string.h>
#include <math.h>
#include <glib.h>
#include <g3d/vector.h>

EAPI
gboolean g3d_matrix_identity(G3DMatrix *matrix)
{
	static G3DMatrix identity[16] = {
		1.0, 0.0, 0.0, 0.0,
		0.0, 1.0, 0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
		0.0, 0.0, 0.0, 1.0 };

	memcpy(matrix, identity, sizeof(G3DMatrix) * 16);
	return TRUE;
}

EAPI
gboolean g3d_matrix_multiply(G3DMatrix *m1, G3DMatrix *m2, G3DMatrix *rm)
{
	guint32 i, j;
	G3DMatrix matrix[16];

	for(i = 0; i < 4; i ++)
		for(j = 0; j < 4; j ++)
#if 0
			matrix[i * 4 + j] =
				m2[0 * 4 + j] * m1[i * 4 + 0] +
				m2[1 * 4 + j] * m1[i * 4 + 1] +
				m2[2 * 4 + j] * m1[i * 4 + 2] +
				m2[3 * 4 + j] * m1[i * 4 + 3];
#else
			matrix[j * 4 + i] =
				m2[j * 4 + 0] * m1[0 * 4 + i] +
				m2[j * 4 + 1] * m1[1 * 4 + i] +
				m2[j * 4 + 2] * m1[2 * 4 + i] +
				m2[j * 4 + 3] * m1[3 * 4 + i];
#endif

	memcpy(rm, matrix, 16 * sizeof(G3DMatrix));
	return TRUE;
}

EAPI
gboolean g3d_matrix_rotate(G3DFloat angle, G3DVector ax, G3DVector ay,
	G3DVector az, G3DMatrix *rm)
{
	g3d_vector_unify(&ax, &ay, &az);
	g3d_matrix_identity(rm);

#if 0
	rm[0 * 4 + 0] = cos(angle) + (ax * ax) * (1 - cos(angle));
	rm[0 * 4 + 1] = ax * ay * (1 - cos(angle)) - az * sin(angle);
	rm[0 * 4 + 2] = ax * az * (1 - cos(angle)) + ay * sin(angle);

	rm[1 * 4 + 0] = ay * ax * (1 - cos(angle)) + az * sin(angle);
	rm[1 * 4 + 1] = cos(angle) + (ay * ay) * (1 - cos(angle));
	rm[1 * 4 + 2] = ay * az * (1 - cos(angle)) - ax * sin(angle);

	rm[2 * 4 + 0] = az * ax * (1 - cos(angle)) - ay * sin(angle);
	rm[2 * 4 + 1] = az * ay * (1 - cos(angle)) + ax * sin(angle);
	rm[2 * 4 + 2] = cos(angle) + (az * az) * (1 - cos(angle));
#else
	rm[0 * 4 + 0] = cos(angle) + (ax * ax) * (1 - cos(angle));
	rm[1 * 4 + 0] = ax * ay * (1 - cos(angle)) - az * sin(angle);
	rm[2 * 4 + 0] = ax * az * (1 - cos(angle)) + ay * sin(angle);

	rm[0 * 4 + 1] = ay * ax * (1 - cos(angle)) + az * sin(angle);
	rm[1 * 4 + 1] = cos(angle) + (ay * ay) * (1 - cos(angle));
	rm[2 * 4 + 1] = ay * az * (1 - cos(angle)) - ax * sin(angle);

	rm[0 * 4 + 2] = az * ax * (1 - cos(angle)) - ay * sin(angle);
	rm[1 * 4 + 2] = az * ay * (1 - cos(angle)) + ax * sin(angle);
	rm[2 * 4 + 2] = cos(angle) + (az * az) * (1 - cos(angle));
#endif

	return TRUE;
}

EAPI
gboolean g3d_matrix_rotate_xyz(G3DFloat rx, G3DFloat ry, G3DFloat rz,
	G3DMatrix *rm)
{
	G3DMatrix matrix[16];

	g3d_matrix_identity(rm);

	g3d_matrix_rotate(rx, 1.0, 0.0, 0.0, matrix);
	g3d_matrix_multiply(matrix, rm, rm);

	g3d_matrix_rotate(ry, 0.0, 1.0, 0.0, matrix);
	g3d_matrix_multiply(matrix, rm, rm);

	g3d_matrix_rotate(rz, 0.0, 0.0, 1.0, matrix);
	g3d_matrix_multiply(matrix, rm, rm);

	return TRUE;
}

EAPI
gboolean g3d_matrix_translate(G3DVector x, G3DVector y, G3DVector z,
	G3DMatrix *rm)
{
	guint32 i;

#if 0
	for(i = 0; i < 4; i ++)
		rm[i * 4 + 3] =
			rm[i * 4 + 0] * x +
			rm[i * 4 + 1] * y +
			rm[i * 4 + 2] * z +
			rm[i * 4 + 3];
#else
	for(i = 0; i < 4; i ++)
		rm[3 * 4 + i] =
			rm[0 * 4 + i] * x +
			rm[1 * 4 + i] * y +
			rm[2 * 4 + i] * z +
			rm[3 * 4 + i];
#endif
	return TRUE;
}

EAPI
gboolean g3d_matrix_scale(G3DVector x, G3DVector y, G3DVector z, G3DMatrix *rm)
{
	G3DMatrix sm[16];

	g3d_matrix_identity(sm);
	sm[0] = x;
	sm[5] = y;
	sm[10] = z;

	g3d_matrix_multiply(rm, sm, rm);

	return TRUE;
}

EAPI
gboolean g3d_matrix_transpose(G3DMatrix *matrix)
{
	G3DMatrix tmp[16];
	gint32 i, j;

	memcpy(tmp, matrix, 16 * sizeof(G3DMatrix));

	for(i = 0; i < 4; i ++)
		for(j = 0; j < 4; j ++)
			matrix[i * 4 + j] = tmp[j * 4 + i];

	return TRUE;
}

static G3DFloat det2x2(G3DFloat a1, G3DFloat a2, G3DFloat b1, G3DFloat b2)
{
	return a1 * b2 - a2 * b1;
}

static G3DFloat det3x3(G3DFloat a1, G3DFloat a2, G3DFloat a3,
	G3DFloat b1, G3DFloat b2, G3DFloat b3,
	G3DFloat c1, G3DFloat c2, G3DFloat c3)
{
	return
		a1 * det2x2(b2, b3, c2, c3) -
		b1 * det2x2(a2, a3, c2, c3) +
		c1 * det2x2(a2, a3, b2, b3);
}

EAPI
G3DFloat g3d_matrix_determinant(G3DMatrix *matrix)
{
	G3DFloat a1, a2, a3, a4;
	G3DFloat b1, b2, b3, b4;
	G3DFloat c1, c2, c3, c4;
	G3DFloat d1, d2, d3, d4;
#if 0
	a1 = matrix[0 * 4 + 0];
	a2 = matrix[0 * 4 + 1];
	a3 = matrix[0 * 4 + 2];
	a4 = matrix[0 * 4 + 3];

	b1 = matrix[1 * 4 + 0];
	b2 = matrix[1 * 4 + 1];
	b3 = matrix[1 * 4 + 2];
	b4 = matrix[1 * 4 + 3];

	c1 = matrix[2 * 4 + 0];
	c2 = matrix[2 * 4 + 1];
	c3 = matrix[2 * 4 + 2];
	c4 = matrix[2 * 4 + 3];

	d1 = matrix[3 * 4 + 0];
	d2 = matrix[3 * 4 + 1];
	d3 = matrix[3 * 4 + 2];
	d4 = matrix[3 * 4 + 3];
#else
	a1 = matrix[0 * 4 + 0];
	b1 = matrix[0 * 4 + 1];
	c1 = matrix[0 * 4 + 2];
	d1 = matrix[0 * 4 + 3];

	a2 = matrix[1 * 4 + 0];
	b2 = matrix[1 * 4 + 1];
	c2 = matrix[1 * 4 + 2];
	d2 = matrix[1 * 4 + 3];

	a3 = matrix[2 * 4 + 0];
	b3 = matrix[2 * 4 + 1];
	c3 = matrix[2 * 4 + 2];
	d3 = matrix[2 * 4 + 3];

	a4 = matrix[3 * 4 + 0];
	b4 = matrix[3 * 4 + 1];
	c4 = matrix[3 * 4 + 2];
	d4 = matrix[3 * 4 + 3];
#endif
	return
		a1 * det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4) -
		b1 * det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4) +
		c1 * det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4) -
		d1 * det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4);
}

gboolean g3d_matrix_dump(G3DMatrix *matrix)
{
#if DEBUG > 0
	gint32 row;

	for(row = 0; row < 4; row ++)
	{
		g_debug("[Matrix] % 2.2f  % 2.2f  % 2.2f  % 2.2f",
			matrix[0 * 4 + row], matrix[1 * 4 + row],
			matrix[2 * 4 + row], matrix[3 * 4 + row]);
	}

	return TRUE;
#else
	return FALSE;
#endif
}