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