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/>.
24 #define MAT_STACK_SIZE 4
27 static int32_t mat[MAT_STACK_SIZE][16];
29 static unsigned int opt;
30 static int32_t ldir[3];
32 static void draw_ptlines(int prim, const struct xvertex *varr, int vcount);
37 xgl_viewport(0, 0, 240, 160);
40 ldir[0] = ldir[1] = 0;
44 void xgl_enable(unsigned int o)
49 void xgl_disable(unsigned int o)
54 void xgl_viewport(int x, int y, int w, int h)
62 void xgl_push_matrix(void)
66 if(mtop >= MAT_STACK_SIZE - 1) return;
69 memcpy(mat[mtop], mat[prev], sizeof mat[0]);
72 void xgl_pop_matrix(void)
77 static int32_t id[] = {
84 void xgl_load_identity(void)
86 memcpy(mat[mtop], id, sizeof mat[0]);
89 void xgl_load_matrix(const int32_t *m)
91 memcpy(mat[mtop], m, sizeof mat[0]);
94 void xgl_get_matrix(int32_t *m)
96 memcpy(m, mat[mtop], sizeof mat[0]);
99 #define M(i,j) (((i) << 2) + (j))
100 #define XMUL(a, b) (((a) >> 8) * ((b) >> 8))
101 void xgl_mult_matrix(const int32_t *m2)
105 int32_t *dest = mat[mtop];
107 memcpy(m1, dest, sizeof m1);
111 *dest++ = XMUL(m1[M(0, j)], m2[M(i, 0)]) +
112 XMUL(m1[M(1, j)], m2[M(i, 1)]) +
113 XMUL(m1[M(2, j)], m2[M(i, 2)]) +
114 XMUL(m1[M(3, j)], m2[M(i, 3)]);
119 #define XSIN(x) (int32_t)(sin(x / 65536.0f) * 65536.0f)
120 #define XCOS(x) (int32_t)(cos(x / 65536.0f) * 65536.0f)
122 void xgl_translate(int32_t x, int32_t y, int32_t z)
124 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
131 void xgl_rotate_x(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_rotate_y(int32_t angle)
145 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
146 int32_t sa = XSIN(angle);
147 int32_t ca = XCOS(angle);
155 void xgl_rotate_z(int32_t angle)
157 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
158 int32_t sa = XSIN(angle);
159 int32_t ca = XCOS(angle);
167 void xgl_scale(int32_t x, int32_t y, int32_t z)
177 static void xform(struct xvertex *out, const struct xvertex *in, const int32_t *m)
179 out->x = XMUL(m[0], in->x) + XMUL(m[4], in->y) + XMUL(m[8], in->z) + m[12];
180 out->y = XMUL(m[1], in->x) + XMUL(m[5], in->y) + XMUL(m[9], in->z) + m[13];
181 out->z = XMUL(m[2], in->x) + XMUL(m[6], in->y) + XMUL(m[10], in->z) + m[14];
184 static void xform_norm(struct xvertex *out, const struct xvertex *in, const int32_t *m)
186 out->nx = XMUL(m[0], in->nx) + XMUL(m[4], in->ny) + XMUL(m[8], in->nz);
187 out->ny = XMUL(m[1], in->nx) + XMUL(m[5], in->ny) + XMUL(m[9], in->nz);
188 out->nz = XMUL(m[2], in->nx) + XMUL(m[6], in->ny) + XMUL(m[10], in->nz);
191 /* d = 1.0 / tan(fov/2) */
192 #define PROJ_D 0x20000
194 void xgl_draw(int prim, const struct xvertex *varr, int vcount)
197 struct xvertex xv[4];
198 struct pvertex pv[4];
202 draw_ptlines(prim, varr, vcount);
206 while(vcount >= prim) {
209 xform(xv, varr, mat[mtop]);
210 xform_norm(xv, varr, mat[mtop]);
219 if(opt & XGL_LIGHTING) {
220 ndotl = (xv->nx >> 8) * ldir[0] + (xv->ny >> 8) * ldir[1] + (xv->nz >> 8) * ldir[2];
221 if(ndotl < 0) ndotl = 0;
222 cidx = 128 + (ndotl >> 9);
223 if(cidx > 255) cidx = 255;
226 xv->x = (xv->x << 1) / (xv->z >> 8); /* assume aspect: ~2 */
227 xv->y = (xv->y << 2) / (xv->z >> 8); /* the shift is * PROJ_D */
228 /* projection result is 24.8 */
230 pv->x = (((xv->x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
231 pv->y = (((0x100 - xv->y) >> 1) * vp[3]) + (vp[1] << 8);
234 for(i=1; i<prim; i++) {
235 xform(xv + i, varr, mat[mtop]);
237 xv[i].x = (xv[i].x << 1) / (xv[i].z >> 8); /* assume aspect: ~2 */
238 xv[i].y = (xv[i].y << 2) / (xv[i].z >> 8); /* the shift is * PROJ_D */
239 /* projection result is 24.8 */
241 pv[i].x = (((xv[i].x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
242 pv[i].y = (((0x100 - xv[i].y) >> 1) * vp[3]) + (vp[1] << 8);
247 polyfill_flat(pv, prim, cidx);
251 void xgl_transform(const struct xvertex *vin, int *x, int *y)
254 xform(&v, vin, mat[mtop]);
256 v.x = (v.x << 1) / (v.z >> 8); /* assume aspect: ~2 */
257 v.y = (v.y << 2) / (v.z >> 8); /* the shift is * PROJ_D */
258 /* projection result is 24.8 */
260 *x = ((((v.x + 0x100) >> 1) * vp[2]) >> 8) + vp[0];
261 *y = ((((0x100 - v.y) >> 1) * vp[3]) >> 8) + vp[1];
264 static void draw_ptlines(int prim, const struct xvertex *varr, int vcount)
267 struct xvertex xv[2];
269 while(vcount >= prim) {
270 for(i=0; i<prim; i++) {
271 xform(xv + i, varr, mat[mtop]);
273 xv[i].x = (xv[i].x << 1) / (xv[i].z >> 8); /* assume aspect: ~2 */
274 xv[i].y = (xv[i].y << 2) / (xv[i].z >> 8); /* the shift is * PROJ_D */
275 /* projection result is 24.8 */
277 xv[i].x = ((((xv[i].x + 0x100) >> 1) * vp[2]) >> 8) + vp[0];
278 xv[i].y = ((((0x100 - xv[i].y) >> 1) * vp[3]) >> 8) + vp[1];
285 clip_line((int*)&xv[0].x, (int*)&xv[0].y, (int*)&xv[1].x, (int*)&xv[1].y, vp[0], vp[1], vp[2] - 1, vp[3] - 1);
287 draw_line(xv[0].x, xv[0].y, xv[1].x, xv[1].y, varr[-2].cidx);
293 mat[mtop][12] = mat[mtop][13] = 0;