torus with crude zsorting
[dosdemo] / src / 3dgfx.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "3dgfx.h"
7 #include "polyfill.h"
8 #include "inttypes.h"
9
10 #define STACK_SIZE      8
11 typedef float g3d_matrix[16];
12
13 #define MAX_VBUF_SIZE   256
14
15 struct g3d_state {
16         unsigned int opt;
17         int frontface;
18
19         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
20         int mtop[G3D_NUM_MATRICES];
21         int mmode;
22
23         g3d_matrix norm_mat;
24
25         int width, height;
26         void *pixels;
27 };
28
29 static void xform4_vec3(const float *mat, float *vec);
30 static void xform3_vec3(const float *mat, float *vec);
31 static void shade(struct g3d_vertex *v);
32
33 static struct g3d_state *st;
34 static const float idmat[] = {
35         1, 0, 0, 0,
36         0, 1, 0, 0,
37         0, 0, 1, 0,
38         0, 0, 0, 1
39 };
40
41 int g3d_init(void)
42 {
43         int i;
44
45         if(!(st = calloc(1, sizeof *st))) {
46                 fprintf(stderr, "failed to allocate G3D context\n");
47                 return -1;
48         }
49
50         for(i=0; i<G3D_NUM_MATRICES; i++) {
51                 g3d_matrix_mode(i);
52                 g3d_load_identity();
53         }
54         return 0;
55 }
56
57 void g3d_destroy(void)
58 {
59         free(st);
60 }
61
62 void g3d_framebuffer(int width, int height, void *pixels)
63 {
64         st->width = width;
65         st->height = height;
66         st->pixels = pixels;
67 }
68
69 void g3d_enable(unsigned int opt)
70 {
71         st->opt |= opt;
72 }
73
74 void g3d_disable(unsigned int opt)
75 {
76         st->opt &= ~opt;
77 }
78
79 void g3d_setopt(unsigned int opt, unsigned int mask)
80 {
81         st->opt = (st->opt & ~mask) | (opt & mask);
82 }
83
84 unsigned int g3d_getopt(unsigned int mask)
85 {
86         return st->opt & mask;
87 }
88
89 void g3d_front_face(unsigned int order)
90 {
91         st->frontface = order;
92 }
93
94 void g3d_matrix_mode(int mmode)
95 {
96         st->mmode = mmode;
97 }
98
99 void g3d_load_identity(void)
100 {
101         int top = st->mtop[st->mmode];
102         memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
103 }
104
105 void g3d_load_matrix(const float *m)
106 {
107         int top = st->mtop[st->mmode];
108         memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
109 }
110
111 #define M(i,j)  (((i) << 2) + (j))
112 void g3d_mult_matrix(const float *m2)
113 {
114         int i, j, top = st->mtop[st->mmode];
115         float m1[16];
116         float *dest = st->mat[st->mmode][top];
117
118         memcpy(m1, dest, sizeof m1);
119
120         for(i=0; i<4; i++) {
121                 for(j=0; j<4; j++) {
122                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
123                                 m1[M(1,j)] * m2[M(i,1)] +
124                                 m1[M(2,j)] * m2[M(i,2)] +
125                                 m1[M(3,j)] * m2[M(i,3)];
126                 }
127         }
128 }
129
130 void g3d_push_matrix(void)
131 {
132         int top = st->mtop[st->mmode];
133         if(top >= G3D_NUM_MATRICES) {
134                 fprintf(stderr, "g3d_push_matrix overflow\n");
135                 return;
136         }
137         memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
138         st->mtop[st->mmode] = top + 1;
139 }
140
141 void g3d_pop_matrix(void)
142 {
143         if(st->mtop[st->mmode] <= 0) {
144                 fprintf(stderr, "g3d_pop_matrix underflow\n");
145                 return;
146         }
147         --st->mtop[st->mmode];
148 }
149
150 void g3d_translate(float x, float y, float z)
151 {
152         float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
153         m[12] = x;
154         m[13] = y;
155         m[14] = z;
156         g3d_mult_matrix(m);
157 }
158
159 void g3d_rotate(float deg, float x, float y, float z)
160 {
161         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
162
163         float angle = M_PI * deg / 180.0f;
164         float sina = sin(angle);
165         float cosa = cos(angle);
166         float one_minus_cosa = 1.0f - cosa;
167         float nxsq = x * x;
168         float nysq = y * y;
169         float nzsq = z * z;
170
171         m[0] = nxsq + (1.0f - nxsq) * cosa;
172         m[4] = x * y * one_minus_cosa - z * sina;
173         m[8] = x * z * one_minus_cosa + y * sina;
174         m[1] = x * y * one_minus_cosa + z * sina;
175         m[5] = nysq + (1.0 - nysq) * cosa;
176         m[9] = y * z * one_minus_cosa - x * sina;
177         m[2] = x * z * one_minus_cosa - y * sina;
178         m[6] = y * z * one_minus_cosa + x * sina;
179         m[10] = nzsq + (1.0 - nzsq) * cosa;
180         m[15] = 1.0f;
181
182         g3d_mult_matrix(m);
183 }
184
185 void g3d_scale(float x, float y, float z)
186 {
187         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
188         m[0] = x;
189         m[5] = y;
190         m[10] = z;
191         m[15] = 1.0f;
192         g3d_mult_matrix(m);
193 }
194
195 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
196 {
197         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
198
199         float dx = right - left;
200         float dy = top - bottom;
201         float dz = zfar - znear;
202
203         m[0] = 2.0 / dx;
204         m[5] = 2.0 / dy;
205         m[10] = -2.0 / dz;
206         m[12] = -(right + left) / dx;
207         m[13] = -(top + bottom) / dy;
208         m[14] = -(zfar + znear) / dz;
209
210         g3d_mult_matrix(m);
211 }
212
213 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
214 {
215         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
216
217         float dx = right - left;
218         float dy = top - bottom;
219         float dz = fr - nr;
220
221         float a = (right + left) / dx;
222         float b = (top + bottom) / dy;
223         float c = -(fr + nr) / dz;
224         float d = -2.0 * fr * nr / dz;
225
226         m[0] = 2.0 * nr / dx;
227         m[5] = 2.0 * nr / dy;
228         m[8] = a;
229         m[9] = b;
230         m[10] = c;
231         m[11] = -1.0f;
232         m[14] = d;
233
234         g3d_mult_matrix(m);
235 }
236
237 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
238 {
239         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
240
241         float vfov = M_PI * vfov_deg / 180.0f;
242         float s = 1.0f / tan(vfov * 0.5f);
243         float range = znear - zfar;
244
245         m[0] = s / aspect;
246         m[5] = s;
247         m[10] = (znear + zfar) / range;
248         m[11] = -1.0f;
249         m[14] = 2.0f * znear * zfar / range;
250
251         g3d_mult_matrix(m);
252 }
253
254 const float *g3d_get_matrix(int which, float *m)
255 {
256         int top = st->mtop[which];
257
258         if(m) {
259                 memcpy(m, st->mat[which][top], 16 * sizeof(float));
260         }
261         return st->mat[which][top];
262 }
263
264 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
265 {
266         g3d_draw_indexed(prim, varr, varr_size, 0, 0);
267 }
268
269 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
270                 const int16_t *iarr, int iarr_size)
271 {
272         int i, j, nfaces;
273         struct pvertex pv[4];
274         struct g3d_vertex v[4];
275         int vnum = prim; /* primitive vertex counts correspond to enum values */
276         int mvtop = st->mtop[G3D_MODELVIEW];
277         int ptop = st->mtop[G3D_PROJECTION];
278
279         /* calc the normal matrix */
280         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
281         st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
282
283         nfaces = (iarr ? iarr_size : varr_size) / vnum;
284
285         for(j=0; j<nfaces; j++) {
286
287                 for(i=0; i<vnum; i++) {
288                         v[i] = iarr ? varr[*iarr++] : *varr++;
289
290                         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
291                         xform3_vec3(st->norm_mat, &v[i].nx);
292
293                         if(st->opt & G3D_LIGHTING) {
294                                 shade(v + i);
295                         }
296                         xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
297                 }
298
299                 /* TODO clipping */
300
301                 for(i=0; i<vnum; i++) {
302                         if(v[i].w != 0.0f) {
303                                 v[i].x /= v[i].w;
304                                 v[i].y /= v[i].w;
305                                 /*v[i].z /= v[i].w;*/
306                         }
307
308                         /* viewport transformation */
309                         v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->width;
310                         v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->height;
311
312                         /* convert pos to 24.8 fixed point */
313                         pv[i].x = (int32_t)(v[i].x * 256.0f);
314                         pv[i].y = (int32_t)(v[i].y * 256.0f);
315                         /* convert tex coords to 16.16 fixed point */
316                         pv[i].u = (int32_t)(v[i].u * 65536.0f);
317                         pv[i].v = (int32_t)(v[i].v * 65536.0f);
318                         /* pass the color through as is */
319                         pv[i].r = v[i].r;
320                         pv[i].g = v[i].g;
321                         pv[i].b = v[i].b;
322                 }
323
324                 /* backface culling */
325                 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
326                         int32_t ax = pv[1].x - pv[0].x;
327                         int32_t ay = pv[1].y - pv[0].y;
328                         int32_t bx = pv[2].x - pv[0].x;
329                         int32_t by = pv[2].y - pv[0].y;
330                         int32_t cross_z = ax * (by >> 8) - ay * (bx >> 8);
331                         int sign = (cross_z >> 31) & 1;
332
333                         if(!(sign ^ st->frontface)) {
334                                 continue;       /* back-facing */
335                         }
336                 }
337
338                 polyfill_flat(pv, vnum);
339         }
340 }
341
342 static void xform4_vec3(const float *mat, float *vec)
343 {
344         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
345         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
346         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
347         float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
348
349         vec[0] = x;
350         vec[1] = y;
351         vec[2] = z;
352         vec[3] = w;
353 }
354
355 static void xform3_vec3(const float *mat, float *vec)
356 {
357         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
358         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
359         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
360
361         vec[0] = x;
362         vec[1] = y;
363         vec[2] = z;
364 }
365
366 static void shade(struct g3d_vertex *v)
367 {
368         v->r = v->g = v->b = 255;
369 }