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