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