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