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