2 gbajam22 entry for the Gameboy Advance
3 Copyright (C) 2022 John Tsiombikas <nuclear@mutantstargoat.com>
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)]);
120 #define XSIN(x) (int32_t)(sin(x / 65536.0f) * 65536.0f)
121 #define XCOS(x) (int32_t)(cos(x / 65536.0f) * 65536.0f)
123 void xgl_translate(int32_t x, int32_t y, int32_t z)
125 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
132 void xgl_rotate_x(int32_t angle)
134 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
135 int32_t sa = XSIN(angle);
136 int32_t ca = XCOS(angle);
144 void xgl_rotate_y(int32_t angle)
146 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
147 int32_t sa = XSIN(angle);
148 int32_t ca = XCOS(angle);
156 void xgl_rotate_z(int32_t angle)
158 int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
159 int32_t sa = XSIN(angle);
160 int32_t ca = XCOS(angle);
168 void xgl_scale(int32_t x, int32_t y, int32_t z)
178 static void xform(struct xvertex *out, const struct xvertex *in, const int32_t *m)
180 out->x = XMUL(m[0], in->x) + XMUL(m[4], in->y) + XMUL(m[8], in->z) + m[12];
181 out->y = XMUL(m[1], in->x) + XMUL(m[5], in->y) + XMUL(m[9], in->z) + m[13];
182 out->z = XMUL(m[2], in->x) + XMUL(m[6], in->y) + XMUL(m[10], in->z) + m[14];
185 static void xform_norm(struct xvertex *out, const struct xvertex *in, const int32_t *m)
187 out->nx = XMUL(m[0], in->nx) + XMUL(m[4], in->ny) + XMUL(m[8], in->nz);
188 out->ny = XMUL(m[1], in->nx) + XMUL(m[5], in->ny) + XMUL(m[9], in->nz);
189 out->nz = XMUL(m[2], in->nx) + XMUL(m[6], in->ny) + XMUL(m[10], in->nz);
192 /* d = 1.0 / tan(fov/2) */
193 #define PROJ_D 0x20000
195 #define NEAR_Z 0x18000
197 void xgl_draw(int prim, const struct xvertex *varr, int vcount)
199 int i, cidx, clipnum;
200 struct xvertex xv[4], xvclip[8];
201 struct pvertex pv[4];
205 draw_ptlines(prim, varr, vcount);
209 while(vcount >= prim) {
210 cidx = 0xff;//varr->cidx;
212 xform(xv, varr, mat[mtop]);
213 xform_norm(xv, varr, mat[mtop]);
216 if(opt & XGL_LIGHTING) {
217 ndotl = (xv->nx >> 8) * ldir[0] + (xv->ny >> 8) * ldir[1] + (xv->nz >> 8) * ldir[2];
218 if(ndotl < 0) ndotl = 0;
219 cidx = 128 + (ndotl >> 9);
220 if(cidx > 255) cidx = 255;
224 /* transform the rest of the vertices to view space */
225 for(i=1; i<prim; i++) {
226 xform(xv + i, varr + i, mat[mtop]);
229 /* clip against near plane */
230 xgl_clip_near(xvclip, &clipnum, xv, prim);
232 for(i=0; i<clipnum; i++) {
233 xvclip[i].x = (xvclip[i].x << 1) / (xvclip[i].z >> 8); /* assume aspect: ~2 */
234 xvclip[i].y = (xvclip[i].y << 2) / (xvclip[i].z >> 8); /* the shift is * PROJ_D */
235 /* transform result is 24.8 */
237 pv[i].x = (((xvclip[i].x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
238 pv[i].y = (((0x100 - xvclip[i].y) >> 1) * vp[3]) + (vp[1] << 8);
241 polyfill_flat(pv, clipnum, cidx);
248 void xgl_transform(const struct xvertex *vin, int *x, int *y)
251 xform(&v, vin, mat[mtop]);
253 v.x = (v.x << 1) / (v.z >> 8); /* assume aspect: ~2 */
254 v.y = (v.y << 2) / (v.z >> 8); /* the shift is * PROJ_D */
255 /* projection result is 24.8 */
257 *x = ((((v.x + 0x100) >> 1) * vp[2]) >> 8) + vp[0];
258 *y = ((((0x100 - v.y) >> 1) * vp[3]) >> 8) + vp[1];
261 static void draw_ptlines(int prim, const struct xvertex *varr, int vcount)
264 struct xvertex xv[2];
266 while(vcount >= prim) {
267 for(i=0; i<prim; i++) {
268 xform(xv + i, varr, mat[mtop]);
270 xv[i].x = (xv[i].x << 1) / (xv[i].z >> 8); /* assume aspect: ~2 */
271 xv[i].y = (xv[i].y << 2) / (xv[i].z >> 8); /* the shift is * PROJ_D */
272 /* projection result is 24.8 */
274 xv[i].x = ((((xv[i].x + 0x100) >> 1) * vp[2]) >> 8) + vp[0];
275 xv[i].y = ((((0x100 - xv[i].y) >> 1) * vp[3]) >> 8) + vp[1];
282 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);
284 draw_line(xv[0].x, xv[0].y, xv[1].x, xv[1].y, 0xff);
290 mat[mtop][12] = mat[mtop][13] = 0;
293 #define ISECT_NEAR(v0, v1) ((((v0)->z - NEAR_Z) << 8) / ((v0)->z - (v1)->z))
295 #define LERP_VATTR(res, v0, v1, t) \
297 (res)->x = (v0)->x + (((v1)->x - (v0)->x) >> 8) * (t); \
298 (res)->y = (v0)->y + (((v1)->y - (v0)->y) >> 8) * (t); \
299 (res)->z = (v0)->z + (((v1)->z - (v0)->z) >> 8) * (t); \
300 (res)->nx = (v0)->nx + (((v1)->nx - (v0)->nx) >> 8) * (t); \
301 (res)->ny = (v0)->ny + (((v1)->ny - (v0)->ny) >> 8) * (t); \
302 (res)->nz = (v0)->nz + (((v1)->nz - (v0)->nz) >> 8) * (t); \
303 (res)->tx = (v0)->tx + (((v1)->tx - (v0)->tx) >> 8) * (t); \
304 (res)->ty = (v0)->ty + (((v1)->ty - (v0)->ty) >> 8) * (t); \
305 (res)->lit = (v0)->lit + (((v1)->lit - (v0)->lit) >> 8) * (t); \
308 static int clip_edge_near(struct xvertex *poly, int *vnumptr, struct xvertex *v0, struct xvertex *v1)
313 struct xvertex *vptr;
315 in0 = v0->z >= NEAR_Z ? 1 : 0;
316 in1 = v1->z >= NEAR_Z ? 1 : 0;
322 poly[vnum++] = *v1; /* append v1 */
328 t = ISECT_NEAR(v0, v1); /* 24:8 */
329 LERP_VATTR(vptr, v0, v1, t);
330 ++vnum; /* append new vertex on the intersection point */
337 t = ISECT_NEAR(v0, v1);
338 LERP_VATTR(vptr, v0, v1, t);
339 ++vnum; /* append new vertex ... */
352 /* special case near-plane clipper */
353 int xgl_clip_near(struct xvertex *vout, int *voutnum, struct xvertex *vin, int vnum)
356 int edges_clipped = 0;
360 for(i=0; i<vnum; i++) {
362 if(nextidx >= vnum) nextidx = 0;
363 res = clip_edge_near(vout, voutnum, vin + i, vin + nextidx);
369 if(*voutnum <= 0) return -1;
370 return edges_clipped > 0 ? 0 : 1;