2 blender for the Gameboy Advance
3 Copyright (C) 2021 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
23 #define MAT_STACK_SIZE 4
26 static int32_t mat[MAT_STACK_SIZE][16];
31 xgl_viewport(0, 0, 240, 160);
35 void xgl_viewport(int x, int y, int w, int h)
43 void xgl_push_matrix(void)
47 if(mtop >= MAT_STACK_SIZE - 1) return;
50 memcpy(mat[mtop], mat[prev], sizeof mat[0]);
53 void xgl_pop_matrix(void)
58 static const int32_t id[] = {
65 void xgl_load_identity(void)
70 void xgl_load_matrix(const int32_t *m)
72 memcpy(mat[mtop], m, sizeof mat[0]);
75 #define M(i,j) (((i) << 2) + (j))
76 #define XMUL(a, b) (((a) >> 8) * ((b) >> 8))
77 void xgl_mult_matrix(const int32_t *m2)
81 int32_t *dest = mat[mtop];
83 memcpy(m1, dest, sizeof m1);
87 *dest++ = XMUL(m1[M(0, j)], m2[M(i, 0)]) +
88 XMUL(m1[M(1, j)], m2[M(i, 1)]) +
89 XMUL(m1[M(2, j)], m2[M(i, 2)]) +
90 XMUL(m1[M(3, j)], m2[M(i, 3)]);
95 #define XSIN(x) (int32_t)(sin(x / 65536.0f) * 65536.0f)
96 #define XCOS(x) (int32_t)(cos(x / 65536.0f) * 65536.0f)
98 void xgl_translate(int32_t x, int32_t y, int32_t z)
100 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
107 void xgl_rotate_x(int32_t angle)
109 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
110 int32_t sa = XSIN(angle);
111 int32_t ca = XCOS(angle);
119 void xgl_rotate_y(int32_t angle)
121 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
122 int32_t sa = XSIN(angle);
123 int32_t ca = XCOS(angle);
131 void xgl_rotate_z(int32_t angle)
133 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
134 int32_t sa = XSIN(angle);
135 int32_t ca = XCOS(angle);
143 void xgl_scale(int32_t x, int32_t y, int32_t z)
153 static void xform(struct xvertex *out, const struct xvertex *in, const int32_t *m)
155 out->x = XMUL(m[0], in->x) + XMUL(m[4], in->y) + XMUL(m[8], in->z) + m[12];
156 out->y = XMUL(m[1], in->x) + XMUL(m[5], in->y) + XMUL(m[9], in->z) + m[13];
157 out->z = XMUL(m[2], in->x) + XMUL(m[6], in->y) + XMUL(m[10], in->z) + m[14];
160 /* d = 1.0 / tan(fov/2) */
163 void xgl_draw(int prim, const struct xvertex *varr, int vcount)
166 struct xvertex xv[4];
167 struct pvertex pv[4];
169 while(vcount >= prim) {
171 for(i=0; i<prim; i++) {
172 xform(xv + i, varr, mat[mtop]);
175 xv[i].x = xv[i].x / (xv[i].z >> 8); /* assume aspect: ~2 */
176 xv[i].y = (xv[i].y << 1) / (xv[i].z >> 8);
177 /* projection result is 24.8 */
180 pv[i].x = (((xv[i].x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
181 pv[i].y = (((0x100 - xv[i].y) >> 1) * vp[3]) + (vp[1] << 8);
185 polyfill_flat(pv, prim, cidx);