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