clipping bugs...
[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 "polyclip.h"
9 #include "inttypes.h"
10 #include "util.h"
11
12 #define STACK_SIZE      8
13 typedef float g3d_matrix[16];
14
15 #define MAX_VBUF_SIZE   256
16 #define MAX_LIGHTS              4
17
18 struct light {
19         float x, y, z;
20         float r, g, b;
21 };
22
23 struct material {
24         float kd[3];
25         float ks[3];
26         float shin;
27 };
28
29 struct g3d_state {
30         unsigned int opt;
31         int frontface;
32         int fill_mode;
33
34         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
35         int mtop[G3D_NUM_MATRICES];
36         int mmode;
37
38         g3d_matrix norm_mat;
39
40         float ambient[3];
41         struct light lt[MAX_LIGHTS];
42         struct material mtl;
43
44         int width, height;
45         void *pixels;
46
47         int vport[4];
48 };
49
50 static void xform4_vec3(const float *mat, float *vec);
51 static void xform3_vec3(const float *mat, float *vec);
52 static void shade(struct g3d_vertex *v);
53
54 static struct g3d_state *st;
55 static const float idmat[] = {
56         1, 0, 0, 0,
57         0, 1, 0, 0,
58         0, 0, 1, 0,
59         0, 0, 0, 1
60 };
61
62 int g3d_init(void)
63 {
64         int i;
65
66         if(!(st = calloc(1, sizeof *st))) {
67                 fprintf(stderr, "failed to allocate G3D context\n");
68                 return -1;
69         }
70         st->fill_mode = POLYFILL_FLAT;
71
72         for(i=0; i<G3D_NUM_MATRICES; i++) {
73                 g3d_matrix_mode(i);
74                 g3d_load_identity();
75         }
76
77         for(i=0; i<MAX_LIGHTS; i++) {
78                 g3d_light_color(i, 1, 1, 1);
79         }
80         g3d_light_ambient(0.1, 0.1, 0.1);
81
82         g3d_mtl_diffuse(1, 1, 1);
83         return 0;
84 }
85
86 void g3d_destroy(void)
87 {
88         free(st);
89 }
90
91 void g3d_framebuffer(int width, int height, void *pixels)
92 {
93         st->width = width;
94         st->height = height;
95         st->pixels = pixels;
96
97         pfill_fb.pixels = pixels;
98         pfill_fb.width = width;
99         pfill_fb.height = height;
100
101         g3d_viewport(0, 0, width, height);
102 }
103
104 void g3d_viewport(int x, int y, int w, int h)
105 {
106         st->vport[0] = x;
107         st->vport[1] = y;
108         st->vport[2] = w;
109         st->vport[3] = h;
110 }
111
112 void g3d_enable(unsigned int opt)
113 {
114         st->opt |= opt;
115 }
116
117 void g3d_disable(unsigned int opt)
118 {
119         st->opt &= ~opt;
120 }
121
122 void g3d_setopt(unsigned int opt, unsigned int mask)
123 {
124         st->opt = (st->opt & ~mask) | (opt & mask);
125 }
126
127 unsigned int g3d_getopt(unsigned int mask)
128 {
129         return st->opt & mask;
130 }
131
132 void g3d_front_face(unsigned int order)
133 {
134         st->frontface = order;
135 }
136
137 void g3d_polygon_mode(int pmode)
138 {
139         st->fill_mode = pmode;
140 }
141
142 void g3d_matrix_mode(int mmode)
143 {
144         st->mmode = mmode;
145 }
146
147 void g3d_load_identity(void)
148 {
149         int top = st->mtop[st->mmode];
150         memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
151 }
152
153 void g3d_load_matrix(const float *m)
154 {
155         int top = st->mtop[st->mmode];
156         memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
157 }
158
159 #define M(i,j)  (((i) << 2) + (j))
160 void g3d_mult_matrix(const float *m2)
161 {
162         int i, j, top = st->mtop[st->mmode];
163         float m1[16];
164         float *dest = st->mat[st->mmode][top];
165
166         memcpy(m1, dest, sizeof m1);
167
168         for(i=0; i<4; i++) {
169                 for(j=0; j<4; j++) {
170                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
171                                 m1[M(1,j)] * m2[M(i,1)] +
172                                 m1[M(2,j)] * m2[M(i,2)] +
173                                 m1[M(3,j)] * m2[M(i,3)];
174                 }
175         }
176 }
177
178 void g3d_push_matrix(void)
179 {
180         int top = st->mtop[st->mmode];
181         if(top >= G3D_NUM_MATRICES) {
182                 fprintf(stderr, "g3d_push_matrix overflow\n");
183                 return;
184         }
185         memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
186         st->mtop[st->mmode] = top + 1;
187 }
188
189 void g3d_pop_matrix(void)
190 {
191         if(st->mtop[st->mmode] <= 0) {
192                 fprintf(stderr, "g3d_pop_matrix underflow\n");
193                 return;
194         }
195         --st->mtop[st->mmode];
196 }
197
198 void g3d_translate(float x, float y, float z)
199 {
200         float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
201         m[12] = x;
202         m[13] = y;
203         m[14] = z;
204         g3d_mult_matrix(m);
205 }
206
207 void g3d_rotate(float deg, float x, float y, float z)
208 {
209         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
210
211         float angle = M_PI * deg / 180.0f;
212         float sina = sin(angle);
213         float cosa = cos(angle);
214         float one_minus_cosa = 1.0f - cosa;
215         float nxsq = x * x;
216         float nysq = y * y;
217         float nzsq = z * z;
218
219         m[0] = nxsq + (1.0f - nxsq) * cosa;
220         m[4] = x * y * one_minus_cosa - z * sina;
221         m[8] = x * z * one_minus_cosa + y * sina;
222         m[1] = x * y * one_minus_cosa + z * sina;
223         m[5] = nysq + (1.0 - nysq) * cosa;
224         m[9] = y * z * one_minus_cosa - x * sina;
225         m[2] = x * z * one_minus_cosa - y * sina;
226         m[6] = y * z * one_minus_cosa + x * sina;
227         m[10] = nzsq + (1.0 - nzsq) * cosa;
228         m[15] = 1.0f;
229
230         g3d_mult_matrix(m);
231 }
232
233 void g3d_scale(float x, float y, float z)
234 {
235         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
236         m[0] = x;
237         m[5] = y;
238         m[10] = z;
239         m[15] = 1.0f;
240         g3d_mult_matrix(m);
241 }
242
243 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
244 {
245         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
246
247         float dx = right - left;
248         float dy = top - bottom;
249         float dz = zfar - znear;
250
251         m[0] = 2.0 / dx;
252         m[5] = 2.0 / dy;
253         m[10] = -2.0 / dz;
254         m[12] = -(right + left) / dx;
255         m[13] = -(top + bottom) / dy;
256         m[14] = -(zfar + znear) / dz;
257
258         g3d_mult_matrix(m);
259 }
260
261 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
262 {
263         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
264
265         float dx = right - left;
266         float dy = top - bottom;
267         float dz = fr - nr;
268
269         float a = (right + left) / dx;
270         float b = (top + bottom) / dy;
271         float c = -(fr + nr) / dz;
272         float d = -2.0 * fr * nr / dz;
273
274         m[0] = 2.0 * nr / dx;
275         m[5] = 2.0 * nr / dy;
276         m[8] = a;
277         m[9] = b;
278         m[10] = c;
279         m[11] = -1.0f;
280         m[14] = d;
281
282         g3d_mult_matrix(m);
283 }
284
285 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
286 {
287         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
288
289         float vfov = M_PI * vfov_deg / 180.0f;
290         float s = 1.0f / tan(vfov * 0.5f);
291         float range = znear - zfar;
292
293         m[0] = s / aspect;
294         m[5] = s;
295         m[10] = (znear + zfar) / range;
296         m[11] = -1.0f;
297         m[14] = 2.0f * znear * zfar / range;
298
299         g3d_mult_matrix(m);
300 }
301
302 const float *g3d_get_matrix(int which, float *m)
303 {
304         int top = st->mtop[which];
305
306         if(m) {
307                 memcpy(m, st->mat[which][top], 16 * sizeof(float));
308         }
309         return st->mat[which][top];
310 }
311
312 void g3d_light_pos(int idx, float x, float y, float z)
313 {
314         int mvtop = st->mtop[G3D_MODELVIEW];
315
316         st->lt[idx].x = x;
317         st->lt[idx].y = y;
318         st->lt[idx].z = z;
319
320         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
321 }
322
323 void g3d_light_color(int idx, float r, float g, float b)
324 {
325         st->lt[idx].r = r;
326         st->lt[idx].g = g;
327         st->lt[idx].b = b;
328 }
329
330 void g3d_light_ambient(float r, float g, float b)
331 {
332         st->ambient[0] = r;
333         st->ambient[1] = g;
334         st->ambient[2] = b;
335 }
336
337 void g3d_mtl_diffuse(float r, float g, float b)
338 {
339         st->mtl.kd[0] = r;
340         st->mtl.kd[1] = g;
341         st->mtl.kd[2] = b;
342 }
343
344 void g3d_mtl_specular(float r, float g, float b)
345 {
346         st->mtl.ks[0] = r;
347         st->mtl.ks[1] = g;
348         st->mtl.ks[2] = b;
349 }
350
351 void g3d_mtl_shininess(float shin)
352 {
353         st->mtl.shin = shin;
354 }
355
356 static INLINE int calc_shift(unsigned int x)
357 {
358         int res = -1;
359         while(x) {
360                 x >>= 1;
361                 ++res;
362         }
363         return res;
364 }
365
366 static INLINE int calc_mask(unsigned int x)
367 {
368         return x - 1;
369 }
370
371 void g3d_set_texture(int xsz, int ysz, void *pixels)
372 {
373         pfill_tex.pixels = pixels;
374         pfill_tex.width = xsz;
375         pfill_tex.height = ysz;
376
377         pfill_tex.xshift = calc_shift(xsz);
378         pfill_tex.yshift = calc_shift(ysz);
379         pfill_tex.xmask = calc_mask(xsz);
380         pfill_tex.ymask = calc_mask(ysz);
381 }
382
383 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
384 {
385         g3d_draw_indexed(prim, varr, varr_size, 0, 0);
386 }
387
388 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
389                 const int16_t *iarr, int iarr_size)
390 {
391         int i, j, nfaces;
392         struct pvertex pv[16];
393         struct g3d_vertex v[16];
394         int vnum = prim; /* primitive vertex counts correspond to enum values */
395         int mvtop = st->mtop[G3D_MODELVIEW];
396         int ptop = st->mtop[G3D_PROJECTION];
397
398         /* calc the normal matrix */
399         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
400         st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
401
402         nfaces = (iarr ? iarr_size : varr_size) / vnum;
403
404         for(j=0; j<nfaces; j++) {
405                 vnum = prim;    /* reset vnum for each iteration */
406
407                 for(i=0; i<vnum; i++) {
408                         v[i] = iarr ? varr[*iarr++] : *varr++;
409
410                         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
411                         xform3_vec3(st->norm_mat, &v[i].nx);
412
413                         if(st->opt & G3D_LIGHTING) {
414                                 shade(v + i);
415                         }
416                         xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
417                 }
418
419                 /* clipping */
420                 for(i=0; i<6; i++) {
421                         struct g3d_vertex tmpv[16];
422                         memcpy(tmpv, v, vnum * sizeof *v);
423
424                         if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
425                                 /* polygon completely outside of view volume. discard */
426                                 vnum = 0;
427                                 break;
428                         }
429                 }
430
431                 if(!vnum) continue;
432
433                 for(i=0; i<vnum; i++) {
434                         if(v[i].w != 0.0f) {
435                                 v[i].x /= v[i].w;
436                                 v[i].y /= v[i].w;
437                                 /*v[i].z /= v[i].w;*/
438                         }
439
440                         /* viewport transformation */
441                         v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
442                         v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
443
444                         /* convert pos to 24.8 fixed point */
445                         pv[i].x = cround64(v[i].x * 256.0f);
446                         pv[i].y = cround64(v[i].y * 256.0f);
447                         /* convert tex coords to 16.16 fixed point */
448                         pv[i].u = cround64(v[i].u * 65536.0f);
449                         pv[i].v = cround64(v[i].v * 65536.0f);
450                         /* pass the color through as is */
451                         pv[i].r = v[i].r;
452                         pv[i].g = v[i].g;
453                         pv[i].b = v[i].b;
454                 }
455
456                 /* backface culling */
457                 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
458                         int32_t ax = pv[1].x - pv[0].x;
459                         int32_t ay = pv[1].y - pv[0].y;
460                         int32_t bx = pv[2].x - pv[0].x;
461                         int32_t by = pv[2].y - pv[0].y;
462                         int32_t cross_z = ax * (by >> 8) - ay * (bx >> 8);
463                         int sign = (cross_z >> 31) & 1;
464
465                         if(!(sign ^ st->frontface)) {
466                                 continue;       /* back-facing */
467                         }
468                 }
469
470                 polyfill(st->fill_mode, pv, vnum);
471         }
472 }
473
474 static void xform4_vec3(const float *mat, float *vec)
475 {
476         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
477         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
478         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
479         float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
480
481         vec[0] = x;
482         vec[1] = y;
483         vec[2] = z;
484         vec[3] = w;
485 }
486
487 static void xform3_vec3(const float *mat, float *vec)
488 {
489         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
490         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
491         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
492
493         vec[0] = x;
494         vec[1] = y;
495         vec[2] = z;
496 }
497
498 #define NORMALIZE(v) \
499         do { \
500                 float len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); \
501                 if(len != 0.0) { \
502                         float s = 1.0 / len; \
503                         v[0] *= s; \
504                         v[1] *= s; \
505                         v[2] *= s; \
506                 } \
507         } while(0)
508
509 static void shade(struct g3d_vertex *v)
510 {
511         int i, r, g, b;
512         float color[3];
513
514         color[0] = st->ambient[0] * st->mtl.kd[0];
515         color[1] = st->ambient[1] * st->mtl.kd[1];
516         color[2] = st->ambient[2] * st->mtl.kd[2];
517
518         for(i=0; i<MAX_LIGHTS; i++) {
519                 float ldir[3];
520                 float ndotl;
521
522                 if(!(st->opt & (G3D_LIGHT0 << i))) {
523                         continue;
524                 }
525
526                 ldir[0] = st->lt[i].x - v->x;
527                 ldir[1] = st->lt[i].y - v->y;
528                 ldir[2] = st->lt[i].z - v->z;
529                 NORMALIZE(ldir);
530
531                 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
532                         ndotl = 0.0f;
533                 }
534
535                 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
536                 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
537                 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
538         }
539
540         r = cround64(color[0] * 255.0);
541         g = cround64(color[1] * 255.0);
542         b = cround64(color[2] * 255.0);
543
544         v->r = r > 255 ? 255 : r;
545         v->g = g > 255 ? 255 : g;
546         v->b = b > 255 ? 255 : b;
547 }