all repos — mgba @ 1bc3170755794ad4c0338f54a310c8ff46112646

mGBA Game Boy Advance Emulator

src/ds/matrix.c (view raw)

 1/* Copyright (c) 2013-2017 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include <mgba/internal/ds/matrix.h>
 7
 8static int32_t _dot(const int32_t* col, const int32_t* row) {
 9	int64_t a;
10	int64_t b;
11	int64_t sum;
12	a = col[0];
13	b = row[0];
14	sum = a * b;
15	a = col[4];
16	b = row[1];
17	sum += a * b;
18	a = col[8];
19	b = row[2];
20	sum += a * b;
21	a = col[12];
22	b = row[3];
23	sum += a * b;
24	return sum >> 12LL;
25}
26
27void DSGXMtxIdentity(struct DSGXMatrix* mtx) {
28	memset(mtx, 0, sizeof(*mtx));
29	mtx->m[0] = MTX_ONE;
30	mtx->m[5] = MTX_ONE;
31	mtx->m[10] = MTX_ONE;
32	mtx->m[15] = MTX_ONE;
33}
34
35void DSGXMtxMultiply(struct DSGXMatrix* mtx, const struct DSGXMatrix* m) {
36	struct DSGXMatrix out;
37	out.m[0] = _dot(&mtx->m[0], &m->m[0]);
38	out.m[1] = _dot(&mtx->m[1], &m->m[0]);
39	out.m[2] = _dot(&mtx->m[2], &m->m[0]);
40	out.m[3] = _dot(&mtx->m[3], &m->m[0]);
41	out.m[4] = _dot(&mtx->m[0], &m->m[4]);
42	out.m[5] = _dot(&mtx->m[1], &m->m[4]);
43	out.m[6] = _dot(&mtx->m[2], &m->m[4]);
44	out.m[7] = _dot(&mtx->m[3], &m->m[4]);
45	out.m[8] = _dot(&mtx->m[0], &m->m[8]);
46	out.m[9] = _dot(&mtx->m[1], &m->m[8]);
47	out.m[10] = _dot(&mtx->m[2], &m->m[8]);
48	out.m[11] = _dot(&mtx->m[3], &m->m[8]);
49	out.m[12] = _dot(&mtx->m[0], &m->m[12]);
50	out.m[13] = _dot(&mtx->m[1], &m->m[12]);
51	out.m[14] = _dot(&mtx->m[2], &m->m[12]);
52	out.m[15] = _dot(&mtx->m[3], &m->m[12]);
53	*mtx = out;
54}
55
56void DSGXMtxScale(struct DSGXMatrix* mtx, const int32_t* m) {
57	struct DSGXMatrix s = {
58		.m = {
59			m[0], 0, 0, 0,
60			0, m[1], 0, 0,
61			0, 0, m[2], 0,
62			0, 0, 0, MTX_ONE
63		}
64	};
65	DSGXMtxMultiply(mtx, &s);
66}
67
68void DSGXMtxTranslate(struct DSGXMatrix* mtx, const int32_t* m) {
69	struct DSGXMatrix t = {
70		.m = {
71			MTX_ONE, 0, 0, 0,
72			0, MTX_ONE, 0, 0,
73			0, 0, MTX_ONE, 0,
74			m[0], m[1], m[2], MTX_ONE
75		}
76	};
77	DSGXMtxMultiply(mtx, &t);
78}