grid axes
[gba_blender] / src / xgl.c
1 /*
2 blender for the Gameboy Advance
3 Copyright (C) 2021  John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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/>.
17 */
18 #include <string.h>
19 #include <math.h>
20 #include "xgl.h"
21 #include "polyfill.h"
22 #include "debug.h"
23
24 #define MAT_STACK_SIZE  4
25
26 static int vp[4];
27 static int32_t mat[MAT_STACK_SIZE][16];
28 static int mtop;
29 static unsigned int opt;
30 static int32_t ldir[3];
31
32 static void draw_ptlines(int prim, const struct xvertex *varr, int vcount);
33
34
35 void xgl_init(void)
36 {
37         xgl_viewport(0, 0, 240, 160);
38         xgl_load_identity();
39
40         ldir[0] = ldir[1] = 0;
41         ldir[2] = -0x100;
42 }
43
44 void xgl_enable(unsigned int o)
45 {
46         opt |= o;
47 }
48
49 void xgl_disable(unsigned int o)
50 {
51         opt &= ~o;
52 }
53
54 void xgl_viewport(int x, int y, int w, int h)
55 {
56         vp[0] = x;
57         vp[1] = y;
58         vp[2] = w;
59         vp[3] = h;
60 }
61
62 void xgl_push_matrix(void)
63 {
64         int prev;
65
66         if(mtop >= MAT_STACK_SIZE - 1) return;
67
68         prev = mtop++;
69         memcpy(mat[mtop], mat[prev], sizeof mat[0]);
70 }
71
72 void xgl_pop_matrix(void)
73 {
74         if(mtop > 0) mtop--;
75 }
76
77 static const int32_t id[] = {
78         0x10000, 0, 0, 0,
79         0, 0x10000, 0, 0,
80         0, 0, 0x10000, 0,
81         0, 0, 0, 0x10000
82 };
83
84 void xgl_load_identity(void)
85 {
86         xgl_load_matrix(id);
87 }
88
89 void xgl_load_matrix(const int32_t *m)
90 {
91         memcpy(mat[mtop], m, sizeof mat[0]);
92 }
93
94 #define M(i,j)  (((i) << 2) + (j))
95 #define XMUL(a, b)      (((a) >> 8) * ((b) >> 8))
96 void xgl_mult_matrix(const int32_t *m2)
97 {
98         int i, j;
99         int32_t m1[16];
100         int32_t *dest = mat[mtop];
101
102         memcpy(m1, dest, sizeof m1);
103
104         for(i=0; i<4; i++) {
105                 for(j=0; j<4; j++) {
106                         *dest++ = XMUL(m1[M(0, j)], m2[M(i, 0)]) +
107                                 XMUL(m1[M(1, j)], m2[M(i, 1)]) +
108                                 XMUL(m1[M(2, j)], m2[M(i, 2)]) +
109                                 XMUL(m1[M(3, j)], m2[M(i, 3)]);
110                 }
111         }
112 }
113
114 #define XSIN(x)         (int32_t)(sin(x / 65536.0f) * 65536.0f)
115 #define XCOS(x)         (int32_t)(cos(x / 65536.0f) * 65536.0f)
116
117 void xgl_translate(int32_t x, int32_t y, int32_t z)
118 {
119         int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
120         m[12] = x;
121         m[13] = y;
122         m[14] = z;
123         xgl_mult_matrix(m);
124 }
125
126 void xgl_rotate_x(int32_t angle)
127 {
128         int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
129         int32_t sa = XSIN(angle);
130         int32_t ca = XCOS(angle);
131         m[5] = ca;
132         m[6] = sa;
133         m[9] = -sa;
134         m[10] = ca;
135         xgl_mult_matrix(m);
136 }
137
138 void xgl_rotate_y(int32_t angle)
139 {
140         int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
141         int32_t sa = XSIN(angle);
142         int32_t ca = XCOS(angle);
143         m[0] = ca;
144         m[2] = -sa;
145         m[8] = sa;
146         m[10] = ca;
147         xgl_mult_matrix(m);
148 }
149
150 void xgl_rotate_z(int32_t angle)
151 {
152         int32_t m[16] = {0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000, 0, 0, 0, 0, 0x10000};
153         int32_t sa = XSIN(angle);
154         int32_t ca = XCOS(angle);
155         m[0] = ca;
156         m[1] = sa;
157         m[4] = -sa;
158         m[5] = ca;
159         xgl_mult_matrix(m);
160 }
161
162 void xgl_scale(int32_t x, int32_t y, int32_t z)
163 {
164         int32_t m[16] = {0};
165         m[0] = x;
166         m[5] = y;
167         m[10] = z;
168         m[15] = 0x10000;
169         xgl_mult_matrix(m);
170 }
171
172 static void xform(struct xvertex *out, const struct xvertex *in, const int32_t *m)
173 {
174         out->x = XMUL(m[0], in->x) + XMUL(m[4], in->y) + XMUL(m[8], in->z) + m[12];
175         out->y = XMUL(m[1], in->x) + XMUL(m[5], in->y) + XMUL(m[9], in->z) + m[13];
176         out->z = XMUL(m[2], in->x) + XMUL(m[6], in->y) + XMUL(m[10], in->z) + m[14];
177 }
178
179 static void xform_norm(struct xvertex *out, const struct xvertex *in, const int32_t *m)
180 {
181         out->nx = XMUL(m[0], in->nx) + XMUL(m[4], in->ny) + XMUL(m[8], in->nz);
182         out->ny = XMUL(m[1], in->nx) + XMUL(m[5], in->ny) + XMUL(m[9], in->nz);
183         out->nz = XMUL(m[2], in->nx) + XMUL(m[6], in->ny) + XMUL(m[10], in->nz);
184 }
185
186 /* d = 1.0 / tan(fov/2) */
187 #define PROJ_D  0x20000
188
189 void xgl_draw(int prim, const struct xvertex *varr, int vcount)
190 {
191         int i, cidx;
192         struct xvertex xv[4];
193         struct pvertex pv[4];
194         int32_t ndotl;
195
196         if(prim < 3) {
197                 draw_ptlines(prim, varr, vcount);
198                 return;
199         }
200
201         while(vcount >= prim) {
202                 cidx = varr->cidx;
203
204                 xform(xv, varr, mat[mtop]);
205                 xform_norm(xv, varr, mat[mtop]);
206
207                 if(xv->nz > 0) {
208                         /* backface */
209                         varr += prim;
210                         vcount -= prim;
211                         continue;
212                 }
213
214                 if(opt & XGL_LIGHTING) {
215                         ndotl = (xv->nx >> 8) * ldir[0] + (xv->ny >> 8) * ldir[1] + (xv->nz >> 8) * ldir[2];
216                         if(ndotl < 0) ndotl = 0;
217                         cidx = 128 + (ndotl >> 9);
218                         if(cidx > 255) cidx = 255;
219                 }
220
221                 xv->x = (xv->x << 1) / (xv->z >> 8);    /* assume aspect: ~2 */
222                 xv->y = (xv->y << 2) / (xv->z >> 8);    /* the shift is * PROJ_D */
223                 /* projection result is 24.8 */
224                 /* viewport */
225                 pv->x = (((xv->x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
226                 pv->y = (((0x100 - xv->y) >> 1) * vp[3]) + (vp[1] << 8);
227                 varr++;
228
229                 for(i=1; i<prim; i++) {
230                         xform(xv + i, varr, mat[mtop]);
231
232                         xv[i].x = (xv[i].x << 1) / (xv[i].z >> 8);      /* assume aspect: ~2 */
233                         xv[i].y = (xv[i].y << 2) / (xv[i].z >> 8);      /* the shift is * PROJ_D */
234                         /* projection result is 24.8 */
235                         /* viewport */
236                         pv[i].x = (((xv[i].x + 0x100) >> 1) * vp[2]) + (vp[0] << 8);
237                         pv[i].y = (((0x100 - xv[i].y) >> 1) * vp[3]) + (vp[1] << 8);
238                         varr++;
239                 }
240                 vcount -= prim;
241
242                 polyfill_flat(pv, prim, cidx);
243         }
244 }
245
246 static void draw_ptlines(int prim, const struct xvertex *varr, int vcount)
247 {
248         int i;
249         struct xvertex xv[2];
250
251         while(vcount >= prim) {
252                 for(i=0; i<prim; i++) {
253                         xform(xv + i, varr, mat[mtop]);
254
255                         xv[i].x = (xv[i].x << 1) / (xv[i].z >> 8);      /* assume aspect: ~2 */
256                         xv[i].y = (xv[i].y << 2) / (xv[i].z >> 8);      /* the shift is * PROJ_D */
257                         /* projection result is 24.8 */
258                         /* viewport */
259                         xv[i].x = ((((xv[i].x + 0x100) >> 1) * vp[2]) >> 8) + vp[0];
260                         xv[i].y = ((((0x100 - xv[i].y) >> 1) * vp[3]) >> 8) + vp[1];
261                         varr++;
262                 }
263                 vcount -= prim;
264
265                 /* line clipping */
266                 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);
267                 draw_line(xv[0].x, xv[0].y, xv[1].x, xv[1].y, varr[-2].cidx);
268         }
269 }