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