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